<s> from oslo_log import log <EOL> from dragonflow . _i18n import _LI , _LE , _LW <EOL> from dragonflow . db . api_nb import OvsPort <EOL> LOG = log . getLogger ( __name__ ) <EOL> class Topology ( object ) : <EOL> def __init__ ( self , controller , enable_selective_topology_distribution ) : <EOL> self . ovs_port_type = ( OvsPort . TYPE_VM , <EOL> OvsPort . TYPE_TUNNEL , <EOL> OvsPort . TYPE_PATCH , <EOL> OvsPort . TYPE_BRIDGE ) <EOL> self . topic_subscribed = { } <EOL> self . enable_selective_topo_dist = enable_selective_topology_distribution <EOL> self . ovs_ports = { } <EOL> self . ovs_to_lport_mapping = { } <EOL> self . controller = controller <EOL> self . nb_api = controller . get_nb_api ( ) <EOL> self . db_store = controller . get_db_store ( ) <EOL> self . openflow_app = controller . get_openflow_app ( ) <EOL> self . chassis_name = controller . get_chassis_name ( ) <EOL> def ovs_port_updated ( self , ovs_port ) : <EOL> """<STR_LIT>""" <EOL> if ovs_port is None : <EOL> LOG . error ( _LE ( "<STR_LIT>" ) ) <EOL> return <EOL> LOG . info ( _LI ( "<STR_LIT>" ) % str ( ovs_port ) ) <EOL> port_id = ovs_port . get_id ( ) <EOL> old_port = self . ovs_ports . get ( port_id ) <EOL> if old_port is None : <EOL> if not self . _check_ovs_port_integrity ( ovs_port ) : <EOL> return <EOL> else : <EOL> action = "<STR_LIT>" <EOL> else : <EOL> action = '<STR_LIT>' <EOL> self . ovs_ports [ port_id ] = ovs_port <EOL> port_type = ovs_port . get_type ( ) <EOL> if port_type not in self . ovs_port_type : <EOL> LOG . error ( _LE ( "<STR_LIT>" ) % str ( ovs_port ) ) <EOL> return <EOL> handler_name = '<STR_LIT:_>' + port_type + '<STR_LIT>' + action <EOL> try : <EOL> handler = getattr ( self , handler_name , None ) <EOL> if handler is not None : <EOL> handler ( ovs_port ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( <EOL> "<STR_LIT>" ) ) <EOL> def ovs_port_deleted ( self , ovs_port_id ) : <EOL> """<STR_LIT>""" <EOL> ovs_port = self . ovs_ports . get ( ovs_port_id ) <EOL> if ovs_port is None : <EOL> return <EOL> port_type = ovs_port . get_type ( ) <EOL> if port_type not in self . ovs_port_type : <EOL> LOG . error ( _LE ( "<STR_LIT>" ) % str ( ovs_port ) ) <EOL> return <EOL> handler_name = '<STR_LIT:_>' + port_type + '<STR_LIT>' <EOL> try : <EOL> handler = getattr ( self , handler_name , None ) <EOL> if handler is not None : <EOL> handler ( ovs_port ) <EOL> else : <EOL> LOG ( _LI ( "<STR_LIT>" ) % handler_name ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> finally : <EOL> del self . ovs_ports [ ovs_port_id ] <EOL> def _check_ovs_port_integrity ( self , ovs_port ) : <EOL> """<STR_LIT>""" <EOL> ofport = ovs_port . get_ofport ( ) <EOL> port_type = ovs_port . get_type ( ) <EOL> if ( ofport is None ) or ( ofport < <NUM_LIT:0> ) or ( port_type is None ) : <EOL> return False <EOL> if ( port_type == OvsPort . TYPE_VM ) and ( ovs_port . get_iface_id ( ) is None ) : <EOL> return False <EOL> return True <EOL> def _vm_port_added ( self , ovs_port ) : <EOL> self . _vm_port_updated ( ovs_port ) <EOL> def _vm_port_updated ( self , ovs_port ) : <EOL> lport_id = ovs_port . get_iface_id ( ) <EOL> lport = self . _get_lport ( lport_id ) <EOL> if lport is None : <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) <EOL> % str ( ovs_port ) ) <EOL> return <EOL> topic = lport . get_topic ( ) <EOL> self . _add_to_topic_subscribed ( topic , lport_id ) <EOL> ovs_port_id = ovs_port . get_id ( ) <EOL> self . ovs_to_lport_mapping [ ovs_port_id ] = { '<STR_LIT>' : lport_id , <EOL> '<STR_LIT>' : topic } <EOL> LOG . info ( _LI ( "<STR_LIT>" ) % str ( lport ) ) <EOL> try : <EOL> self . controller . logical_port_updated ( lport ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % str ( lport ) ) <EOL> def _vm_port_deleted ( self , ovs_port ) : <EOL> ovs_port_id = ovs_port . get_id ( ) <EOL> lport_id = ovs_port . get_iface_id ( ) <EOL> lport = self . db_store . get_port ( lport_id ) <EOL> if lport is None : <EOL> lport = self . ovs_to_lport_mapping . get ( ovs_port_id ) <EOL> if lport is None : <EOL> return <EOL> topic = lport . get ( '<STR_LIT>' ) <EOL> del self . ovs_to_lport_mapping [ ovs_port_id ] <EOL> self . _del_from_topic_subscribed ( topic , lport_id ) <EOL> return <EOL> topic = lport . get_topic ( ) <EOL> LOG . info ( _LI ( "<STR_LIT>" ) % str ( lport ) ) <EOL> try : <EOL> self . controller . logical_port_deleted ( lport_id ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( <EOL> '<STR_LIT>' ) % lport_id ) <EOL> finally : <EOL> del self . ovs_to_lport_mapping [ ovs_port_id ] <EOL> self . _del_from_topic_subscribed ( topic , lport_id ) <EOL> def _add_to_topic_subscribed ( self , topic , lport_id ) : <EOL> if not self . enable_selective_topo_dist : <EOL> return <EOL> if topic not in self . topic_subscribed : <EOL> LOG . info ( _LI ( "<STR_LIT>" ) % <EOL> { "<STR_LIT>" : topic , "<STR_LIT:id>" : lport_id } ) <EOL> self . nb_api . subscriber . register_topic ( topic ) <EOL> self . _pull_tenant_topology_from_db ( topic , lport_id ) <EOL> self . topic_subscribed [ topic ] = set ( [ lport_id ] ) <EOL> else : <EOL> self . topic_subscribed [ topic ] . add ( lport_id ) <EOL> def _del_from_topic_subscribed ( self , topic , lport_id ) : <EOL> if not self . enable_selective_topo_dist : <EOL> return <EOL> port_ids = self . topic_subscribed [ topic ] <EOL> port_ids . remove ( lport_id ) <EOL> if len ( port_ids ) == <NUM_LIT:0> : <EOL> LOG . info ( _LI ( "<STR_LIT>" ) % <EOL> { "<STR_LIT>" : topic , "<STR_LIT:id>" : lport_id } ) <EOL> del self . topic_subscribed [ topic ] <EOL> self . nb_api . subscriber . unregister_topic ( topic ) <EOL> self . _clear_tenant_topology ( topic ) <EOL> def _pull_tenant_topology_from_db ( self , tenant_id , lport_id ) : <EOL> switches = self . nb_api . get_all_logical_switches ( tenant_id ) <EOL> for switch in switches : <EOL> self . controller . logical_switch_updated ( switch ) <EOL> sg_groups = self . nb_api . get_security_groups ( tenant_id ) <EOL> for sg_group in sg_groups : <EOL> self . controller . security_group_updated ( sg_group ) <EOL> ports = self . nb_api . get_all_logical_ports ( tenant_id ) <EOL> for port in ports : <EOL> if port . get_id ( ) == lport_id : <EOL> continue <EOL> self . controller . logical_port_updated ( port ) <EOL> routers = self . nb_api . get_routers ( tenant_id ) <EOL> for router in routers : <EOL> self . controller . router_updated ( router ) <EOL> floating_ips = self . nb_api . get_floatingips ( tenant_id ) <EOL> for floating_ip in floating_ips : <EOL> self . controller . floatingip_updated ( floating_ip ) <EOL> def _clear_tenant_topology ( self , tenant_id ) : <EOL> ports = self . db_store . get_ports ( ) <EOL> for port in ports : <EOL> if tenant_id == port . get_topic ( ) : <EOL> self . controller . logical_port_deleted ( port . get_id ( ) ) <EOL> floating_ips = self . db_store . get_floatingips ( ) <EOL> for floating_ip in floating_ips : <EOL> if tenant_id == floating_ip . get_topic ( ) : <EOL> self . controller . floatingip_deleted ( floating_ip . get_name ( ) ) <EOL> switches = self . db_store . get_lswitchs ( ) <EOL> for switch in switches : <EOL> if tenant_id == switch . get_topic ( ) : <EOL> self . controller . logical_switch_deleted ( switch . get_id ( ) ) <EOL> routers = self . db_store . get_routers ( ) <EOL> for router in routers : <EOL> if tenant_id == router . get_topic ( ) : <EOL> self . controller . router_deleted ( router . get_name ( ) ) <EOL> sg_groups = self . db_store . get_security_groups ( ) <EOL> for sg_group in sg_groups : <EOL> if tenant_id == sg_group . topic : <EOL> self . controller . security_group_deleted ( sg_group . name ) <EOL> def _get_lport ( self , port_id , topic = None ) : <EOL> lport = self . db_store . get_port ( port_id ) <EOL> if lport is None : <EOL> lport = self . nb_api . get_logical_port ( port_id , topic ) <EOL> return lport </s>
