Browse Source

Cleans up logic

Simplifies the logic a bit and reduces some code duplication.

Change-Id: I7e83bfe061bceb5ecb6cb10a29015926f4e555bc
Claudiu Belu 1 year ago
parent
commit
d35fd046ce

+ 0
- 15
networking_hyperv/neutron/agent/hnv_neutron_agent.py View File

@@ -23,7 +23,6 @@ from neutron.conf.agent import common as neutron_config
23 23
 from oslo_log import log as logging
24 24
 
25 25
 from networking_hyperv.common.i18n import _LI    # noqa
26
-from networking_hyperv.neutron import _common_utils as c_util
27 26
 from networking_hyperv.neutron.agent import layer2 as hyperv_base
28 27
 from networking_hyperv.neutron import config
29 28
 from networking_hyperv.neutron import constants as h_const
@@ -32,8 +31,6 @@ from networking_hyperv.neutron import neutron_client
32 31
 LOG = logging.getLogger(__name__)
33 32
 CONF = config.CONF
34 33
 
35
-_port_synchronized = c_util.get_port_synchronized_decorator('n-hv-agent-')
36
-
37 34
 
38 35
 class HNVAgent(hyperv_base.Layer2Agent):
39 36
 
@@ -92,18 +89,6 @@ class HNVAgent(hyperv_base.Layer2Agent):
92 89
             vendor_id=h_const.VENDOR_ID,
93 90
             vendor_name=h_const.VENDOR_NAME)
94 91
 
95
-    @_port_synchronized
96
-    def _treat_vif_port(self, port_id, network_id, network_type,
97
-                        physical_network, segmentation_id,
98
-                        admin_state_up, port_security_enabled,
99
-                        set_port_sriov=False):
100
-        if admin_state_up:
101
-            self._port_bound(port_id, network_id, network_type,
102
-                             physical_network, segmentation_id,
103
-                             port_security_enabled, set_port_sriov)
104
-        else:
105
-            self._port_unbound(port_id)
106
-
107 92
 
108 93
 def main():
109 94
     """The entry point for the HNV Agent."""

+ 10
- 18
networking_hyperv/neutron/agent/hyperv_neutron_agent.py View File

@@ -209,6 +209,13 @@ class HyperVNeutronAgent(hyperv_base.Layer2Agent):
209 209
             self._utils.add_metrics_collection_acls(port_id)
210 210
             self._port_metric_retries[port_id] = self._metrics_max_retries
211 211
 
212
+        # check if security groups is enabled.
213
+        # if not, teardown the security group rules
214
+        if self._enable_security_groups:
215
+            self._sec_groups_agent.refresh_firewall([port_id])
216
+        else:
217
+            self._utils.remove_all_security_rules(port_id)
218
+
212 219
         self._utils.set_vswitch_port_mac_spoofing(port_id,
213 220
                                                   port_security_enabled)
214 221
 
@@ -236,24 +243,9 @@ class HyperVNeutronAgent(hyperv_base.Layer2Agent):
236 243
                             "metrics.", port_id)
237 244
                 del self._port_metric_retries[port_id]
238 245
 
239
-    @_port_synchronized
240
-    def _treat_vif_port(self, port_id, network_id, network_type,
241
-                        physical_network, segmentation_id,
242
-                        admin_state_up, port_security_enabled,
243
-                        set_port_sriov=False):
244
-        if admin_state_up:
245
-            self._port_bound(port_id, network_id, network_type,
246
-                             physical_network, segmentation_id,
247
-                             port_security_enabled, set_port_sriov)
248
-            # check if security groups is enabled.
249
-            # if not, teardown the security group rules
250
-            if self._enable_security_groups:
251
-                self._sec_groups_agent.refresh_firewall([port_id])
252
-            else:
253
-                self._utils.remove_all_security_rules(port_id)
254
-        else:
255
-            self._port_unbound(port_id)
256
-            self._sec_groups_agent.remove_devices_filter([port_id])
246
+    def _port_unbound(self, port_id, vnic_deleted=False):
247
+        super(HyperVNeutronAgent, self)._port_unbound(port_id, vnic_deleted)
248
+        self._sec_groups_agent.remove_devices_filter([port_id])
257 249
 
258 250
     def _process_added_port(self, device_details):
