2020-03-18 20:04:38 -04:00
|
|
|
import logging
|
|
|
|
|
2018-11-02 15:20:08 -04:00
|
|
|
from django.db.models.signals import post_save, pre_delete
|
2018-02-01 11:39:13 -05:00
|
|
|
from django.dispatch import receiver
|
|
|
|
|
2020-04-15 15:46:41 -04:00
|
|
|
from .choices import CableStatusChoices
|
2018-10-18 15:43:55 -04:00
|
|
|
from .models import Cable, Device, VirtualChassis
|
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)
|
|
|
|
def update_connected_endpoints(instance, **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-03-18 20:04:38 -04:00
|
|
|
logger.debug("Updating termination A for cable {}".format(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-03-18 20:04:38 -04:00
|
|
|
logger.debug("Updating termination B for cable {}".format(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-04-15 15:46:41 -04:00
|
|
|
# Update any endpoints for this Cable.
|
|
|
|
endpoints = instance.termination_a.get_path_endpoints() + instance.termination_b.get_path_endpoints()
|
|
|
|
for endpoint in endpoints:
|
2020-04-15 17:09:04 -04:00
|
|
|
path, split_ends = endpoint.trace()
|
2020-04-15 15:46:41 -04:00
|
|
|
# Determine overall path status (connected or planned)
|
|
|
|
path_status = True
|
|
|
|
for segment in path:
|
|
|
|
if segment[1] is None or segment[1].status != CableStatusChoices.STATUS_CONNECTED:
|
|
|
|
path_status = False
|
|
|
|
break
|
|
|
|
|
|
|
|
endpoint_a = path[0][0]
|
|
|
|
endpoint_b = path[-1][2]
|
|
|
|
|
|
|
|
if getattr(endpoint_a, 'is_path_endpoint', False) and getattr(endpoint_b, 'is_path_endpoint', False):
|
|
|
|
logger.debug("Updating path endpoints: {} <---> {}".format(endpoint_a, endpoint_b))
|
|
|
|
endpoint_a.connected_endpoint = endpoint_b
|
|
|
|
endpoint_a.connection_status = path_status
|
|
|
|
endpoint_a.save()
|
|
|
|
endpoint_b.connected_endpoint = endpoint_a
|
|
|
|
endpoint_b.connection_status = path_status
|
|
|
|
endpoint_b.save()
|
2018-10-18 15:43:55 -04:00
|
|
|
|
|
|
|
|
2018-10-31 15:01:01 -04:00
|
|
|
@receiver(pre_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')
|
|
|
|
|
2020-04-15 15:46:41 -04:00
|
|
|
endpoints = instance.termination_a.get_path_endpoints() + instance.termination_b.get_path_endpoints()
|
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-03-18 20:04:38 -04:00
|
|
|
logger.debug("Nullifying termination A for cable {}".format(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-03-18 20:04:38 -04:00
|
|
|
logger.debug("Nullifying termination B for cable {}".format(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-04-15 15:46:41 -04:00
|
|
|
# If this Cable was part of any complete end-to-end paths, tear them down.
|
|
|
|
for endpoint in endpoints:
|
|
|
|
logger.debug(f"Removing path information for {endpoint}")
|
|
|
|
if hasattr(endpoint, 'connected_endpoint'):
|
|
|
|
endpoint.connected_endpoint = None
|
|
|
|
endpoint.connection_status = None
|
|
|
|
endpoint.save()
|