Exemplo n.º 1
0
 def test_get_port_binding_by_status_and_host(self):
     bindings = []
     self.assertIsNone(
         utils.get_port_binding_by_status_and_host(bindings,
                                                   constants.INACTIVE))
     bindings.extend([
         ports.PortBinding(port_id=uuidutils.generate_uuid(),
                           host='host-1',
                           status=constants.INACTIVE),
         ports.PortBinding(port_id=uuidutils.generate_uuid(),
                           host='host-2',
                           status=constants.INACTIVE)
     ])
     self.assertEqual(
         'host-1',
         utils.get_port_binding_by_status_and_host(
             bindings, constants.INACTIVE)[pb_ext.HOST])
     self.assertEqual(
         'host-2',
         utils.get_port_binding_by_status_and_host(
             bindings, constants.INACTIVE, host='host-2')[pb_ext.HOST])
     self.assertIsNone(
         utils.get_port_binding_by_status_and_host(bindings,
                                                   constants.ACTIVE))
     self.assertRaises(exceptions.PortBindingNotFound,
                       utils.get_port_binding_by_status_and_host, bindings,
                       constants.ACTIVE, 'host', True, 'port_id')
Exemplo n.º 2
0
    def test_check_network_ports_by_binding_types(self):
        port1 = self._create_test_port()
        network_id = port1.network_id
        ports.PortBinding(self.context,
                          host='host1',
                          port_id=port1.id,
                          status='ACTIVE',
                          vnic_type='vnic_type1',
                          vif_type='vif_type1').create()

        port2 = self._create_test_port(network_id=network_id)
        ports.PortBinding(self.context,
                          host='host2',
                          port_id=port2.id,
                          status='ACTIVE',
                          vnic_type='vnic_type2',
                          vif_type='vif_type2').create()

        self.assertTrue(
            ports.Port.check_network_ports_by_binding_types(
                self.context,
                network_id,
                binding_types=['vif_type1', 'vif_type2']))

        self.assertFalse(
            ports.Port.check_network_ports_by_binding_types(
                self.context,
                network_id,
                binding_types=['vif_type1', 'vif_type2'],
                negative_search=True))
Exemplo n.º 3
0
    def setUp(self):
        super(L3AgentNDPProxyTestFramework, self).setUp()
        self.conf.set_override('extensions', ['ndp_proxy'], 'agent')
        self.agent = neutron_l3_agent.L3NATAgentWithStateReport(HOSTNAME,
                                                                self.conf)
        self.np_ext = np.NDPProxyAgentExtension()

        port_id1 = uuidutils.generate_uuid()
        port1_binding = ports_obj.PortBinding(port_id=port_id1,
                                              host=self.agent.host)
        port1_obj = ports_obj.Port(id=port_id1, bindings=[port1_binding])
        port_id2 = uuidutils.generate_uuid()
        port2_binding = ports_obj.PortBinding(port_id=port_id1,
                                              host='fake_host')
        port2_obj = ports_obj.Port(id=port_id2, bindings=[port2_binding])
        self.ports = [port1_obj, port2_obj]
        self.port_binding_map = {port_id1: port1_binding,
                                 port_id2: port2_binding}
        self.ndpproxy1 = np_obj.NDPProxy(
            context=None, id=uuidutils.generate_uuid(),
            router_id=uuidutils.generate_uuid(),
            port_id=port_id1, ip_address='2002::1:3')
        self.ndpproxy2 = np_obj.NDPProxy(
            context=None, id=uuidutils.generate_uuid(),
            router_id=uuidutils.generate_uuid(),
            port_id=port_id2, ip_address='2002::1:4')
        self.ndp_proxies = [self.ndpproxy1, self.ndpproxy2]
        agent_configurations = {
            'agent_mode': constants.L3_AGENT_MODE_DVR_NO_EXTERNAL}
        self.agent_obj = agent_obj.Agent(
            id=uuidutils.generate_uuid(), host=self.agent.host,
            agent_type=constants.AGENT_TYPE_L3,
            configurations=agent_configurations)
        self._set_pull_mock()
