Exemplo n.º 1
0
 def _verify_get_nsx_switch_and_port_id(self, exp_ls_uuid, exp_lp_uuid):
     # The nsxlib and db calls are mocked, therefore the cluster
     # and the neutron_port_id parameters can be set to None
     ls_uuid, lp_uuid = nsx_utils.get_nsx_switch_and_port_id(
         db_api.get_session(), None, None)
     self.assertEqual(exp_ls_uuid, ls_uuid)
     self.assertEqual(exp_lp_uuid, lp_uuid)
    def _nsx_delete_port(self, context, port_data):
        # FIXME(salvatore-orlando): On the NSX platform we do not really have
        # external networks. So deleting regular ports from external networks
        # does not make sense. However we cannot raise as this would break
        # unit tests.

        # NOTE(rods): reporting mark's comment on havana version of this patch.
        # Akanda does want ports for external networks so this method is
        # basically same with external check removed

        # ---------------------------------------------------------------------
        # Original code:
        # if self._network_is_external(context, port_data['network_id']):
        #     LOG.info(_("NSX plugin does not support regular VIF ports on "
        #                "external networks. Port %s will be down."),
        #              port_data['network_id'])
        #     return
        # ---------------------------------------------------------------------

        nsx_switch_id, nsx_port_id = nsx_utils.get_nsx_switch_and_port_id(
            context.session, self.cluster, port_data['id'])
        if not nsx_port_id:
            LOG.debug(_("Port '%s' was already deleted on NSX platform"), id)
            return
        # TODO(bgh): if this is a bridged network and the lswitch we just got
        # back will have zero ports after the delete we should garbage collect
        # the lswitch.
        try:
            switchlib.delete_port(self.cluster, nsx_switch_id, nsx_port_id)
            LOG.debug(_("_nsx_delete_port completed for port %(port_id)s "
                        "on network %(net_id)s"),
                      {'port_id': port_data['id'],
                       'net_id': port_data['network_id']})
        except n_exc.NotFound:
            LOG.warning(_("Port %s not found in NSX"), port_data['id'])
Exemplo n.º 3
0
    def update_port_precommit(self, context):
        #TODO: mac_learning

        port_data = context.current

        nsx_switch_id, nsx_port_id = nsx_utils.get_nsx_switch_and_port_id(
            context._plugin_context.session,
            self.cluster,
            port_data['id']
        )

        nsx_sec_profile_ids = self._convert_to_nsx_secgroup_ids(
            context,
            port_data.get('security_groups') or []
        )

        # ensure port_security_enabled flag set

        if nsx_switch_id:
            switchlib.update_port(
                self.cluster,
                nsx_switch_id,
                nsx_port_id,
                port_data['id'],
                port_data['tenant_id'],
                port_data['name'],
                port_data['device_id'],
                port_data['admin_state_up'],
                port_data['mac_address'],
                port_data['fixed_ips'],
                port_security_enabled=port_data['port_security_enabled'],
                security_profiles=nsx_sec_profile_ids,
                mac_learning_enabled=None, # TODO
                allowed_address_pairs=port_data['allowed_address_pairs']
            )
Exemplo n.º 4
0
 def _verify_get_nsx_switch_and_port_id(self, exp_ls_uuid, exp_lp_uuid):
     # The nsxlib and db calls are mocked, therefore the cluster
     # and the neutron_port_id parameters can be set to None
     ls_uuid, lp_uuid = nsx_utils.get_nsx_switch_and_port_id(
         db_api.get_session(), None, None)
     self.assertEqual(exp_ls_uuid, ls_uuid)
     self.assertEqual(exp_lp_uuid, lp_uuid)
