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
|
2020-09-30 15:07:56 -04:00
|
|
|
from django.db import transaction
|
2018-02-01 11:39:13 -05:00
|
|
|
from django.dispatch import receiver
|
|
|
|
|
2020-10-01 16:42:57 -04:00
|
|
|
from .choices import CableStatusChoices
|
2020-09-30 15:07:56 -04:00
|
|
|
from .models import Cable, CablePath, Device, PathEndpoint, VirtualChassis
|
2020-10-01 14:16:43 -04:00
|
|
|
from .utils import object_to_path_node, trace_path
|
2020-09-30 15:07:56 -04:00
|
|
|
|
|
|
|
|
2020-10-01 14:16:43 -04:00
|
|
|
def create_cablepath(node):
|
2020-09-30 15:07:56 -04:00
|
|
|
"""
|
|
|
|
Create CablePaths for all paths originating from the specified node.
|
|
|
|
"""
|
2020-10-05 11:13:33 -04:00
|
|
|
path, destination, is_active = trace_path(node)
|
2020-10-01 14:16:43 -04:00
|
|
|
if path:
|
2020-10-05 11:13:33 -04:00
|
|
|
cp = CablePath(origin=node, path=path, destination=destination, is_active=is_active)
|
2020-09-30 15:07:56 -04:00
|
|
|
cp.save()
|
|
|
|
|
|
|
|
|
|
|
|
def rebuild_paths(obj):
|
|
|
|
"""
|
|
|
|
Rebuild all CablePaths which traverse the specified node
|
|
|
|
"""
|
|
|
|
node = object_to_path_node(obj)
|
|
|
|
cable_paths = CablePath.objects.filter(path__contains=[node])
|
|
|
|
|
|
|
|
with transaction.atomic():
|
|
|
|
for cp in cable_paths:
|
|
|
|
cp.delete()
|
2020-10-01 14:16:43 -04:00
|
|
|
create_cablepath(cp.origin)
|
2018-02-01 11:39:13 -05:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Cable)
|
2020-09-30 15:07:56 -04:00
|
|
|
def update_connected_endpoints(instance, created, **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')
|
2018-10-31 15:01:01 -04:00
|
|
|
|
|
|
|
# Cache the Cable on its two termination points
|
2018-11-19 12:37:53 -05:00
|
|
|
if instance.termination_a.cable != instance:
|
2020-09-30 15:07:56 -04:00
|
|
|
logger.debug(f"Updating termination A for cable {instance}")
|
2018-11-19 12:37:53 -05:00
|
|
|
instance.termination_a.cable = instance
|
|
|
|
instance.termination_a.save()
|
|
|
|
if instance.termination_b.cable != instance:
|
2020-09-30 15:07:56 -04:00
|
|
|
logger.debug(f"Updating termination B for cable {instance}")
|
2018-11-19 12:37:53 -05:00
|
|
|
instance.termination_b.cable = instance
|
|
|
|
instance.termination_b.save()
|
2018-10-31 15:01:01 -04:00
|
|
|
|
2020-09-30 15:07:56 -04:00
|
|
|
# Create/update cable paths
|
|
|
|
if created:
|
|
|
|
for termination in (instance.termination_a, instance.termination_b):
|
|
|
|
if isinstance(termination, PathEndpoint):
|
2020-10-01 14:16:43 -04:00
|
|
|
create_cablepath(termination)
|
2020-09-30 15:07:56 -04:00
|
|
|
else:
|
|
|
|
rebuild_paths(termination)
|
2020-10-01 16:42:57 -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 != CableStatusChoices.STATUS_CONNECTED:
|
2020-10-05 11:13:33 -04:00
|
|
|
CablePath.objects.filter(path__contains=[object_to_path_node(instance)]).update(is_active=False)
|
2020-10-01 16:42:57 -04:00
|
|
|
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)
|
2018-10-18 15:43:55 -04:00
|
|
|
def nullify_connected_endpoints(instance, **kwargs):
|
2018-11-19 12:37:53 -05:00
|
|
|
"""
|
|
|
|
When a Cable is deleted, check for and update its two connected endpoints
|
|
|
|
"""
|
2020-03-18 20:04:38 -04:00
|
|
|
logger = logging.getLogger('netbox.dcim.cable')
|
|
|
|
|
2018-10-31 15:01:01 -04:00
|
|
|
# Disassociate the Cable from its termination points
|
2018-11-08 12:15:56 -05:00
|
|
|
if instance.termination_a is not None:
|
2020-09-30 15:07:56 -04:00
|
|
|
logger.debug(f"Nullifying termination A for cable {instance}")
|
2018-11-08 12:15:56 -05:00
|
|
|
instance.termination_a.cable = None
|
|
|
|
instance.termination_a.save()
|
|
|
|
if instance.termination_b is not None:
|
2020-09-30 15:07:56 -04:00
|
|
|
logger.debug(f"Nullifying termination B for cable {instance}")
|
2018-11-08 12:15:56 -05:00
|
|
|
instance.termination_b.cable = None
|
|
|
|
instance.termination_b.save()
|
2018-10-31 15:01:01 -04:00
|
|
|
|
2020-10-02 17:16:43 -04:00
|
|
|
# Delete and retrace any dependent cable paths
|
|
|
|
for cablepath in CablePath.objects.filter(path__contains=[object_to_path_node(instance)]):
|
2020-10-05 11:13:33 -04:00
|
|
|
path, destination, is_active = trace_path(cablepath.origin)
|
2020-10-02 17:16:43 -04:00
|
|
|
if path:
|
|
|
|
CablePath.objects.filter(pk=cablepath.pk).update(
|
|
|
|
path=path,
|
|
|
|
destination_type=ContentType.objects.get_for_model(destination) if destination else None,
|
|
|
|
destination_id=destination.pk if destination else None,
|
2020-10-05 11:13:33 -04:00
|
|
|
is_active=is_active
|
2020-10-02 17:16:43 -04:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
cablepath.delete()
|