Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes: #11079 - Handle cables across multiple rear-port positions #13337

Merged
merged 24 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7c09eb2
Catch AssertionError's in signals. Handle accordingly
DanSheps Aug 1, 2023
32c4f3c
Alter cable logic to handle certain additional path types.
DanSheps Aug 4, 2023
a797995
Fix failures and add test
DanSheps Aug 5, 2023
7a15b2b
More tests
DanSheps Aug 7, 2023
4cf5ac5
Remove not needed tests, add additional tests
DanSheps Aug 7, 2023
4fb67dd
Finish tests, correct some behaviour
DanSheps Aug 7, 2023
b335b67
Add check for mid-span device not allowed condition
DanSheps Aug 7, 2023
3ca03d3
Remove excess import
DanSheps Aug 7, 2023
b32ecb5
Remove logging import
DanSheps Aug 7, 2023
d2a851b
Remove logging import
DanSheps Aug 7, 2023
b2c35e0
Merge branch 'develop' into 11079-Cablepath_catch_assertions
DanSheps Aug 31, 2023
880e79a
Minor tweaks based on Arthur's feedback
DanSheps Sep 1, 2023
837811d
Update netbox/dcim/tests/test_cablepaths.py
DanSheps Sep 7, 2023
e0f3292
Update netbox/dcim/models/cables.py
DanSheps Sep 7, 2023
1f4baa7
Changes to account for required SVG rendering changes and based on fe…
DanSheps Sep 8, 2023
de216aa
More tweaks for cable path checking
DanSheps Sep 12, 2023
359b366
Improve handling of links with multi-terminations
DanSheps Sep 12, 2023
5bb373f
Improved SVG rendering of multiple rear ports (with positions) per pa…
DanSheps Sep 12, 2023
4ae7e2f
Include missing assert to ensure links are same type.
DanSheps Sep 12, 2023
58ca489
Clean up tests
jeremystretch Sep 18, 2023
fc8b5e1
Remove unused objects from tests
jeremystretch Sep 18, 2023
d87df88
Changes requested to tests and update comments/doctstrings
DanSheps Sep 19, 2023
1c2e552
Merge remote-tracking branch 'origin/11079-Cablepath_catch_assertions…
DanSheps Sep 19, 2023
704f9ab
Fix parent reference
DanSheps Sep 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 69 additions & 34 deletions netbox/dcim/models/cables.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from utilities.querysets import RestrictedQuerySet
from utilities.utils import to_meters
from wireless.models import WirelessLink
from .device_components import FrontPort, RearPort
from .device_components import FrontPort, RearPort, PathEndpoint

