Example #1
0
    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:
                if necutils.cmp_dpid(portinfo.datapath_id, datapath_id) and portinfo.port_no == p["port_no"]:
                    LOG.debug(_("update_ports(): ignore unchanged portinfo in " "port_added message (port_id=%s)."), id)
                    continue
                ndb.del_portinfo(session, id)
            port = self._get_port(rpc_context, id)
            if port:
                ndb.add_portinfo(session, id, datapath_id, p["port_no"], mac=p.get("mac", ""))
                # NOTE: Make sure that packet filters on this port exist while
                # the port is active to avoid unexpected packet transfer.
                if portinfo:
                    self.plugin.deactivate_port(rpc_context, port)
                    self.plugin.deactivate_packet_filters_by_port(rpc_context, id)
                self.plugin.activate_packet_filters_by_port(rpc_context, id)
                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 not necutils.cmp_dpid(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)
                self.plugin.deactivate_packet_filters_by_port(rpc_context, id)
Example #2
0
    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:
                if (necutils.cmp_dpid(portinfo.datapath_id, datapath_id) and
                    portinfo.port_no == p['port_no']):
                    LOG.debug(_("update_ports(): ignore unchanged portinfo in "
                                "port_added message (port_id=%s)."), id)
                    continue
                ndb.del_portinfo(session, id)
            port = self._get_port(rpc_context, id)
            if port:
                ndb.add_portinfo(session, id, datapath_id, p['port_no'],
                                 mac=p.get('mac', ''))
                # NOTE: Make sure that packet filters on this port exist while
                # the port is active to avoid unexpected packet transfer.
                if portinfo:
                    self.plugin.deactivate_port(rpc_context, port,
                                                raise_exc=False)
                    self.plugin.deactivate_packet_filters_by_port(
                        rpc_context, id, raise_exc=False)
                self.plugin.activate_packet_filters_by_port(rpc_context, id)
                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 not necutils.cmp_dpid(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, raise_exc=False)
                self.plugin.deactivate_packet_filters_by_port(
                    rpc_context, id, raise_exc=False)
Example #3
0
 def test_cmp_dpid(self):
     self.assertTrue(utils.cmp_dpid('0xabcd', '0xabcd'))
     self.assertTrue(utils.cmp_dpid('abcd', '0xabcd'))
     self.assertTrue(utils.cmp_dpid('0x000000000000abcd', '0xabcd'))
     self.assertTrue(utils.cmp_dpid('0x000000000000abcd', '0x00abcd'))
     self.assertFalse(utils.cmp_dpid('0x000000000000abcd', '0xabc0'))
     self.assertFalse(utils.cmp_dpid('0x000000000000abcd', '0x00abc0'))
Example #4
0
    def _process_portbindings_portinfo_update(self, context, port_data, port):
        """Update portinfo according to bindings:profile in update_port().

        :param context: neutron api request context
        :param port_data: port attributes passed in PUT request
        :param port: port attributes to be returned
        :returns: 'ADD', 'MOD', 'DEL' or None
        """
        if portbindings.PROFILE not in port_data:
            return
        profile = port_data.get(portbindings.PROFILE)
        # If binding:profile is None or an empty dict,
        # it means binding:.profile needs to be cleared.
        # TODO(amotoki): Allow Make None in binding:profile in
        # the API layer. See LP bug #1220011.
        profile_set = attrs.is_attr_set(profile) and profile
        cur_portinfo = ndb.get_portinfo(context.session, port["id"])
        if profile_set:
            portinfo = self._validate_portinfo(profile)
            portinfo_changed = "ADD"
            if cur_portinfo:
                if (
                    necutils.cmp_dpid(portinfo["datapath_id"], cur_portinfo.datapath_id)
                    and portinfo["port_no"] == cur_portinfo.port_no
                ):
                    return
                ndb.del_portinfo(context.session, port["id"])
                portinfo_changed = "MOD"
            portinfo["mac"] = port["mac_address"]
            ndb.add_portinfo(context.session, port["id"], **portinfo)
        elif cur_portinfo:
            portinfo_changed = "DEL"
            portinfo = None
            ndb.del_portinfo(context.session, port["id"])
        else:
            portinfo = None
            portinfo_changed = None
        self._extend_port_dict_binding_portinfo(port, portinfo)
        return portinfo_changed
Example #5
0
    def _process_portbindings_portinfo_update(self, context, port_data, port):
        """Update portinfo according to bindings:profile in update_port().

        :param context: neutron api request context
        :param port_data: port attributes passed in PUT request
        :param port: port attributes to be returned
        :returns: 'ADD', 'MOD', 'DEL' or None
        """
        if portbindings.PROFILE not in port_data:
            return
        profile = port_data.get(portbindings.PROFILE)
        # If binding:profile is None or an empty dict,
        # it means binding:.profile needs to be cleared.
        # TODO(amotoki): Allow Make None in binding:profile in
        # the API layer. See LP bug #1220011.
        profile_set = attrs.is_attr_set(profile) and profile
        cur_portinfo = ndb.get_portinfo(context.session, port['id'])
        if profile_set:
            portinfo = self._validate_portinfo(profile)
            portinfo_changed = 'ADD'
            if cur_portinfo:
                if (necutils.cmp_dpid(portinfo['datapath_id'],
                                      cur_portinfo.datapath_id)
                        and portinfo['port_no'] == cur_portinfo.port_no):
                    return
                ndb.del_portinfo(context.session, port['id'])
                portinfo_changed = 'MOD'
            portinfo['mac'] = port['mac_address']
            ndb.add_portinfo(context.session, port['id'], **portinfo)
        elif cur_portinfo:
            portinfo_changed = 'DEL'
            portinfo = None
            ndb.del_portinfo(context.session, port['id'])
        else:
            portinfo = None
            portinfo_changed = None
        self._extend_port_dict_binding_portinfo(port, portinfo)
        return portinfo_changed
Example #6
0
 def test_cmp_dpid_with_exception(self):
     self.assertFalse(utils.cmp_dpid('0xabcx', '0xabcx'))
     self.assertFalse(utils.cmp_dpid(None, None))