def _test_portinfo_change(self, portinfo_change_first=True): with self.port() as port: port_id = port['port']['id'] self.assertEqual(self.ofc.create_ofc_port.call_count, 0) portinfo = {'id': port_id, 'port_no': 123} self._rpcapi_update_ports(added=[portinfo]) self.assertEqual(self.ofc.create_ofc_port.call_count, 1) self.assertEqual(self.ofc.delete_ofc_port.call_count, 0) self.assertEqual(ndb.get_portinfo(self.context.session, port_id).port_no, 123) if portinfo_change_first: portinfo = {'id': port_id, 'port_no': 456} self._rpcapi_update_ports(added=[portinfo]) # OFC port is recreated. self.assertEqual(self.ofc.create_ofc_port.call_count, 2) self.assertEqual(self.ofc.delete_ofc_port.call_count, 1) self.assertEqual(ndb.get_portinfo(self.context.session, port_id).port_no, 456) if not portinfo_change_first: # The port is expected to delete when exiting with-clause. self.assertEqual(self.ofc.create_ofc_port.call_count, 1) self.assertEqual(self.ofc.delete_ofc_port.call_count, 1) portinfo = {'id': port_id, 'port_no': 456} self._rpcapi_update_ports(added=[portinfo]) # No OFC operations are expected. self.assertEqual(self.ofc.create_ofc_port.call_count, 1) self.assertEqual(self.ofc.delete_ofc_port.call_count, 1) self.assertEqual(ndb.get_portinfo(self.context.session, port_id).port_no, 456)
def update_ports(self, rpc_context, **kwargs): """Update ports' information and activate/deavtivate them. Expected input format is: {'topic': 'q-agent-notifier', 'agent_id': 'nec-q-agent.' + <hostname>, 'datapath_id': <datapath_id of br-int on remote host>, 'port_added': [<new PortInfo>,...], 'port_removed': [<removed Port ID>,...]} """ LOG.debug("NECPluginV2RPCCallbacks.update_ports() called, " "kwargs=%s ." % kwargs) topic = kwargs['topic'] datapath_id = kwargs['datapath_id'] for p in kwargs.get('port_added', []): id = p['id'] port = self.plugin.get_port(self.admin_context, id) if port and ndb.get_portinfo(id): ndb.del_portinfo(id) self.plugin.deactivate_port(self.admin_context, port) ndb.add_portinfo(id, datapath_id, p['port_no'], mac=p.get('mac', '')) self.plugin.activate_port_if_ready(self.admin_context, port) for id in kwargs.get('port_removed', []): port = self.plugin.get_port(self.admin_context, id) if port and ndb.get_portinfo(id): ndb.del_portinfo(id) self.plugin.deactivate_port(self.admin_context, port)
def update_ports(self, rpc_context, **kwargs): """Update ports' information and activate/deavtivate them. Expected input format is: {'topic': 'q-agent-notifier', 'agent_id': 'nec-q-agent.' + <hostname>, 'datapath_id': <datapath_id of br-int on remote host>, 'port_added': [<new PortInfo>,...], 'port_removed': [<removed Port ID>,...]} """ LOG.debug(_("NECPluginV2RPCCallbacks.update_ports() called, " "kwargs=%s ."), kwargs) topic = kwargs['topic'] datapath_id = kwargs['datapath_id'] session = rpc_context.session for p in kwargs.get('port_added', []): id = p['id'] port = self.plugin.get_port(rpc_context, id) if port and ndb.get_portinfo(session, id): ndb.del_portinfo(session, id) self.plugin.deactivate_port(rpc_context, port) ndb.add_portinfo(session, id, datapath_id, p['port_no'], mac=p.get('mac', '')) self.plugin.activate_port_if_ready(rpc_context, port) for id in kwargs.get('port_removed', []): port = self.plugin.get_port(rpc_context, id) if port and ndb.get_portinfo(session, id): ndb.del_portinfo(session, id) self.plugin.deactivate_port(rpc_context, port)
def testf_del_portinfo(self): """test delete portinfo""" i, d, p, v, m, n = self.get_portinfo_random_params() ndb.add_portinfo(self.session, i, d, p, v, m) portinfo = ndb.get_portinfo(self.session, i) self.assertEqual(portinfo.id, i) ndb.del_portinfo(self.session, i) portinfo_none = ndb.get_portinfo(self.session, i) self.assertEqual(None, portinfo_none)
def testf_del_portinfo(self): """test delete portinfo""" i, d, p, v, m, n = self.get_portinfo_random_params() ndb.add_portinfo(i, d, p, v, m) portinfo = ndb.get_portinfo(i) self.assertEqual(portinfo.id, i) ndb.del_portinfo(i) portinfo_none = ndb.get_portinfo(i) self.assertEqual(None, portinfo_none)
def update_ports(self, rpc_context, **kwargs): """Update ports' information and activate/deavtivate them. Expected input format is: {'topic': 'q-agent-notifier', 'agent_id': 'nec-q-agent.' + <hostname>, 'datapath_id': <datapath_id of br-int on remote host>, 'port_added': [<new PortInfo>,...], 'port_removed': [<removed Port ID>,...]} """ LOG.debug( _("NECPluginV2RPCCallbacks.update_ports() called, " "kwargs=%s ."), kwargs) datapath_id = kwargs['datapath_id'] session = rpc_context.session for p in kwargs.get('port_added', []): id = p['id'] portinfo = ndb.get_portinfo(session, id) if portinfo: ndb.del_portinfo(session, id) ndb.add_portinfo(session, id, datapath_id, p['port_no'], mac=p.get('mac', '')) port = self._get_port(rpc_context, id) if port: if portinfo: self.plugin.deactivate_port(rpc_context, port) self.plugin.activate_port_if_ready(rpc_context, port) for id in kwargs.get('port_removed', []): portinfo = ndb.get_portinfo(session, id) if not portinfo: LOG.debug( _("update_ports(): ignore port_removed message " "due to portinfo for port_id=%s was not " "registered"), id) continue if portinfo.datapath_id != datapath_id: LOG.debug( _("update_ports(): ignore port_removed message " "received from different host " "(registered_datapath_id=%(registered)s, " "received_datapath_id=%(received)s)."), { 'registered': portinfo.datapath_id, 'received': datapath_id }) continue ndb.del_portinfo(session, id) port = self._get_port(rpc_context, id) if port: self.plugin.deactivate_port(rpc_context, port)
def teste_get_portinfo(self): """test get portinfo""" i, d, p, v, m, n = self.get_portinfo_random_params() ndb.add_portinfo(self.session, i, d, p, v, m) portinfo = ndb.get_portinfo(self.session, i) self.assertEqual(portinfo.id, i) self.assertEqual(portinfo.datapath_id, d) self.assertEqual(portinfo.port_no, p) self.assertEqual(portinfo.vlan_id, v) self.assertEqual(portinfo.mac, m) portinfo_none = ndb.get_portinfo(self.session, n) self.assertEqual(None, portinfo_none)
def teste_get_portinfo(self): """test get portinfo""" i, d, p, v, m, n = self.get_portinfo_random_params() ndb.add_portinfo(i, d, p, v, m) portinfo = ndb.get_portinfo(i) self.assertEqual(portinfo.id, i) self.assertEqual(portinfo.datapath_id, d) self.assertEqual(portinfo.port_no, p) self.assertEqual(portinfo.vlan_id, v) self.assertEqual(portinfo.mac, m) portinfo_none = ndb.get_portinfo(n) self.assertEqual(None, portinfo_none)
def update_ports(self, rpc_context, **kwargs): """Update ports' information and activate/deavtivate them. Expected input format is: {'topic': 'q-agent-notifier', 'agent_id': 'nec-q-agent.' + <hostname>, 'datapath_id': <datapath_id of br-int on remote host>, 'port_added': [<new PortInfo>,...], 'port_removed': [<removed Port ID>,...]} """ LOG.debug(_("NECPluginV2RPCCallbacks.update_ports() called, " "kwargs=%s ."), kwargs) datapath_id = kwargs["datapath_id"] session = rpc_context.session for p in kwargs.get("port_added", []): id = p["id"] portinfo = ndb.get_portinfo(session, id) if portinfo: ndb.del_portinfo(session, id) ndb.add_portinfo(session, id, datapath_id, p["port_no"], mac=p.get("mac", "")) port = self._get_port(rpc_context, id) if port: if portinfo: self.plugin.deactivate_port(rpc_context, port) self.plugin.activate_port_if_ready(rpc_context, port) for id in kwargs.get("port_removed", []): portinfo = ndb.get_portinfo(session, id) if not portinfo: LOG.debug( _( "update_ports(): ignore port_removed message " "due to portinfo for port_id=%s was not " "registered" ), id, ) continue if portinfo.datapath_id != datapath_id: LOG.debug( _( "update_ports(): ignore port_removed message " "received from different host " "(registered_datapath_id=%(registered)s, " "received_datapath_id=%(received)s)." ), {"registered": portinfo.datapath_id, "received": datapath_id}, ) continue ndb.del_portinfo(session, id) port = self._get_port(rpc_context, id) if port: self.plugin.deactivate_port(rpc_context, port)
def test_portinfo_added_unknown_port(self): portinfo = {'id': 'dummy-p1', 'port_no': 123} self._rpcapi_update_ports(added=[portinfo]) self.assertIsNotNone(ndb.get_portinfo(self.context.session, 'dummy-p1')) self.assertEqual(self.ofc.exists_ofc_port.call_count, 0) self.assertEqual(self.ofc.create_ofc_port.call_count, 0)
def _activate_packet_filter_if_ready(self, context, packet_filter, network=None, in_port=None): """Activate packet_filter by creating filter on OFC if ready. Conditions to create packet_filter on OFC are: * packet_filter admin_state is UP * network admin_state is UP * (if 'in_port' is specified) portinfo is available """ net_id = packet_filter['network_id'] if not network: network = super(NECPluginV2, self).get_network(context, net_id) in_port_id = packet_filter.get("in_port") if in_port_id and not in_port: in_port = super(NECPluginV2, self).get_port(context, in_port_id) pf_status = OperationalStatus.ACTIVE if not packet_filter['admin_state_up']: LOG.debug( _("_activate_packet_filter_if_ready(): skip, " "packet_filter.admin_state_up is False.")) pf_status = OperationalStatus.DOWN elif not network['admin_state_up']: LOG.debug( _("_activate_packet_filter_if_ready(): skip, " "network.admin_state_up is False.")) pf_status = OperationalStatus.DOWN elif in_port_id and in_port_id is in_port.get('id'): LOG.debug( _("_activate_packet_filter_if_ready(): skip, " "invalid in_port_id.")) pf_status = OperationalStatus.DOWN elif in_port_id and not ndb.get_portinfo(context.session, in_port_id): LOG.debug( _("_activate_packet_filter_if_ready(): skip, " "no portinfo for in_port.")) pf_status = OperationalStatus.DOWN if pf_status in [OperationalStatus.ACTIVE]: if self.ofc.exists_ofc_packet_filter(context, packet_filter['id']): LOG.debug( _("_activate_packet_filter_if_ready(): skip, " "ofc_packet_filter already exists.")) else: try: (self.ofc.create_ofc_packet_filter(context, packet_filter['id'], packet_filter)) except (nexc.OFCException, nexc.OFCConsistencyBroken) as exc: reason = _("create_ofc_packet_filter() failed due to " "%s") % exc LOG.error(reason) pf_status = OperationalStatus.ERROR if pf_status is not packet_filter['status']: self._update_resource_status(context, "packet_filter", packet_filter['id'], pf_status)
def activate_port_if_ready(self, context, port, network=None): """Activate port by creating port on OFC if ready. Activate port and packet_filters associated with the port. Conditions to activate port on OFC are: * port admin_state is UP * network admin_state is UP * portinfo are available (to identify port on OFC) """ if not network: network = super(NECPluginV2, self).get_network(context, port['network_id']) port_status = OperationalStatus.ACTIVE if not port['admin_state_up']: LOG.debug("activate_port_if_ready(): skip, " "port.admin_state_up is False.") port_status = OperationalStatus.DOWN elif not network['admin_state_up']: LOG.debug("activate_port_if_ready(): skip, " "network.admin_state_up is False.") port_status = OperationalStatus.DOWN elif not ndb.get_portinfo(port['id']): LOG.debug("activate_port_if_ready(): skip, " "no portinfo for this port.") port_status = OperationalStatus.DOWN # activate packet_filters before creating port on OFC. if self.packet_filter_enabled: if port_status is OperationalStatus.ACTIVE: filters = dict(in_port=[port['id']], status=[OperationalStatus.DOWN], admin_state_up=[True]) pfs = (super(NECPluginV2, self).get_packet_filters(context, filters=filters)) for pf in pfs: self._activate_packet_filter_if_ready(context, pf, network=network, in_port=port) if port_status in [OperationalStatus.ACTIVE]: if self.ofc.exists_ofc_port(port['id']): LOG.debug("activate_port_if_ready(): skip, " "ofc_port already exists.") else: try: self.ofc.create_ofc_port(port['tenant_id'], port['network_id'], port['id']) except (nexc.OFCException, nexc.OFCConsistencyBroken) as exc: reason = "create_ofc_port() failed due to %s" % exc LOG.error(reason) port_status = OperationalStatus.ERROR if port_status is not port['status']: self._update_resource_status(context, "port", port['id'], port_status)
def activate_port_if_ready(self, context, port, network=None): """Activate port by creating port on OFC if ready. Activate port and packet_filters associated with the port. Conditions to activate port on OFC are: * port admin_state is UP * network admin_state is UP * portinfo are available (to identify port on OFC) """ if not network: network = super(NECPluginV2, self).get_network(context, port['network_id']) port_status = OperationalStatus.ACTIVE if not port['admin_state_up']: LOG.debug("activate_port_if_ready(): skip, " "port.admin_state_up is False.") port_status = OperationalStatus.DOWN elif not network['admin_state_up']: LOG.debug("activate_port_if_ready(): skip, " "network.admin_state_up is False.") port_status = OperationalStatus.DOWN elif not ndb.get_portinfo(port['id']): LOG.debug("activate_port_if_ready(): skip, " "no portinfo for this port.") port_status = OperationalStatus.DOWN # activate packet_filters before creating port on OFC. if self.packet_filter_enabled: if port_status is OperationalStatus.ACTIVE: filters = dict(in_port=[port['id']], status=[OperationalStatus.DOWN], admin_state_up=[True]) pfs = (super(NECPluginV2, self). get_packet_filters(context, filters=filters)) for pf in pfs: self._activate_packet_filter_if_ready(context, pf, network=network, in_port=port) if port_status in [OperationalStatus.ACTIVE]: if self.ofc.exists_ofc_port(port['id']): LOG.debug("activate_port_if_ready(): skip, " "ofc_port already exists.") else: try: self.ofc.create_ofc_port(port['tenant_id'], port['network_id'], port['id']) except (nexc.OFCException, nexc.OFCConsistencyBroken) as exc: reason = "create_ofc_port() failed due to %s" % exc LOG.error(reason) port_status = OperationalStatus.ERROR if port_status is not port['status']: self._update_resource_status(context, "port", port['id'], port_status)
def create_ofc_port(self, tenant_id, network_id, port_id): ofc_tenant_id = self._get_ofc_id("ofc_tenant", tenant_id) ofc_net_id = self._get_ofc_id("ofc_network", network_id) portinfo = ndb.get_portinfo(port_id) if not portinfo: raise nexc.PortInfoNotFound(id=port_id) ofc_port_id = self.driver.create_port(ofc_tenant_id, ofc_net_id, portinfo, port_id) ndb.add_ofc_item(nmodels.OFCPort, ofc_port_id, port_id)
def create_ofc_port(self, context, port_id, port): ofc_net_id = self._get_ofc_id(context, "ofc_network", port['network_id']) ofc_net_id = self.driver.convert_ofc_network_id( context, ofc_net_id, port['tenant_id']) portinfo = ndb.get_portinfo(context.session, port_id) if not portinfo: raise nexc.PortInfoNotFound(id=port_id) ofc_port_id = self.driver.create_port(ofc_net_id, portinfo, port_id) self._add_ofc_item(context, "ofc_port", port_id, ofc_port_id)
def _activate_packet_filter_if_ready(self, context, packet_filter, network=None, in_port=None): """Activate packet_filter by creating filter on OFC if ready. Conditions to create packet_filter on OFC are: * packet_filter admin_state is UP * network admin_state is UP * (if 'in_port' is specified) portinfo is available """ net_id = packet_filter['network_id'] if not network: network = super(NECPluginV2, self).get_network(context, net_id) in_port_id = packet_filter.get("in_port") if in_port_id and not in_port: in_port = super(NECPluginV2, self).get_port(context, in_port_id) pf_status = OperationalStatus.ACTIVE if not packet_filter['admin_state_up']: LOG.debug("_activate_packet_filter_if_ready(): skip, " "packet_filter.admin_state_up is False.") pf_status = OperationalStatus.DOWN elif not network['admin_state_up']: LOG.debug("_activate_packet_filter_if_ready(): skip, " "network.admin_state_up is False.") pf_status = OperationalStatus.DOWN elif in_port_id and in_port_id is in_port.get('id'): LOG.debug("_activate_packet_filter_if_ready(): skip, " "invalid in_port_id.") pf_status = OperationalStatus.DOWN elif in_port_id and not ndb.get_portinfo(in_port_id): LOG.debug("_activate_packet_filter_if_ready(): skip, " "no portinfo for in_port.") pf_status = OperationalStatus.DOWN if pf_status in [OperationalStatus.ACTIVE]: if self.ofc.exists_ofc_packet_filter(packet_filter['id']): LOG.debug("_activate_packet_filter_if_ready(): skip, " "ofc_packet_filter already exists.") else: try: (self.ofc. create_ofc_packet_filter(packet_filter['tenant_id'], packet_filter['network_id'], packet_filter['id'], packet_filter)) except (nexc.OFCException, nexc.OFCConsistencyBroken) as exc: reason = ("create_ofc_packet_filter() failed due to " "%s" % exc) LOG.error(reason) pf_status = OperationalStatus.ERROR if pf_status is not packet_filter['status']: self._update_resource_status(context, "packet_filter", packet_filter['id'], pf_status)
def create_ofc_packet_filter(self, tenant_id, network_id, filter_id, filter_dict): ofc_tenant_id = self._get_ofc_id("ofc_tenant", tenant_id) ofc_net_id = self._get_ofc_id("ofc_network", network_id) in_port_id = filter_dict.get('in_port') portinfo = None if in_port_id: portinfo = ndb.get_portinfo(in_port_id) if not portinfo: raise nexc.PortInfoNotFound(id=in_port_id) ofc_pf_id = self.driver.create_filter(ofc_tenant_id, ofc_net_id, filter_dict, portinfo, filter_id) ndb.add_ofc_item(nmodels.OFCFilter, ofc_pf_id, filter_id)
def create_ofc_packet_filter(self, context, filter_id, filter_dict): ofc_net_id = self._get_ofc_id(context, "ofc_network", filter_dict['network_id']) ofc_net_id = self.driver.convert_ofc_network_id( context, ofc_net_id, filter_dict['tenant_id']) in_port_id = filter_dict.get('in_port') portinfo = None if in_port_id: portinfo = ndb.get_portinfo(context.session, in_port_id) if not portinfo: raise nexc.PortInfoNotFound(id=in_port_id) ofc_pf_id = self.driver.create_filter(ofc_net_id, filter_dict, portinfo, filter_id) self._add_ofc_item(context, "ofc_packet_filter", filter_id, ofc_pf_id)
def _get_portinfo(self, port_id): return ndb.get_portinfo(self.context.session, port_id)