Skip to content

Commit 55ed520

Browse files
committed
refactor
1 parent 3e8df8d commit 55ed520

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

python/neutron-understack/neutron_understack/tests/test_trunk.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
import pytest
55

66
from neutron_understack.nautobot import Nautobot
7+
from neutron_understack.neutron_understack_mech import UnderstackDriver
78
from neutron_understack.trunk import SubportSegmentationIDError
89
from neutron_understack.trunk import UnderStackTrunkDriver
910

10-
trunk_driver = UnderStackTrunkDriver.create("plugin_driver")
11-
1211

1312
@pytest.fixture
1413
def subport() -> MagicMock:
@@ -35,6 +34,11 @@ def nautobot_client() -> Nautobot:
3534
return MagicMock(spec_set=Nautobot)
3635

3736

37+
driver = UnderstackDriver()
38+
driver.nb = Nautobot("", "")
39+
trunk_driver = UnderStackTrunkDriver.create(driver)
40+
41+
3842
@patch("neutron_understack.utils.fetch_subport_network_id", return_value="112233")
3943
def test_subports_added_when_ucvni_tenan_vlan_id_is_not_set_yet(
4044
nautobot_client, payload

python/neutron-understack/neutron_understack/trunk.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
from oslo_config import cfg
1010
from oslo_log import log
1111

12-
from neutron_understack import config
1312
from neutron_understack import utils
14-
from neutron_understack.nautobot import Nautobot
15-
16-
config.register_ml2_understack_opts(cfg.CONF)
1713

1814
LOG = log.getLogger(__name__)
1915

@@ -48,8 +44,7 @@ def __init__(
4844
agent_type=agent_type,
4945
can_trunk_bound_port=can_trunk_bound_port,
5046
)
51-
conf = cfg.CONF.ml2_understack
52-
self.nb = Nautobot(conf.nb_url, conf.nb_token)
47+
self.nb = self.plugin_driver.nb
5348

5449
@property
5550
def is_loaded(self):
@@ -83,41 +78,46 @@ def register(self, resource, event, trigger, payload=None):
8378
self.trunk_created, resources.TRUNK, events.AFTER_CREATE, cancellable=True
8479
)
8580

86-
def _configure_tenant_vlan_id(
87-
self, tenant_vlan_id: int | None, ucvni_uuid: str, subport: SubPort
81+
def _handle_segmentation_id_mismatch(
82+
self, subport: SubPort, ucvni_uuid: str, tenant_vlan_id: int
8883
) -> None:
84+
subport.delete()
85+
raise SubportSegmentationIDError(
86+
seg_id=subport.segmentation_id,
87+
net_id=ucvni_uuid,
88+
nb_seg_id=tenant_vlan_id,
89+
subport_id=subport.port_id,
90+
)
91+
92+
def _configure_tenant_vlan_id(self, ucvni_uuid: str, subport: SubPort) -> None:
8993
subport_seg_id = subport.segmentation_id
90-
if not tenant_vlan_id:
91-
self.nb.add_tenant_vlan_tag_to_ucvni(
92-
network_uuid=ucvni_uuid, vlan_tag=subport_seg_id
93-
)
94-
LOG.info(
95-
"Segmentation ID: %(seg_id)s is now set on Nautobot's UCVNI "
96-
"UUID: %(ucvni_uuid)s in the tenant_vlan_id custom field",
97-
{"seg_id": subport_seg_id, "ucvni_uuid": ucvni_uuid},
98-
)
99-
elif tenant_vlan_id != subport_seg_id:
100-
subport.delete()
101-
raise SubportSegmentationIDError(
102-
seg_id=subport_seg_id,
103-
net_id=ucvni_uuid,
104-
nb_seg_id=tenant_vlan_id,
105-
subport_id=subport.port_id,
106-
)
94+
self.nb.add_tenant_vlan_tag_to_ucvni(
95+
network_uuid=ucvni_uuid, vlan_tag=subport_seg_id
96+
)
97+
LOG.info(
98+
"Segmentation ID: %(seg_id)s is now set on Nautobot's UCVNI "
99+
"UUID: %(ucvni_uuid)s in the tenant_vlan_id custom field",
100+
{"seg_id": subport_seg_id, "ucvni_uuid": ucvni_uuid},
101+
)
107102

108103
def _subports_added(self, subports: list[SubPort]) -> None:
109104
for subport in subports:
110-
subport_id = subport.port_id
111-
subport_network_id = utils.fetch_subport_network_id(subport_id=subport_id)
105+
subport_network_id = utils.fetch_subport_network_id(
106+
subport_id=subport.port_id
107+
)
112108
ucvni_tenant_vlan_id = self.nb.fetch_ucvni_tenant_vlan_id(
113109
network_id=subport_network_id
114110
)
115-
116-
self._configure_tenant_vlan_id(
117-
tenant_vlan_id=ucvni_tenant_vlan_id,
118-
ucvni_uuid=subport_network_id,
119-
subport=subport,
120-
)
111+
if not ucvni_tenant_vlan_id:
112+
self._configure_tenant_vlan_id(
113+
ucvni_uuid=subport_network_id, subport=subport
114+
)
115+
elif ucvni_tenant_vlan_id != subport.segmentation_id:
116+
self._handle_segmentation_id_mismatch(
117+
subport=subport,
118+
ucvni_uuid=subport_network_id,
119+
tenant_vlan_id=ucvni_tenant_vlan_id,
120+
)
121121

122122
def subports_added(self, resource, event, trunk_plugin, payload):
123123
subports = payload.metadata["subports"]

0 commit comments

Comments
 (0)