Esempio 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)
Esempio n. 2
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(_LW("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})