Exemplo n.º 4
0
    def test_get_ports_by_vnic_type_and_host(self):
        port1 = self._create_test_port()
        ports.PortBinding(
            self.context,
            host='host1', port_id=port1.id, status='ACTIVE',
            vnic_type='vnic_type1', vif_type='vif_type1').create()

        port2 = self._create_test_port()
        ports.PortBinding(
            self.context,
            host='host1', port_id=port2.id, status='ACTIVE',
            vnic_type='vnic_type2', vif_type='vif_type1').create()

        self.assertEqual(1, len(
            ports.Port.get_ports_by_vnic_type_and_host(
                self.context, 'vnic_type1', 'host1')))
Exemplo n.º 5
0
 def setUp(self):
     super(TestCacheBackedPluginApi, self).setUp()
     self._api = rpc.CacheBackedPluginApi(lib_topics.PLUGIN)
     self._api._legacy_interface = mock.Mock()
     self._api.remote_resource_cache = mock.Mock()
     self._network_id = uuidutils.generate_uuid()
     self._segment_id = uuidutils.generate_uuid()
     self._segment = network.NetworkSegment(
         id=self._segment_id,
         network_id=self._network_id,
         network_type=constants.TYPE_FLAT)
     self._port_id = uuidutils.generate_uuid()
     self._network = network.Network(id=self._network_id,
                                     segments=[self._segment])
     self._port = ports.Port(
         id=self._port_id,
         network_id=self._network_id,
         mac_address=netaddr.EUI('fa:16:3e:ec:c7:d9'),
         admin_state_up=True,
         security_group_ids=set([uuidutils.generate_uuid()]),
         fixed_ips=[],
         allowed_address_pairs=[],
         device_owner=constants.DEVICE_OWNER_COMPUTE_PREFIX,
         bindings=[
             ports.PortBinding(port_id=self._port_id,
                               host='host1',
                               status=constants.ACTIVE,
                               profile={})
         ],
         binding_levels=[
             ports.PortBindingLevel(port_id=self._port_id,
                                    host='host1',
                                    level=0,
                                    segment=self._segment)
         ])
Exemplo n.º 6
0
 def _test__legacy_notifier_binding_activated(self):
     updated_port = ports.Port(
         id=self._port_id, name='updated_port',
         bindings=[ports.PortBinding(port_id=self._port_id,
                                     host='host2',
                                     status=constants.ACTIVE),
                   ports.PortBinding(port_id=self._port_id,
                                     host='host1',
                                     status=constants.INACTIVE)])
     self._api._legacy_notifier(resources.PORT, events.AFTER_UPDATE, self,
                                mock.ANY,
                                changed_fields=set(['name', 'bindings']),
                                resource_id=self._port_id,
                                existing=self._port, updated=updated_port)
     self._api._legacy_interface.port_update.assert_not_called()
     self._api._legacy_interface.port_delete.assert_not_called()
Exemplo n.º 7
0
    def test_update_network_external_ports(self):
        """Test update network with external ports.

        - port10: no QoS port policy
        - port11: no QoS port policy but external
        - port12: qos_policy0
        """
        policies_ports = [(self.qos_policies[0].id, {self.ports[0].id})]
        self.ports[2].qos_policy_id = self.qos_policies[0].id
        self.ports[2].update()
        port_obj.PortBinding(self.ctx,
                             port_id=self.ports[1].id,
                             host='host',
                             profile={},
                             vif_type='',
                             vnic_type=portbindings_api.VNIC_DIRECT).create()
        with mock.patch.object(self.qos_driver._driver._nb_idl,
                               'get_lswitch_port') as mock_lsp:
            mock_lsp.side_effect = [
                mock.Mock(type=ovn_const.LSP_TYPE_LOCALNET),
                mock.Mock(type=ovn_const.LSP_TYPE_EXTERNAL)
            ]
            for qos_policy_id, reference_ports in policies_ports:
                self.networks[0].qos_policy_id = qos_policy_id
                self.networks[0].update()
                original_network = {'qos_policy_id': self.qos_policies[0]}
                reviewed_port_ids, _, _ = self.qos_driver.update_network(
                    mock.ANY, self.networks[0], original_network, reset=True)
                self.assertEqual(reference_ports, reviewed_port_ids)
                calls = [
                    mock.call(mock.ANY, self.ports[0].id,
                              self.ports[0].network_id, qos_policy_id, None)
                ]
                self.mock_rules.assert_has_calls(calls)
                self.mock_rules.reset_mock()
