2022-05-06 16:17:27 -04:00
|
|
|
from collections import defaultdict
|
2020-03-18 20:04:38 -04:00
|
|
|
import logging
|
|
|
|
|
2020-09-30 15:07:56 -04:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
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
|
|
|
|
|
2021-10-13 14:31:30 -04:00
|
|
|
from .choices import LinkStatusChoices
|
2022-05-02 08:26:02 -04:00
|
|
|
from .models import Cable, CablePath, CableTermination, Device, PathEndpoint, PowerPanel, Rack, Location, VirtualChassis
|
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
|
|
|
|
#
|
|
|
|
|
|
|
|
|
2018-10-18 15:43:55 -04:00
|
|
|
@receiver(post_save, 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
|
|
|
"""
|
|
|
|
When a Cable is saved, check for and update its two connected endpoints
|
|
|
|
"""
|
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-05-03 16:30:39 -04:00
|
|
|
# Save any new CableTerminations
|
|
|
|
CableTermination.objects.bulk_create([
|
|
|
|
term for term in instance.terminations if not term.pk
|
|
|
|
])
|
|
|
|
|
2022-05-10 09:53:55 -04:00
|
|
|
# Split terminations into A/B sets and save link assignments
|
2022-05-03 16:30:39 -04:00
|
|
|
# TODO: Update link peers
|
2022-05-10 09:53:55 -04:00
|
|
|
_terms = defaultdict(list)
|
|
|
|
for t in instance.terminations:
|
|
|
|
if t.termination.cable != instance:
|
|
|
|
t.termination.cable = instance
|
|
|
|
t.termination.save()
|
|
|
|
_terms[t.cable_end].append(t.termination)
|
2022-05-03 16:30:39 -04:00
|
|
|
|
|
|
|
# Create/update cable paths
|
|
|
|
if created:
|
|
|
|
for terms in _terms.values():
|
2022-05-06 16:17:27 -04:00
|
|
|
# Examine type of first termination to determine object type (all must be the same)
|
2022-05-10 09:53:55 -04:00
|
|
|
if isinstance(terms[0], PathEndpoint):
|
2022-05-03 16:30:39 -04:00
|
|
|
create_cablepath(terms)
|
2022-05-10 10:30:58 -04:00
|
|
|
else:
|
|
|
|
rebuild_paths(terms)
|
2022-05-10 13:45:52 -04:00
|
|
|
elif instance.status != instance._orig_status:
|
|
|
|
# We currently don't support modifying either termination of an existing Cable. (This
|
|
|
|
# may change in the future.) However, we do need to capture status changes and update
|
|
|
|
# any CablePaths accordingly.
|
|
|
|
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
|
|
|
|
|
|
|
|
2022-05-03 16:30:39 -04:00
|
|
|
# @receiver(post_save, sender=CableTermination)
|
|
|
|
# def cache_cable_on_endpoints(instance, created, raw=False, **kwargs):
|
|
|
|
# if not raw:
|
|
|
|
# model = instance.termination_type.model_class()
|
|
|
|
# model.objects.filter(pk=instance.termination_id).update(cable=instance.cable)
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# @receiver(post_delete, sender=CableTermination)
|
|
|
|
# def clear_cable_on_endpoints(instance, **kwargs):
|
|
|
|
# model = instance.termination_type.model_class()
|
|
|
|
# model.objects.filter(pk=instance.termination_id).update(cable=None)
|
2022-05-02 08:26:02 -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):
|
|
|
|
"""
|
|
|
|
Disassociate the Cable from the termination object.
|
|
|
|
"""
|
|
|
|
model = instance.termination_type.model_class()
|
|
|
|
model.objects.filter(pk=instance.termination_id).update(_link_peer_type=None, _link_peer_id=None)
|