__all__ = (
'Cable',
Expand Down Expand Up @@ -478,11 +478,8 @@ def from_origin(cls, terminations):
Cable or WirelessLink connects (interfaces, console ports, circuit termination, etc.). All terminations must be
of the same type and must belong to the same parent object.
"""
import logging
from circuits.models import CircuitTermination

logger = logging.getLogger('netbox.dcim.cablepath')

if not terminations:
return None

Expand All @@ -501,9 +498,15 @@ def from_origin(cls, terminations):
# Terminations must all be of the same type
assert all(isinstance(t, type(terminations[0])) for t in terminations[1:])

# All mid-span terminations must all be attached to the same device
jsenecal marked this conversation as resolved.
Show resolved Hide resolved
if not isinstance(terminations[0], PathEndpoint):
assert all(t.device == terminations[0].device for t in terminations[1:])
DanSheps marked this conversation as resolved.
Show resolved Hide resolved

# Check for a split path (e.g. rear port fanning out to multiple front ports with
# different cables attached)
if len(set(t.link for t in terminations)) > 1:
if len(set(t.link for t in terminations)) > 1 and (
position_stack and len(terminations) != len(position_stack[-1])
):
is_split = True
break

Expand All @@ -512,46 +515,66 @@ def from_origin(cls, terminations):
object_to_path_node(t) for t in terminations
])

# Step 2: Determine the attached link (Cable or WirelessLink), if any
link = terminations[0].link
if link is None and len(path) == 1:
# Step 2: Determine the attached links (Cable or WirelessLink), if any
links = [termination.link for termination in terminations if termination.link is not None]
if len(links) == 0 and len(path) == 1:
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
# If this is the start of the path and no link exists, return None
return None
elif link is None:
elif len(links) == 0:
# Otherwise, halt the trace if no link exists
break
assert type(link) in (Cable, WirelessLink)
assert all(type(link) in (Cable, WirelessLink) for link in links)

# Create set of links in path. Cannot use list(set()) as it does this in a non-deterministic manner
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
links_path = []
for link in links:
if object_to_path_node(link) not in links_path:
arthanson marked this conversation as resolved.
Show resolved Hide resolved
links_path.append(object_to_path_node(link))

# Step 3: Record the links
path.append(links_path)

# Step 3: Record the link and update path status if not "connected"
path.append([object_to_path_node(link)])
if hasattr(link, 'status') and link.status != LinkStatusChoices.STATUS_CONNECTED:
# Step 4: Update the path status if a link is not connected
links_status = [
link.status for link in links if hasattr(link, 'status') and
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
link.status != LinkStatusChoices.STATUS_CONNECTED
]
if len(links_status) > 0 and len(links) != len(links_status):
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
is_active = False

# Step 4: Determine the far-end terminations
if isinstance(link, Cable):
# Step 5: Determine the far-end terminations
if isinstance(links[0], Cable):
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
termination_type = ContentType.objects.get_for_model(terminations[0])
local_cable_terminations = CableTermination.objects.filter(
termination_type=termination_type,
termination_id__in=[t.pk for t in terminations]
)
# Terminations must all belong to same end of Cable
local_cable_end = local_cable_terminations[0].cable_end
assert all(ct.cable_end == local_cable_end for ct in local_cable_terminations[1:])
remote_cable_terminations = CableTermination.objects.filter(
cable=link,
cable_end='A' if local_cable_end == 'B' else 'B'
)

q_filter = Q()
arthanson marked this conversation as resolved.
Show resolved Hide resolved
for lct in local_cable_terminations:
q_filter |= Q(cable=lct.cable, cable_end='A' if lct.cable_end == 'B' else 'B')
DanSheps marked this conversation as resolved.
Show resolved Hide resolved

assert q_filter is not Q()
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
remote_cable_terminations = CableTermination.objects.filter(q_filter)
remote_terminations = [ct.termination for ct in remote_cable_terminations]
else:
# WirelessLink
remote_terminations = [link.interface_b] if link.interface_a is terminations[0] else [link.interface_a]
remote_terminations = [
link.interface_b if link.interface_a is terminations[0] else link.interface_a for link in links
]

# Step 5: Record the far-end termination object(s)
# Remote Terminations must all be of the same type, otherwise return a split path
if not all(isinstance(t, type(remote_terminations[0])) for t in remote_terminations[1:]):
is_complete = False
is_split = True
break

# Step 6: Record the far-end termination object(s)
path.append([
object_to_path_node(t) for t in remote_terminations if t is not None
])

# Step 6: Determine the "next hop" terminations, if applicable
# Step 7: Determine the "next hop" terminations, if applicable
if not remote_terminations:
break

Expand All @@ -560,21 +583,26 @@ def from_origin(cls, terminations):
rear_ports = RearPort.objects.filter(
pk__in=[t.rear_port_id for t in remote_terminations]
)
if len(rear_ports) > 1:
logger.warning(f'All rear-port positions do not match. Cannot continue path trace.')
assert all(rp.positions == 1 for rp in rear_ports)
elif rear_ports[0].positions > 1:
if len(rear_ports) > 1 or rear_ports[0].positions > 1:
position_stack.append([fp.rear_port_position for fp in remote_terminations])

terminations = rear_ports

elif isinstance(remote_terminations[0], RearPort):

if len(remote_terminations) > 1 or remote_terminations[0].positions == 1:
if len(remote_terminations) == 1 and remote_terminations[0].positions == 1:
front_ports = FrontPort.objects.filter(
rear_port_id__in=[rp.pk for rp in remote_terminations],
rear_port_position=1
)
elif len(remote_terminations) > 1 and position_stack:
positions = position_stack.pop()
assert len(remote_terminations) == len(positions)
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
q_filter = Q()
for rt in remote_terminations:
position = positions.pop()
q_filter |= Q(rear_port_id=rt.pk, rear_port_position=position)
assert q_filter is not Q()
front_ports = FrontPort.objects.filter(q_filter)
elif position_stack:
front_ports = FrontPort.objects.filter(
rear_port_id=remote_terminations[0].pk,
Expand Down Expand Up @@ -616,18 +644,25 @@ def from_origin(cls, terminations):

terminations = [circuit_termination]

# Anything else marks the end of the path
else:
is_complete = True
# Check for non-symmetric path
if all(isinstance(t, type(remote_terminations[0])) for t in remote_terminations[1:]):
is_complete = True
else:
# Unsupported topology, mark as split and exit
is_complete = False
is_split = True
break

return cls(
cablepath = cls(
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
path=path,
is_complete=is_complete,
is_active=is_active,
is_split=is_split
)

return cablepath

def retrace(self):
"""
Retrace the path from the currently-defined originating termination(s)
Expand Down
6 changes: 1 addition & 5 deletions netbox/dcim/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ def update_connected_endpoints(instance, created, raw=False, **kwargs):
if not nodes:
continue
if isinstance(nodes[0], PathEndpoint):
try:
create_cablepath(nodes)
except AssertionError:
# This is likely an unsupported path. Catch the assertion error and don't save the path
logger.error(f'Unsupported path from nodes: {[node.name for node in nodes].join(",")}')
create_cablepath(nodes)
else:
rebuild_paths(nodes)

Expand Down
Loading