'Handles deallocating various network resources for an instance.
rpc.called by network_api
kwargs can contain fixed_ips to circumvent another db lookup'

def deallocate_for_instance(self, context, **kwargs):

    read_deleted_context = context.elevated(read_deleted='yes')
    if ('instance' in kwargs):
        instance = kwargs['instance']
        instance_uuid = instance.uuid
        host = instance.host
    else:
        instance_id = kwargs['instance_id']
        if uuidutils.is_uuid_like(instance_id):
            instance = objects.Instance.get_by_uuid(read_deleted_context, instance_id)
        else:
            instance = objects.Instance.get_by_id(read_deleted_context, instance_id)
        instance_uuid = instance.uuid
        host = kwargs.get('host')
    try:
        requested_networks = kwargs.get('requested_networks')
        if requested_networks:
            if isinstance(requested_networks, objects.NetworkRequestList):
                requested_networks = requested_networks.as_tuples()
            network_ids = set([net_id for (net_id, ip) in requested_networks])
            fixed_ips = [ip for (net_id, ip) in requested_networks if ip]
        else:
            fixed_ip_list = objects.FixedIPList.get_by_instance_uuid(read_deleted_context, instance_uuid)
            network_ids = set([str(fixed_ip.network_id) for fixed_ip in fixed_ip_list])
            fixed_ips = [str(ip.address) for ip in fixed_ip_list]
    except exception.FixedIpNotFoundForInstance:
        network_ids = set([])
        fixed_ips = []
    LOG.debug('Network    deallocation    for    instance', instance_uuid=instance_uuid)
    for fixed_ip in fixed_ips:
        self.deallocate_fixed_ip(context, fixed_ip, host=host, instance=instance)
    if CONF.update_dns_entries:
        self.network_rpcapi.update_dns(context, list(network_ids))
    objects.VirtualInterface.delete_by_instance_uuid(read_deleted_context, instance_uuid)
    LOG.info(_LI("Network    deallocated    for    instance    (fixed    IPs:    '%s')"), fixed_ips, instance_uuid=instance_uuid)
