Exemplo n.º 1
0
def get_nsx_switch_and_port_id(session, cluster, neutron_port_id):
    """Return the NSX switch and port uuids for a given neutron port.

    First, look up the Neutron database. If not found, execute
    a query on NSX platform as the mapping might be missing because
    the port was created before upgrading to grizzly.

    This routine also retrieves the identifier of the logical switch in
    the backend where the port is plugged. Prior to Icehouse this
    information was not available in the Neutron Database. For dealing
    with pre-existing records, this routine will query the backend
    for retrieving the correct switch identifier.

    As of Icehouse release it is not indeed anymore possible to assume
    the backend logical switch identifier is equal to the neutron
    network identifier.
    """
    nvp_switch_id, nvp_port_id = nicira_db.get_nsx_switch_and_port_id(
        session, neutron_port_id)
    if not nvp_switch_id:
        # Find logical switch for port from backend
        # This is a rather expensive query, but it won't be executed
        # more than once for each port in Neutron's lifetime
        nvp_ports = nvplib.query_lswitch_lports(
            cluster,
            '*',
            relations='LogicalSwitchConfig',
            filters={
                'tag': neutron_port_id,
                'tag_scope': 'q_port_id'
            })
        # Only one result expected
        # NOTE(salv-orlando): Not handling the case where more than one
        # port is found with the same neutron port tag
        if not nvp_ports:
            LOG.warn(_("Unable to find NVP port for Neutron port %s"),
                     neutron_port_id)
            # This method is supposed to return a tuple
            return None, None
        nvp_port = nvp_ports[0]
        nvp_switch_id = (nvp_port['_relations']['LogicalSwitchConfig']['uuid'])
        with session.begin(subtransactions=True):
            if nvp_port_id:
                # Mapping already exists. Delete before recreating
                nicira_db.delete_neutron_nsx_port_mapping(
                    session, neutron_port_id)
            else:
                nvp_port_id = nvp_port['uuid']
            # (re)Create DB mapping
            nicira_db.add_neutron_nsx_port_mapping(session, neutron_port_id,
                                                   nvp_switch_id, nvp_port_id)
    return nvp_switch_id, nvp_port_id
Exemplo n.º 2
0
def get_nsx_switch_and_port_id(session, cluster, neutron_port_id):
    """Return the NSX switch and port uuids for a given neutron port.

    First, look up the Neutron database. If not found, execute
    a query on NSX platform as the mapping might be missing because
    the port was created before upgrading to grizzly.

    This routine also retrieves the identifier of the logical switch in
    the backend where the port is plugged. Prior to Icehouse this
    information was not available in the Neutron Database. For dealing
    with pre-existing records, this routine will query the backend
    for retrieving the correct switch identifier.

    As of Icehouse release it is not indeed anymore possible to assume
    the backend logical switch identifier is equal to the neutron
    network identifier.
    """
    nvp_switch_id, nvp_port_id = nicira_db.get_nsx_switch_and_port_id(
        session, neutron_port_id)
    if not nvp_switch_id:
        # Find logical switch for port from backend
        # This is a rather expensive query, but it won't be executed
        # more than once for each port in Neutron's lifetime
        nvp_ports = nvplib.query_lswitch_lports(
            cluster, '*', relations='LogicalSwitchConfig',
            filters={'tag': neutron_port_id,
                     'tag_scope': 'q_port_id'})
        # Only one result expected
        # NOTE(salv-orlando): Not handling the case where more than one
        # port is found with the same neutron port tag
        if not nvp_ports:
            LOG.warn(_("Unable to find NVP port for Neutron port %s"),
                     neutron_port_id)
            # This method is supposed to return a tuple
            return None, None
        nvp_port = nvp_ports[0]
        nvp_switch_id = (nvp_port['_relations']
                         ['LogicalSwitchConfig']['uuid'])
        with session.begin(subtransactions=True):
            if nvp_port_id:
                # Mapping already exists. Delete before recreating
                nicira_db.delete_neutron_nsx_port_mapping(
                    session, neutron_port_id)
            else:
                nvp_port_id = nvp_port['uuid']
            # (re)Create DB mapping
            nicira_db.add_neutron_nsx_port_mapping(
                session, neutron_port_id,
                nvp_switch_id, nvp_port_id)
    return nvp_switch_id, nvp_port_id
