def allocate_fixed_ip(self, context, instance_id, network, **kwargs):
        # TODO(vish): when this is called by compute, we can associate compute
        #             with a network, or a cluster of computes with a network
        #             and use that network here with a method like
        #             network_get_by_compute_host
        address = None

        # NOTE(vish) This db query could be removed if we pass az and name
        #            (or the whole instance object).
        instance = objects.Instance.get_by_uuid(context, instance_id)
        LOG.debug('Allocate fixed IP on network %s', network['uuid'],
                  instance=instance)

        # A list of cleanup functions to call on error
        cleanup = []

        # Check the quota; can't put this in the API because we get
        # called into from other places
        quotas = self.quotas_cls(context=context)
        quota_project, quota_user = quotas_obj.ids_from_instance(context,
                                                                 instance)
        try:
            quotas.reserve(fixed_ips=1, project_id=quota_project,
                           user_id=quota_user)
            cleanup.append(functools.partial(quotas.rollback, context))
        except exception.OverQuota as exc:
            usages = exc.kwargs['usages']
            used = (usages['fixed_ips']['in_use'] +
                    usages['fixed_ips']['reserved'])
            LOG.warning(_LW("Quota exceeded for project %(pid)s, tried to "
                            "allocate fixed IP. %(used)s of %(allowed)s are "
                            "in use or are already reserved."),
                        {'pid': quota_project, 'used': used,
                         'allowed': exc.kwargs['quotas']['fixed_ips']},
                        instance_uuid=instance_id)
            raise exception.FixedIpLimitExceeded()

        try:
            if network['cidr']:

                # NOTE(mriedem): allocate the vif before associating the
                # instance to reduce a race window where a previous instance
                # was associated with the fixed IP and has released it, because
                # release_fixed_ip will disassociate if allocated is False.
                vif = objects.VirtualInterface.get_by_instance_and_network(
                        context, instance_id, network['id'])
                if vif is None:
                    LOG.debug('vif for network %(network)s is used up, '
                              'trying to create new vif',
                              {'network': network['id']}, instance=instance)
                    vif = self._add_virtual_interface(context,
                        instance_id, network['id'])

                address = kwargs.get('address', None)
                if address:
                    LOG.debug('Associating instance with specified fixed IP '
                              '%(address)s in network %(network)s on subnet '
                              '%(cidr)s.' %
                              {'address': address, 'network': network['id'],
                               'cidr': network['cidr']},
                              instance=instance)
                    fip = objects.FixedIP.associate(
                            context, str(address), instance_id, network['id'],
                            vif_id=vif.id)
                else:
                    LOG.debug('Associating instance with fixed IP from pool '
                              'in network %(network)s on subnet %(cidr)s.' %
                              {'network': network['id'],
                               'cidr': network['cidr']},
                              instance=instance)
                    fip = objects.FixedIP.associate_pool(
                        context.elevated(), network['id'], instance_id,
                        vif_id=vif.id)
                    LOG.debug('Associated instance with fixed IP: %s', fip,
                              instance=instance)
                    address = str(fip.address)

                cleanup.append(functools.partial(fip.disassociate, context))

                LOG.debug('Refreshing security group members for instance.',
                          instance=instance)
                self._do_trigger_security_group_members_refresh_for_instance(
                    instance_id)
                cleanup.append(functools.partial(
                    self._do_trigger_security_group_members_refresh_for_instance,  # noqa
                    instance_id))

            name = instance.display_name

            if self._validate_instance_zone_for_dns_domain(context, instance):
                self.instance_dns_manager.create_entry(
                    name, str(fip.address), "A", self.instance_dns_domain)
                cleanup.append(functools.partial(
                        self.instance_dns_manager.delete_entry,
                        name, self.instance_dns_domain))

                self.instance_dns_manager.create_entry(
                    instance_id, str(fip.address), "A",
                    self.instance_dns_domain)
                cleanup.append(functools.partial(
                        self.instance_dns_manager.delete_entry,
                        instance_id, self.instance_dns_domain))

            LOG.debug('Setting up network %(network)s on host %(host)s.' %
                      {'network': network['id'], 'host': self.host},
                      instance=instance)
            self._setup_network_on_host(context, network)
            cleanup.append(functools.partial(
                    self._teardown_network_on_host,
                    context, network))

            quotas.commit()
            if address is None:
                # TODO(mriedem): should _setup_network_on_host return the addr?
                LOG.debug('Fixed IP is setup on network %s but not returning '
                          'the specific IP from the base network manager.',
                          network['uuid'], instance=instance)
            else:
                LOG.debug('Allocated fixed IP %s on network %s', address,
                          network['uuid'], instance=instance)
            return address

        except Exception:
            with excutils.save_and_reraise_exception():
                for f in cleanup:
                    try:
                        f()
                    except Exception:
                        LOG.warning(_LW('Error cleaning up fixed IP '
                                        'allocation. Manual cleanup may '
                                        'be required.'), exc_info=True)
Gets a fixed IP from the pool.
dataset/ETHPy150Open BU-NU-CLOUD-SP16/Trusted-Platform-Module-nova/nova/network/manager.py/NetworkManager.allocate_fixed_ip