259 251
         super(HyperVNeutronAgent, self)._process_added_port(

+ 9
- 2
networking_hyperv/neutron/agent/layer2.py View File

@@ -33,6 +33,7 @@ from oslo_service import loopingcall
33 33
 import six
34 34
 
35 35
 from networking_hyperv.common.i18n import _, _LI, _LE    # noqa
36
+from networking_hyperv.neutron import _common_utils as c_util
36 37
 from networking_hyperv.neutron.agent import base as base_agent
37 38
 from networking_hyperv.neutron import config
38 39
 from networking_hyperv.neutron import constants
@@ -42,6 +43,7 @@ LOG = logging.getLogger(__name__)
42 43
 CONF = config.CONF
43 44
 
44 45
 _synchronized = lockutils.synchronized_with_prefix('n-hv-agent-')
46
+_port_synchronized = c_util.get_port_synchronized_decorator('n-hv-agent-')
45 47
 
46 48
 
47 49
 class Layer2Agent(base_agent.BaseAgent):
@@ -465,9 +467,14 @@ class Layer2Agent(base_agent.BaseAgent):
465 467
         """Provision the network with the received information."""
466 468
         pass
467 469
 
468
-    @abc.abstractmethod
470
+    @_port_synchronized
469 471
     def _treat_vif_port(self, port_id, network_id, network_type,
470 472
                         physical_network, segmentation_id,
471 473
                         admin_state_up, port_security_enabled,
472 474
                         set_port_sriov=False):
473
-        pass
475
+        if admin_state_up:
476
+            self._port_bound(port_id, network_id, network_type,
477
+                             physical_network, segmentation_id,
478
+                             port_security_enabled, set_port_sriov)
479
+        else:
480
+            self._port_unbound(port_id)

+ 0
- 24
networking_hyperv/tests/unit/neutron/agent/test_hnv_neutron_agent.py View File

@@ -103,30 +103,6 @@ class TestHNVAgent(test_base.HyperVBaseTestCase):
103 103
             vendor_id=constants.VENDOR_ID,
104 104
             vendor_name=constants.VENDOR_NAME)
105 105
 
106
-    @mock.patch.object(hnv_agent.HNVAgent, '_port_bound')
107
-    def test_treat_vif_port_state_up(self, mock_port_bound):
108
-        self.agent._treat_vif_port(
109
-            mock.sentinel.port_id, mock.sentinel.network_id,
110
-            mock.sentinel.network_type, mock.sentinel.physical_network,
111
-            mock.sentinel.segmentation_id, True,
112
-            mock.sentinel.port_security_enabled)
113
-
114
-        mock_port_bound.assert_called_once_with(
115
-            mock.sentinel.port_id, mock.sentinel.network_id,
116
-            mock.sentinel.network_type, mock.sentinel.physical_network,
117
-            mock.sentinel.segmentation_id, mock.sentinel.port_security_enabled,
118
-            False)
119
-
120
-    @mock.patch.object(hnv_agent.HNVAgent, '_port_unbound')
121
-    def test_treat_vif_port_state_down(self, mock_port_unbound):
122
-        self.agent._treat_vif_port(
123
-            mock.sentinel.port_id, mock.sentinel.network_id,
124
-            mock.sentinel.network_type, mock.sentinel.physical_network,
125
-            mock.sentinel.segmentation_id, False,
126
-            mock.sentinel.port_security_enabled)
127
-
128
-        mock_port_unbound.assert_called_once_with(mock.sentinel.port_id)
129
-
130 106
 
131 107
 class TestMain(test_base.BaseTestCase):
132 108
 

+ 39
- 107
networking_hyperv/tests/unit/neutron/agent/test_hyperv_neutron_agent.py View File

@@ -20,6 +20,7 @@ Unit tests for Windows Hyper-V virtual switch neutron driver
20 20
 
21 21
 import sys
22 22
 
23
+import ddt
23 24
 import mock
24 25
 from neutron.common import topics
25 26
 from os_win import exceptions
@@ -232,77 +233,55 @@ class TestHyperVNeutronAgent(base.HyperVBaseTestCase):
232 233
             constants.TYPE_LOCAL,
233 234
             mock.sentinel.FAKE_PHYSICAL_NETWORK)
