<s> import copy <EOL> import time <EOL> import uuid <EOL> from keystoneauth1 import loading as ks_loading <EOL> from neutronclient . common import exceptions as neutron_client_exc <EOL> from neutronclient . v2_0 import client as clientv20 <EOL> from oslo_config import cfg <EOL> from oslo_log import log as logging <EOL> from oslo_utils import excutils <EOL> from oslo_utils import uuidutils <EOL> import six <EOL> from nova . api . openstack import extensions <EOL> from nova . compute import utils as compute_utils <EOL> import nova . conf <EOL> from nova import exception <EOL> from nova . i18n import _ , _LE , _LI , _LW <EOL> from nova . network import base_api <EOL> from nova . network import model as network_model <EOL> from nova . network . neutronv2 import constants <EOL> from nova import objects <EOL> from nova . objects import fields as obj_fields <EOL> from nova . pci import manager as pci_manager <EOL> from nova . pci import request as pci_request <EOL> from nova . pci import utils as pci_utils <EOL> from nova . pci import whitelist as pci_whitelist <EOL> neutron_opts = [ <EOL> cfg . StrOpt ( '<STR_LIT:url>' , <EOL> default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT> , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> ] <EOL> NEUTRON_GROUP = '<STR_LIT>' <EOL> CONF = nova . conf . CONF <EOL> CONF . register_opts ( neutron_opts , NEUTRON_GROUP ) <EOL> deprecations = { '<STR_LIT>' : [ cfg . DeprecatedOpt ( '<STR_LIT>' , <EOL> group = NEUTRON_GROUP ) ] , <EOL> '<STR_LIT>' : [ cfg . DeprecatedOpt ( '<STR_LIT>' , <EOL> group = NEUTRON_GROUP ) ] , <EOL> '<STR_LIT>' : [ cfg . DeprecatedOpt ( '<STR_LIT>' , <EOL> group = NEUTRON_GROUP ) ] } <EOL> _neutron_options = ks_loading . register_session_conf_options ( <EOL> CONF , NEUTRON_GROUP , deprecated_opts = deprecations ) <EOL> ks_loading . register_auth_conf_options ( CONF , NEUTRON_GROUP ) <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> LOG = logging . getLogger ( __name__ ) <EOL> soft_external_network_attach_authorize = extensions . soft_core_authorizer ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> _SESSION = None <EOL> _ADMIN_AUTH = None <EOL> def list_opts ( ) : <EOL> opts = copy . deepcopy ( _neutron_options ) <EOL> opts . insert ( <NUM_LIT:0> , ks_loading . get_auth_common_conf_options ( ) [ <NUM_LIT:0> ] ) <EOL> plugins = [ '<STR_LIT:password>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for name in plugins : <EOL> plugin = ks_loading . get_plugin_loader ( name ) <EOL> for plugin_option in ks_loading . get_auth_plugin_conf_options ( plugin ) : <EOL> for option in opts : <EOL> if option . name == plugin_option . name : <EOL> break <EOL> else : <EOL> opts . append ( plugin_option ) <EOL> opts . sort ( key = lambda x : x . name ) <EOL> return [ ( NEUTRON_GROUP , opts ) ] <EOL> def reset_state ( ) : <EOL> global _ADMIN_AUTH <EOL> global _SESSION <EOL> _ADMIN_AUTH = None <EOL> _SESSION = None <EOL> def _load_auth_plugin ( conf ) : <EOL> auth_plugin = ks_loading . load_auth_from_conf_options ( conf , NEUTRON_GROUP ) <EOL> if auth_plugin : <EOL> return auth_plugin <EOL> err_msg = _ ( '<STR_LIT>' ) % conf . neutron . auth_plugin <EOL> raise neutron_client_exc . Unauthorized ( message = err_msg ) <EOL> def get_client ( context , admin = False ) : <EOL> global _ADMIN_AUTH <EOL> global _SESSION <EOL> auth_plugin = None <EOL> if not _SESSION : <EOL> _SESSION = ks_loading . load_session_from_conf_options ( <EOL> CONF , NEUTRON_GROUP ) <EOL> if admin or ( context . is_admin and not context . auth_token ) : <EOL> if not _ADMIN_AUTH : <EOL> _ADMIN_AUTH = _load_auth_plugin ( CONF ) <EOL> auth_plugin = _ADMIN_AUTH <EOL> elif context . auth_token : <EOL> auth_plugin = context . get_auth_plugin ( ) <EOL> if not auth_plugin : <EOL> raise neutron_client_exc . Unauthorized ( ) <EOL> return clientv20 . Client ( session = _SESSION , <EOL> auth = auth_plugin , <EOL> endpoint_override = CONF . neutron . url , <EOL> region_name = CONF . neutron . region_name ) <EOL> def _is_not_duplicate ( item , items , items_list_name , instance ) : <EOL> present = item in items <EOL> if present : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : item , <EOL> '<STR_LIT>' : items_list_name , <EOL> '<STR_LIT>' : items } , <EOL> instance = instance ) <EOL> return not present <EOL> class API ( base_api . NetworkAPI ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , skip_policy_check = False ) : <EOL> super ( API , self ) . __init__ ( skip_policy_check = skip_policy_check ) <EOL> self . last_neutron_extension_sync = None <EOL> self . extensions = { } <EOL> def setup_networks_on_host ( self , context , instance , host = None , <EOL> teardown = False ) : <EOL> """<STR_LIT>""" <EOL> def _get_available_networks ( self , context , project_id , <EOL> net_ids = None , neutron = None ) : <EOL> """<STR_LIT>""" <EOL> if not neutron : <EOL> neutron = get_client ( context ) <EOL> if net_ids : <EOL> search_opts = { '<STR_LIT:id>' : net_ids } <EOL> nets = neutron . list_networks ( ** search_opts ) . get ( '<STR_LIT>' , [ ] ) <EOL> else : <EOL> search_opts = { '<STR_LIT>' : project_id , '<STR_LIT>' : False } <EOL> nets = neutron . list_networks ( ** search_opts ) . get ( '<STR_LIT>' , [ ] ) <EOL> search_opts = { '<STR_LIT>' : True } <EOL> nets += neutron . list_networks ( ** search_opts ) . get ( '<STR_LIT>' , [ ] ) <EOL> _ensure_requested_network_ordering ( <EOL> lambda x : x [ '<STR_LIT:id>' ] , <EOL> nets , <EOL> net_ids ) <EOL> return nets <EOL> def _create_port ( self , port_client , instance , network_id , port_req_body , <EOL> fixed_ip = None , security_group_ids = None , <EOL> available_macs = None , dhcp_opts = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if fixed_ip : <EOL> port_req_body [ '<STR_LIT:port>' ] [ '<STR_LIT>' ] = [ <EOL> { '<STR_LIT>' : str ( fixed_ip ) } ] <EOL> port_req_body [ '<STR_LIT:port>' ] [ '<STR_LIT>' ] = network_id <EOL> port_req_body [ '<STR_LIT:port>' ] [ '<STR_LIT>' ] = True <EOL> port_req_body [ '<STR_LIT:port>' ] [ '<STR_LIT>' ] = instance . project_id <EOL> if security_group_ids : <EOL> port_req_body [ '<STR_LIT:port>' ] [ '<STR_LIT>' ] = security_group_ids <EOL> if available_macs is not None : <EOL> if not available_macs : <EOL> raise exception . PortNotFree ( <EOL> instance = instance . uuid ) <EOL> mac_address = available_macs . pop ( ) <EOL> port_req_body [ '<STR_LIT:port>' ] [ '<STR_LIT>' ] = mac_address <EOL> if dhcp_opts is not None : <EOL> port_req_body [ '<STR_LIT:port>' ] [ '<STR_LIT>' ] = dhcp_opts <EOL> port = port_client . create_port ( port_req_body ) <EOL> port_id = port [ '<STR_LIT:port>' ] [ '<STR_LIT:id>' ] <EOL> if ( port [ '<STR_LIT:port>' ] . get ( '<STR_LIT>' ) == <EOL> network_model . VIF_TYPE_BINDING_FAILED ) : <EOL> port_client . delete_port ( port_id ) <EOL> raise exception . PortBindingFailed ( port_id = port_id ) <EOL> LOG . debug ( '<STR_LIT>' , port_id , <EOL> instance = instance ) <EOL> return port_id <EOL> except neutron_client_exc . InvalidIpForNetworkClient : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : fixed_ip , '<STR_LIT>' : network_id } , <EOL> instance = instance ) <EOL> msg = ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % <EOL> { '<STR_LIT>' : fixed_ip , '<STR_LIT>' : network_id } ) <EOL> raise exception . InvalidInput ( reason = msg ) <EOL> except neutron_client_exc . IpAddressInUseClient : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , fixed_ip , instance = instance ) <EOL> msg = _ ( "<STR_LIT>" ) % fixed_ip <EOL> raise exception . FixedIpAlreadyInUse ( message = msg ) <EOL> except neutron_client_exc . OverQuotaClient : <EOL> LOG . warning ( _LW ( <EOL> '<STR_LIT>' ) , <EOL> port_req_body [ '<STR_LIT:port>' ] [ '<STR_LIT>' ] , instance = instance ) <EOL> raise exception . PortLimitExceeded ( ) <EOL> except neutron_client_exc . IpAddressGenerationFailureClient : <EOL> LOG . warning ( _LW ( '<STR_LIT>' ) , <EOL> network_id , instance = instance ) <EOL> raise exception . NoMoreFixedIps ( net = network_id ) <EOL> except neutron_client_exc . MacAddressInUseClient : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : mac_address , '<STR_LIT>' : network_id } , <EOL> instance = instance ) <EOL> raise exception . PortInUse ( port_id = mac_address ) <EOL> except neutron_client_exc . NeutronClientException : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> network_id , instance = instance ) <EOL> def _check_external_network_attach ( self , context , nets ) : <EOL> """<STR_LIT>""" <EOL> if not soft_external_network_attach_authorize ( context ) : <EOL> for net in nets : <EOL> if net . get ( '<STR_LIT>' ) and not net . get ( '<STR_LIT>' ) : <EOL> raise exception . ExternalNetworkAttachForbidden ( <EOL> network_uuid = net [ '<STR_LIT:id>' ] ) <EOL> def _unbind_ports ( self , context , ports , <EOL> neutron , port_client = None ) : <EOL> """<STR_LIT>""" <EOL> port_binding = self . _has_port_binding_extension ( context , <EOL> refresh_cache = True , neutron = neutron ) <EOL> if port_client is None : <EOL> port_client = ( neutron if not port_binding else <EOL> get_client ( context , admin = True ) ) <EOL> for port_id in ports : <EOL> if port_id is None : <EOL> continue <EOL> port_req_body = { '<STR_LIT:port>' : { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } } <EOL> if port_binding : <EOL> port_req_body [ '<STR_LIT:port>' ] [ '<STR_LIT>' ] = None <EOL> port_req_body [ '<STR_LIT:port>' ] [ '<STR_LIT>' ] = { } <EOL> try : <EOL> port_client . update_port ( port_id , port_req_body ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , port_id ) <EOL> def _process_requested_networks ( self , context , instance , neutron , <EOL> requested_networks , hypervisor_macs = None ) : <EOL> """<STR_LIT>""" <EOL> available_macs = None <EOL> if hypervisor_macs is not None : <EOL> available_macs = set ( hypervisor_macs ) <EOL> ports = { } <EOL> net_ids = [ ] <EOL> ordered_networks = [ ] <EOL> if requested_networks : <EOL> for request in requested_networks : <EOL> if request . port_id : <EOL> port = self . _show_port ( context , request . port_id , <EOL> neutron_client = neutron ) <EOL> if port [ '<STR_LIT>' ] != instance . project_id : <EOL> raise exception . PortNotUsable ( port_id = request . port_id , <EOL> instance = instance . uuid ) <EOL> if port . get ( '<STR_LIT>' ) : <EOL> raise exception . PortInUse ( port_id = request . port_id ) <EOL> if port . get ( '<STR_LIT>' ) : <EOL> if port [ '<STR_LIT>' ] != instance . hostname : <EOL> raise exception . PortNotUsableDNS ( <EOL> port_id = request . port_id , <EOL> instance = instance . uuid , value = port [ '<STR_LIT>' ] , <EOL> hostname = instance . hostname ) <EOL> if ( port . get ( '<STR_LIT>' ) == <EOL> network_model . VIF_TYPE_BINDING_FAILED ) : <EOL> raise exception . PortBindingFailed ( <EOL> port_id = request . port_id ) <EOL> if hypervisor_macs is not None : <EOL> if port [ '<STR_LIT>' ] not in hypervisor_macs : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> { '<STR_LIT:port>' : request . port_id , <EOL> '<STR_LIT>' : port [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : hypervisor_macs } , <EOL> instance = instance ) <EOL> raise exception . PortNotUsable ( <EOL> port_id = request . port_id , <EOL> instance = instance . uuid ) <EOL> available_macs . discard ( port [ '<STR_LIT>' ] ) <EOL> request . network_id = port [ '<STR_LIT>' ] <EOL> ports [ request . port_id ] = port <EOL> if request . network_id : <EOL> net_ids . append ( request . network_id ) <EOL> ordered_networks . append ( request ) <EOL> return ports , net_ids , ordered_networks , available_macs <EOL> def _process_security_groups ( self , instance , neutron , security_groups ) : <EOL> """<STR_LIT>""" <EOL> security_group_ids = [ ] <EOL> if len ( security_groups ) : <EOL> search_opts = { '<STR_LIT>' : instance . project_id } <EOL> user_security_groups = neutron . list_security_groups ( <EOL> ** search_opts ) . get ( '<STR_LIT>' ) <EOL> for security_group in security_groups : <EOL> name_match = None <EOL> uuid_match = None <EOL> for user_security_group in user_security_groups : <EOL> if user_security_group [ '<STR_LIT:name>' ] == security_group : <EOL> if name_match : <EOL> raise exception . NoUniqueMatch ( <EOL> _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % <EOL> security_group ) <EOL> name_match = user_security_group [ '<STR_LIT:id>' ] <EOL> if user_security_group [ '<STR_LIT:id>' ] == security_group : <EOL> uuid_match = user_security_group [ '<STR_LIT:id>' ] <EOL> if name_match : <EOL> security_group_ids . append ( name_match ) <EOL> elif uuid_match : <EOL> security_group_ids . append ( uuid_match ) <EOL> else : <EOL> raise exception . SecurityGroupNotFound ( <EOL> security_group_id = security_group ) <EOL> return security_group_ids <EOL> def allocate_for_instance ( self , context , instance , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> hypervisor_macs = kwargs . get ( '<STR_LIT>' , None ) <EOL> neutron = get_client ( context ) <EOL> port_client = ( neutron if not <EOL> self . _has_port_binding_extension ( context , <EOL> refresh_cache = True , neutron = neutron ) else <EOL> get_client ( context , admin = True ) ) <EOL> admin_client = port_client if neutron != port_client else None <EOL> LOG . debug ( '<STR_LIT>' , instance = instance ) <EOL> if not instance . project_id : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> raise exception . InvalidInput ( <EOL> reason = msg % instance . uuid ) <EOL> requested_networks = kwargs . get ( '<STR_LIT>' ) <EOL> dhcp_opts = kwargs . get ( '<STR_LIT>' , None ) <EOL> bind_host_id = kwargs . get ( '<STR_LIT>' ) <EOL> ports , net_ids , ordered_networks , available_macs = ( <EOL> self . _process_requested_networks ( context , <EOL> instance , neutron , requested_networks , hypervisor_macs ) ) <EOL> nets = self . _get_available_networks ( context , instance . project_id , <EOL> net_ids , neutron = neutron ) <EOL> if not nets : <EOL> if requested_networks : <EOL> for request in requested_networks : <EOL> if not request . port_id and request . network_id : <EOL> raise exception . NetworkNotFound ( <EOL> network_id = request . network_id ) <EOL> else : <EOL> LOG . debug ( "<STR_LIT>" , instance = instance ) <EOL> return network_model . NetworkInfo ( [ ] ) <EOL> if ( not requested_networks <EOL> or requested_networks . is_single_unspecified ) : <EOL> if not nets : <EOL> raise exception . InterfaceAttachFailedNoNetwork ( <EOL> project_id = instance . project_id ) <EOL> if len ( nets ) > <NUM_LIT:1> : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise exception . NetworkAmbiguous ( msg ) <EOL> ordered_networks . append ( <EOL> objects . NetworkRequest ( network_id = nets [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] ) ) <EOL> self . _check_external_network_attach ( context , nets ) <EOL> security_groups = kwargs . get ( '<STR_LIT>' , [ ] ) <EOL> security_group_ids = self . _process_security_groups ( <EOL> instance , neutron , security_groups ) <EOL> preexisting_port_ids = [ ] <EOL> created_port_ids = [ ] <EOL> ports_in_requested_order = [ ] <EOL> nets_in_requested_order = [ ] <EOL> for request in ordered_networks : <EOL> network = None <EOL> for net in nets : <EOL> if net [ '<STR_LIT:id>' ] == request . network_id : <EOL> network = net <EOL> break <EOL> else : <EOL> continue <EOL> nets_in_requested_order . append ( network ) <EOL> if ( security_groups and not ( <EOL> network [ '<STR_LIT>' ] <EOL> and network . get ( '<STR_LIT>' , True ) ) ) : <EOL> raise exception . SecurityGroupCannotBeApplied ( ) <EOL> zone = '<STR_LIT>' % instance . availability_zone <EOL> port_req_body = { '<STR_LIT:port>' : { '<STR_LIT>' : instance . uuid , <EOL> '<STR_LIT>' : zone } } <EOL> try : <EOL> self . _populate_neutron_extension_values ( <EOL> context , instance , request . pci_request_id , port_req_body , <EOL> network = network , neutron = neutron , <EOL> bind_host_id = bind_host_id ) <EOL> self . _populate_mac_address ( instance , request . pci_request_id , <EOL> port_req_body ) <EOL> if request . port_id : <EOL> port = ports [ request . port_id ] <EOL> port_client . update_port ( port [ '<STR_LIT:id>' ] , port_req_body ) <EOL> preexisting_port_ids . append ( port [ '<STR_LIT:id>' ] ) <EOL> ports_in_requested_order . append ( port [ '<STR_LIT:id>' ] ) <EOL> else : <EOL> created_port = self . _create_port ( <EOL> port_client , instance , request . network_id , <EOL> port_req_body , request . address , <EOL> security_group_ids , available_macs , dhcp_opts ) <EOL> created_port_ids . append ( created_port ) <EOL> ports_in_requested_order . append ( created_port ) <EOL> self . _update_port_dns_name ( context , instance , network , <EOL> ports_in_requested_order [ - <NUM_LIT:1> ] , <EOL> neutron ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> self . _unbind_ports ( context , <EOL> preexisting_port_ids , <EOL> neutron , port_client ) <EOL> self . _delete_ports ( neutron , instance , created_port_ids ) <EOL> nw_info = self . get_instance_nw_info ( <EOL> context , instance , networks = nets_in_requested_order , <EOL> port_ids = ports_in_requested_order , <EOL> admin_client = admin_client , <EOL> preexisting_port_ids = preexisting_port_ids , <EOL> update_cells = True ) <EOL> return network_model . NetworkInfo ( [ vif for vif in nw_info <EOL> if vif [ '<STR_LIT:id>' ] in created_port_ids + <EOL> preexisting_port_ids ] ) <EOL> def _refresh_neutron_extensions_cache ( self , context , neutron = None ) : <EOL> """<STR_LIT>""" <EOL> if ( not self . last_neutron_extension_sync or <EOL> ( ( time . time ( ) - self . last_neutron_extension_sync ) <EOL> >= CONF . neutron . extension_sync_interval ) ) : <EOL> if neutron is None : <EOL> neutron = get_client ( context ) <EOL> extensions_list = neutron . list_extensions ( ) [ '<STR_LIT>' ] <EOL> self . last_neutron_extension_sync = time . time ( ) <EOL> self . extensions . clear ( ) <EOL> self . extensions = { ext [ '<STR_LIT:name>' ] : ext for ext in extensions_list } <EOL> def _has_port_binding_extension ( self , context , refresh_cache = False , <EOL> neutron = None ) : <EOL> if refresh_cache : <EOL> self . _refresh_neutron_extensions_cache ( context , neutron = neutron ) <EOL> return constants . PORTBINDING_EXT in self . extensions <EOL> @ staticmethod <EOL> def _populate_neutron_binding_profile ( instance , pci_request_id , <EOL> port_req_body ) : <EOL> """<STR_LIT>""" <EOL> if pci_request_id : <EOL> pci_dev = pci_manager . get_instance_pci_devs ( <EOL> instance , pci_request_id ) . pop ( ) <EOL> devspec = pci_whitelist . get_pci_device_devspec ( pci_dev ) <EOL> profile = { '<STR_LIT>' : "<STR_LIT>" % ( pci_dev . vendor_id , <EOL> pci_dev . product_id ) , <EOL> '<STR_LIT>' : pci_dev . address , <EOL> '<STR_LIT>' : <EOL> devspec . get_tags ( ) . get ( '<STR_LIT>' ) <EOL> } <EOL> port_req_body [ '<STR_LIT:port>' ] [ '<STR_LIT>' ] = profile <EOL> @ staticmethod <EOL> def _populate_mac_address ( instance , pci_request_id , port_req_body ) : <EOL> """<STR_LIT>""" <EOL> if pci_request_id is not None : <EOL> pci_devs = pci_manager . get_instance_pci_devs ( <EOL> instance , pci_request_id ) <EOL> if len ( pci_devs ) != <NUM_LIT:1> : <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> pci_request , instance = instance ) <EOL> return <EOL> pci_dev = pci_devs [ <NUM_LIT:0> ] <EOL> if pci_dev . dev_type == obj_fields . PciDeviceType . SRIOV_PF : <EOL> try : <EOL> mac = pci_utils . get_mac_by_pci_address ( pci_dev . address ) <EOL> except exception . PciDeviceNotFoundById as e : <EOL> LOG . error ( <EOL> _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { "<STR_LIT>" : pci_dev . address , "<STR_LIT:e>" : e } , instance = instance ) <EOL> else : <EOL> port_req_body [ '<STR_LIT:port>' ] [ '<STR_LIT>' ] = mac <EOL> def _populate_neutron_extension_values ( self , context , instance , <EOL> pci_request_id , port_req_body , <EOL> network = None , neutron = None , <EOL> bind_host_id = None ) : <EOL> """<STR_LIT>""" <EOL> self . _refresh_neutron_extensions_cache ( context , neutron = neutron ) <EOL> if constants . QOS_QUEUE in self . extensions : <EOL> flavor = instance . get_flavor ( ) <EOL> rxtx_factor = flavor . get ( '<STR_LIT>' ) <EOL> port_req_body [ '<STR_LIT:port>' ] [ '<STR_LIT>' ] = rxtx_factor <EOL> has_port_binding_extension = ( <EOL> self . _has_port_binding_extension ( context , neutron = neutron ) ) <EOL> if has_port_binding_extension : <EOL> port_req_body [ '<STR_LIT:port>' ] [ '<STR_LIT>' ] = bind_host_id <EOL> self . _populate_neutron_binding_profile ( instance , <EOL> pci_request_id , <EOL> port_req_body ) <EOL> if constants . DNS_INTEGRATION in self . extensions : <EOL> if ( not has_port_binding_extension <EOL> or not network . get ( '<STR_LIT>' ) ) : <EOL> port_req_body [ '<STR_LIT:port>' ] [ '<STR_LIT>' ] = instance . hostname <EOL> def _update_port_dns_name ( self , context , instance , network , port_id , <EOL> neutron ) : <EOL> """<STR_LIT>""" <EOL> if ( constants . DNS_INTEGRATION in self . extensions and <EOL> self . _has_port_binding_extension ( context ) and <EOL> network . get ( '<STR_LIT>' ) ) : <EOL> try : <EOL> port_req_body = { '<STR_LIT:port>' : { '<STR_LIT>' : instance . hostname } } <EOL> neutron . update_port ( port_id , port_req_body ) <EOL> except neutron_client_exc . BadRequest : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : instance . hostname } , instance = instance ) <EOL> msg = ( _ ( '<STR_LIT>' <EOL> '<STR_LIT:name>' ) % { '<STR_LIT>' : instance . hostname } ) <EOL> raise exception . InvalidInput ( reason = msg ) <EOL> def _delete_ports ( self , neutron , instance , ports , raise_if_fail = False ) : <EOL> exceptions = [ ] <EOL> for port in ports : <EOL> try : <EOL> neutron . delete_port ( port ) <EOL> except neutron_client_exc . NeutronClientException as e : <EOL> if e . status_code == <NUM_LIT> : <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) , port , <EOL> instance = instance ) <EOL> else : <EOL> exceptions . append ( e ) <EOL> LOG . warning ( <EOL> _LW ( "<STR_LIT>" ) , <EOL> port , instance = instance , exc_info = True ) <EOL> if len ( exceptions ) > <NUM_LIT:0> and raise_if_fail : <EOL> raise exceptions [ <NUM_LIT:0> ] <EOL> def deallocate_for_instance ( self , context , instance , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' , instance = instance ) <EOL> search_opts = { '<STR_LIT>' : instance . uuid } <EOL> neutron = get_client ( context ) <EOL> data = neutron . list_ports ( ** search_opts ) <EOL> ports = [ port [ '<STR_LIT:id>' ] for port in data . get ( '<STR_LIT>' , [ ] ) ] <EOL> requested_networks = kwargs . get ( '<STR_LIT>' ) or [ ] <EOL> if isinstance ( requested_networks , objects . NetworkRequestList ) : <EOL> requested_networks = requested_networks . as_tuples ( ) <EOL> ports_to_skip = set ( [ port_id for nets , fips , port_id , pci_request_id <EOL> in requested_networks ] ) <EOL> ports_to_skip |= set ( self . _get_preexisting_port_ids ( instance ) ) <EOL> ports = set ( ports ) - ports_to_skip <EOL> self . _unbind_ports ( context , ports_to_skip , neutron ) <EOL> self . _delete_ports ( neutron , instance , ports , raise_if_fail = True ) <EOL> base_api . update_instance_cache_with_nw_info ( self , context , instance , <EOL> network_model . NetworkInfo ( [ ] ) ) <EOL> def allocate_port_for_instance ( self , context , instance , port_id , <EOL> network_id = None , requested_ip = None , <EOL> bind_host_id = None ) : <EOL> """<STR_LIT>""" <EOL> requested_networks = objects . NetworkRequestList ( <EOL> objects = [ objects . NetworkRequest ( network_id = network_id , <EOL> address = requested_ip , <EOL> port_id = port_id , <EOL> pci_request_id = None ) ] ) <EOL> return self . allocate_for_instance ( context , instance , <EOL> requested_networks = requested_networks , <EOL> bind_host_id = bind_host_id ) <EOL> def deallocate_port_for_instance ( self , context , instance , port_id ) : <EOL> """<STR_LIT>""" <EOL> neutron = get_client ( context ) <EOL> preexisting_ports = self . _get_preexisting_port_ids ( instance ) <EOL> if port_id in preexisting_ports : <EOL> self . _unbind_ports ( context , [ port_id ] , neutron ) <EOL> else : <EOL> self . _delete_ports ( neutron , instance , [ port_id ] , <EOL> raise_if_fail = True ) <EOL> return self . get_instance_nw_info ( context , instance ) <EOL> def list_ports ( self , context , ** search_opts ) : <EOL> """<STR_LIT>""" <EOL> return get_client ( context ) . list_ports ( ** search_opts ) <EOL> def show_port ( self , context , port_id ) : <EOL> """<STR_LIT>""" <EOL> return dict ( port = self . _show_port ( context , port_id ) ) <EOL> def _show_port ( self , context , port_id , neutron_client = None , fields = None ) : <EOL> """<STR_LIT>""" <EOL> if not neutron_client : <EOL> neutron_client = get_client ( context ) <EOL> try : <EOL> if fields : <EOL> result = neutron_client . show_port ( port_id , fields = fields ) <EOL> else : <EOL> result = neutron_client . show_port ( port_id ) <EOL> return result . get ( '<STR_LIT:port>' ) <EOL> except neutron_client_exc . PortNotFoundClient : <EOL> raise exception . PortNotFound ( port_id = port_id ) <EOL> except neutron_client_exc . Unauthorized : <EOL> raise exception . Forbidden ( ) <EOL> except neutron_client_exc . NeutronClientException as exc : <EOL> msg = ( _ ( "<STR_LIT>" ) % <EOL> { '<STR_LIT>' : port_id , '<STR_LIT>' : exc } ) <EOL> raise exception . NovaException ( message = msg ) <EOL> def _get_instance_nw_info ( self , context , instance , networks = None , <EOL> port_ids = None , admin_client = None , <EOL> preexisting_port_ids = None , ** kwargs ) : <EOL> LOG . debug ( '<STR_LIT>' , instance = instance ) <EOL> compute_utils . refresh_info_cache_for_instance ( context , instance ) <EOL> nw_info = self . _build_network_info_model ( context , instance , networks , <EOL> port_ids , admin_client , <EOL> preexisting_port_ids ) <EOL> return network_model . NetworkInfo . hydrate ( nw_info ) <EOL> def _gather_port_ids_and_networks ( self , context , instance , networks = None , <EOL> port_ids = None ) : <EOL> """<STR_LIT>""" <EOL> if ( ( networks is None and port_ids is not None ) or <EOL> ( port_ids is None and networks is not None ) ) : <EOL> message = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise exception . NovaException ( message = message ) <EOL> ifaces = compute_utils . get_nw_info_for_instance ( instance ) <EOL> if port_ids is None : <EOL> port_ids = [ iface [ '<STR_LIT:id>' ] for iface in ifaces ] <EOL> net_ids = [ iface [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] for iface in ifaces ] <EOL> if networks is None : <EOL> networks = self . _get_available_networks ( context , <EOL> instance . project_id , <EOL> net_ids ) <EOL> else : <EOL> networks_ids = [ network [ '<STR_LIT:id>' ] for network in networks ] <EOL> networks = networks + [ <EOL> { '<STR_LIT:id>' : iface [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT:name>' : iface [ '<STR_LIT>' ] [ '<STR_LIT:label>' ] , <EOL> '<STR_LIT>' : iface [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] } <EOL> for iface in ifaces <EOL> if _is_not_duplicate ( iface [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] , <EOL> networks_ids , <EOL> "<STR_LIT>" , <EOL> instance ) ] <EOL> port_ids = [ iface [ '<STR_LIT:id>' ] for iface in ifaces <EOL> if _is_not_duplicate ( iface [ '<STR_LIT:id>' ] , <EOL> port_ids , <EOL> "<STR_LIT>" , <EOL> instance ) ] + port_ids <EOL> return networks , port_ids <EOL> @ base_api . refresh_cache <EOL> def add_fixed_ip_to_instance ( self , context , instance , network_id ) : <EOL> """<STR_LIT>""" <EOL> neutron = get_client ( context ) <EOL> search_opts = { '<STR_LIT>' : network_id } <EOL> data = neutron . list_subnets ( ** search_opts ) <EOL> ipam_subnets = data . get ( '<STR_LIT>' , [ ] ) <EOL> if not ipam_subnets : <EOL> raise exception . NetworkNotFoundForInstance ( <EOL> instance_id = instance . uuid ) <EOL> zone = '<STR_LIT>' % instance . availability_zone <EOL> search_opts = { '<STR_LIT>' : instance . uuid , <EOL> '<STR_LIT>' : zone , <EOL> '<STR_LIT>' : network_id } <EOL> data = neutron . list_ports ( ** search_opts ) <EOL> ports = data [ '<STR_LIT>' ] <EOL> for p in ports : <EOL> for subnet in ipam_subnets : <EOL> fixed_ips = p [ '<STR_LIT>' ] <EOL> fixed_ips . append ( { '<STR_LIT>' : subnet [ '<STR_LIT:id>' ] } ) <EOL> port_req_body = { '<STR_LIT:port>' : { '<STR_LIT>' : fixed_ips } } <EOL> try : <EOL> neutron . update_port ( p [ '<STR_LIT:id>' ] , port_req_body ) <EOL> return self . _get_instance_nw_info ( context , instance ) <EOL> except Exception as ex : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> LOG . debug ( msg , { '<STR_LIT>' : p [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : subnet [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : ex } , instance = instance ) <EOL> raise exception . NetworkNotFoundForInstance ( <EOL> instance_id = instance . uuid ) <EOL> @ base_api . refresh_cache <EOL> def remove_fixed_ip_from_instance ( self , context , instance , address ) : <EOL> """<STR_LIT>""" <EOL> neutron = get_client ( context ) <EOL> zone = '<STR_LIT>' % instance . availability_zone <EOL> search_opts = { '<STR_LIT>' : instance . uuid , <EOL> '<STR_LIT>' : zone , <EOL> '<STR_LIT>' : '<STR_LIT>' % address } <EOL> data = neutron . list_ports ( ** search_opts ) <EOL> ports = data [ '<STR_LIT>' ] <EOL> for p in ports : <EOL> fixed_ips = p [ '<STR_LIT>' ] <EOL> new_fixed_ips = [ ] <EOL> for fixed_ip in fixed_ips : <EOL> if fixed_ip [ '<STR_LIT>' ] != address : <EOL> new_fixed_ips . append ( fixed_ip ) <EOL> port_req_body = { '<STR_LIT:port>' : { '<STR_LIT>' : new_fixed_ips } } <EOL> try : <EOL> neutron . update_port ( p [ '<STR_LIT:id>' ] , port_req_body ) <EOL> except Exception as ex : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> LOG . debug ( msg , { '<STR_LIT>' : p [ '<STR_LIT:id>' ] , '<STR_LIT>' : ex } , <EOL> instance = instance ) <EOL> return self . _get_instance_nw_info ( context , instance ) <EOL> raise exception . FixedIpNotFoundForSpecificInstance ( <EOL> instance_uuid = instance . uuid , ip = address ) <EOL> def _get_port_vnic_info ( self , context , neutron , port_id ) : <EOL> """<STR_LIT>""" <EOL> phynet_name = None <EOL> port = self . _show_port ( context , port_id , neutron_client = neutron , <EOL> fields = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> vnic_type = port . get ( '<STR_LIT>' , <EOL> network_model . VNIC_TYPE_NORMAL ) <EOL> if vnic_type in network_model . VNIC_TYPES_SRIOV : <EOL> net_id = port [ '<STR_LIT>' ] <EOL> net = neutron . show_network ( net_id , <EOL> fields = '<STR_LIT>' ) . get ( '<STR_LIT>' ) <EOL> phynet_name = net . get ( '<STR_LIT>' ) <EOL> return vnic_type , phynet_name <EOL> def create_pci_requests_for_sriov_ports ( self , context , pci_requests , <EOL> requested_networks ) : <EOL> """<STR_LIT>""" <EOL> if not requested_networks : <EOL> return <EOL> neutron = get_client ( context , admin = True ) <EOL> for request_net in requested_networks : <EOL> phynet_name = None <EOL> vnic_type = network_model . VNIC_TYPE_NORMAL <EOL> if request_net . port_id : <EOL> vnic_type , phynet_name = self . _get_port_vnic_info ( <EOL> context , neutron , request_net . port_id ) <EOL> pci_request_id = None <EOL> if vnic_type in network_model . VNIC_TYPES_SRIOV : <EOL> spec = { pci_request . PCI_NET_TAG : phynet_name } <EOL> dev_type = pci_request . DEVICE_TYPE_FOR_VNIC_TYPE . get ( vnic_type ) <EOL> if dev_type : <EOL> spec [ pci_request . PCI_DEVICE_TYPE_TAG ] = dev_type <EOL> request = objects . InstancePCIRequest ( <EOL> count = <NUM_LIT:1> , <EOL> spec = [ spec ] , <EOL> request_id = str ( uuid . uuid4 ( ) ) ) <EOL> pci_requests . requests . append ( request ) <EOL> pci_request_id = request . request_id <EOL> request_net . pci_request_id = pci_request_id <EOL> def _ports_needed_per_instance ( self , context , neutron , requested_networks ) : <EOL> ports_needed_per_instance = <NUM_LIT:0> <EOL> if requested_networks is None or len ( requested_networks ) == <NUM_LIT:0> : <EOL> nets = self . _get_available_networks ( context , context . project_id , <EOL> neutron = neutron ) <EOL> if len ( nets ) > <NUM_LIT:1> : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise exception . NetworkAmbiguous ( msg ) <EOL> else : <EOL> ports_needed_per_instance = <NUM_LIT:1> <EOL> else : <EOL> net_ids_requested = [ ] <EOL> if isinstance ( requested_networks [ <NUM_LIT:0> ] , tuple ) : <EOL> requested_networks = objects . NetworkRequestList ( <EOL> objects = [ objects . NetworkRequest . from_tuple ( t ) <EOL> for t in requested_networks ] ) <EOL> for request in requested_networks : <EOL> if request . port_id : <EOL> port = self . _show_port ( context , request . port_id , <EOL> neutron_client = neutron ) <EOL> if port . get ( '<STR_LIT>' , None ) : <EOL> raise exception . PortInUse ( port_id = request . port_id ) <EOL> if not port . get ( '<STR_LIT>' ) : <EOL> raise exception . PortRequiresFixedIP ( <EOL> port_id = request . port_id ) <EOL> request . network_id = port [ '<STR_LIT>' ] <EOL> else : <EOL> ports_needed_per_instance += <NUM_LIT:1> <EOL> net_ids_requested . append ( request . network_id ) <EOL> if request . address : <EOL> search_opts = { '<STR_LIT>' : request . network_id , <EOL> '<STR_LIT>' : '<STR_LIT>' % ( <EOL> request . address ) , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> existing_ports = neutron . list_ports ( <EOL> ** search_opts ) [ '<STR_LIT>' ] <EOL> if existing_ports : <EOL> i_uuid = existing_ports [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> raise exception . FixedIpAlreadyInUse ( <EOL> address = request . address , <EOL> instance_uuid = i_uuid ) <EOL> if net_ids_requested : <EOL> nets = self . _get_available_networks ( <EOL> context , context . project_id , net_ids_requested , <EOL> neutron = neutron ) <EOL> for net in nets : <EOL> if not net . get ( '<STR_LIT>' ) : <EOL> raise exception . NetworkRequiresSubnet ( <EOL> network_uuid = net [ '<STR_LIT:id>' ] ) <EOL> if len ( nets ) != len ( net_ids_requested ) : <EOL> requested_netid_set = set ( net_ids_requested ) <EOL> returned_netid_set = set ( [ net [ '<STR_LIT:id>' ] for net in nets ] ) <EOL> lostid_set = requested_netid_set - returned_netid_set <EOL> if lostid_set : <EOL> id_str = '<STR_LIT>' <EOL> for _id in lostid_set : <EOL> id_str = id_str and id_str + '<STR_LIT:U+002CU+0020>' + _id or _id <EOL> raise exception . NetworkNotFound ( network_id = id_str ) <EOL> return ports_needed_per_instance <EOL> def validate_networks ( self , context , requested_networks , num_instances ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' , requested_networks ) <EOL> neutron = get_client ( context ) <EOL> ports_needed_per_instance = self . _ports_needed_per_instance ( <EOL> context , neutron , requested_networks ) <EOL> if ports_needed_per_instance : <EOL> quotas = neutron . show_quota ( tenant_id = context . project_id ) [ '<STR_LIT>' ] <EOL> if quotas . get ( '<STR_LIT:port>' , - <NUM_LIT:1> ) == - <NUM_LIT:1> : <EOL> return num_instances <EOL> params = dict ( tenant_id = context . project_id , fields = [ '<STR_LIT:id>' ] ) <EOL> ports = neutron . list_ports ( ** params ) [ '<STR_LIT>' ] <EOL> free_ports = quotas . get ( '<STR_LIT:port>' ) - len ( ports ) <EOL> if free_ports < <NUM_LIT:0> : <EOL> msg = ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % <EOL> { '<STR_LIT>' : len ( ports ) , <EOL> '<STR_LIT>' : quotas . get ( '<STR_LIT:port>' ) } ) <EOL> raise exception . PortLimitExceeded ( msg ) <EOL> ports_needed = ports_needed_per_instance * num_instances <EOL> if free_ports >= ports_needed : <EOL> return num_instances <EOL> else : <EOL> return free_ports // ports_needed_per_instance <EOL> return num_instances <EOL> def _get_instance_uuids_by_ip ( self , context , address ) : <EOL> """<STR_LIT>""" <EOL> search_opts = { "<STR_LIT>" : '<STR_LIT>' % address } <EOL> data = get_client ( context ) . list_ports ( ** search_opts ) <EOL> ports = data . get ( '<STR_LIT>' , [ ] ) <EOL> return [ { '<STR_LIT>' : port [ '<STR_LIT>' ] } for port in ports <EOL> if port [ '<STR_LIT>' ] ] <EOL> def _get_port_id_by_fixed_address ( self , client , <EOL> instance , address ) : <EOL> """<STR_LIT>""" <EOL> zone = '<STR_LIT>' % instance . availability_zone <EOL> search_opts = { '<STR_LIT>' : instance . uuid , <EOL> '<STR_LIT>' : zone } <EOL> data = client . list_ports ( ** search_opts ) <EOL> ports = data [ '<STR_LIT>' ] <EOL> port_id = None <EOL> for p in ports : <EOL> for ip in p [ '<STR_LIT>' ] : <EOL> if ip [ '<STR_LIT>' ] == address : <EOL> port_id = p [ '<STR_LIT:id>' ] <EOL> break <EOL> if not port_id : <EOL> raise exception . FixedIpNotFoundForAddress ( address = address ) <EOL> return port_id <EOL> @ base_api . refresh_cache <EOL> def associate_floating_ip ( self , context , instance , <EOL> floating_address , fixed_address , <EOL> affect_auto_assigned = False ) : <EOL> """<STR_LIT>""" <EOL> client = get_client ( context ) <EOL> port_id = self . _get_port_id_by_fixed_address ( client , instance , <EOL> fixed_address ) <EOL> fip = self . _get_floating_ip_by_address ( client , floating_address ) <EOL> param = { '<STR_LIT>' : port_id , <EOL> '<STR_LIT>' : fixed_address } <EOL> client . update_floatingip ( fip [ '<STR_LIT:id>' ] , { '<STR_LIT>' : param } ) <EOL> if fip [ '<STR_LIT>' ] : <EOL> port = self . _show_port ( context , fip [ '<STR_LIT>' ] , <EOL> neutron_client = client ) <EOL> orig_instance_uuid = port [ '<STR_LIT>' ] <EOL> msg_dict = dict ( address = floating_address , <EOL> instance_id = orig_instance_uuid ) <EOL> LOG . info ( _LI ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , msg_dict , <EOL> instance = instance ) <EOL> orig_instance = objects . Instance . get_by_uuid ( context , <EOL> orig_instance_uuid ) <EOL> base_api . update_instance_cache_with_nw_info ( self , context , <EOL> orig_instance ) <EOL> def get_all ( self , context ) : <EOL> """<STR_LIT>""" <EOL> client = get_client ( context ) <EOL> networks = client . list_networks ( ) . get ( '<STR_LIT>' ) <EOL> network_objs = [ ] <EOL> for network in networks : <EOL> network_objs . append ( objects . Network ( context = context , <EOL> name = network [ '<STR_LIT:name>' ] , <EOL> label = network [ '<STR_LIT:name>' ] , <EOL> uuid = network [ '<STR_LIT:id>' ] ) ) <EOL> return objects . NetworkList ( context = context , <EOL> objects = network_objs ) <EOL> def get ( self , context , network_uuid ) : <EOL> """<STR_LIT>""" <EOL> client = get_client ( context ) <EOL> try : <EOL> network = client . show_network ( network_uuid ) . get ( '<STR_LIT>' ) or { } <EOL> except neutron_client_exc . NetworkNotFoundClient : <EOL> raise exception . NetworkNotFound ( network_id = network_uuid ) <EOL> net_obj = objects . Network ( context = context , <EOL> name = network [ '<STR_LIT:name>' ] , <EOL> label = network [ '<STR_LIT:name>' ] , <EOL> uuid = network [ '<STR_LIT:id>' ] ) <EOL> return net_obj <EOL> def delete ( self , context , network_uuid ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def disassociate ( self , context , network_uuid ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def associate ( self , context , network_uuid , host = base_api . SENTINEL , <EOL> project = base_api . SENTINEL ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def get_fixed_ip ( self , context , id ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def get_fixed_ip_by_address ( self , context , address ) : <EOL> """<STR_LIT>""" <EOL> uuid_maps = self . _get_instance_uuids_by_ip ( context , address ) <EOL> if len ( uuid_maps ) == <NUM_LIT:1> : <EOL> return uuid_maps [ <NUM_LIT:0> ] <EOL> elif not uuid_maps : <EOL> raise exception . FixedIpNotFoundForAddress ( address = address ) <EOL> else : <EOL> raise exception . FixedIpAssociatedWithMultipleInstances ( <EOL> address = address ) <EOL> def _setup_net_dict ( self , client , network_id ) : <EOL> if not network_id : <EOL> return { } <EOL> pool = client . show_network ( network_id ) [ '<STR_LIT>' ] <EOL> return { pool [ '<STR_LIT:id>' ] : pool } <EOL> def _setup_port_dict ( self , context , client , port_id ) : <EOL> if not port_id : <EOL> return { } <EOL> port = self . _show_port ( context , port_id , neutron_client = client ) <EOL> return { port [ '<STR_LIT:id>' ] : port } <EOL> def _setup_pools_dict ( self , client ) : <EOL> pools = self . _get_floating_ip_pools ( client ) <EOL> return { i [ '<STR_LIT:id>' ] : i for i in pools } <EOL> def _setup_ports_dict ( self , client , project_id = None ) : <EOL> search_opts = { '<STR_LIT>' : project_id } if project_id else { } <EOL> ports = client . list_ports ( ** search_opts ) [ '<STR_LIT>' ] <EOL> return { p [ '<STR_LIT:id>' ] : p for p in ports } <EOL> def get_floating_ip ( self , context , id ) : <EOL> """<STR_LIT>""" <EOL> client = get_client ( context ) <EOL> try : <EOL> fip = client . show_floatingip ( id ) [ '<STR_LIT>' ] <EOL> except neutron_client_exc . NeutronClientException as e : <EOL> if e . status_code == <NUM_LIT> : <EOL> raise exception . FloatingIpNotFound ( id = id ) <EOL> else : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , id ) <EOL> pool_dict = self . _setup_net_dict ( client , <EOL> fip [ '<STR_LIT>' ] ) <EOL> port_dict = self . _setup_port_dict ( context , client , fip [ '<STR_LIT>' ] ) <EOL> return self . _format_floating_ip_model ( fip , pool_dict , port_dict ) <EOL> def _get_floating_ip_pools ( self , client , project_id = None ) : <EOL> search_opts = { constants . NET_EXTERNAL : True } <EOL> if project_id : <EOL> search_opts . update ( { '<STR_LIT>' : project_id } ) <EOL> data = client . list_networks ( ** search_opts ) <EOL> return data [ '<STR_LIT>' ] <EOL> def get_floating_ip_pools ( self , context ) : <EOL> """<STR_LIT>""" <EOL> client = get_client ( context ) <EOL> pools = self . _get_floating_ip_pools ( client ) <EOL> return [ n [ '<STR_LIT:name>' ] or n [ '<STR_LIT:id>' ] for n in pools ] <EOL> def _format_floating_ip_model ( self , fip , pool_dict , port_dict ) : <EOL> pool = pool_dict [ fip [ '<STR_LIT>' ] ] <EOL> result = { '<STR_LIT:id>' : fip [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT:address>' : fip [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : pool [ '<STR_LIT:name>' ] or pool [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : fip [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : fip [ '<STR_LIT>' ] , <EOL> } <EOL> result [ '<STR_LIT>' ] = { '<STR_LIT:address>' : fip [ '<STR_LIT>' ] } <EOL> if fip [ '<STR_LIT>' ] : <EOL> instance_uuid = port_dict [ fip [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] <EOL> result [ '<STR_LIT>' ] = { '<STR_LIT>' : instance_uuid } <EOL> result [ '<STR_LIT>' ] [ '<STR_LIT>' ] = instance_uuid <EOL> else : <EOL> result [ '<STR_LIT>' ] = None <EOL> return result <EOL> def get_floating_ip_by_address ( self , context , address ) : <EOL> """<STR_LIT>""" <EOL> client = get_client ( context ) <EOL> fip = self . _get_floating_ip_by_address ( client , address ) <EOL> pool_dict = self . _setup_net_dict ( client , <EOL> fip [ '<STR_LIT>' ] ) <EOL> port_dict = self . _setup_port_dict ( context , client , fip [ '<STR_LIT>' ] ) <EOL> return self . _format_floating_ip_model ( fip , pool_dict , port_dict ) <EOL> def get_floating_ips_by_project ( self , context ) : <EOL> client = get_client ( context ) <EOL> project_id = context . project_id <EOL> fips = self . _safe_get_floating_ips ( client , tenant_id = project_id ) <EOL> if not fips : <EOL> return [ ] <EOL> pool_dict = self . _setup_pools_dict ( client ) <EOL> port_dict = self . _setup_ports_dict ( client , project_id ) <EOL> return [ self . _format_floating_ip_model ( fip , pool_dict , port_dict ) <EOL> for fip in fips ] <EOL> def get_instance_id_by_floating_address ( self , context , address ) : <EOL> """<STR_LIT>""" <EOL> client = get_client ( context ) <EOL> fip = self . _get_floating_ip_by_address ( client , address ) <EOL> if not fip [ '<STR_LIT>' ] : <EOL> return None <EOL> port = self . _show_port ( context , fip [ '<STR_LIT>' ] , neutron_client = client ) <EOL> return port [ '<STR_LIT>' ] <EOL> def get_vifs_by_instance ( self , context , instance ) : <EOL> raise NotImplementedError ( ) <EOL> def get_vif_by_mac_address ( self , context , mac_address ) : <EOL> raise NotImplementedError ( ) <EOL> def _get_floating_ip_pool_id_by_name_or_id ( self , client , name_or_id ) : <EOL> search_opts = { constants . NET_EXTERNAL : True , '<STR_LIT>' : '<STR_LIT:id>' } <EOL> if uuidutils . is_uuid_like ( name_or_id ) : <EOL> search_opts . update ( { '<STR_LIT:id>' : name_or_id } ) <EOL> else : <EOL> search_opts . update ( { '<STR_LIT:name>' : name_or_id } ) <EOL> data = client . list_networks ( ** search_opts ) <EOL> nets = data [ '<STR_LIT>' ] <EOL> if len ( nets ) == <NUM_LIT:1> : <EOL> return nets [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] <EOL> elif len ( nets ) == <NUM_LIT:0> : <EOL> raise exception . FloatingIpPoolNotFound ( ) <EOL> else : <EOL> msg = ( _ ( "<STR_LIT>" ) <EOL> % name_or_id ) <EOL> raise exception . NovaException ( message = msg ) <EOL> def allocate_floating_ip ( self , context , pool = None ) : <EOL> """<STR_LIT>""" <EOL> client = get_client ( context ) <EOL> pool = pool or CONF . default_floating_pool <EOL> pool_id = self . _get_floating_ip_pool_id_by_name_or_id ( client , pool ) <EOL> param = { '<STR_LIT>' : { '<STR_LIT>' : pool_id } } <EOL> try : <EOL> fip = client . create_floatingip ( param ) <EOL> except ( neutron_client_exc . IpAddressGenerationFailureClient , <EOL> neutron_client_exc . ExternalIpAddressExhaustedClient ) as e : <EOL> raise exception . NoMoreFloatingIps ( six . text_type ( e ) ) <EOL> except neutron_client_exc . OverQuotaClient as e : <EOL> raise exception . FloatingIpLimitExceeded ( six . text_type ( e ) ) <EOL> except neutron_client_exc . BadRequest as e : <EOL> raise exception . FloatingIpBadRequest ( six . text_type ( e ) ) <EOL> return fip [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> def _safe_get_floating_ips ( self , client , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return client . list_floatingips ( ** kwargs ) [ '<STR_LIT>' ] <EOL> except neutron_client_exc . NotFound : <EOL> return [ ] <EOL> except neutron_client_exc . NeutronClientException as e : <EOL> if e . status_code == <NUM_LIT> : <EOL> return [ ] <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' % ( k , v ) <EOL> for k , v in six . iteritems ( kwargs ) ] ) ) <EOL> def _get_floating_ip_by_address ( self , client , address ) : <EOL> """<STR_LIT>""" <EOL> if not address : <EOL> raise exception . FloatingIpNotFoundForAddress ( address = address ) <EOL> fips = self . _safe_get_floating_ips ( client , floating_ip_address = address ) <EOL> if len ( fips ) == <NUM_LIT:0> : <EOL> raise exception . FloatingIpNotFoundForAddress ( address = address ) <EOL> elif len ( fips ) > <NUM_LIT:1> : <EOL> raise exception . FloatingIpMultipleFoundForAddress ( address = address ) <EOL> return fips [ <NUM_LIT:0> ] <EOL> def _get_floating_ips_by_fixed_and_port ( self , client , fixed_ip , port ) : <EOL> """<STR_LIT>""" <EOL> return self . _safe_get_floating_ips ( client , fixed_ip_address = fixed_ip , <EOL> port_id = port ) <EOL> def release_floating_ip ( self , context , address , <EOL> affect_auto_assigned = False ) : <EOL> """<STR_LIT>""" <EOL> self . _release_floating_ip ( context , address ) <EOL> def disassociate_and_release_floating_ip ( self , context , instance , <EOL> floating_ip ) : <EOL> """<STR_LIT>""" <EOL> self . _release_floating_ip ( context , floating_ip [ '<STR_LIT:address>' ] , <EOL> raise_if_associated = False ) <EOL> def _release_floating_ip ( self , context , address , <EOL> raise_if_associated = True ) : <EOL> client = get_client ( context ) <EOL> fip = self . _get_floating_ip_by_address ( client , address ) <EOL> if raise_if_associated and fip [ '<STR_LIT>' ] : <EOL> raise exception . FloatingIpAssociated ( address = address ) <EOL> client . delete_floatingip ( fip [ '<STR_LIT:id>' ] ) <EOL> @ base_api . refresh_cache <EOL> def disassociate_floating_ip ( self , context , instance , address , <EOL> affect_auto_assigned = False ) : <EOL> """<STR_LIT>""" <EOL> client = get_client ( context ) <EOL> fip = self . _get_floating_ip_by_address ( client , address ) <EOL> client . update_floatingip ( fip [ '<STR_LIT:id>' ] , { '<STR_LIT>' : { '<STR_LIT>' : None } } ) <EOL> def migrate_instance_start ( self , context , instance , migration ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def migrate_instance_finish ( self , context , instance , migration ) : <EOL> """<STR_LIT>""" <EOL> self . _update_port_binding_for_instance ( context , instance , <EOL> migration [ '<STR_LIT>' ] ) <EOL> def add_network_to_project ( self , context , project_id , network_uuid = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def _nw_info_get_ips ( self , client , port ) : <EOL> network_IPs = [ ] <EOL> for fixed_ip in port [ '<STR_LIT>' ] : <EOL> fixed = network_model . FixedIP ( address = fixed_ip [ '<STR_LIT>' ] ) <EOL> floats = self . _get_floating_ips_by_fixed_and_port ( <EOL> client , fixed_ip [ '<STR_LIT>' ] , port [ '<STR_LIT:id>' ] ) <EOL> for ip in floats : <EOL> fip = network_model . IP ( address = ip [ '<STR_LIT>' ] , <EOL> type = '<STR_LIT>' ) <EOL> fixed . add_floating_ip ( fip ) <EOL> network_IPs . append ( fixed ) <EOL> return network_IPs <EOL> def _nw_info_get_subnets ( self , context , port , network_IPs ) : <EOL> subnets = self . _get_subnets_from_port ( context , port ) <EOL> for subnet in subnets : <EOL> subnet [ '<STR_LIT>' ] = [ fixed_ip for fixed_ip in network_IPs <EOL> if fixed_ip . is_in_subnet ( subnet ) ] <EOL> return subnets <EOL> def _nw_info_build_network ( self , port , networks , subnets ) : <EOL> network_name = None <EOL> network_mtu = None <EOL> for net in networks : <EOL> if port [ '<STR_LIT>' ] == net [ '<STR_LIT:id>' ] : <EOL> network_name = net [ '<STR_LIT:name>' ] <EOL> tenant_id = net [ '<STR_LIT>' ] <EOL> network_mtu = net . get ( '<STR_LIT>' ) <EOL> break <EOL> else : <EOL> tenant_id = port [ '<STR_LIT>' ] <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT:id>' : port [ '<STR_LIT>' ] , '<STR_LIT>' : tenant_id } ) <EOL> bridge = None <EOL> ovs_interfaceid = None <EOL> should_create_bridge = None <EOL> vif_type = port . get ( '<STR_LIT>' ) <EOL> port_details = port . get ( '<STR_LIT>' , { } ) <EOL> if vif_type == network_model . VIF_TYPE_OVS : <EOL> bridge = port_details . get ( network_model . VIF_DETAILS_BRIDGE_NAME , <EOL> CONF . neutron . ovs_bridge ) <EOL> ovs_interfaceid = port [ '<STR_LIT:id>' ] <EOL> elif vif_type == network_model . VIF_TYPE_BRIDGE : <EOL> bridge = port_details . get ( network_model . VIF_DETAILS_BRIDGE_NAME , <EOL> "<STR_LIT>" + port [ '<STR_LIT>' ] ) <EOL> should_create_bridge = True <EOL> elif vif_type == network_model . VIF_TYPE_DVS : <EOL> bridge = port [ '<STR_LIT>' ] <EOL> elif ( vif_type == network_model . VIF_TYPE_VHOSTUSER and <EOL> port_details . get ( network_model . VIF_DETAILS_VHOSTUSER_OVS_PLUG , <EOL> False ) ) : <EOL> bridge = port_details . get ( network_model . VIF_DETAILS_BRIDGE_NAME , <EOL> CONF . neutron . ovs_bridge ) <EOL> ovs_interfaceid = port [ '<STR_LIT:id>' ] <EOL> if bridge is not None and vif_type != network_model . VIF_TYPE_DVS : <EOL> bridge = bridge [ : network_model . NIC_NAME_LEN ] <EOL> network = network_model . Network ( <EOL> id = port [ '<STR_LIT>' ] , <EOL> bridge = bridge , <EOL> injected = CONF . flat_injected , <EOL> label = network_name , <EOL> tenant_id = tenant_id , <EOL> mtu = network_mtu <EOL> ) <EOL> network [ '<STR_LIT>' ] = subnets <EOL> port_profile = port . get ( '<STR_LIT>' ) <EOL> if port_profile : <EOL> physical_network = port_profile . get ( '<STR_LIT>' ) <EOL> if physical_network : <EOL> network [ '<STR_LIT>' ] = physical_network <EOL> if should_create_bridge is not None : <EOL> network [ '<STR_LIT>' ] = should_create_bridge <EOL> return network , ovs_interfaceid <EOL> def _get_preexisting_port_ids ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> net_info = compute_utils . get_nw_info_for_instance ( instance ) <EOL> if not net_info : <EOL> LOG . debug ( '<STR_LIT>' , <EOL> instance = instance ) <EOL> return [ vif [ '<STR_LIT:id>' ] for vif in net_info <EOL> if vif . get ( '<STR_LIT>' ) ] <EOL> def _build_network_info_model ( self , context , instance , networks = None , <EOL> port_ids = None , admin_client = None , <EOL> preexisting_port_ids = None ) : <EOL> """<STR_LIT>""" <EOL> search_opts = { '<STR_LIT>' : instance . project_id , <EOL> '<STR_LIT>' : instance . uuid , } <EOL> if admin_client is None : <EOL> client = get_client ( context , admin = True ) <EOL> else : <EOL> client = admin_client <EOL> data = client . list_ports ( ** search_opts ) <EOL> current_neutron_ports = data . get ( '<STR_LIT>' , [ ] ) <EOL> nw_info_refresh = networks is None and port_ids is None <EOL> networks , port_ids = self . _gather_port_ids_and_networks ( <EOL> context , instance , networks , port_ids ) <EOL> nw_info = network_model . NetworkInfo ( ) <EOL> if preexisting_port_ids is None : <EOL> preexisting_port_ids = [ ] <EOL> preexisting_port_ids = set ( <EOL> preexisting_port_ids + self . _get_preexisting_port_ids ( instance ) ) <EOL> current_neutron_port_map = { } <EOL> for current_neutron_port in current_neutron_ports : <EOL> current_neutron_port_map [ current_neutron_port [ '<STR_LIT:id>' ] ] = ( <EOL> current_neutron_port ) <EOL> for port_id in port_ids : <EOL> current_neutron_port = current_neutron_port_map . get ( port_id ) <EOL> if current_neutron_port : <EOL> vif_active = False <EOL> if ( current_neutron_port [ '<STR_LIT>' ] is False <EOL> or current_neutron_port [ '<STR_LIT:status>' ] == '<STR_LIT>' ) : <EOL> vif_active = True <EOL> network_IPs = self . _nw_info_get_ips ( client , <EOL> current_neutron_port ) <EOL> subnets = self . _nw_info_get_subnets ( context , <EOL> current_neutron_port , <EOL> network_IPs ) <EOL> devname = "<STR_LIT>" + current_neutron_port [ '<STR_LIT:id>' ] <EOL> devname = devname [ : network_model . NIC_NAME_LEN ] <EOL> network , ovs_interfaceid = ( <EOL> self . _nw_info_build_network ( current_neutron_port , <EOL> networks , subnets ) ) <EOL> preserve_on_delete = ( current_neutron_port [ '<STR_LIT:id>' ] in <EOL> preexisting_port_ids ) <EOL> nw_info . append ( network_model . VIF ( <EOL> id = current_neutron_port [ '<STR_LIT:id>' ] , <EOL> address = current_neutron_port [ '<STR_LIT>' ] , <EOL> network = network , <EOL> vnic_type = current_neutron_port . get ( '<STR_LIT>' , <EOL> network_model . VNIC_TYPE_NORMAL ) , <EOL> type = current_neutron_port . get ( '<STR_LIT>' ) , <EOL> profile = current_neutron_port . get ( '<STR_LIT>' ) , <EOL> details = current_neutron_port . get ( '<STR_LIT>' ) , <EOL> ovs_interfaceid = ovs_interfaceid , <EOL> devname = devname , <EOL> active = vif_active , <EOL> preserve_on_delete = preserve_on_delete ) ) <EOL> elif nw_info_refresh : <EOL> LOG . info ( _LI ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , port_id , <EOL> instance = instance ) <EOL> return nw_info <EOL> def _get_subnets_from_port ( self , context , port ) : <EOL> """<STR_LIT>""" <EOL> fixed_ips = port [ '<STR_LIT>' ] <EOL> if not fixed_ips : <EOL> return [ ] <EOL> search_opts = { '<STR_LIT:id>' : [ ip [ '<STR_LIT>' ] for ip in fixed_ips ] } <EOL> data = get_client ( context ) . list_subnets ( ** search_opts ) <EOL> ipam_subnets = data . get ( '<STR_LIT>' , [ ] ) <EOL> subnets = [ ] <EOL> for subnet in ipam_subnets : <EOL> subnet_dict = { '<STR_LIT>' : subnet [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : network_model . IP ( <EOL> address = subnet [ '<STR_LIT>' ] , <EOL> type = '<STR_LIT>' ) , <EOL> } <EOL> search_opts = { '<STR_LIT>' : subnet [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> data = get_client ( context ) . list_ports ( ** search_opts ) <EOL> dhcp_ports = data . get ( '<STR_LIT>' , [ ] ) <EOL> for p in dhcp_ports : <EOL> for ip_pair in p [ '<STR_LIT>' ] : <EOL> if ip_pair [ '<STR_LIT>' ] == subnet [ '<STR_LIT:id>' ] : <EOL> subnet_dict [ '<STR_LIT>' ] = ip_pair [ '<STR_LIT>' ] <EOL> break <EOL> subnet_object = network_model . Subnet ( ** subnet_dict ) <EOL> for dns in subnet . get ( '<STR_LIT>' , [ ] ) : <EOL> subnet_object . add_dns ( <EOL> network_model . IP ( address = dns , type = '<STR_LIT>' ) ) <EOL> for route in subnet . get ( '<STR_LIT>' , [ ] ) : <EOL> subnet_object . add_route ( <EOL> network_model . Route ( cidr = route [ '<STR_LIT>' ] , <EOL> gateway = network_model . IP ( <EOL> address = route [ '<STR_LIT>' ] , <EOL> type = '<STR_LIT>' ) ) ) <EOL> subnets . append ( subnet_object ) <EOL> return subnets <EOL> def get_dns_domains ( self , context ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def add_dns_entry ( self , context , address , name , dns_type , domain ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def modify_dns_entry ( self , context , name , address , domain ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def delete_dns_entry ( self , context , name , domain ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def delete_dns_domain ( self , context , domain ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def get_dns_entries_by_address ( self , context , address , domain ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def get_dns_entries_by_name ( self , context , name , domain ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def create_private_dns_domain ( self , context , domain , availability_zone ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def create_public_dns_domain ( self , context , domain , project = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def setup_instance_network_on_host ( self , context , instance , host ) : <EOL> """<STR_LIT>""" <EOL> self . _update_port_binding_for_instance ( context , instance , host ) <EOL> def cleanup_instance_network_on_host ( self , context , instance , host ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _update_port_binding_for_instance ( self , context , instance , host ) : <EOL> if not self . _has_port_binding_extension ( context , refresh_cache = True ) : <EOL> return <EOL> neutron = get_client ( context , admin = True ) <EOL> search_opts = { '<STR_LIT>' : instance . uuid , <EOL> '<STR_LIT>' : instance . project_id } <EOL> data = neutron . list_ports ( ** search_opts ) <EOL> ports = data [ '<STR_LIT>' ] <EOL> for p in ports : <EOL> if p . get ( '<STR_LIT>' ) != host : <EOL> try : <EOL> neutron . update_port ( p [ '<STR_LIT:id>' ] , <EOL> { '<STR_LIT:port>' : { '<STR_LIT>' : host } } ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) , <EOL> p [ '<STR_LIT:id>' ] , instance = instance ) <EOL> def update_instance_vnic_index ( self , context , instance , vif , index ) : <EOL> """<STR_LIT>""" <EOL> self . _refresh_neutron_extensions_cache ( context ) <EOL> if constants . VNIC_INDEX_EXT in self . extensions : <EOL> neutron = get_client ( context ) <EOL> port_req_body = { '<STR_LIT:port>' : { '<STR_LIT>' : index } } <EOL> try : <EOL> neutron . update_port ( vif [ '<STR_LIT:id>' ] , port_req_body ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . exception ( _LE ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> vif [ '<STR_LIT:id>' ] , instance = instance ) <EOL> def _ensure_requested_network_ordering ( accessor , unordered , preferred ) : <EOL> """<STR_LIT>""" <EOL> if preferred : <EOL> unordered . sort ( key = lambda i : preferred . index ( accessor ( i ) ) ) </s>