Exemplo n.º 3
0
    def test_add_neutron_nsx_port_mapping_raise_on_duplicate_constraint(self):
        neutron_net_id = 'foo_neutron_network_id'
        neutron_port_id = 'foo_neutron_port_id'
        nsx_port_id_1 = 'foo_nsx_port_id_1'
        nsx_port_id_2 = 'foo_nsx_port_id_2'
        nsx_switch_id = 'foo_nsx_switch_id'
        self._setup_neutron_network_and_port(neutron_net_id, neutron_port_id)

        nsx_db.add_neutron_nsx_port_mapping(self.ctx.session, neutron_port_id,
                                            nsx_switch_id, nsx_port_id_1)
        # Call the method twice to trigger a db duplicate constraint error,
        # this time with a different nsx port id!
        self.assertRaises(d_exc.DBDuplicateEntry,
                          nsx_db.add_neutron_nsx_port_mapping,
                          self.ctx.session, neutron_port_id, nsx_switch_id,
                          nsx_port_id_2)
Exemplo n.º 4
0
    def test_add_neutron_nsx_port_mapping_raise_on_duplicate_constraint(self):
        neutron_net_id = 'foo_neutron_network_id'
        neutron_port_id = 'foo_neutron_port_id'
        nsx_port_id_1 = 'foo_nsx_port_id_1'
        nsx_port_id_2 = 'foo_nsx_port_id_2'
        nsx_switch_id = 'foo_nsx_switch_id'
        self._setup_neutron_network_and_port(neutron_net_id, neutron_port_id)

        nicira_db.add_neutron_nsx_port_mapping(
            self.ctx.session, neutron_port_id, nsx_switch_id, nsx_port_id_1)
        # Call the method twice to trigger a db duplicate constraint error,
        # this time with a different nsx port id!
        self.assertRaises(d_exc.DBDuplicateEntry,
                          nicira_db.add_neutron_nsx_port_mapping,
                          self.ctx.session, neutron_port_id,
                          nsx_switch_id, nsx_port_id_2)
Exemplo n.º 5
0
    def test_add_neutron_nsx_port_mapping_handle_duplicate_constraint(self):
        neutron_net_id = 'foo_neutron_network_id'
        neutron_port_id = 'foo_neutron_port_id'
        nsx_port_id = 'foo_nsx_port_id'
        nsx_switch_id = 'foo_nsx_switch_id'
        self._setup_neutron_network_and_port(neutron_net_id, neutron_port_id)

        nicira_db.add_neutron_nsx_port_mapping(
            self.ctx.session, neutron_port_id, nsx_switch_id, nsx_port_id)
        # Call the method twice to trigger a db duplicate constraint error
        nicira_db.add_neutron_nsx_port_mapping(
            self.ctx.session, neutron_port_id, nsx_switch_id, nsx_port_id)
        result = (self.ctx.session.query(nicira_models.NeutronNsxPortMapping).
                  filter_by(neutron_id=neutron_port_id).one())
        self.assertEqual(nsx_port_id, result.nsx_port_id)
        self.assertEqual(neutron_port_id, result.neutron_id)
Exemplo n.º 6
0
    def test_add_neutron_nsx_port_mapping_handle_duplicate_constraint(self):
        neutron_net_id = 'foo_neutron_network_id'
        neutron_port_id = 'foo_neutron_port_id'
        nsx_port_id = 'foo_nsx_port_id'
        nsx_switch_id = 'foo_nsx_switch_id'
        self._setup_neutron_network_and_port(neutron_net_id, neutron_port_id)

        nicira_db.add_neutron_nsx_port_mapping(
            self.ctx.session, neutron_port_id, nsx_switch_id, nsx_port_id)
        # Call the method twice to trigger a db duplicate constraint error
        nicira_db.add_neutron_nsx_port_mapping(
            self.ctx.session, neutron_port_id, nsx_switch_id, nsx_port_id)
        result = (self.ctx.session.query(nicira_models.NeutronNsxPortMapping).
                  filter_by(neutron_id=neutron_port_id).one())
        self.assertEqual(nsx_port_id, result.nsx_port_id)
        self.assertEqual(neutron_port_id, result.neutron_id)