Exemplo n.º 5
0
    def synchronize_port(self, context, neutron_port_data, lswitchport=None, ext_networks=None):
        """Synchronize a Neutron port with its NSX counterpart."""
        # Skip synchronization for ports on external networks
        if not ext_networks:
            ext_networks = [
                net["id"]
                for net in context.session.query(models_v2.Network).join(
                    external_net_db.ExternalNetwork,
                    (models_v2.Network.id == external_net_db.ExternalNetwork.network_id),
                )
            ]
        if neutron_port_data["network_id"] in ext_networks:
            with context.session.begin(subtransactions=True):
                neutron_port_data["status"] = constants.PORT_STATUS_ACTIVE
                return

        if not lswitchport:
            # Try to get port from nsx
            try:
                ls_uuid, lp_uuid = nsx_utils.get_nsx_switch_and_port_id(
                    context.session, self._cluster, neutron_port_data["id"]
                )
                if lp_uuid:
                    lswitchport = switchlib.get_port(self._cluster, ls_uuid, lp_uuid, relations="LogicalPortStatus")
            except (exceptions.PortNotFoundOnNetwork):
                # NOTE(salv-orlando): We should be catching
                # api_exc.ResourceNotFound here instead
                # of PortNotFoundOnNetwork when the id exists but
                # the logical switch port was not found
                LOG.warning(_("Logical switch port for neutron port %s " "not found on NSX."), neutron_port_data["id"])
                lswitchport = None
            else:
                # If lswitchport is not None, update the cache.
                # It could be none if the port was deleted from the backend
                if lswitchport:
                    self._nsx_cache.update_lswitchport(lswitchport)
        # Note(salv-orlando): It might worth adding a check to verify neutron
        # resource tag in nsx entity matches Neutron id.
        # By default assume things go wrong
        status = constants.PORT_STATUS_ERROR
        if lswitchport:
            lp_status = lswitchport["_relations"]["LogicalPortStatus"]["fabric_status_up"]
            status = lp_status and constants.PORT_STATUS_ACTIVE or constants.PORT_STATUS_DOWN

        # Update db object
        if status == neutron_port_data["status"]:
            # do nothing
            return

        with context.session.begin(subtransactions=True):
            try:
                port = self._plugin._get_port(context, neutron_port_data["id"])
            except exceptions.PortNotFound:
                pass
            else:
                port.status = status
                LOG.debug(
                    _("Updating status for neutron resource %(q_id)s to:" " %(status)s"),
                    {"q_id": neutron_port_data["id"], "status": status},
                )
Exemplo n.º 6
0
    def synchronize_port(self, context, neutron_port_data,
                         lswitchport=None, ext_networks=None):
        """Synchronize a Neutron port with its NVP counterpart."""
        # Skip synchronization for ports on external networks
        if not ext_networks:
            ext_networks = [net['id'] for net in context.session.query(
                models_v2.Network).join(
                    external_net_db.ExternalNetwork,
                    (models_v2.Network.id ==
                     external_net_db.ExternalNetwork.network_id))]
        if neutron_port_data['network_id'] in ext_networks:
            with context.session.begin(subtransactions=True):
                neutron_port_data['status'] = constants.PORT_STATUS_ACTIVE
                return

        if not lswitchport:
            # Try to get port from nvp
            try:
                ls_uuid, lp_uuid = nsx_utils.get_nsx_switch_and_port_id(
                    context.session, self._cluster, neutron_port_data['id'])
                if lp_uuid:
                    lswitchport = switchlib.get_port(
                        self._cluster, ls_uuid, lp_uuid,
                        relations='LogicalPortStatus')
            except (exceptions.PortNotFoundOnNetwork):
                # NOTE(salv-orlando): We should be catching
                # api_exc.ResourceNotFound here instead
                # of PortNotFoundOnNetwork when the id exists but
                # the logical switch port was not found
                LOG.warning(_("Logical switch port for neutron port %s "
                              "not found on NVP."), neutron_port_data['id'])
                lswitchport = None
            else:
                # If lswitchport is not None, update the cache.
                # It could be none if the port was deleted from the backend
                if lswitchport:
                    self._nvp_cache.update_lswitchport(lswitchport)
        # Note(salv-orlando): It might worth adding a check to verify neutron
        # resource tag in nvp entity matches Neutron id.
        # By default assume things go wrong
        status = constants.PORT_STATUS_ERROR
        if lswitchport:
            lp_status = (lswitchport['_relations']
                         ['LogicalPortStatus']
                         ['fabric_status_up'])
            status = (lp_status and
                      constants.PORT_STATUS_ACTIVE
                      or constants.PORT_STATUS_DOWN)
        # Update db object
        self._update_neutron_object(context, neutron_port_data, status)
