'Deallocate all network resources related to the instance.'

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

    LOG.debug('deallocate_for_instance()', instance=instance)
    search_opts = {'device_id': instance.uuid}
    neutron = get_client(context)
    data = neutron.list_ports(**search_opts)
    ports = [port['id'] for port in data.get('ports', [])]
    requested_networks = (kwargs.get('requested_networks') or [])
    if isinstance(requested_networks, objects.NetworkRequestList):
        requested_networks = requested_networks.as_tuples()
    ports_to_skip = set([port_id for (nets, fips, port_id, pci_request_id) in requested_networks])
    ports_to_skip |= set(self._get_preexisting_port_ids(instance))
    ports = (set(ports) - ports_to_skip)
    self._unbind_ports(context, ports_to_skip, neutron)
    self._delete_ports(neutron, instance, ports, raise_if_fail=True)
    objects.VirtualInterface.delete_by_instance_uuid(context, instance.uuid)
    base_api.update_instance_cache_with_nw_info(self, context, instance, network_model.NetworkInfo([]))
