10201 first fix and debugging prints

This commit is contained in:
Arthur 2022-12-28 17:47:40 -08:00
parent 735fa4aa31
commit 1270bc8818
3 changed files with 87 additions and 13 deletions

View File

@ -119,6 +119,10 @@ class Cable(PrimaryModel):
@a_terminations.setter @a_terminations.setter
def a_terminations(self, value): 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._terminations_modified = True
self._a_terminations = value self._a_terminations = value
@ -133,10 +137,15 @@ class Cable(PrimaryModel):
@b_terminations.setter @b_terminations.setter
def b_terminations(self, value): 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._terminations_modified = True
self._b_terminations = value self._b_terminations = value
def clean(self): def clean(self):
print("cable clean")
super().clean() super().clean()
# Validate length and length_unit # Validate length and length_unit
@ -168,7 +177,31 @@ class Cable(PrimaryModel):
for termination in self.b_terminations: for termination in self.b_terminations:
CableTermination(cable=self, cable_end='B', termination=termination).clean() 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): def save(self, *args, **kwargs):
print("cable save")
self.print_a_terminations()
self.print_b_terminations()
self.print_paths()
_created = self.pk is None _created = self.pk is None
# Store the given length (if any) in meters for use in database ordering # 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) 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): def get_status_color(self):
return LinkStatusChoices.colors.get(self.status) return LinkStatusChoices.colors.get(self.status)
@ -313,7 +351,7 @@ class CableTermination(models.Model):
) )
def delete(self, *args, **kwargs): def delete(self, *args, **kwargs):
print("cable termination delete")
# Delete the cable association on the terminating object # Delete the cable association on the terminating object
termination_model = self.termination._meta.model termination_model = self.termination._meta.model
termination_model.objects.filter(pk=self.termination_id).update( 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 # Ensure all originating terminations are attached to the same link
if len(terminations) > 1: 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:]) assert all(t.link == terminations[0].link for t in terminations[1:])
path = [] path = []
@ -610,6 +651,8 @@ class CablePath(models.Model):
""" """
Retrace the path from the currently-defined originating termination(s) 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) _new = self.from_origin(self.origins)
if _new: if _new:
self.path = _new.path self.path = _new.path

View File

@ -80,8 +80,8 @@ class ComponentModel(NetBoxModel):
def __str__(self): def __str__(self):
if self.label: if self.label:
return f"{self.name} ({self.label})" return f"{self.pk}: {self.name} ({self.label})"
return self.name return f"{self.pk}: {self.name}"
def to_objectchange(self, action): def to_objectchange(self, action):
objectchange = super().to_objectchange(action) objectchange = super().to_objectchange(action)
@ -859,6 +859,10 @@ class Interface(ModularComponentModel, BaseInterface, CabledObjectModel, PathEnd
def l2vpn_termination(self): def l2vpn_termination(self):
return self.l2vpn_terminations.first() return self.l2vpn_terminations.first()
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
print(f"Interface: {self} link: {self.link}")
# #
# Pass-through ports # Pass-through ports

View File

@ -70,11 +70,36 @@ def clear_virtualchassis_members(instance, **kwargs):
# Cables # 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) @receiver(trace_paths, sender=Cable)
def update_connected_endpoints(instance, created, raw=False, **kwargs): def update_connected_endpoints(instance, created, raw=False, **kwargs):
""" """
When a Cable is saved with new terminations, retrace any affected cable paths. When a Cable is saved with new terminations, retrace any affected cable paths.
""" """
print("update_connected_endpoints")
logger = logging.getLogger('netbox.dcim.cable') logger = logging.getLogger('netbox.dcim.cable')
if raw: if raw:
logger.debug(f"Skipping endpoint updates for imported cable {instance}") 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) a_terminations.append(t.termination)
else: else:
b_terminations.append(t.termination) 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) print(f"a_terminations: {a_terminations}")
if not nodes: print(f"b_terminations: {b_terminations}")
continue a_terminations_in_path = termination_in_path(a_terminations, instance)
if isinstance(nodes[0], PathEndpoint): b_terminations_in_path = termination_in_path(b_terminations, instance)
create_cablepath(nodes)
else: create_or_rebuild_paths(a_terminations, a_terminations_in_path)
rebuild_paths(nodes) create_or_rebuild_paths(b_terminations, b_terminations_in_path)
# Update status of CablePaths if Cable status has been changed # Update status of CablePaths if Cable status has been changed
elif instance.status != instance._orig_status: 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 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): for cablepath in CablePath.objects.filter(_nodes__contains=instance):
cablepath.retrace() cablepath.retrace()
@ -120,11 +146,12 @@ def nullify_connected_endpoints(instance, **kwargs):
""" """
Disassociate the Cable from the termination object, and retrace any affected CablePaths. Disassociate the Cable from the termination object, and retrace any affected CablePaths.
""" """
print("nullify_connected_endpoints")
model = instance.termination_type.model_class() model = instance.termination_type.model_class()
model.objects.filter(pk=instance.termination_id).update(cable=None, cable_end='') model.objects.filter(pk=instance.termination_id).update(cable=None, cable_end='')
for cablepath in CablePath.objects.filter(_nodes__contains=instance.cable): # for cablepath in CablePath.objects.filter(_nodes__contains=instance.cable):
cablepath.retrace() # cablepath.retrace()
@receiver(post_save, sender=FrontPort) @receiver(post_save, sender=FrontPort)