2020-03-18 20:04:38 -04:00
|
|
|
import logging
|
|
|
|
|
2020-10-02 17:16:43 -04:00
|
|
|
from django.db.models.signals import post_save, post_delete, pre_delete
|
2018-02-01 11:39:13 -05:00
|
|
|
from django.dispatch import receiver
|
|
|
|
|
2022-07-07 12:48:44 -04:00
|
|
|
from .choices import CableEndChoices, LinkStatusChoices
|
2022-05-02 08:26:02 -04:00
|
|
|
from .models import Cable, CablePath, CableTermination, Device, PathEndpoint, PowerPanel, Rack, Location, VirtualChassis
|
2022-05-18 15:49:52 -04:00
|
|
|
from .models.cables import trace_paths
|
2021-10-13 13:28:14 -04:00
|
|
|
from .utils import create_cablepath, rebuild_paths
|
2018-02-01 11:39:13 -05:00
|
|
|
|
|
|
|
|
2020-12-22 15:22:53 -05:00
|
|
|
#
|
2021-03-05 09:56:47 -05:00
|
|
|
# Location/rack/device assignment
|
2020-12-22 15:22:53 -05:00
|
|
|
#
|
|
|
|
|
2021-03-03 13:30:33 -05:00
|
|
|
@receiver(post_save, sender=Location)
|
|
|
|
def handle_location_site_change(instance, created, **kwargs):
|
2020-12-22 15:22:53 -05:00
|
|
|
"""
|
2021-03-03 14:28:07 -05:00
|
|
|
Update child objects if Site assignment has changed. We intentionally recurse through each child
|
2020-12-22 15:22:53 -05:00
|
|
|
object instead of calling update() on the QuerySet to ensure the proper change records get created for each.
|
|
|
|
"""
|
|
|
|
if not created:
|
2021-03-05 09:56:47 -05:00
|
|
|
instance.get_descendants().update(site=instance.site)
|
|
|
|
locations = instance.get_descendants(include_self=True).values_list('pk', flat=True)
|
|
|
|
Rack.objects.filter(location__in=locations).update(site=instance.site)
|
|
|
|
Device.objects.filter(location__in=locations).update(site=instance.site)
|
|
|
|
PowerPanel.objects.filter(location__in=locations).update(site=instance.site)
|
2020-12-22 15:22:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Rack)
|
|
|
|
def handle_rack_site_change(instance, created, **kwargs):
|
|
|
|
"""
|
2021-03-03 14:28:07 -05:00
|
|
|
Update child Devices if Site or Location assignment has changed.
|
2020-12-22 15:22:53 -05:00
|
|
|
"""
|
|
|
|
if not created:
|
2021-03-05 09:56:47 -05:00
|
|
|
Device.objects.filter(rack=instance).update(site=instance.site, location=instance.location)
|
2020-12-22 15:22:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Virtual chassis
|
|
|
|
#
|
|
|
|
|
2018-02-01 13:52:41 -05:00
|
|
|
@receiver(post_save, sender=VirtualChassis)
|
|
|
|
def assign_virtualchassis_master(instance, created, **kwargs):
|
|
|
|
"""
|
2020-06-24 15:12:22 -04:00
|
|
|
When a VirtualChassis is created, automatically assign its master device (if any) to the VC.
|
2018-02-01 13:52:41 -05:00
|
|
|
"""
|
2020-06-24 15:12:22 -04:00
|
|
|
if created and instance.master:
|
2020-06-24 15:57:52 -04:00
|
|
|
master = Device.objects.get(pk=instance.master.pk)
|
|
|
|
master.virtual_chassis = instance
|
|
|
|
master.vc_position = 1
|
|
|
|
master.save()
|
2018-02-01 13:52:41 -05:00
|
|
|
|
|
|
|
|
2018-02-01 11:39:13 -05:00
|
|
|
@receiver(pre_delete, sender=VirtualChassis)
|
|
|
|
def clear_virtualchassis_members(instance, **kwargs):
|
|
|
|
"""
|
|
|
|
When a VirtualChassis is deleted, nullify the vc_position and vc_priority fields of its prior members.
|
|
|
|
"""
|
2019-08-20 17:16:00 -04:00
|
|
|
devices = Device.objects.filter(virtual_chassis=instance.pk)
|
|
|
|
for device in devices:
|
2019-08-20 17:20:46 -04:00
|
|
|
device.vc_position = None
|
|
|
|
device.vc_priority = None
|
|
|
|
device.save()
|
2018-10-18 15:43:55 -04:00
|
|
|
|
|
|
|
|
2020-12-22 15:22:53 -05:00
|
|
|
#
|
|
|
|
# Cables
|
|
|
|
#
|
|
|
|
|
2022-05-18 15:49:52 -04:00
|
|
|
@receiver(trace_paths, sender=Cable)
|
2020-11-03 13:32:05 -05:00
|
|
|
def update_connected_endpoints(instance, created, raw=False, **kwargs):
|
2018-11-19 12:37:53 -05:00
|
|
|
"""
|
2022-06-03 14:15:28 -04:00
|
|
|
When a Cable is saved with new terminations, retrace any affected cable paths.
|
2018-11-19 12:37:53 -05:00
|
|
|
"""
|
2020-03-18 20:04:38 -04:00
|
|
|
logger = logging.getLogger('netbox.dcim.cable')
|
2020-11-03 13:32:05 -05:00
|
|
|
if raw:
|
|
|
|
logger.debug(f"Skipping endpoint updates for imported cable {instance}")
|
|
|
|
return
|
2018-10-31 15:01:01 -04:00
|
|
|
|
2022-06-03 14:15:28 -04:00
|
|
|
# Update cable paths if new terminations have been set
|
2022-07-13 15:35:37 -04:00
|
|
|
if instance._terminations_modified:
|
2022-06-03 14:15:28 -04:00
|
|
|
a_terminations = []
|
|
|
|
b_terminations = []
|
|
|
|
for t in instance.terminations.all():
|
2022-07-07 12:48:44 -04:00
|
|
|
if t.cable_end == CableEndChoices.SIDE_A:
|
2022-06-03 14:15:28 -04:00
|
|
|
a_terminations.append(t.termination)
|
|
|
|
else:
|
|
|
|
b_terminations.append(t.termination)
|
|
|
|
for nodes in [a_terminations, b_terminations]:
|
2022-05-06 16:17:27 -04:00
|
|
|
# Examine type of first termination to determine object type (all must be the same)
|
2022-05-18 15:49:52 -04:00
|
|
|
if not nodes:
|
|
|
|
continue
|
|
|
|
if isinstance(nodes[0], PathEndpoint):
|
|
|
|
create_cablepath(nodes)
|
2022-05-10 10:30:58 -04:00
|
|
|
else:
|
2022-05-18 15:49:52 -04:00
|
|
|
rebuild_paths(nodes)
|
2022-06-03 14:15:28 -04:00
|
|
|
|
|
|
|
# Update status of CablePaths if Cable status has been changed
|
2022-05-10 13:45:52 -04:00
|
|
|
elif instance.status != instance._orig_status:
|
|
|
|
if instance.status != LinkStatusChoices.STATUS_CONNECTED:
|
|
|
|
CablePath.objects.filter(_nodes__contains=instance).update(is_active=False)
|
|
|
|
else:
|
|
|
|
rebuild_paths([instance])
|
2018-10-18 15:43:55 -04:00
|
|
|
|
|
|
|
|
2020-10-02 17:16:43 -04:00
|
|
|
@receiver(post_delete, sender=Cable)
|
2022-05-10 11:58:48 -04:00
|
|
|
def retrace_cable_paths(instance, **kwargs):
|
2018-11-19 12:37:53 -05:00
|
|
|
"""
|
2022-05-10 11:58:48 -04:00
|
|
|
When a Cable is deleted, check for and update its connected endpoints
|
2018-11-19 12:37:53 -05:00
|
|
|
"""
|
2022-05-03 16:30:39 -04:00
|
|
|
for cablepath in CablePath.objects.filter(_nodes__contains=instance):
|
2022-05-10 11:58:48 -04:00
|
|
|
cablepath.retrace()
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=CableTermination)
|
|
|
|
def nullify_connected_endpoints(instance, **kwargs):
|
|
|
|
"""
|
2022-08-03 12:46:16 -04:00
|
|
|
Disassociate the Cable from the termination object, and retrace any affected CablePaths.
|
2022-05-10 11:58:48 -04:00
|
|
|
"""
|
|
|
|
model = instance.termination_type.model_class()
|
2022-06-01 16:48:56 -04:00
|
|
|
model.objects.filter(pk=instance.termination_id).update(cable=None, cable_end='')
|
2022-08-03 12:46:16 -04:00
|
|
|
|
|
|
|
for cablepath in CablePath.objects.filter(_nodes__contains=instance.cable):
|
|
|
|
cablepath.retrace()
|