Exemplo n.º 8
0
 def _get_port(self, vif_type, vnic_type):
     port_id = uuidutils.generate_uuid()
     port_binding = ports.PortBinding(self.ctxt,
                                      port_id=port_id,
                                      vif_type=vif_type,
                                      vnic_type=vnic_type)
     return ports.Port(self.ctxt,
                       id=uuidutils.generate_uuid(),
                       binding=port_binding)
Exemplo n.º 9
0
    def test_v1_4_to_v1_3_converts_binding_to_portbinding_object(self):
        port_v1_4 = self._create_test_port()
        port_v1_3 = port_v1_4.obj_to_primitive(target_version='1.3')

        # Port has no bindings, so binding attribute should be None
        self.assertIsNone(port_v1_3['versioned_object.data']['binding'])
        active_binding = ports.PortBinding(self.context,
                                           port_id=port_v1_4.id,
                                           host='host1',
                                           vif_type='type')
        inactive_binding = ports.PortBinding(self.context,
                                             port_id=port_v1_4.id,
                                             host='host2',
                                             vif_type='type',
                                             status=constants.INACTIVE)
        active_binding.create()
        inactive_binding.create()
        port_v1_4 = ports.Port.get_object(self.context, id=port_v1_4.id)
        port_v1_3 = port_v1_4.obj_to_primitive(target_version='1.3')
        binding = port_v1_3['versioned_object.data']['binding']

        # Port has active binding, so the binding attribute should point to it
        self.assertEqual('host1', binding['versioned_object.data']['host'])
        active_binding.delete()
        port_v1_4 = ports.Port.get_object(self.context, id=port_v1_4.id)
        port_v1_3 = port_v1_4.obj_to_primitive(target_version='1.3')

        # Port has no active bindings, so binding attribute should be None
        self.assertIsNone(port_v1_3['versioned_object.data']['binding'])

        # bindings attribute in V1.4 port should have one inactive binding
        primitive = port_v1_4.obj_to_primitive()
        self.assertEqual(1,
                         len(primitive['versioned_object.data']['bindings']))
        binding = primitive['versioned_object.data']['bindings'][0]
        self.assertEqual(constants.INACTIVE,
                         binding['versioned_object.data']['status'])

        # Port with no binding attribute should be handled without raising
        # exception
        primitive['versioned_object.data'].pop('bindings')
        port_v1_4_no_binding = port_v1_4.obj_from_primitive(primitive)
        port_v1_4_no_binding.obj_to_primitive(target_version='1.3')
