Skip to content

Commit 05b2cb4

Browse files
author
Milan Fencik
committed
refactor
1 parent d242ada commit 05b2cb4

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

python/neutron-understack/neutron_understack/nautobot.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,16 @@ def remove_port_network_associations(
281281
url = f"/api/dcim/interfaces/{interface_uuid}/"
282282
payload = {}
283283

284-
current_untagged_network = current["untagged_vlan"]["network"]["id"]
285-
if current_untagged_network in network_ids_to_remove:
284+
if (
285+
current["untagged_vlan"] and current["untagged_vlan"]["id"]
286+
in network_ids_to_remove
287+
):
286288
payload["untagged_vlan"] = None
287289

288290
payload["tagged_vlans"] = [
289291
tagged_vlan["id"]
290292
for tagged_vlan in current["tagged_vlans"]
291-
if (
292-
tagged_vlan.get("network")
293-
and tagged_vlan.get("network")["id"] not in network_ids_to_remove
294-
)
293+
if tagged_vlan["id"] not in network_ids_to_remove
295294
]
296295

297296
return self.make_api_request("PATCH", url, payload)

python/neutron-understack/neutron_understack/neutron_understack_mech.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,16 +265,17 @@ def update_port_postcommit(self, context):
265265
self.invoke_undersync(vlan_group_name)
266266

267267
def _tenant_network_port_cleanup(self, context: PortContext):
268-
network_id = context.current["network_id"]
269268
trunk_details = context.current.get("trunk_details", {})
269+
segment_id = context.original_top_bound_segment["id"]
270+
original_binding = context.original["binding:profile"]
270271
connected_interface_uuid = utils.fetch_connected_interface_uuid(
271-
context.original["binding:profile"], LOG
272-
)
272+
original_binding, LOG)
273273

274-
networks_to_remove = set(self._fetch_subports_network_ids(trunk_details))
275-
networks_to_remove.add(network_id)
274+
if not utils.ports_bound_to_segment(segment_id):
275+
context.release_dynamic_segment(segment_id)
276+
self.nb.delete_vlan(segment_id)
276277

277-
# TODO: does this needs to clean up subports too?
278+
networks_to_remove = {segment_id}
278279

279280
LOG.debug(
280281
"update_port_postcommit removing vlans %s from interface %s ",
@@ -286,6 +287,13 @@ def _tenant_network_port_cleanup(self, context: PortContext):
286287
connected_interface_uuid, networks_to_remove
287288
)
288289

290+
if trunk_details:
291+
self.trunk_driver.clean_trunk(
292+
trunk_details=trunk_details,
293+
binding_profile=original_binding,
294+
host=context.original_host,
295+
)
296+
289297
def delete_port_precommit(self, context):
290298
pass
291299

python/neutron-understack/neutron_understack/trunk.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ def _add_subports_networks_to_parent_port_switchport(
209209

210210
self.nb.add_port_vlan_associations(
211211
interface_uuid=connected_interface_id,
212-
native_vlan_id=None,
213212
vlan_group_name=vlan_group_name,
214213
allowed_vlans_ids=allowed_vlan_ids,
215214
)
@@ -220,11 +219,11 @@ def _add_subports_networks_to_parent_port_switchport(
220219
dry_run=cfg.CONF.ml2_understack.undersync_dry_run,
221220
)
222221

223-
def clean_trunk(self, trunk_details: dict, port_id: str) -> None:
222+
def clean_trunk(self, trunk_details: dict, binding_profile: dict, host: str) -> None:
224223
subports = trunk_details.get("sub_ports", [])
225-
parent_port_obj = utils.fetch_port_object(port_id)
226224
self._handle_subports_removal(
227-
parent_port=parent_port_obj,
225+
binding_profile=binding_profile,
226+
binding_host=host,
228227
subports=subports,
229228
invoke_undersync=False,
230229
)
@@ -233,11 +232,15 @@ def _clean_parent_port_switchport_config(
233232
self, trunk: Trunk, subports: [SubPort]
234233
) -> None:
235234
parent_port_obj = utils.fetch_port_object(trunk.port_id)
236-
if utils.parent_port_is_bound(parent_port_obj):
237-
self._handle_subports_removal(
238-
parent_port=parent_port_obj,
239-
subports=subports,
240-
)
235+
if not utils.parent_port_is_bound(parent_port_obj):
236+
return
237+
binding_profile = parent_port_obj.bindings[0].profile
238+
binding_host = parent_port_obj.bindings[0].host
239+
self._handle_subports_removal(
240+
binding_profile=binding_profile,
241+
binding_host=binding_host,
242+
subports=subports,
243+
)
241244

242245
def _delete_binding_level(self, port_id: str, host: str) -> PortBindingLevel:
243246
binding_level = utils.port_binding_level_by_port_id(port_id, host)
@@ -267,10 +270,12 @@ def _trigger_undersync(self, vlan_group_name: str) -> None:
267270
)
268271

269272
def _handle_subports_removal(
270-
self, parent_port: Port, subports: list[SubPort], invoke_undersync: bool = True
273+
self,
274+
binding_profile: dict,
275+
binding_host: str,
276+
subports: list[SubPort],
277+
invoke_undersync: bool = True
271278
) -> None:
272-
binding_profile = parent_port.bindings[0].profile
273-
binding_host = parent_port.bindings[0].host
274279
vlan_group_name = utils.vlan_group_name_from_binding_profile(binding_profile)
275280
connected_interface_id = utils.fetch_connected_interface_uuid(
276281
binding_profile, LOG

0 commit comments

Comments
 (0)