Exemplo n.º 7
0
    def synchronize_port(self, context, neutron_port_data,
                         lswitchport=None, ext_networks=None):
        """Synchronize a Neutron port with its NVP counterpart."""
        # Skip synchronization for ports on external networks
        if not ext_networks:
            ext_networks = [net['id'] for net in context.session.query(
                models_v2.Network).join(
                    external_net_db.ExternalNetwork,
                    (models_v2.Network.id ==
                     external_net_db.ExternalNetwork.network_id))]
        if neutron_port_data['network_id'] in ext_networks:
            with context.session.begin(subtransactions=True):
                neutron_port_data['status'] = constants.PORT_STATUS_ACTIVE
                return

        if not lswitchport:
            # Try to get port from nvp
            try:
                ls_uuid, lp_uuid = nsx_utils.get_nsx_switch_and_port_id(
                    context.session, self._cluster, neutron_port_data['id'])
                if lp_uuid:
                    lswitchport = switchlib.get_port(
                        self._cluster, ls_uuid, lp_uuid,
                        relations='LogicalPortStatus')
            except (exceptions.PortNotFoundOnNetwork):
                # NOTE(salv-orlando): We should be catching
                # api_exc.ResourceNotFound here instead
                # of PortNotFoundOnNetwork when the id exists but
                # the logical switch port was not found
                LOG.warning(_("Logical switch port for neutron port %s "
                              "not found on NVP."), neutron_port_data['id'])
                lswitchport = None
            else:
                # If lswitchport is not None, update the cache.
                # It could be none if the port was deleted from the backend
                if lswitchport:
                    self._nvp_cache.update_lswitchport(lswitchport)
        # Note(salv-orlando): It might worth adding a check to verify neutron
        # resource tag in nvp entity matches Neutron id.
        # By default assume things go wrong
        status = constants.PORT_STATUS_ERROR
        if lswitchport:
            lp_status = (lswitchport['_relations']
                         ['LogicalPortStatus']
                         ['fabric_status_up'])
            status = (lp_status and
                      constants.PORT_STATUS_ACTIVE
                      or constants.PORT_STATUS_DOWN)
        # Update db object
        self._update_neutron_object(context, neutron_port_data, status)
Exemplo n.º 8
0
    def delete_port_precommit(self, context):
        port_data = context.current

        if port_data['device_owner'] == n_const.DEVICE_OWNER_FLOATINGIP:
             return  # no need to process further for fip


        nsx_switch_id, nsx_port_id = nsx_utils.get_nsx_switch_and_port_id(
            context._plugin_context.session,
            self.cluster,
            port_data['id']
        )

        try:
            switchlib.delete_port(self.cluster, nsx_switch_id, nsx_port_id)
            LOG.debug(
                "_nsx_delete_port completed for port %(port_id)s on network "
                "%(net_id)s",
                {'port_id': port_data['id'], 'net_id': port_data['network_id']}
            )
        except n_exc.NotFound:
            LOG.warning(_("Port %s not found in NSX"), port_data['id'])
Exemplo n.º 9
0
    def _nsx_delete_port(self, context, port_data):
        # FIXME(salvatore-orlando): On the NSX platform we do not really have
        # external networks. So deleting regular ports from external networks
        # does not make sense. However we cannot raise as this would break
        # unit tests.

        # NOTE(rods): reporting mark's comment on havana version of this patch.
        # Akanda does want ports for external networks so this method is
        # basically same with external check removed

        # ---------------------------------------------------------------------
        # Original code:
        # if self._network_is_external(context, port_data['network_id']):
        #     LOG.info(_("NSX plugin does not support regular VIF ports on "
        #                "external networks. Port %s will be down."),
        #              port_data['network_id'])
        #     return
        # ---------------------------------------------------------------------

        nsx_switch_id, nsx_port_id = nsx_utils.get_nsx_switch_and_port_id(
            context.session, self.cluster, port_data['id'])
        if not nsx_port_id:
            LOG.debug(_("Port '%s' was already deleted on NSX platform"), id)
            return
        # TODO(bgh): if this is a bridged network and the lswitch we just got
        # back will have zero ports after the delete we should garbage collect
        # the lswitch.
        try:
            switchlib.delete_port(self.cluster, nsx_switch_id, nsx_port_id)
            LOG.debug(
                _("_nsx_delete_port completed for port %(port_id)s "
                  "on network %(net_id)s"), {
                      'port_id': port_data['id'],
                      'net_id': port_data['network_id']
                  })
        except n_exc.NotFound:
            LOG.warning(_("Port %s not found in NSX"), port_data['id'])