Exemplo n.º 10
0
    def _get_ports_with_policy(self, context, policy):
        networks_ids = policy.get_bound_networks()

        ports_with_net_policy = ports_object.Port.get_objects(
            context, network_id=networks_ids)

        # Filter only these ports which don't have overwritten policy
        ports_with_net_policy = [
            port for port in ports_with_net_policy
            if port.qos_policy_id is None
        ]

        ports_ids = policy.get_bound_ports()
        ports_with_policy = ports_object.Port.get_objects(context,
                                                          id=ports_ids)
        t_ports = list(set(ports_with_policy + ports_with_net_policy))

        t_ctx = t_context.get_context_from_neutron_context(context)
        for t_port in t_ports:
            mappings = db_api.get_bottom_mappings_by_top_id(
                t_ctx, t_port.id, t_constants.RT_PORT)
            if mappings:
                b_pod, b_port_id = mappings[0]
                b_region_name = b_pod['region_name']
                b_client = self._get_client(region_name=b_region_name)
                b_port = b_client.get_ports(t_ctx, b_port_id)
                new_binding = ports_object.PortBinding(
                    port_id=t_port.id,
                    vif_type=b_port.get('binding:vif_type',
                                        portbindings.VIF_TYPE_UNBOUND),
                    vnic_type=b_port.get('binding:vnic_type',
                                         portbindings.VNIC_NORMAL))
                t_port.binding = new_binding
            else:
                new_binding = ports_object.PortBinding(
                    port_id=t_port.id,
                    vif_type=portbindings.VIF_TYPE_UNBOUND,
                    vnic_type=portbindings.VNIC_NORMAL)
                t_port.binding = new_binding

        return t_ports
Exemplo n.º 11
0
    def _test__legacy_notifier_binding_activated(self):
        updated_port = ports.Port(
            id=self._port_id, name='updated_port',
            bindings=[ports.PortBinding(port_id=self._port_id,
                                        host='host2',
                                        status=constants.ACTIVE),
                      ports.PortBinding(port_id=self._port_id,
                                        host='host1',
                                        status=constants.INACTIVE)])
        self._api._legacy_notifier(
            resources.PORT, events.AFTER_UPDATE, self,
            payload=events.DBEventPayload(
                mock.ANY,
                metadata={
                    'changed_fields': set(['name', 'bindings'])
                },
                resource_id=self._port_id,
                states=(self._port, updated_port)))

        self._api._legacy_interface.port_update.assert_not_called()
        self._api._legacy_interface.port_delete.assert_not_called()
Exemplo n.º 12
0
    def test_get_port_from_mac_and_pci_slot(self):
        obj = self._make_object(self.obj_fields[0])
        obj.create()
        mac_address = obj.mac_address
        pci_slot = '0000:04:00.1'
        port = ports.Port.get_port_from_mac_and_pci_slot(
            self.context, mac_address, pci_slot=pci_slot)
        self.assertIsNone(port)

        port_binding = ports.PortBinding(
            self.context, port_id=obj.id, host='any_host',
            vif_type=portbindings.VIF_TYPE_OTHER,
            vnic_type=portbindings.VNIC_DIRECT, profile={'pci_slot': pci_slot})
        port_binding.create()
        port = ports.Port.get_port_from_mac_and_pci_slot(
            self.context, mac_address, pci_slot=pci_slot)
        self.assertEqual(obj.id, port.id)
Exemplo n.º 13
0
 def setUp(self):
     super(NDPProxyExtensionTestCaseBase, self).setUp()
     self.context = context.get_admin_context()
     self.connection = mock.Mock()
     self.ext_port_id = _uuid()
     self.ex_net_id = _uuid()
     self.ex_gw_port = {
         'id': self.ext_port_id,
         'network_id': self.ex_net_id,
         'gw_port_host': HOSTNAME
     }
     self.fake_router_id = _uuid()
     self.port_id = _uuid()
     self.agent_api = l3_ext_api.L3AgentExtensionAPI(None, None)
     self.np_ext = np.NDPProxyAgentExtension()
     self.np_ext.consume_api(self.agent_api)
     self.np_ext.initialize(self.connection, lib_const.L3_AGENT_MODE)
     self.ndpproxy = np_obj.NDPProxy(context=None,
                                     id=_uuid(),
                                     router_id=self.fake_router_id,
                                     port_id=self.port_id,
                                     ip_address='2002::1:3')
     port_binding = ports_obj.PortBinding(port_id=self.port_id,
                                          host=HOSTNAME)
     port_obj = ports_obj.Port(id=self.port_id, bindings=[port_binding])
     self.ndp_proxies = [self.ndpproxy]
     self.ports = [port_obj]
     agent_configurations = {
         'agent_mode': lib_const.L3_AGENT_MODE_DVR_NO_EXTERNAL
     }
     self.agent_obj = agent_obj.Agent(id=_uuid(),
                                      host=HOSTNAME,
                                      agent_type=lib_const.AGENT_TYPE_L3,
                                      configurations=agent_configurations)
     self.ip_wrapper = mock.patch('neutron.agent.linux.'
                                  'ip_lib.IPWrapper').start()
     self._set_pull_mock()
