<s> from neutron_lib import constants <EOL> from oslo_log import log <EOL> from oslo_serialization import jsonutils <EOL> from neutron . _i18n import _LW <EOL> from neutron . extensions import portbindings <EOL> from neutron . plugins . ml2 import db <EOL> from neutron . plugins . ml2 import driver_api as api <EOL> LOG = log . getLogger ( __name__ ) <EOL> class MechanismDriverContext ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , plugin , plugin_context ) : <EOL> self . _plugin = plugin <EOL> self . _plugin_context = plugin_context <EOL> class NetworkContext ( MechanismDriverContext , api . NetworkContext ) : <EOL> def __init__ ( self , plugin , plugin_context , network , <EOL> original_network = None ) : <EOL> super ( NetworkContext , self ) . __init__ ( plugin , plugin_context ) <EOL> self . _network = network <EOL> self . _original_network = original_network <EOL> self . _segments = db . get_network_segments ( plugin_context . session , <EOL> network [ '<STR_LIT:id>' ] ) <EOL> @ property <EOL> def current ( self ) : <EOL> return self . _network <EOL> @ property <EOL> def original ( self ) : <EOL> return self . _original_network <EOL> @ property <EOL> def network_segments ( self ) : <EOL> return self . _segments <EOL> class SubnetContext ( MechanismDriverContext , api . SubnetContext ) : <EOL> def __init__ ( self , plugin , plugin_context , subnet , network , <EOL> original_subnet = None ) : <EOL> super ( SubnetContext , self ) . __init__ ( plugin , plugin_context ) <EOL> self . _subnet = subnet <EOL> self . _original_subnet = original_subnet <EOL> self . _network_context = NetworkContext ( plugin , plugin_context , <EOL> network ) <EOL> @ property <EOL> def current ( self ) : <EOL> return self . _subnet <EOL> @ property <EOL> def original ( self ) : <EOL> return self . _original_subnet <EOL> @ property <EOL> def network ( self ) : <EOL> return self . _network_context <EOL> class PortContext ( MechanismDriverContext , api . PortContext ) : <EOL> def __init__ ( self , plugin , plugin_context , port , network , binding , <EOL> binding_levels , original_port = None ) : <EOL> super ( PortContext , self ) . __init__ ( plugin , plugin_context ) <EOL> self . _port = port <EOL> self . _original_port = original_port <EOL> self . _network_context = NetworkContext ( plugin , plugin_context , <EOL> network ) <EOL> self . _binding = binding <EOL> self . _binding_levels = binding_levels <EOL> self . _segments_to_bind = None <EOL> self . _new_bound_segment = None <EOL> self . _next_segments_to_bind = None <EOL> if original_port : <EOL> self . _original_vif_type = binding . vif_type <EOL> self . _original_vif_details = self . _plugin . _get_vif_details ( binding ) <EOL> self . _original_binding_levels = self . _binding_levels <EOL> else : <EOL> self . _original_vif_type = None <EOL> self . _original_vif_details = None <EOL> self . _original_binding_levels = None <EOL> self . _new_port_status = None <EOL> def _prepare_to_bind ( self , segments_to_bind ) : <EOL> self . _segments_to_bind = segments_to_bind <EOL> self . _new_bound_segment = None <EOL> self . _next_segments_to_bind = None <EOL> def _clear_binding_levels ( self ) : <EOL> self . _binding_levels = [ ] <EOL> def _push_binding_level ( self , binding_level ) : <EOL> self . _binding_levels . append ( binding_level ) <EOL> def _pop_binding_level ( self ) : <EOL> return self . _binding_levels . pop ( ) <EOL> @ property <EOL> def current ( self ) : <EOL> return self . _port <EOL> @ property <EOL> def original ( self ) : <EOL> return self . _original_port <EOL> @ property <EOL> def status ( self ) : <EOL> if self . _port [ '<STR_LIT>' ] == constants . DEVICE_OWNER_DVR_INTERFACE : <EOL> return self . _binding . status <EOL> return self . _port [ '<STR_LIT:status>' ] <EOL> @ property <EOL> def original_status ( self ) : <EOL> if self . _original_port : <EOL> return self . _original_port [ '<STR_LIT:status>' ] <EOL> @ property <EOL> def network ( self ) : <EOL> return self . _network_context <EOL> @ property <EOL> def binding_levels ( self ) : <EOL> if self . _binding_levels : <EOL> return [ { <EOL> api . BOUND_DRIVER : level . driver , <EOL> api . BOUND_SEGMENT : self . _expand_segment ( level . segment_id ) <EOL> } for level in self . _binding_levels ] <EOL> @ property <EOL> def original_binding_levels ( self ) : <EOL> if self . _original_binding_levels : <EOL> return [ { <EOL> api . BOUND_DRIVER : level . driver , <EOL> api . BOUND_SEGMENT : self . _expand_segment ( level . segment_id ) <EOL> } for level in self . _original_binding_levels ] <EOL> @ property <EOL> def top_bound_segment ( self ) : <EOL> if self . _binding_levels : <EOL> return self . _expand_segment ( self . _binding_levels [ <NUM_LIT:0> ] . segment_id ) <EOL> @ property <EOL> def original_top_bound_segment ( self ) : <EOL> if self . _original_binding_levels : <EOL> return self . _expand_segment ( <EOL> self . _original_binding_levels [ <NUM_LIT:0> ] . segment_id ) <EOL> @ property <EOL> def bottom_bound_segment ( self ) : <EOL> if self . _binding_levels : <EOL> return self . _expand_segment ( self . _binding_levels [ - <NUM_LIT:1> ] . segment_id ) <EOL> @ property <EOL> def original_bottom_bound_segment ( self ) : <EOL> if self . _original_binding_levels : <EOL> return self . _expand_segment ( <EOL> self . _original_binding_levels [ - <NUM_LIT:1> ] . segment_id ) <EOL> def _expand_segment ( self , segment_id ) : <EOL> segment = db . get_segment_by_id ( self . _plugin_context . session , <EOL> segment_id ) <EOL> if not segment : <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) , segment_id ) <EOL> return segment <EOL> @ property <EOL> def host ( self ) : <EOL> if self . _port [ '<STR_LIT>' ] == constants . DEVICE_OWNER_DVR_INTERFACE : <EOL> return self . _binding . host <EOL> return self . _port . get ( portbindings . HOST_ID ) <EOL> @ property <EOL> def original_host ( self ) : <EOL> if self . _port [ '<STR_LIT>' ] == constants . DEVICE_OWNER_DVR_INTERFACE : <EOL> return self . _original_port and self . _binding . host <EOL> else : <EOL> return ( self . _original_port and <EOL> self . _original_port . get ( portbindings . HOST_ID ) ) <EOL> @ property <EOL> def vif_type ( self ) : <EOL> return self . _binding . vif_type <EOL> @ property <EOL> def original_vif_type ( self ) : <EOL> return self . _original_vif_type <EOL> @ property <EOL> def vif_details ( self ) : <EOL> return self . _plugin . _get_vif_details ( self . _binding ) <EOL> @ property <EOL> def original_vif_details ( self ) : <EOL> return self . _original_vif_details <EOL> @ property <EOL> def segments_to_bind ( self ) : <EOL> return self . _segments_to_bind <EOL> def host_agents ( self , agent_type ) : <EOL> return self . _plugin . get_agents ( self . _plugin_context , <EOL> filters = { '<STR_LIT>' : [ agent_type ] , <EOL> '<STR_LIT:host>' : [ self . _binding . host ] } ) <EOL> def set_binding ( self , segment_id , vif_type , vif_details , <EOL> status = None ) : <EOL> self . _new_bound_segment = segment_id <EOL> self . _binding . vif_type = vif_type <EOL> self . _binding . vif_details = jsonutils . dumps ( vif_details ) <EOL> self . _new_port_status = status <EOL> def continue_binding ( self , segment_id , next_segments_to_bind ) : <EOL> self . _new_bound_segment = segment_id <EOL> self . _next_segments_to_bind = next_segments_to_bind <EOL> def allocate_dynamic_segment ( self , segment ) : <EOL> network_id = self . _network_context . current [ '<STR_LIT:id>' ] <EOL> return self . _plugin . type_manager . allocate_dynamic_segment ( <EOL> self . _plugin_context . session , network_id , segment ) <EOL> def release_dynamic_segment ( self , segment_id ) : <EOL> return self . _plugin . type_manager . release_dynamic_segment ( <EOL> self . _plugin_context . session , segment_id ) </s>
