From 1270bc8818e563f491227432bc8ec299a78d3720 Mon Sep 17 00:00:00 2001 From: Arthur Date: Wed, 28 Dec 2022 17:47:40 -0800 Subject: [PATCH] 10201 first fix and debugging prints --- netbox/dcim/models/cables.py | 45 ++++++++++++++++++++++- netbox/dcim/models/device_components.py | 8 +++-- netbox/dcim/signals.py | 47 +++++++++++++++++++------ 3 files changed, 87 insertions(+), 13 deletions(-) diff --git a/netbox/dcim/models/cables.py b/netbox/dcim/models/cables.py index 48c1f92db..5fd06c061 100644 --- a/netbox/dcim/models/cables.py +++ b/netbox/dcim/models/cables.py @@ -119,6 +119,10 @@ class Cable(PrimaryModel): @a_terminations.setter def a_terminations(self, value): + if self.pk: + print(f"a_terminations setter a_terminations: {self.a_terminations} value: {value}") + else: + print("a_terminations setter no pk") self._terminations_modified = True self._a_terminations = value @@ -133,10 +137,15 @@ class Cable(PrimaryModel): @b_terminations.setter def b_terminations(self, value): + if self.pk: + print(f"b_terminations setter b_terminations: {self.b_terminations} value: {value}") + else: + print("b_terminations setter no pk") self._terminations_modified = True self._b_terminations = value def clean(self): + print("cable clean") super().clean() # Validate length and length_unit @@ -168,7 +177,31 @@ class Cable(PrimaryModel): for termination in self.b_terminations: CableTermination(cable=self, cable_end='B', termination=termination).clean() + def _print_terminations(self, terminations): + if len(terminations) > 1: + print(f"terminations[0].link: {terminations[0].link}") + for termination in terminations: + print(f"{termination.id}: {termination}") + print(f"termination: {termination} termination.link: {termination.link}") + + def print_a_terminations(self): + print("--- a termination ---") + self._print_terminations(self.a_terminations) + + def print_b_terminations(self): + print("--- b termination ---") + self._print_terminations(self.b_terminations) + + def print_paths(self): + print("--- cable paths ---") + for cablepath in CablePath.objects.filter(_nodes__contains=self): + print(f"{cablepath} - {cablepath.path}") + def save(self, *args, **kwargs): + print("cable save") + self.print_a_terminations() + self.print_b_terminations() + self.print_paths() _created = self.pk is None # Store the given length (if any) in meters for use in database ordering @@ -206,6 +239,11 @@ class Cable(PrimaryModel): trace_paths.send(Cable, instance=self, created=_created) + print("cable save end") + self.print_a_terminations() + self.print_b_terminations() + self.print_paths() + def get_status_color(self): return LinkStatusChoices.colors.get(self.status) @@ -313,7 +351,7 @@ class CableTermination(models.Model): ) def delete(self, *args, **kwargs): - + print("cable termination delete") # Delete the cable association on the terminating object termination_model = self.termination._meta.model termination_model.objects.filter(pk=self.termination_id).update( @@ -467,6 +505,9 @@ class CablePath(models.Model): # Ensure all originating terminations are attached to the same link if len(terminations) > 1: + print(f"terminations[0].link: {terminations[0].link}") + for t in terminations[1:]: + print(f"t: {t} t.link: {t.link}") assert all(t.link == terminations[0].link for t in terminations[1:]) path = [] @@ -610,6 +651,8 @@ class CablePath(models.Model): """ Retrace the path from the currently-defined originating termination(s) """ + print(f"retrace for cable path: {self.id}") + print(f"self.origins: {self.origins}") _new = self.from_origin(self.origins) if _new: self.path = _new.path diff --git a/netbox/dcim/models/device_components.py b/netbox/dcim/models/device_components.py index 658423e52..5c9cad7f7 100644 --- a/netbox/dcim/models/device_components.py +++ b/netbox/dcim/models/device_components.py @@ -80,8 +80,8 @@ class ComponentModel(NetBoxModel): def __str__(self): if self.label: - return f"{self.name} ({self.label})" - return self.name + return f"{self.pk}: {self.name} ({self.label})" + return f"{self.pk}: {self.name}" def to_objectchange(self, action): objectchange = super().to_objectchange(action) @@ -859,6 +859,10 @@ class Interface(ModularComponentModel, BaseInterface, CabledObjectModel, PathEnd def l2vpn_termination(self): return self.l2vpn_terminations.first() + def save(self, *args, **kwargs): + super().save(*args, **kwargs) + print(f"Interface: {self} link: {self.link}") + # # Pass-through ports diff --git a/netbox/dcim/signals.py b/netbox/dcim/signals.py index 522bb76c0..94518ed42 100644 --- a/netbox/dcim/signals.py +++ b/netbox/dcim/signals.py @@ -70,11 +70,36 @@ def clear_virtualchassis_members(instance, **kwargs): # Cables # +def termination_in_path(terminations, instance): + if terminations and isinstance(terminations[0], PathEndpoint): + if CablePath.objects.filter(_nodes__contains=instance).filter(_nodes__contains=terminations[0]): + return True + + return False + + +def create_or_rebuild_paths(nodes, in_path): + if not nodes: + return + + if isinstance(nodes[0], PathEndpoint): + if in_path: + print(f"rebuild_paths1 for: {nodes}") + rebuild_paths(nodes) + else: + print(f"create_cablepath for: {nodes}") + create_cablepath(nodes) + else: + print(f"rebuild_paths2 for: {nodes}") + rebuild_paths(nodes) + + @receiver(trace_paths, sender=Cable) def update_connected_endpoints(instance, created, raw=False, **kwargs): """ When a Cable is saved with new terminations, retrace any affected cable paths. """ + print("update_connected_endpoints") logger = logging.getLogger('netbox.dcim.cable') if raw: logger.debug(f"Skipping endpoint updates for imported cable {instance}") @@ -89,14 +114,14 @@ def update_connected_endpoints(instance, created, raw=False, **kwargs): a_terminations.append(t.termination) else: b_terminations.append(t.termination) - for nodes in [a_terminations, b_terminations]: - # Examine type of first termination to determine object type (all must be the same) - if not nodes: - continue - if isinstance(nodes[0], PathEndpoint): - create_cablepath(nodes) - else: - rebuild_paths(nodes) + + print(f"a_terminations: {a_terminations}") + print(f"b_terminations: {b_terminations}") + a_terminations_in_path = termination_in_path(a_terminations, instance) + b_terminations_in_path = termination_in_path(b_terminations, instance) + + create_or_rebuild_paths(a_terminations, a_terminations_in_path) + create_or_rebuild_paths(b_terminations, b_terminations_in_path) # Update status of CablePaths if Cable status has been changed elif instance.status != instance._orig_status: @@ -111,6 +136,7 @@ def retrace_cable_paths(instance, **kwargs): """ When a Cable is deleted, check for and update its connected endpoints """ + print("retrace_cable_paths") for cablepath in CablePath.objects.filter(_nodes__contains=instance): cablepath.retrace() @@ -120,11 +146,12 @@ def nullify_connected_endpoints(instance, **kwargs): """ Disassociate the Cable from the termination object, and retrace any affected CablePaths. """ + print("nullify_connected_endpoints") model = instance.termination_type.model_class() model.objects.filter(pk=instance.termination_id).update(cable=None, cable_end='') - for cablepath in CablePath.objects.filter(_nodes__contains=instance.cable): - cablepath.retrace() + # for cablepath in CablePath.objects.filter(_nodes__contains=instance.cable): + # cablepath.retrace() @receiver(post_save, sender=FrontPort)