234 235
 
236
+    @ddt.data({'enable_security_groups': True},
237
+              {'network_type': constants.TYPE_LOCAL},
238
+              {'network_type': constants.TYPE_VLAN},
239
+              {'network_type': constants.TYPE_NVGRE})
240
+    @ddt.unpack
235 241
     @mock.patch.object(layer2.Layer2Agent, '_port_bound')
236
-    def _test_port_bound(self, enable_metrics, mock_super_bound):
242
+    def test_port_bound(self, mock_super_bound,
243
+                        network_type=constants.TYPE_FLAT,
244
+                        enable_security_groups=False):
237 245
         net_uuid = 'my-net-uuid'
238
-        port = mock.MagicMock()
239
-
240
-        self.agent._enable_metrics_collection = enable_metrics
246
+        self.agent._enable_metrics_collection = True
247
+        self.agent._enable_security_groups = enable_security_groups
241 248
         self.agent._network_vswitch_map[net_uuid] = mock.sentinel.vswitch_name
242 249
 
243
-        self.agent._port_bound(port, net_uuid,
244
-                               'vlan',
250
+        self.agent._port_bound(mock.sentinel.port_id, net_uuid,
251
+                               network_type,
245 252
                                mock.sentinel.physical_network,
246 253
                                mock.sentinel.segmentation_id,
247 254
                                mock.sentinel.port_security_enabled,
248 255
                                False)
249 256
 
250 257
         mock_super_bound.assert_called_once_with(
251
-            port, net_uuid, 'vlan',
258
+            mock.sentinel.port_id, net_uuid, network_type,
252 259
             mock.sentinel.physical_network,
253 260
             mock.sentinel.segmentation_id, mock.sentinel.port_security_enabled,
254 261
             False)
255
-        self.assertEqual(enable_metrics,
256
-                         self.agent._utils.add_metrics_collection_acls.called)
257 262
 
258
-    def test_port_bound_enable_metrics(self):
259
-        self._test_port_bound(True)
263
+        if network_type == constants.TYPE_VLAN:
264
+            self.agent._vlan_driver.bind_vlan_port.assert_called_once_with(
265
+                mock.sentinel.port_id, mock.sentinel.segmentation_id)
266
+        elif network_type == constants.TYPE_NVGRE:
267
+            self.agent._nvgre_ops.bind_nvgre_port.assert_called_once_with(
268
+                mock.sentinel.segmentation_id, mock.sentinel.vswitch_name,
269
+                mock.sentinel.port_id)
260 270
 
261
-    def test_port_bound_no_metrics(self):
262
-        self._test_port_bound(False)
263
-
264
-    @mock.patch.object(hyperv_agent.HyperVNeutronAgent,
265
-                       '_provision_network')
266
-    def _check_port_bound_net_type(self, mock_provision_network, network_type):
267
-        net_uuid = 'my-net-uuid'
268
-        fake_map = {'vswitch_name': mock.sentinel.vswitch_name,
269
-                    'ports': []}
270
-
271
-        def fake_prov_network(*args, **kwargs):
272
-            self.agent._network_vswitch_map[net_uuid] = fake_map
271
+        self.agent._utils.add_metrics_collection_acls.assert_called_once_with(
272
+            mock.sentinel.port_id)
273 273
 
274
-        mock_provision_network.side_effect = fake_prov_network
274
+        if enable_security_groups:
275
+            refresh_firewall = self.agent._sec_groups_agent.refresh_firewall
276
+            refresh_firewall.assert_called_once_with([mock.sentinel.port_id])
277
+        else:
278
+            remove_sec_rules = self.agent._utils.remove_all_security_rules
279
+            remove_sec_rules.assert_called_once_with(mock.sentinel.port_id)
275 280
 
276
-        self.agent._port_bound(mock.sentinel.port_id, net_uuid, network_type,
277
-                               mock.sentinel.physical_network,
278
-                               mock.sentinel.segmentation_id,
279
-                               mock.sentinel.port_security_enabled,
280
-                               mock.sentinel.set_port_sriov)
281
-
282
-        self.assertIn(mock.sentinel.port_id, fake_map['ports'])
283
-        mock_provision_network.assert_called_once_with(
284
-            mock.sentinel.port_id, net_uuid, network_type,
285
-            mock.sentinel.physical_network, mock.sentinel.segmentation_id)
286
-        self.agent._utils.connect_vnic_to_vswitch.assert_called_once_with(
287
-            vswitch_name=mock.sentinel.vswitch_name,
288
-            switch_port_name=mock.sentinel.port_id)
289
-        self.agent._utils.set_vswitch_port_mac_spoofing(
281
+        set_mac_spoofing = self.agent._utils.set_vswitch_port_mac_spoofing
282
+        set_mac_spoofing.assert_called_once_with(
290 283
             mock.sentinel.port_id, mock.sentinel.port_security_enabled)
291 284
 
292
-    def test_port_bound_vlan(self):
293
-        self._check_port_bound_net_type(network_type=constants.TYPE_VLAN)
294
-
295
-        self.agent._vlan_driver.bind_vlan_port.assert_called_once_with(
296
-            mock.sentinel.port_id, mock.sentinel.segmentation_id)
297
-
298
-    def test_port_bound_nvgre(self):
299
-        self.agent._nvgre_enabled = True
300
-        self._check_port_bound_net_type(network_type=constants.TYPE_NVGRE)
301
-
302
-        self.agent._nvgre_ops.bind_nvgre_port.assert_called_once_with(
303
-            mock.sentinel.segmentation_id, mock.sentinel.vswitch_name,
304
-            mock.sentinel.port_id)
305
-
306 285
     def test_port_enable_control_metrics_ok(self):
307 286
         self.agent._enable_metrics_collection = True
308 287
         self.agent._port_metric_retries[self._FAKE_PORT_ID] = (
@@ -338,62 +317,15 @@ class TestHyperVNeutronAgent(base.HyperVBaseTestCase):
338 317
         self.agent._port_enable_control_metrics()
339 318
         self.assertNotIn(self._FAKE_PORT_ID, self.agent._port_metric_retries)
340 319
 
341
-    @mock.patch.object(hyperv_agent.HyperVNeutronAgent,
342
-                       '_port_unbound')
343
-    def test_vif_port_state_down(self, mock_port_unbound):
344
-        self.agent._treat_vif_port(
345
-            mock.sentinel.port_id, mock.sentinel.network_id,
346
-            mock.sentinel.network_type, mock.sentinel.physical_network,
347
-            mock.sentinel.segmentation_id, False,
348
-            mock.sentinel.port_security_enabled)
349
-
350
-        mock_port_unbound.assert_called_once_with(mock.sentinel.port_id)
351
-        sg_agent = self.agent._sec_groups_agent
352
-        sg_agent.remove_devices_filter.assert_called_once_with(
353
-            [mock.sentinel.port_id])
354
-
355
-    @mock.patch.object(hyperv_agent.HyperVNeutronAgent,
356
-                       '_port_bound')
357
-    def _check_treat_vif_port_state_up(self, mock_port_bound):
358
-        self.agent._treat_vif_port(
359
-            mock.sentinel.port_id, mock.sentinel.network_id,
360
-            mock.sentinel.network_type, mock.sentinel.physical_network,
361
-            mock.sentinel.segmentation_id, True,
362
-            mock.sentinel.port_security_enabled)
363
-
364
-        mock_port_bound.assert_called_once_with(
365
-            mock.sentinel.port_id, mock.sentinel.network_id,
366
-            mock.sentinel.network_type, mock.sentinel.physical_network,
367
-            mock.sentinel.segmentation_id, mock.sentinel.port_security_enabled,
368
-            False)
369
-
370
-    def test_treat_vif_port_sg_enabled(self):
371
-        self.agent._enable_security_groups = True
372
-
373
-        self._check_treat_vif_port_state_up()
374
-
375
-        sg_agent = self.agent._sec_groups_agent
376
-        sg_agent.refresh_firewall.assert_called_once_with(
377
-            [mock.sentinel.port_id])
378
-
379
-    def test_treat_vif_port_sg_disabled(self):
380
-        self.agent._enable_security_groups = False
381
-
382
-        self._check_treat_vif_port_state_up()
383
-
384
-        self.agent._utils.remove_all_security_rules.assert_called_once_with(
385
-            mock.sentinel.port_id)
320
+    @mock.patch.object(layer2.Layer2Agent, '_port_unbound')
321
+    def test_port_unbound(self, mock_super_unbound):
322
+        self.agent._port_unbound(mock.sentinel.port_id,
323
+                                 mock.sentinel.vnic_deleted)
386 324
 
387
-    def _get_fake_port_details(self):
388
-        return {
389
-            'device': mock.sentinel.device,
390
-            'port_id': mock.sentinel.port_id,
391
-            'network_id': mock.sentinel.network_id,
392
-            'network_type': mock.sentinel.network_type,
393
-            'physical_network': mock.sentinel.physical_network,
394
-            'segmentation_id': mock.sentinel.segmentation_id,
395
-            'admin_state_up': mock.sentinel.admin_state_up
396
-        }
325
+        mock_super_unbound.assert_called_once_with(
326
+            mock.sentinel.port_id, mock.sentinel.vnic_deleted)
327
+        remove_dev_filter = self.agent._sec_groups_agent.remove_devices_filter
328
+        remove_dev_filter.assert_called_once_with([mock.sentinel.port_id])
397 329
 
398 330
     @mock.patch.object(layer2.Layer2Agent, "_process_added_port")
399 331
     def test_process_added_port(self, mock_process):

+ 26
- 6
networking_hyperv/tests/unit/neutron/agent/test_layer2.py View File

@@ -48,11 +48,6 @@ class _Layer2Agent(agent_base.Layer2Agent):
48 48
                            physical_network, segmentation_id):
49 49
         pass
50 50
 
51
-    def _treat_vif_port(self, port_id, network_id, network_type,
52
-                        physical_network, segmentation_id,
53
-                        admin_state_up, port_security_enabled):
54
-        pass
55
-
56 51
 
57 52
 @ddt.ddt
58 53
 class TestLayer2Agent(test_base.HyperVBaseTestCase):
@@ -632,7 +627,8 @@ class TestLayer2Agent(test_base.HyperVBaseTestCase):
632 627
         port = {'id': mock.sentinel.port_id}
633 628
         self._agent.port_update(self._agent._context, port)
634 629
 
635
-    def test_port_update(self):
630
+    @mock.patch.object(agent_base.Layer2Agent, '_treat_vif_port')
631
+    def test_port_update(self, mock_treat_vif_port):
636 632
         self._agent._utils.vnic_port_exists.return_value = True
637 633
         port = {'id': mock.sentinel.port_id,
638 634
                 'network_id': mock.sentinel.network_id,
@@ -660,3 +656,27 @@ class TestLayer2Agent(test_base.HyperVBaseTestCase):
660 656
     def test_network_delete_not_defined(self, mock_reclaim_local_network):
661 657
         self._agent.network_delete(mock.sentinel.context, mock.sentinel.net_id)
662 658
         self.assertFalse(mock_reclaim_local_network.called)
659
+
660
+    @mock.patch.object(agent_base.Layer2Agent, '_port_bound')
661
+    def test_treat_vif_port_state_up(self, mock_port_bound):
662
+        self._agent._treat_vif_port(
663
+            mock.sentinel.port_id, mock.sentinel.network_id,
664
+            mock.sentinel.network_type, mock.sentinel.physical_network,
665
+            mock.sentinel.segmentation_id, True,
666
+            mock.sentinel.port_security_enabled)
667
+
668
+        mock_port_bound.assert_called_once_with(
669
+            mock.sentinel.port_id, mock.sentinel.network_id,
670
+            mock.sentinel.network_type, mock.sentinel.physical_network,
671
+            mock.sentinel.segmentation_id, mock.sentinel.port_security_enabled,
672
+            False)
673
+
674
+    @mock.patch.object(agent_base.Layer2Agent, '_port_unbound')
675
+    def test_treat_vif_port_state_down(self, mock_port_unbound):
676
+        self._agent._treat_vif_port(
677
+            mock.sentinel.port_id, mock.sentinel.network_id,
678
+            mock.sentinel.network_type, mock.sentinel.physical_network,
679
+            mock.sentinel.segmentation_id, False,
680
+            mock.sentinel.port_security_enabled)
681
+
682
+        mock_port_unbound.assert_called_once_with(mock.sentinel.port_id)

Loading…
Cancel
Save