<s> """<STR_LIT>""" <EOL> import eventlet <EOL> import os <EOL> import threading <EOL> import time <EOL> from oslo_concurrency import lockutils <EOL> from oslo_config import cfg <EOL> from oslo_log import log as logging <EOL> from oslo_serialization import jsonutils <EOL> from oslo_utils import excutils <EOL> from networking_cisco . _i18n import _LE , _LI , _LW <EOL> from neutron . common import constants as n_const <EOL> from neutron . db import api as db_api <EOL> from neutron . extensions import portbindings <EOL> from neutron . plugins . common import constants as p_const <EOL> from neutron . plugins . ml2 import db as ml2_db <EOL> from neutron . plugins . ml2 import driver_api as api <EOL> from networking_cisco . plugins . ml2 . drivers . cisco . nexus import ( <EOL> config as conf ) <EOL> from networking_cisco . plugins . ml2 . drivers . cisco . nexus import ( <EOL> constants as const ) <EOL> from networking_cisco . plugins . ml2 . drivers . cisco . nexus import ( <EOL> exceptions as excep ) <EOL> from networking_cisco . plugins . ml2 . drivers . cisco . nexus import ( <EOL> nexus_db_v2 as nxos_db ) <EOL> from networking_cisco . plugins . ml2 . drivers . cisco . nexus import ( <EOL> nexus_network_driver ) <EOL> LOG = logging . getLogger ( __name__ ) <EOL> HOST_NOT_FOUND = _LW ( "<STR_LIT>" ) <EOL> DELAY_MONITOR_THREAD = <NUM_LIT:30> <EOL> class CiscoNexusCfgMonitor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , driver , mdriver ) : <EOL> self . _driver = driver <EOL> self . _mdriver = mdriver <EOL> switch_connections = self . _mdriver . get_switch_ips ( ) <EOL> for switch_ip in switch_connections : <EOL> self . _mdriver . set_switch_ip_and_active_state ( <EOL> switch_ip , const . SWITCH_INACTIVE ) <EOL> if not self . _mdriver . is_replay_enabled ( ) : <EOL> try : <EOL> self . _initialize_trunk_interfaces_to_none ( switch_ip ) <EOL> except Exception : <EOL> pass <EOL> def _configure_nexus_type ( self , switch_ip , nexus_type ) : <EOL> if nexus_type not in ( const . NEXUS_3K , const . NEXUS_5K , <EOL> const . NEXUS_7K , const . NEXUS_9K ) : <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : nexus_type , '<STR_LIT>' : switch_ip } ) <EOL> return <EOL> if ( self . _mdriver . get_switch_nexus_type ( switch_ip ) == <EOL> const . NEXUS_TYPE_INVALID ) : <EOL> self . _mdriver . set_switch_nexus_type ( switch_ip , nexus_type ) <EOL> def _initialize_trunk_interfaces_to_none ( self , switch_ip ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> switch_ifs = self . _mdriver . _get_switch_interfaces ( switch_ip ) <EOL> if not switch_ifs : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> switch_ip ) <EOL> return <EOL> self . _driver . initialize_all_switch_interfaces ( switch_ifs ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : switch_ip } ) <EOL> self . _mdriver . register_switch_as_inactive ( switch_ip , <EOL> '<STR_LIT>' ) <EOL> for switch_ip , intf_type , port , is_native , ch_grp in switch_ifs : <EOL> try : <EOL> reserved = nxos_db . get_reserved_port_binding ( <EOL> switch_ip , <EOL> self . _mdriver . format_interface_name ( <EOL> intf_type , port ) ) <EOL> except excep . NexusPortBindingNotFound : <EOL> continue <EOL> if reserved [ <NUM_LIT:0> ] . channel_group != ch_grp : <EOL> self . _mdriver . _change_baremetal_interfaces ( <EOL> switch_ip , intf_type , port , <EOL> reserved [ <NUM_LIT:0> ] . channel_group , ch_grp ) <EOL> if self . _mdriver . is_replay_enabled ( ) : <EOL> return <EOL> try : <EOL> mgr = self . _driver . nxos_connect ( switch_ip ) <EOL> self . _driver . _close_session ( mgr , switch_ip ) <EOL> except Exception : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : switch_ip } ) <EOL> def replay_config ( self , switch_ip ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( "<STR_LIT>" , <EOL> { '<STR_LIT>' : switch_ip } ) <EOL> try : <EOL> self . _initialize_trunk_interfaces_to_none ( switch_ip ) <EOL> except Exception : <EOL> return <EOL> nve_bindings = nxos_db . get_nve_switch_bindings ( switch_ip ) <EOL> for x in nve_bindings : <EOL> try : <EOL> self . _driver . create_nve_member ( switch_ip , <EOL> const . NVE_INT_NUM , x . vni , x . mcast_group ) <EOL> except Exception as e : <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : switch_ip , '<STR_LIT>' : x . vni , <EOL> '<STR_LIT>' : e } ) <EOL> self . _mdriver . register_switch_as_inactive ( switch_ip , <EOL> '<STR_LIT>' ) <EOL> return <EOL> try : <EOL> port_bindings = nxos_db . get_nexusport_switch_bindings ( switch_ip ) <EOL> except excep . NexusPortBindingNotFound : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : switch_ip } ) <EOL> return <EOL> try : <EOL> self . _mdriver . configure_switch_entries ( <EOL> switch_ip , port_bindings ) <EOL> except Exception as e : <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : switch_ip , '<STR_LIT>' : e } ) <EOL> self . _mdriver . register_switch_as_inactive ( switch_ip , <EOL> '<STR_LIT>' ) <EOL> def check_connections ( self ) : <EOL> """<STR_LIT>""" <EOL> switch_connections = self . _mdriver . get_all_switch_ips ( ) <EOL> for switch_ip in switch_connections : <EOL> state = self . _mdriver . get_switch_ip_and_active_state ( switch_ip ) <EOL> config_failure = self . _mdriver . get_switch_replay_failure ( <EOL> const . FAIL_CONFIG , switch_ip ) <EOL> contact_failure = self . _mdriver . get_switch_replay_failure ( <EOL> const . FAIL_CONTACT , switch_ip ) <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> { '<STR_LIT>' : switch_ip , '<STR_LIT:state>' : state , <EOL> '<STR_LIT>' : contact_failure , <EOL> '<STR_LIT>' : config_failure } ) <EOL> try : <EOL> nexus_type = self . _driver . get_nexus_type ( switch_ip ) <EOL> except Exception : <EOL> if state != const . SWITCH_INACTIVE : <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , { '<STR_LIT>' : switch_ip } ) <EOL> self . _mdriver . set_switch_ip_and_active_state ( <EOL> switch_ip , const . SWITCH_INACTIVE ) <EOL> else : <EOL> self . _mdriver . incr_switch_replay_failure ( <EOL> const . FAIL_CONTACT , switch_ip ) <EOL> else : <EOL> if state == const . SWITCH_RESTORE_S2 : <EOL> try : <EOL> self . _mdriver . configure_next_batch_of_vlans ( switch_ip ) <EOL> except Exception as e : <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : switch_ip , '<STR_LIT>' : e } ) <EOL> self . _mdriver . register_switch_as_inactive ( <EOL> switch_ip , '<STR_LIT>' ) <EOL> continue <EOL> if state == const . SWITCH_INACTIVE : <EOL> self . _configure_nexus_type ( switch_ip , nexus_type ) <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : switch_ip } ) <EOL> self . _mdriver . set_switch_ip_and_active_state ( <EOL> switch_ip , const . SWITCH_RESTORE_S1 ) <EOL> self . _driver . keep_ssh_caching ( ) <EOL> self . replay_config ( switch_ip ) <EOL> self . _driver . init_ssh_caching ( ) <EOL> if self . _mdriver . get_switch_ip_and_active_state ( <EOL> switch_ip ) == const . SWITCH_INACTIVE : <EOL> self . _mdriver . incr_switch_replay_failure ( <EOL> const . FAIL_CONFIG , switch_ip ) <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : switch_ip } ) <EOL> else : <EOL> self . _mdriver . reset_switch_replay_failure ( <EOL> const . FAIL_CONFIG , switch_ip ) <EOL> self . _mdriver . reset_switch_replay_failure ( <EOL> const . FAIL_CONTACT , switch_ip ) <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : switch_ip } ) <EOL> class CiscoNexusMechanismDriver ( api . MechanismDriver ) : <EOL> """<STR_LIT>""" <EOL> def initialize ( self ) : <EOL> conf . ML2MechCiscoConfig ( ) <EOL> self . _nexus_switches = conf . ML2MechCiscoConfig . nexus_dict <EOL> LOG . debug ( "<STR_LIT>" , self . _nexus_switches ) <EOL> self . _switch_state = { } <EOL> self . driver = nexus_network_driver . CiscoNexusDriver ( ) <EOL> self . _ppid = os . getpid ( ) <EOL> self . monitor = CiscoNexusCfgMonitor ( self . driver , self ) <EOL> self . timer = None <EOL> self . monitor_timeout = conf . cfg . CONF . ml2_cisco . switch_heartbeat_time <EOL> self . monitor_lock = threading . Lock ( ) <EOL> if self . is_replay_enabled ( ) : <EOL> eventlet . spawn_after ( DELAY_MONITOR_THREAD , self . _monitor_thread ) <EOL> def format_interface_name ( self , intf_type , port , ch_grp = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if ch_grp > <NUM_LIT:0> : <EOL> return '<STR_LIT>' % str ( ch_grp ) <EOL> return '<STR_LIT>' % ( intf_type , port ) <EOL> def split_interface_name ( self , interface , ch_grp = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> interface = interface . lower ( ) <EOL> if ch_grp != <NUM_LIT:0> : <EOL> intf_type = '<STR_LIT>' <EOL> port = str ( ch_grp ) <EOL> elif '<STR_LIT::>' in interface : <EOL> intf_type , port = interface . split ( '<STR_LIT::>' ) <EOL> elif interface . startswith ( '<STR_LIT>' ) : <EOL> interface = interface . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) <EOL> _ , intf_type , port = interface . partition ( '<STR_LIT>' ) <EOL> else : <EOL> intf_type , port = '<STR_LIT>' , interface <EOL> return intf_type , port <EOL> def is_replay_enabled ( self ) : <EOL> return conf . cfg . CONF . ml2_cisco . switch_heartbeat_time > <NUM_LIT:0> <EOL> def set_switch_ip_and_active_state ( self , switch_ip , state ) : <EOL> if not self . is_replay_enabled ( ) : <EOL> return <EOL> try : <EOL> nxos_db . get_reserved_switch_binding ( <EOL> switch_ip ) <EOL> except excep . NexusPortBindingNotFound : <EOL> nxos_db . add_reserved_switch_binding ( <EOL> switch_ip , state ) <EOL> nxos_db . update_reserved_switch_binding ( <EOL> switch_ip , state ) <EOL> def get_switch_ip_and_active_state ( self , switch_ip ) : <EOL> if not self . is_replay_enabled ( ) : <EOL> return const . SWITCH_ACTIVE <EOL> binding = nxos_db . get_reserved_switch_binding ( <EOL> switch_ip ) <EOL> if len ( binding ) == <NUM_LIT:1> : <EOL> return binding [ <NUM_LIT:0> ] . port_id <EOL> else : <EOL> return const . SWITCH_INACTIVE <EOL> def _switch_defined ( self , switch_ip ) : <EOL> """<STR_LIT>""" <EOL> if ( ( switch_ip , const . USERNAME ) in self . _nexus_switches and <EOL> ( switch_ip , const . PASSWORD ) in self . _nexus_switches ) : <EOL> return True <EOL> else : <EOL> return False <EOL> def register_switch_as_inactive ( self , switch_ip , func_name ) : <EOL> self . set_switch_ip_and_active_state ( switch_ip , const . SWITCH_INACTIVE ) <EOL> LOG . exception ( <EOL> _LE ( "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : func_name } ) <EOL> def is_switch_active ( self , switch_ip ) : <EOL> if self . is_replay_enabled ( ) : <EOL> switch_state = self . get_switch_ip_and_active_state ( switch_ip ) <EOL> active_states = [ const . SWITCH_ACTIVE , const . SWITCH_RESTORE_S2 ] <EOL> return switch_state in active_states <EOL> else : <EOL> return True <EOL> def set_switch_nexus_type ( self , switch_ip , type ) : <EOL> self . _switch_state [ switch_ip , '<STR_LIT>' ] = type <EOL> def get_switch_nexus_type ( self , switch_ip ) : <EOL> if ( switch_ip , '<STR_LIT>' ) in self . _switch_state : <EOL> return self . _switch_state [ switch_ip , '<STR_LIT>' ] <EOL> else : <EOL> return - <NUM_LIT:1> <EOL> def _save_switch_vlan_range ( self , switch_ip , vlan_range ) : <EOL> self . _switch_state [ switch_ip , '<STR_LIT>' ] = vlan_range <EOL> def _get_switch_vlan_range ( self , switch_ip ) : <EOL> if ( switch_ip , '<STR_LIT>' ) in self . _switch_state : <EOL> return self . _switch_state [ switch_ip , '<STR_LIT>' ] <EOL> else : <EOL> return [ ] <EOL> def _save_switch_vxlan_range ( self , switch_ip , vxlan_range ) : <EOL> self . _switch_state [ switch_ip , '<STR_LIT>' ] = vxlan_range <EOL> def _get_switch_vxlan_range ( self , switch_ip ) : <EOL> if ( switch_ip , '<STR_LIT>' ) in self . _switch_state : <EOL> return self . _switch_state [ switch_ip , '<STR_LIT>' ] <EOL> else : <EOL> return [ ] <EOL> def _pop_vlan_range ( self , switch_ip , size ) : <EOL> """<STR_LIT>""" <EOL> vlan_range = self . _get_switch_vlan_range ( switch_ip ) <EOL> sized_range = '<STR_LIT>' <EOL> fr = <NUM_LIT:0> <EOL> to = <NUM_LIT:0> <EOL> while size > <NUM_LIT:0> and vlan_range : <EOL> vlan_id , vni , vlan_name = vlan_range . pop ( <NUM_LIT:0> ) <EOL> size -= <NUM_LIT:1> <EOL> if fr == <NUM_LIT:0> and to == <NUM_LIT:0> : <EOL> fr = vlan_id <EOL> to = vlan_id <EOL> else : <EOL> diff = vlan_id - to <EOL> if diff == <NUM_LIT:1> : <EOL> to = vlan_id <EOL> else : <EOL> if fr == to : <EOL> sized_range += str ( to ) + '<STR_LIT:U+002C>' <EOL> else : <EOL> sized_range += str ( fr ) + '<STR_LIT:->' <EOL> sized_range += str ( to ) + '<STR_LIT:U+002C>' <EOL> fr = vlan_id <EOL> to = vlan_id <EOL> if fr != <NUM_LIT:0> : <EOL> if fr == to : <EOL> sized_range += str ( to ) <EOL> else : <EOL> sized_range += str ( fr ) + '<STR_LIT:->' <EOL> sized_range += str ( to ) <EOL> self . _save_switch_vlan_range ( switch_ip , vlan_range ) <EOL> return sized_range <EOL> def _valid_replay_key ( self , fail_key , switch_ip ) : <EOL> if ( switch_ip , const . REPLAY_FAILURES ) not in self . _switch_state : <EOL> self . _switch_state [ switch_ip , const . REPLAY_FAILURES ] = { <EOL> const . FAIL_CONTACT : <NUM_LIT:0> , <EOL> const . FAIL_CONFIG : <NUM_LIT:0> } <EOL> return fail_key in self . _switch_state [ switch_ip , <EOL> const . REPLAY_FAILURES ] <EOL> def reset_switch_replay_failure ( self , fail_key , switch_ip ) : <EOL> if self . _valid_replay_key ( fail_key , switch_ip ) : <EOL> self . _switch_state [ switch_ip , const . REPLAY_FAILURES ] [ fail_key ] = <NUM_LIT:0> <EOL> def incr_switch_replay_failure ( self , fail_key , switch_ip ) : <EOL> if self . _valid_replay_key ( fail_key , switch_ip ) : <EOL> self . _switch_state [ switch_ip , const . REPLAY_FAILURES ] [ fail_key ] += <NUM_LIT:1> <EOL> def get_switch_replay_failure ( self , fail_key , switch_ip ) : <EOL> if self . _valid_replay_key ( fail_key , switch_ip ) : <EOL> return self . _switch_state [ switch_ip , <EOL> const . REPLAY_FAILURES ] [ fail_key ] <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def get_all_switch_ips ( self ) : <EOL> """<STR_LIT>""" <EOL> switch_connections = [ ] <EOL> try : <EOL> bindings = nxos_db . get_reserved_switch_binding ( ) <EOL> except excep . NexusPortBindingNotFound : <EOL> LOG . error ( _LE ( "<STR_LIT>" ) ) <EOL> bindings = [ ] <EOL> for switch in bindings : <EOL> switch_connections . append ( switch . switch_ip ) <EOL> return switch_connections <EOL> def _valid_network_segment ( self , segment ) : <EOL> return ( cfg . CONF . ml2_cisco . managed_physical_network is None or <EOL> cfg . CONF . ml2_cisco . managed_physical_network == <EOL> segment [ api . PHYSICAL_NETWORK ] ) <EOL> def _is_supported_deviceowner ( self , port ) : <EOL> return ( port [ '<STR_LIT>' ] . startswith ( '<STR_LIT>' ) or <EOL> port [ '<STR_LIT>' ] . startswith ( '<STR_LIT>' ) or <EOL> port [ '<STR_LIT>' ] == n_const . DEVICE_OWNER_DHCP or <EOL> port [ '<STR_LIT>' ] == n_const . DEVICE_OWNER_ROUTER_HA_INTF ) <EOL> def _is_status_active ( self , port ) : <EOL> return port [ '<STR_LIT:status>' ] == n_const . PORT_STATUS_ACTIVE <EOL> def _is_baremetal ( self , port ) : <EOL> """<STR_LIT>""" <EOL> return ( port [ portbindings . VNIC_TYPE ] == <EOL> portbindings . VNIC_BAREMETAL ) <EOL> def _get_baremetal_switch_info ( self , link_info ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> switch_info = link_info [ '<STR_LIT>' ] <EOL> if not isinstance ( switch_info , dict ) : <EOL> switch_info = jsonutils . loads ( switch_info ) <EOL> except Exception as e : <EOL> LOG . error ( _LE ( "<STR_LIT>" ) , <EOL> { "<STR_LIT>" : e } ) <EOL> switch_info = { } <EOL> return switch_info <EOL> def _supported_baremetal_transaction ( self , context ) : <EOL> """<STR_LIT>""" <EOL> port = context . current <EOL> if not self . _is_baremetal ( port ) : <EOL> return False <EOL> if portbindings . PROFILE not in port : <EOL> return False <EOL> profile = port [ portbindings . PROFILE ] <EOL> if '<STR_LIT>' not in profile : <EOL> return False <EOL> all_link_info = profile [ '<STR_LIT>' ] <EOL> selected = False <EOL> for link_info in all_link_info : <EOL> if '<STR_LIT>' not in link_info : <EOL> return False <EOL> switch_info = self . _get_baremetal_switch_info ( <EOL> link_info ) <EOL> if '<STR_LIT>' in switch_info : <EOL> switch_ip = switch_info [ '<STR_LIT>' ] <EOL> else : <EOL> return False <EOL> if self . _switch_defined ( switch_ip ) : <EOL> selected = True <EOL> else : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % switch_ip ) <EOL> if not selected : <EOL> return False <EOL> selected = False <EOL> for segment in context . segments_to_bind : <EOL> if ( segment [ api . NETWORK_TYPE ] == p_const . TYPE_VLAN and <EOL> segment [ api . SEGMENTATION_ID ] ) : <EOL> context . set_binding ( segment [ api . ID ] , <EOL> portbindings . VIF_TYPE_OTHER , <EOL> { } , <EOL> status = n_const . PORT_STATUS_ACTIVE ) <EOL> selected = True <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> { '<STR_LIT:id>' : segment [ api . ID ] , <EOL> '<STR_LIT>' : segment [ api . SEGMENTATION_ID ] , <EOL> '<STR_LIT>' : segment [ api . PHYSICAL_NETWORK ] , <EOL> '<STR_LIT>' : segment [ api . NETWORK_TYPE ] , <EOL> '<STR_LIT:count>' : len ( all_link_info ) } ) <EOL> break <EOL> return selected <EOL> def _gather_configured_ports ( self , switch_ip , attr , host_list ) : <EOL> """<STR_LIT>""" <EOL> for port_id in ( <EOL> self . _nexus_switches [ switch_ip , attr ] . split ( '<STR_LIT:U+002C>' ) ) : <EOL> intf_type , port = self . split_interface_name ( port_id ) <EOL> host_list . append ( ( switch_ip , intf_type , port , <EOL> const . NOT_NATIVE ) ) <EOL> def _get_baremetal_switches ( self , port ) : <EOL> """<STR_LIT>""" <EOL> all_switches = set ( ) <EOL> active_switches = set ( ) <EOL> all_link_info = port [ portbindings . PROFILE ] [ '<STR_LIT>' ] <EOL> for link_info in all_link_info : <EOL> switch_info = self . _get_baremetal_switch_info ( link_info ) <EOL> if not switch_info : <EOL> continue <EOL> switch_ip = switch_info [ '<STR_LIT>' ] <EOL> if not self . _switch_defined ( switch_ip ) : <EOL> continue <EOL> all_switches . add ( switch_ip ) <EOL> if self . is_switch_active ( switch_ip ) : <EOL> active_switches . add ( switch_ip ) <EOL> return list ( all_switches ) , list ( active_switches ) <EOL> def _get_baremetal_connections ( self , port , <EOL> only_active_switch = False , <EOL> from_segment = False ) : <EOL> """<STR_LIT>""" <EOL> connections = [ ] <EOL> all_link_info = port [ portbindings . PROFILE ] [ '<STR_LIT>' ] <EOL> for link_info in all_link_info : <EOL> intf_type , port = self . split_interface_name ( <EOL> link_info [ '<STR_LIT>' ] ) <EOL> switch_info = self . _get_baremetal_switch_info ( <EOL> link_info ) <EOL> if not switch_info : <EOL> continue <EOL> switch_ip = switch_info [ '<STR_LIT>' ] <EOL> if not self . _switch_defined ( switch_ip ) : <EOL> continue <EOL> if ( only_active_switch and <EOL> not self . is_switch_active ( switch_ip ) ) : <EOL> continue <EOL> if '<STR_LIT>' in switch_info : <EOL> is_native = switch_info [ '<STR_LIT>' ] <EOL> else : <EOL> is_native = const . NOT_NATIVE <EOL> if not from_segment : <EOL> try : <EOL> reserved = nxos_db . get_reserved_port_binding ( <EOL> switch_ip , <EOL> self . format_interface_name ( <EOL> intf_type , port ) ) <EOL> if reserved [ <NUM_LIT:0> ] . channel_group > <NUM_LIT:0> : <EOL> intf_type , port = self . split_interface_name ( <EOL> '<STR_LIT>' , reserved [ <NUM_LIT:0> ] . channel_group ) <EOL> except excep . NexusPortBindingNotFound : <EOL> pass <EOL> connections . append ( ( switch_ip , intf_type , port , is_native ) ) <EOL> return connections <EOL> def _change_baremetal_interfaces ( self , switch_ip , intf_type , <EOL> port , old_ch_grp , ch_grp ) : <EOL> """<STR_LIT>""" <EOL> if old_ch_grp == ch_grp : <EOL> return <EOL> reserved_port_id = self . format_interface_name ( <EOL> intf_type , port ) <EOL> old_port_id = self . format_interface_name ( <EOL> intf_type , port , old_ch_grp ) <EOL> new_port_id = self . format_interface_name ( <EOL> intf_type , port , ch_grp ) <EOL> try : <EOL> bindings = nxos_db . get_nexus_switchport_binding ( <EOL> old_port_id , switch_ip ) <EOL> except Exception : <EOL> return <EOL> for row in bindings : <EOL> if nxos_db . is_reserved_binding ( row ) : <EOL> continue <EOL> try : <EOL> nxos_db . get_nexusport_binding ( <EOL> new_port_id , row . vlan_id , <EOL> switch_ip , row . instance_id ) <EOL> except excep . NexusPortBindingNotFound : <EOL> nxos_db . add_nexusport_binding ( <EOL> new_port_id , row . vlan_id , row . vni , <EOL> switch_ip , row . instance_id , <EOL> row . is_provider_vlan , <EOL> row . is_native ) <EOL> try : <EOL> nxos_db . remove_nexusport_binding ( <EOL> old_port_id , row . vlan_id , row . vni , <EOL> switch_ip , row . instance_id , <EOL> row . is_provider_vlan ) <EOL> except Exception : <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT:port>' : old_port_id , <EOL> '<STR_LIT>' : row . vlan_id , <EOL> '<STR_LIT>' : row . vni , <EOL> '<STR_LIT>' : switch_ip , <EOL> '<STR_LIT:id>' : row . instance_id } ) <EOL> continue <EOL> nxos_db . update_reserved_port_binding ( <EOL> switch_ip , <EOL> reserved_port_id , <EOL> ch_grp ) <EOL> def _init_baremetal_trunk_interfaces ( self , port_seg , segment , vni ) : <EOL> """<STR_LIT>""" <EOL> list_to_init = [ ] <EOL> inactive_switch = [ ] <EOL> reserved_exists = [ ] <EOL> connections = self . _get_baremetal_connections ( <EOL> port_seg , False , True ) <EOL> for switch_ip , intf_type , port , is_native in connections : <EOL> try : <EOL> reserved = nxos_db . get_reserved_port_binding ( <EOL> switch_ip , <EOL> self . format_interface_name ( <EOL> intf_type , port ) ) <EOL> reserved_exists . append ( <EOL> ( switch_ip , intf_type , port , is_native , <EOL> reserved [ <NUM_LIT:0> ] . channel_group ) ) <EOL> except excep . NexusPortBindingNotFound : <EOL> if self . is_switch_active ( switch_ip ) : <EOL> list_to_init . append ( <EOL> ( switch_ip , intf_type , port , is_native ) ) <EOL> else : <EOL> inactive_switch . append ( <EOL> ( switch_ip , intf_type , port , is_native , <NUM_LIT:0> ) ) <EOL> self . driver . initialize_all_switch_interfaces ( list_to_init ) <EOL> list_to_init += inactive_switch <EOL> for switch_ip , intf_type , port , is_native , ch_grp in list_to_init : <EOL> nxos_db . add_reserved_port_binding ( <EOL> switch_ip , <EOL> self . format_interface_name ( intf_type , port ) , <EOL> ch_grp ) <EOL> device_id = port_seg . get ( '<STR_LIT>' ) <EOL> vlan_id = segment . get ( api . SEGMENTATION_ID ) <EOL> is_provider_vlan = False <EOL> list_to_init += reserved_exists <EOL> for switch_ip , intf_type , port , is_native , ch_grp in list_to_init : <EOL> port_id = self . format_interface_name ( intf_type , port , ch_grp ) <EOL> try : <EOL> nxos_db . get_nexusport_binding ( <EOL> port_id , vlan_id , switch_ip , device_id ) <EOL> except excep . NexusPortBindingNotFound : <EOL> nxos_db . add_nexusport_binding ( <EOL> port_id , str ( vlan_id ) , str ( vni ) , <EOL> switch_ip , device_id , <EOL> is_provider_vlan , <EOL> is_native ) <EOL> def _get_host_switches ( self , host_id ) : <EOL> """<STR_LIT>""" <EOL> all_switches = set ( ) <EOL> active_switches = set ( ) <EOL> for switch_ip , attr in self . _nexus_switches : <EOL> if str ( attr ) == str ( host_id ) : <EOL> all_switches . add ( switch_ip ) <EOL> if self . is_switch_active ( switch_ip ) : <EOL> active_switches . add ( switch_ip ) <EOL> return list ( all_switches ) , list ( active_switches ) <EOL> def _get_host_connections ( self , host_id , <EOL> only_active_switch = False ) : <EOL> """<STR_LIT>""" <EOL> host_found = False <EOL> host_connections = [ ] <EOL> for switch_ip , attr in self . _nexus_switches : <EOL> if str ( attr ) == str ( host_id ) : <EOL> host_found = True <EOL> if ( only_active_switch and <EOL> not self . is_switch_active ( switch_ip ) ) : <EOL> continue <EOL> self . _gather_configured_ports ( <EOL> switch_ip , attr , host_connections ) <EOL> if not host_found : <EOL> LOG . warning ( HOST_NOT_FOUND , host_id ) <EOL> return host_connections <EOL> def _get_port_connections ( self , port , host_id , <EOL> only_active_switch = False ) : <EOL> if host_id : <EOL> return self . _get_host_connections ( <EOL> host_id , only_active_switch ) <EOL> else : <EOL> return self . _get_baremetal_connections ( <EOL> port , only_active_switch ) <EOL> def _get_active_port_connections ( self , port , host_id ) : <EOL> return self . _get_port_connections ( port , host_id , True ) <EOL> def _get_known_baremetal_interfaces ( self , requested_switch_ip ) : <EOL> """<STR_LIT>""" <EOL> switch_ifs = [ ] <EOL> try : <EOL> port_info = nxos_db . get_reserved_port_binding ( <EOL> requested_switch_ip ) <EOL> except excep . NexusPortBindingNotFound : <EOL> port_info = [ ] <EOL> for binding in port_info : <EOL> intf_type , port = self . split_interface_name ( <EOL> binding . port_id ) <EOL> switch_ifs . append ( <EOL> ( requested_switch_ip , intf_type , port , <EOL> binding . is_native ) ) <EOL> return switch_ifs <EOL> def _get_config_switch_interfaces ( self , requested_switch_ip ) : <EOL> """<STR_LIT>""" <EOL> switch_ifs = [ ] <EOL> defined_attributes = [ const . USERNAME , const . PASSWORD , const . SSHPORT , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> for switch_ip , attr in self . _nexus_switches : <EOL> if ( switch_ip == requested_switch_ip and <EOL> str ( attr ) not in defined_attributes ) : <EOL> self . _gather_configured_ports ( <EOL> switch_ip , attr , switch_ifs ) <EOL> return switch_ifs <EOL> def _get_switch_interfaces ( self , requested_switch_ip ) : <EOL> """<STR_LIT>""" <EOL> all_switch_ifs = self . _get_config_switch_interfaces ( <EOL> requested_switch_ip ) <EOL> all_switch_ifs += self . _get_known_baremetal_interfaces ( <EOL> requested_switch_ip ) <EOL> return all_switch_ifs <EOL> def get_switch_ips ( self ) : <EOL> switch_connections = [ ] <EOL> for switch_ip , attr in self . _nexus_switches : <EOL> if str ( attr ) == '<STR_LIT:username>' : <EOL> switch_connections . append ( switch_ip ) <EOL> return switch_connections <EOL> def _get_switch_nve_info ( self , host_id ) : <EOL> host_nve_connections = [ ] <EOL> for switch_ip , attr in self . _nexus_switches : <EOL> if str ( attr ) == str ( host_id ) : <EOL> host_nve_connections . append ( switch_ip ) <EOL> if not host_nve_connections : <EOL> LOG . warning ( HOST_NOT_FOUND , host_id ) <EOL> return host_nve_connections <EOL> def _configure_nve_db ( self , vni , device_id , mcast_group , host_id ) : <EOL> """<STR_LIT>""" <EOL> host_nve_connections = self . _get_switch_nve_info ( host_id ) <EOL> for switch_ip in host_nve_connections : <EOL> if not nxos_db . get_nve_vni_member_bindings ( vni , switch_ip , <EOL> device_id ) : <EOL> nxos_db . add_nexusnve_binding ( vni , switch_ip , device_id , <EOL> mcast_group ) <EOL> def _configure_nve_member ( self , vni , device_id , mcast_group , host_id ) : <EOL> """<STR_LIT>""" <EOL> host_nve_connections = self . _get_switch_nve_info ( host_id ) <EOL> for switch_ip in host_nve_connections : <EOL> if cfg . CONF . ml2_cisco . vxlan_global_config : <EOL> nve_bindings = nxos_db . get_nve_switch_bindings ( switch_ip ) <EOL> if len ( nve_bindings ) == <NUM_LIT:1> : <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> loopback = self . _nexus_switches . get ( <EOL> ( switch_ip , '<STR_LIT>' ) , '<STR_LIT:0>' ) <EOL> self . driver . enable_vxlan_feature ( switch_ip , <EOL> const . NVE_INT_NUM , loopback ) <EOL> member_bindings = nxos_db . get_nve_vni_switch_bindings ( vni , <EOL> switch_ip ) <EOL> if len ( member_bindings ) == <NUM_LIT:1> : <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> self . driver . create_nve_member ( switch_ip , const . NVE_INT_NUM , <EOL> vni , mcast_group ) <EOL> def _delete_nve_db ( self , vni , device_id , mcast_group , host_id ) : <EOL> """<STR_LIT>""" <EOL> rows = nxos_db . get_nve_vni_deviceid_bindings ( vni , device_id ) <EOL> for row in rows : <EOL> nxos_db . remove_nexusnve_binding ( vni , row . switch_ip , device_id ) <EOL> def _delete_nve_member ( self , vni , device_id , mcast_group , host_id ) : <EOL> """<STR_LIT>""" <EOL> host_nve_connections = self . _get_switch_nve_info ( host_id ) <EOL> for switch_ip in host_nve_connections : <EOL> if not nxos_db . get_nve_vni_switch_bindings ( vni , switch_ip ) : <EOL> self . driver . delete_nve_member ( switch_ip , <EOL> const . NVE_INT_NUM , vni ) <EOL> if ( cfg . CONF . ml2_cisco . vxlan_global_config and <EOL> not nxos_db . get_nve_switch_bindings ( switch_ip ) ) : <EOL> self . driver . disable_vxlan_feature ( switch_ip ) <EOL> def _configure_nxos_db ( self , port , vlan_id , device_id , host_id , vni , <EOL> is_provider_vlan ) : <EOL> """<STR_LIT>""" <EOL> host_connections = self . _get_port_connections ( port , host_id ) <EOL> for switch_ip , intf_type , nexus_port , is_native in host_connections : <EOL> port_id = self . format_interface_name ( intf_type , nexus_port ) <EOL> try : <EOL> nxos_db . get_nexusport_binding ( port_id , vlan_id , switch_ip , <EOL> device_id ) <EOL> except excep . NexusPortBindingNotFound : <EOL> nxos_db . add_nexusport_binding ( port_id , str ( vlan_id ) , str ( vni ) , <EOL> switch_ip , device_id , <EOL> is_provider_vlan , <EOL> is_native ) <EOL> def _gather_config_parms ( self , is_provider_vlan , vlan_id ) : <EOL> """<STR_LIT>""" <EOL> if is_provider_vlan : <EOL> vlan_name = cfg . CONF . ml2_cisco . provider_vlan_name_prefix <EOL> auto_create = cfg . CONF . ml2_cisco . provider_vlan_auto_create <EOL> auto_trunk = cfg . CONF . ml2_cisco . provider_vlan_auto_trunk <EOL> else : <EOL> vlan_name = cfg . CONF . ml2_cisco . vlan_name_prefix <EOL> auto_create = True <EOL> auto_trunk = True <EOL> if auto_create : <EOL> vlan_name_max_len = ( <EOL> const . NEXUS_MAX_VLAN_NAME_LEN - len ( str ( vlan_id ) ) ) <EOL> if len ( vlan_name ) > vlan_name_max_len : <EOL> vlan_name = vlan_name [ : vlan_name_max_len ] <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) , <EOL> vlan_name ) <EOL> vlan_name = vlan_name + str ( vlan_id ) <EOL> return vlan_name , auto_create , auto_trunk <EOL> def _configure_port_binding ( self , is_provider_vlan , duplicate_type , <EOL> is_native , <EOL> switch_ip , vlan_id , <EOL> intf_type , nexus_port , vni ) : <EOL> """<STR_LIT>""" <EOL> if duplicate_type == const . DUPLICATE_PORT : <EOL> return <EOL> vlan_name , auto_create , auto_trunk = self . _gather_config_parms ( <EOL> is_provider_vlan , vlan_id ) <EOL> if duplicate_type == const . DUPLICATE_VLAN : <EOL> auto_create = False <EOL> if auto_create and auto_trunk : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> vlan_name ) <EOL> self . driver . create_and_trunk_vlan ( <EOL> switch_ip , vlan_id , vlan_name , intf_type , <EOL> nexus_port , vni , is_native ) <EOL> elif auto_create : <EOL> LOG . debug ( "<STR_LIT>" , vlan_name ) <EOL> self . driver . create_vlan_segment ( switch_ip , vlan_id , <EOL> vlan_name , vni ) <EOL> elif auto_trunk : <EOL> LOG . debug ( "<STR_LIT>" , vlan_name ) <EOL> self . driver . send_enable_vlan_on_trunk_int ( <EOL> switch_ip , vlan_id , <EOL> intf_type , nexus_port , is_native ) <EOL> def _get_compressed_vlan_list ( self , pvlan_ids ) : <EOL> """<STR_LIT>""" <EOL> if not pvlan_ids : <EOL> return [ ] <EOL> pvlan_list = list ( pvlan_ids ) <EOL> pvlan_list . sort ( ) <EOL> compressed_list = [ ] <EOL> begin = - <NUM_LIT:1> <EOL> prev_vlan = - <NUM_LIT:1> <EOL> for port_vlan in pvlan_list : <EOL> if prev_vlan == - <NUM_LIT:1> : <EOL> prev_vlan = port_vlan <EOL> else : <EOL> if ( port_vlan - prev_vlan ) == <NUM_LIT:1> : <EOL> if begin == - <NUM_LIT:1> : <EOL> begin = prev_vlan <EOL> prev_vlan = port_vlan <EOL> else : <EOL> if begin == - <NUM_LIT:1> : <EOL> compressed_list . append ( str ( prev_vlan ) ) <EOL> else : <EOL> compressed_list . append ( "<STR_LIT>" % ( begin , prev_vlan ) ) <EOL> begin = - <NUM_LIT:1> <EOL> prev_vlan = port_vlan <EOL> if begin == - <NUM_LIT:1> : <EOL> compressed_list . append ( str ( prev_vlan ) ) <EOL> else : <EOL> compressed_list . append ( "<STR_LIT>" % ( begin , prev_vlan ) ) <EOL> return compressed_list <EOL> def _restore_port_binding ( self , <EOL> switch_ip , pvlan_ids , <EOL> port , is_native ) : <EOL> """<STR_LIT>""" <EOL> intf_type , nexus_port = self . split_interface_name ( port ) <EOL> concat_vlans = '<STR_LIT>' <EOL> compressed_vlans = self . _get_compressed_vlan_list ( pvlan_ids ) <EOL> for pvlan in compressed_vlans : <EOL> if concat_vlans == '<STR_LIT>' : <EOL> concat_vlans = "<STR_LIT:%s>" % pvlan <EOL> else : <EOL> concat_vlans += "<STR_LIT>" % pvlan <EOL> if len ( concat_vlans ) >= const . CREATE_PORT_VLAN_LENGTH : <EOL> self . driver . send_enable_vlan_on_trunk_int ( <EOL> switch_ip , concat_vlans , <EOL> intf_type , nexus_port , is_native ) <EOL> concat_vlans = '<STR_LIT>' <EOL> if len ( concat_vlans ) : <EOL> self . driver . send_enable_vlan_on_trunk_int ( <EOL> switch_ip , concat_vlans , <EOL> intf_type , nexus_port , is_native ) <EOL> def _restore_vxlan_entries ( self , switch_ip , vlans ) : <EOL> """<STR_LIT>""" <EOL> count = <NUM_LIT:1> <EOL> conf_str = '<STR_LIT>' <EOL> vnsegment_sent = <NUM_LIT:0> <EOL> while vnsegment_sent < const . CREATE_VLAN_BATCH and vlans : <EOL> vlan_id , vni , vlan_name = vlans . pop ( <NUM_LIT:0> ) <EOL> conf_str += self . driver . get_create_vlan ( <EOL> switch_ip , vlan_id , vni ) <EOL> if ( count == const . CREATE_VLAN_SEND_SIZE ) : <EOL> self . driver . send_edit_string ( switch_ip , conf_str ) <EOL> vnsegment_sent += count <EOL> conf_str = '<STR_LIT>' <EOL> count = <NUM_LIT:1> <EOL> else : <EOL> count += <NUM_LIT:1> <EOL> if conf_str : <EOL> vnsegment_sent += count <EOL> self . driver . send_edit_string ( switch_ip , conf_str ) <EOL> conf_str = '<STR_LIT>' <EOL> LOG . debug ( "<STR_LIT>" , <EOL> switch_ip , vnsegment_sent ) <EOL> def _configure_port_entries ( self , port , vlan_id , device_id , host_id , vni , <EOL> is_provider_vlan ) : <EOL> """<STR_LIT>""" <EOL> connections = self . _get_active_port_connections ( port , host_id ) <EOL> vlan_already_created = [ ] <EOL> starttime = time . time ( ) <EOL> for switch_ip , intf_type , nexus_port , is_native in connections : <EOL> all_bindings = nxos_db . get_nexusvlan_binding ( vlan_id , switch_ip ) <EOL> previous_bindings = [ row for row in all_bindings <EOL> if row . instance_id != device_id ] <EOL> if previous_bindings and ( switch_ip in vlan_already_created ) : <EOL> duplicate_type = const . DUPLICATE_VLAN <EOL> else : <EOL> vlan_already_created . append ( switch_ip ) <EOL> duplicate_type = const . NO_DUPLICATE <EOL> port_starttime = time . time ( ) <EOL> try : <EOL> self . _configure_port_binding ( <EOL> is_provider_vlan , duplicate_type , <EOL> is_native , <EOL> switch_ip , vlan_id , <EOL> intf_type , nexus_port , <EOL> vni ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> self . driver . capture_and_print_timeshot ( <EOL> port_starttime , "<STR_LIT>" , <EOL> switch = switch_ip ) <EOL> self . driver . capture_and_print_timeshot ( <EOL> starttime , "<STR_LIT>" , <EOL> switch = switch_ip ) <EOL> self . driver . capture_and_print_timeshot ( <EOL> port_starttime , "<STR_LIT>" , <EOL> switch = switch_ip ) <EOL> self . driver . capture_and_print_timeshot ( <EOL> starttime , "<STR_LIT>" ) <EOL> def configure_next_batch_of_vlans ( self , switch_ip ) : <EOL> """<STR_LIT>""" <EOL> next_range = self . _pop_vlan_range ( <EOL> switch_ip , const . CREATE_VLAN_BATCH ) <EOL> if next_range : <EOL> try : <EOL> self . driver . set_all_vlan_states ( <EOL> switch_ip , next_range ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : switch_ip } ) <EOL> self . _save_switch_vlan_range ( switch_ip , [ ] ) <EOL> vxlan_range = self . _get_switch_vxlan_range ( switch_ip ) <EOL> if vxlan_range : <EOL> try : <EOL> self . _restore_vxlan_entries ( switch_ip , vxlan_range ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : switch_ip } ) <EOL> self . _save_switch_vxlan_range ( switch_ip , [ ] ) <EOL> if ( not self . _get_switch_vlan_range ( switch_ip ) and <EOL> not self . _get_switch_vxlan_range ( switch_ip ) ) : <EOL> self . set_switch_ip_and_active_state ( <EOL> switch_ip , const . SWITCH_ACTIVE ) <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : switch_ip } ) <EOL> else : <EOL> LOG . debug ( ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : switch_ip } ) <EOL> def configure_switch_entries ( self , switch_ip , port_bindings ) : <EOL> """<STR_LIT>""" <EOL> prev_vlan = - <NUM_LIT:1> <EOL> prev_vni = - <NUM_LIT:1> <EOL> prev_port = None <EOL> prev_is_native = False <EOL> starttime = time . time ( ) <EOL> port_bindings . sort ( key = lambda x : ( x . port_id , x . vlan_id , x . vni ) ) <EOL> self . driver . capture_and_print_timeshot ( starttime , "<STR_LIT>" , <EOL> switch = switch_ip ) <EOL> vlans = set ( ) <EOL> pvlans = set ( ) <EOL> interface_count = <NUM_LIT:0> <EOL> duplicate_port = <NUM_LIT:0> <EOL> vlan_count = <NUM_LIT:0> <EOL> for port in port_bindings : <EOL> if nxos_db . is_reserved_binding ( port ) : <EOL> continue <EOL> vlan_name , auto_create , auto_trunk = self . _gather_config_parms ( <EOL> port . is_provider_vlan , port . vlan_id ) <EOL> if port . port_id == prev_port : <EOL> if port . vlan_id == prev_vlan and port . vni == prev_vni : <EOL> duplicate_port += <NUM_LIT:1> <EOL> continue <EOL> else : <EOL> vlan_count += <NUM_LIT:1> <EOL> if auto_create : <EOL> vlans . add ( ( port . vlan_id , port . vni , vlan_name ) ) <EOL> if auto_trunk : <EOL> pvlans . add ( port . vlan_id ) <EOL> else : <EOL> if prev_port : <EOL> interface_count += <NUM_LIT:1> <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> switch_ip , prev_port , vlan_count , duplicate_port ) <EOL> duplicate_port = <NUM_LIT:0> <EOL> vlan_count = <NUM_LIT:0> <EOL> if pvlans : <EOL> self . _restore_port_binding ( <EOL> switch_ip , pvlans , prev_port , prev_is_native ) <EOL> pvlans . clear ( ) <EOL> if auto_create : <EOL> vlans . add ( ( port . vlan_id , port . vni , vlan_name ) ) <EOL> if auto_trunk : <EOL> pvlans . add ( port . vlan_id ) <EOL> prev_port = port . port_id <EOL> prev_is_native = port . is_native <EOL> if pvlans : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> switch_ip , port . port_id , vlan_count , duplicate_port ) <EOL> self . _restore_port_binding ( <EOL> switch_ip , pvlans , prev_port , prev_is_native ) <EOL> LOG . debug ( "<STR_LIT>" , <EOL> interface_count + <NUM_LIT:1> , switch_ip ) <EOL> self . driver . capture_and_print_timeshot ( starttime , "<STR_LIT>" , <EOL> switch = switch_ip ) <EOL> vlans = list ( vlans ) <EOL> if vlans : <EOL> vlans . sort ( ) <EOL> vlan , vni , vlan_name = vlans [ <NUM_LIT:0> ] <EOL> if vni == <NUM_LIT:0> : <EOL> self . _save_switch_vlan_range ( switch_ip , vlans ) <EOL> else : <EOL> self . _save_switch_vxlan_range ( switch_ip , vlans ) <EOL> self . set_switch_ip_and_active_state ( <EOL> switch_ip , const . SWITCH_RESTORE_S2 ) <EOL> self . configure_next_batch_of_vlans ( switch_ip ) <EOL> self . driver . capture_and_print_timeshot ( starttime , "<STR_LIT>" , <EOL> switch = switch_ip ) <EOL> def _delete_nxos_db ( self , unused , vlan_id , device_id , host_id , vni , <EOL> is_provider_vlan ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> rows = nxos_db . get_nexusvm_bindings ( vlan_id , device_id ) <EOL> for row in rows : <EOL> nxos_db . remove_nexusport_binding ( row . port_id , row . vlan_id , <EOL> row . vni , row . switch_ip , row . instance_id , <EOL> row . is_provider_vlan ) <EOL> except excep . NexusPortBindingNotFound : <EOL> return <EOL> def _delete_switch_entry ( self , port , vlan_id , device_id , host_id , vni , <EOL> is_provider_vlan ) : <EOL> """<STR_LIT>""" <EOL> connections = self . _get_active_port_connections ( port , host_id ) <EOL> vlan_already_removed = [ ] <EOL> for switch_ip , intf_type , nexus_port , is_native in connections : <EOL> port_id = self . format_interface_name ( intf_type , nexus_port ) <EOL> auto_create = True <EOL> auto_trunk = True <EOL> if is_provider_vlan : <EOL> auto_create = cfg . CONF . ml2_cisco . provider_vlan_auto_create <EOL> auto_trunk = cfg . CONF . ml2_cisco . provider_vlan_auto_trunk <EOL> try : <EOL> nxos_db . get_port_vlan_switch_binding ( port_id , vlan_id , <EOL> switch_ip ) <EOL> except excep . NexusPortBindingNotFound : <EOL> pass <EOL> else : <EOL> continue <EOL> if auto_trunk : <EOL> self . driver . disable_vlan_on_trunk_int ( <EOL> switch_ip , vlan_id , intf_type , nexus_port , <EOL> is_native ) <EOL> if auto_create : <EOL> try : <EOL> nxos_db . get_nexusvlan_binding ( vlan_id , switch_ip ) <EOL> except excep . NexusPortBindingNotFound : <EOL> if switch_ip not in vlan_already_removed : <EOL> self . driver . delete_vlan ( switch_ip , vlan_id ) <EOL> vlan_already_removed . append ( switch_ip ) <EOL> def _is_segment_nexus_vxlan ( self , segment ) : <EOL> return segment [ api . NETWORK_TYPE ] == const . TYPE_NEXUS_VXLAN <EOL> def _get_segments ( self , top_segment , bottom_segment ) : <EOL> if top_segment is None : <EOL> return None , None <EOL> elif self . _is_segment_nexus_vxlan ( top_segment ) : <EOL> return bottom_segment , top_segment <EOL> else : <EOL> return top_segment , None <EOL> def _is_vm_migrating ( self , context , vlan_segment , orig_vlan_segment ) : <EOL> if not vlan_segment and orig_vlan_segment : <EOL> return ( context . current . get ( portbindings . HOST_ID ) != <EOL> context . original . get ( portbindings . HOST_ID ) ) <EOL> def _log_missing_segment ( self ) : <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) ) <EOL> def _is_valid_segment ( self , segment ) : <EOL> valid_segment = True <EOL> if segment : <EOL> if ( segment [ api . NETWORK_TYPE ] != p_const . TYPE_VLAN or <EOL> not self . _valid_network_segment ( segment ) ) : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : segment [ api . NETWORK_TYPE ] , <EOL> '<STR_LIT>' : segment [ api . PHYSICAL_NETWORK ] } ) <EOL> valid_segment = False <EOL> else : <EOL> self . _log_missing_segment ( ) <EOL> valid_segment = False <EOL> return valid_segment <EOL> def _port_action_vlan ( self , port , segment , func , vni ) : <EOL> """<STR_LIT>""" <EOL> if not self . _is_valid_segment ( segment ) : <EOL> return <EOL> device_id = port . get ( '<STR_LIT>' ) <EOL> if self . _is_baremetal ( port ) : <EOL> host_id = '<STR_LIT>' <EOL> else : <EOL> host_id = port . get ( portbindings . HOST_ID ) <EOL> vlan_id = segment . get ( api . SEGMENTATION_ID ) <EOL> is_provider = False <EOL> settings = { "<STR_LIT>" : vlan_id , <EOL> "<STR_LIT>" : device_id , <EOL> "<STR_LIT>" : host_id , <EOL> "<STR_LIT>" : is_provider is not None } <EOL> missing_fields = [ field for field , value in settings . items ( ) <EOL> if ( field != '<STR_LIT>' and not value ) ] <EOL> if not missing_fields : <EOL> func ( port , vlan_id , device_id , host_id , vni , is_provider ) <EOL> else : <EOL> raise excep . NexusMissingRequiredFields ( <EOL> fields = '<STR_LIT:U+0020>' . join ( missing_fields ) ) <EOL> def _port_action_vxlan ( self , port , segment , func ) : <EOL> """<STR_LIT>""" <EOL> if segment is None : <EOL> self . _log_missing_segment ( ) <EOL> return <EOL> device_id = port . get ( '<STR_LIT>' ) <EOL> mcast_group = segment . get ( api . PHYSICAL_NETWORK ) <EOL> host_id = port . get ( portbindings . HOST_ID ) <EOL> vni = segment . get ( api . SEGMENTATION_ID ) <EOL> if vni and device_id and mcast_group and host_id : <EOL> func ( vni , device_id , mcast_group , host_id ) <EOL> return vni <EOL> else : <EOL> fields = "<STR_LIT>" if not vni else "<STR_LIT>" <EOL> fields += "<STR_LIT>" if not device_id else "<STR_LIT>" <EOL> fields += "<STR_LIT>" if not mcast_group else "<STR_LIT>" <EOL> fields += "<STR_LIT>" if not host_id else "<STR_LIT>" <EOL> raise excep . NexusMissingRequiredFields ( fields = fields ) <EOL> def _monitor_thread ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . monitor_lock : <EOL> self . monitor . check_connections ( ) <EOL> self . timer = threading . Timer ( self . monitor_timeout , <EOL> self . _monitor_thread ) <EOL> self . timer . start ( ) <EOL> def _stop_monitor_thread ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . timer : <EOL> self . timer . cancel ( ) <EOL> self . timer = None <EOL> @ lockutils . synchronized ( '<STR_LIT>' ) <EOL> def create_port_postcommit ( self , context ) : <EOL> """<STR_LIT>""" <EOL> port = context . current <EOL> if self . _is_supported_deviceowner ( port ) : <EOL> if self . _is_baremetal ( context . current ) : <EOL> host_id = '<STR_LIT>' <EOL> all_switches , active_switches = ( <EOL> self . _get_baremetal_switches ( context . current ) ) <EOL> else : <EOL> host_id = context . current . get ( portbindings . HOST_ID ) <EOL> all_switches , active_switches = ( <EOL> self . _get_host_switches ( host_id ) ) <EOL> verified_active_switches = [ ] <EOL> for switch_ip in active_switches : <EOL> try : <EOL> self . driver . get_nexus_type ( switch_ip ) <EOL> verified_active_switches . append ( switch_ip ) <EOL> except Exception : <EOL> pass <EOL> if all_switches and not verified_active_switches : <EOL> raise excep . NexusConnectFailed ( <EOL> nexus_host = all_switches [ <NUM_LIT:0> ] , config = "<STR_LIT:None>" , <EOL> exc = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> @ lockutils . synchronized ( '<STR_LIT>' ) <EOL> def update_port_precommit ( self , context ) : <EOL> """<STR_LIT>""" <EOL> vlan_segment , vxlan_segment = self . _get_segments ( <EOL> context . top_bound_segment , <EOL> context . bottom_bound_segment ) <EOL> orig_vlan_segment , orig_vxlan_segment = self . _get_segments ( <EOL> context . original_top_bound_segment , <EOL> context . original_bottom_bound_segment ) <EOL> if self . _is_vm_migrating ( context , vlan_segment , orig_vlan_segment ) : <EOL> vni = self . _port_action_vxlan ( context . original , orig_vxlan_segment , <EOL> self . _delete_nve_db ) if orig_vxlan_segment else <NUM_LIT:0> <EOL> self . _port_action_vlan ( context . original , orig_vlan_segment , <EOL> self . _delete_nxos_db , vni ) <EOL> else : <EOL> if ( self . _is_supported_deviceowner ( context . current ) and <EOL> self . _is_status_active ( context . current ) and <EOL> not self . _is_baremetal ( context . current ) ) : <EOL> vni = self . _port_action_vxlan ( context . current , vxlan_segment , <EOL> self . _configure_nve_db ) if vxlan_segment else <NUM_LIT:0> <EOL> self . _port_action_vlan ( context . current , vlan_segment , <EOL> self . _configure_nxos_db , vni ) <EOL> @ lockutils . synchronized ( '<STR_LIT>' ) <EOL> def update_port_postcommit ( self , context ) : <EOL> """<STR_LIT>""" <EOL> vlan_segment , vxlan_segment = self . _get_segments ( <EOL> context . top_bound_segment , <EOL> context . bottom_bound_segment ) <EOL> orig_vlan_segment , orig_vxlan_segment = self . _get_segments ( <EOL> context . original_top_bound_segment , <EOL> context . original_bottom_bound_segment ) <EOL> if self . _is_vm_migrating ( context , vlan_segment , orig_vlan_segment ) : <EOL> vni = self . _port_action_vxlan ( context . original , orig_vxlan_segment , <EOL> self . _delete_nve_member ) if orig_vxlan_segment else <NUM_LIT:0> <EOL> self . _port_action_vlan ( context . original , orig_vlan_segment , <EOL> self . _delete_switch_entry , vni ) <EOL> else : <EOL> if ( self . _is_supported_deviceowner ( context . current ) and <EOL> self . _is_status_active ( context . current ) ) : <EOL> if self . _is_baremetal ( context . current ) : <EOL> self . _init_baremetal_trunk_interfaces ( <EOL> context . current , vlan_segment , <NUM_LIT:0> ) <EOL> host_id = '<STR_LIT>' <EOL> all_switches , active_switches = ( <EOL> self . _get_baremetal_switches ( context . current ) ) <EOL> else : <EOL> host_id = context . current . get ( portbindings . HOST_ID ) <EOL> all_switches , active_switches = ( <EOL> self . _get_host_switches ( host_id ) ) <EOL> if not active_switches and all_switches : <EOL> raise excep . NexusConnectFailed ( <EOL> nexus_host = all_switches [ <NUM_LIT:0> ] , config = "<STR_LIT:None>" , <EOL> exc = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> vni = self . _port_action_vxlan ( context . current , vxlan_segment , <EOL> self . _configure_nve_member ) if vxlan_segment else <NUM_LIT:0> <EOL> self . _port_action_vlan ( context . current , vlan_segment , <EOL> self . _configure_port_entries , vni ) <EOL> @ lockutils . synchronized ( '<STR_LIT>' ) <EOL> def delete_port_precommit ( self , context ) : <EOL> """<STR_LIT>""" <EOL> if self . _is_supported_deviceowner ( context . current ) : <EOL> vlan_segment , vxlan_segment = self . _get_segments ( <EOL> context . top_bound_segment , <EOL> context . bottom_bound_segment ) <EOL> vni = self . _port_action_vxlan ( context . current , vxlan_segment , <EOL> self . _delete_nve_db ) if vxlan_segment else <NUM_LIT:0> <EOL> self . _port_action_vlan ( context . current , vlan_segment , <EOL> self . _delete_nxos_db , vni ) <EOL> @ lockutils . synchronized ( '<STR_LIT>' ) <EOL> def delete_port_postcommit ( self , context ) : <EOL> """<STR_LIT>""" <EOL> if self . _is_supported_deviceowner ( context . current ) : <EOL> vlan_segment , vxlan_segment = self . _get_segments ( <EOL> context . top_bound_segment , <EOL> context . bottom_bound_segment ) <EOL> vni = self . _port_action_vxlan ( context . current , vxlan_segment , <EOL> self . _delete_nve_member ) if vxlan_segment else <NUM_LIT:0> <EOL> self . _port_action_vlan ( context . current , vlan_segment , <EOL> self . _delete_switch_entry , vni ) <EOL> def bind_port ( self , context ) : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> { '<STR_LIT:port>' : context . current [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : context . network . current [ '<STR_LIT:id>' ] } ) <EOL> if self . _supported_baremetal_transaction ( context ) : <EOL> return <EOL> for segment in context . segments_to_bind : <EOL> if self . _is_segment_nexus_vxlan ( segment ) : <EOL> host_id = context . current . get ( portbindings . HOST_ID ) <EOL> host_connections = self . _get_port_connections ( <EOL> context . current , <EOL> host_id ) <EOL> if not host_connections : <EOL> return <EOL> for switch_ip , attr2 , attr3 , attr4 in host_connections : <EOL> physnet = self . _nexus_switches . get ( ( switch_ip , '<STR_LIT>' ) ) <EOL> if physnet : <EOL> break <EOL> else : <EOL> raise excep . PhysnetNotConfigured ( host_id = host_id , <EOL> host_connections = host_connections ) <EOL> vlan_segment = { api . NETWORK_TYPE : p_const . TYPE_VLAN , <EOL> api . PHYSICAL_NETWORK : physnet } <EOL> context . allocate_dynamic_segment ( vlan_segment ) <EOL> network_id = context . current [ '<STR_LIT>' ] <EOL> dynamic_segment = ml2_db . get_dynamic_segment ( <EOL> db_api . get_session ( ) , network_id , physnet ) <EOL> if dynamic_segment : <EOL> context . continue_binding ( segment [ api . ID ] , <EOL> [ dynamic_segment ] ) <EOL> else : <EOL> raise excep . NoDynamicSegmentAllocated ( <EOL> network_id = network_id , physnet = physnet ) <EOL> else : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> { '<STR_LIT:id>' : segment [ api . ID ] , <EOL> '<STR_LIT>' : segment [ api . SEGMENTATION_ID ] , <EOL> '<STR_LIT>' : segment [ api . PHYSICAL_NETWORK ] , <EOL> '<STR_LIT>' : segment [ api . NETWORK_TYPE ] } ) </s>