Exemplo n.º 14
0
 def _setup_neutron_portbinding(self, port_id, vif_type, host):
     port_obj.PortBinding(
         self.ctx, port_id=port_id, vif_type=vif_type, host=host).create()
Exemplo n.º 15
0
    def test_update_port(self):
        port = self.ports[0]
        original_port = self.ports[1]

        # Remove QoS policy
        original_port.qos_policy_id = self.qos_policies[0].id
        self.qos_driver.update_port(mock.ANY, port, original_port)
        self.mock_rules.assert_called_once_with(mock.ANY, port.id,
                                                port.network_id, None, None)

        # Change from port policy (qos_policy0) to network policy (qos_policy1)
        self.mock_rules.reset_mock()
        port.qos_network_policy_id = self.qos_policies[1].id
        self.qos_driver.update_port(mock.ANY, port, original_port)
        self.mock_rules.assert_called_once_with(mock.ANY, port.id,
                                                port.network_id,
                                                self.qos_policies[1].id, None)

        # No change (qos_policy0)
        self.mock_rules.reset_mock()
        port.qos_policy_id = self.qos_policies[0].id
        original_port.qos_policy_id = self.qos_policies[0].id
        self.qos_driver.update_port(mock.ANY, port, original_port)
        self.mock_rules.assert_not_called()

        # No change (no policy)
        self.mock_rules.reset_mock()
        port.qos_policy_id = None
        port.qos_network_policy_id = None
        original_port.qos_policy_id = None
        original_port.qos_network_policy_id = None
        self.qos_driver.update_port(mock.ANY, port, original_port)
        self.mock_rules.assert_not_called()

        # Reset (no policy)
        self.qos_driver.update_port(mock.ANY, port, original_port, reset=True)
        self.mock_rules.assert_called_once_with(mock.ANY, port.id,
                                                port.network_id, None, None)

        # Reset (qos_policy0, regardless of being the same a in the previous
        # state)
        self.mock_rules.reset_mock()
        port.qos_policy_id = self.qos_policies[0].id
        original_port.qos_policy_id = self.qos_policies[1].id
        self.qos_driver.update_port(mock.ANY, port, original_port, reset=True)
        self.mock_rules.assert_called_once_with(mock.ANY, port.id,
                                                port.network_id,
                                                self.qos_policies[0].id, None)

        # External port, OVN QoS extension does not apply.
        self.mock_rules.reset_mock()
        port.qos_policy_id = self.qos_policies[0].id
        port_obj.PortBinding(self.ctx,
                             port_id=port.id,
                             host='host',
                             profile={},
                             vif_type='',
                             vnic_type=portbindings_api.VNIC_DIRECT).create()
        # NOTE(ralonsoh): this OVO retrieval must include, in the port object,
        # the port binding register created.
        port = port_obj.Port.get_object(self.ctx, id=port.id)
        self.qos_driver.update_port(mock.ANY, port, original_port)
        self.mock_rules.assert_not_called()
Exemplo n.º 16
0
def add_port_binding(context, port_id):
    binding = port_obj.PortBinding(context,
                                   port_id=port_id,
                                   vif_type=portbindings.VIF_TYPE_UNBOUND)
    binding.create()
    return binding