signals.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import logging
  2. from django.db.models import Q
  3. from django.db.models.signals import post_delete, post_save
  4. from django.dispatch import receiver
  5. from dcim.choices import CableEndChoices, LinkStatusChoices
  6. from ipam.models import Prefix
  7. from virtualization.models import Cluster, VMInterface
  8. from wireless.models import WirelessLAN
  9. from .models import (
  10. Cable,
  11. CablePath,
  12. CableTermination,
  13. ConsolePort,
  14. ConsoleServerPort,
  15. Device,
  16. DeviceBay,
  17. FrontPort,
  18. Interface,
  19. InventoryItem,
  20. Location,
  21. ModuleBay,
  22. PathEndpoint,
  23. PortMapping,
  24. PowerOutlet,
  25. PowerPanel,
  26. PowerPort,
  27. Rack,
  28. RearPort,
  29. Site,
  30. VirtualChassis,
  31. )
  32. from .models.cables import trace_paths
  33. from .utils import create_cablepaths, rebuild_paths
  34. COMPONENT_MODELS = (
  35. ConsolePort,
  36. ConsoleServerPort,
  37. DeviceBay,
  38. FrontPort,
  39. Interface,
  40. InventoryItem,
  41. ModuleBay,
  42. PowerOutlet,
  43. PowerPort,
  44. RearPort,
  45. )
  46. #
  47. # Location/rack/device assignment
  48. #
  49. @receiver(post_save, sender=Location)
  50. def handle_location_site_change(instance, created, **kwargs):
  51. """
  52. Update child objects if Site assignment has changed. We intentionally recurse through each child
  53. object instead of calling update() on the QuerySet to ensure the proper change records get created for each.
  54. """
  55. if not created:
  56. instance.get_descendants().update(site=instance.site)
  57. locations = instance.get_descendants(include_self=True).values_list('pk', flat=True)
  58. Rack.objects.filter(location__in=locations).update(site=instance.site)
  59. Device.objects.filter(location__in=locations).update(site=instance.site)
  60. PowerPanel.objects.filter(location__in=locations).update(site=instance.site)
  61. CableTermination.objects.filter(_location__in=locations).update(_site=instance.site)
  62. # Update component models for devices in these locations
  63. for model in COMPONENT_MODELS:
  64. model.objects.filter(device__location__in=locations).update(_site=instance.site)
  65. @receiver(post_save, sender=Rack)
  66. def handle_rack_site_change(instance, created, **kwargs):
  67. """
  68. Update child Devices if Site or Location assignment has changed.
  69. """
  70. if not created:
  71. Device.objects.filter(rack=instance).update(site=instance.site, location=instance.location)
  72. # Update component models for devices in this rack
  73. for model in COMPONENT_MODELS:
  74. model.objects.filter(device__rack=instance).update(
  75. _site=instance.site,
  76. _location=instance.location,
  77. )
  78. @receiver(post_save, sender=Device)
  79. def handle_device_site_change(instance, created, **kwargs):
  80. """
  81. Update child components to update the parent Site, Location, and Rack when a Device is saved.
  82. """
  83. if not created:
  84. for model in COMPONENT_MODELS:
  85. model.objects.filter(device=instance).update(
  86. _site=instance.site,
  87. _location=instance.location,
  88. _rack=instance.rack,
  89. )
  90. #
  91. # Virtual chassis
  92. #
  93. @receiver(post_save, sender=VirtualChassis)
  94. def assign_virtualchassis_master(instance, created, **kwargs):
  95. """
  96. When a VirtualChassis is created, automatically assign its master device (if any) to the VC.
  97. """
  98. if created and instance.master:
  99. master = Device.objects.get(pk=instance.master.pk)
  100. master.virtual_chassis = instance
  101. master.vc_position = 1
  102. master.save()
  103. #
  104. # Cables
  105. #
  106. @receiver(trace_paths, sender=Cable)
  107. def update_connected_endpoints(instance, created, raw=False, **kwargs):
  108. """
  109. When a Cable is saved with new terminations, retrace any affected cable paths.
  110. """
  111. logger = logging.getLogger('netbox.dcim.cable')
  112. if raw:
  113. logger.debug(f"Skipping endpoint updates for imported cable {instance}")
  114. return
  115. # Update cable paths if new terminations have been set
  116. if instance._terminations_modified:
  117. a_terminations = []
  118. b_terminations = []
  119. # Note: instance.terminations.all() is not safe to use here as it might be stale
  120. for t in CableTermination.objects.filter(cable=instance):
  121. if t.cable_end == CableEndChoices.SIDE_A:
  122. a_terminations.append(t.termination)
  123. else:
  124. b_terminations.append(t.termination)
  125. for nodes in [a_terminations, b_terminations]:
  126. # Examine type of first termination to determine object type (all must be the same)
  127. if not nodes:
  128. continue
  129. if isinstance(nodes[0], PathEndpoint):
  130. create_cablepaths(nodes)
  131. else:
  132. rebuild_paths(nodes)
  133. # Update status of CablePaths if Cable status has been changed
  134. elif instance.status != instance._orig_status:
  135. if instance.status != LinkStatusChoices.STATUS_CONNECTED:
  136. CablePath.objects.filter(_nodes__contains=instance).update(is_active=False)
  137. else:
  138. rebuild_paths([instance])
  139. @receiver(post_delete, sender=Cable)
  140. def retrace_cable_paths(instance, **kwargs):
  141. """
  142. When a Cable is deleted, check for and update its connected endpoints
  143. """
  144. for cablepath in CablePath.objects.filter(_nodes__contains=instance):
  145. cablepath.retrace()
  146. @receiver((post_delete, post_save), sender=PortMapping)
  147. def update_passthrough_port_paths(instance, **kwargs):
  148. """
  149. When a PortMapping is created or deleted, retrace any CablePaths which traverse its front and/or rear ports.
  150. """
  151. for cablepath in CablePath.objects.filter(
  152. Q(_nodes__contains=instance.front_port) | Q(_nodes__contains=instance.rear_port)
  153. ):
  154. cablepath.retrace()
  155. @receiver(post_delete, sender=CableTermination)
  156. def nullify_connected_endpoints(instance, **kwargs):
  157. """
  158. Disassociate the Cable from the termination object, and retrace any affected CablePaths.
  159. """
  160. model = instance.termination_type.model_class()
  161. model.objects.filter(pk=instance.termination_id).update(cable=None, cable_end='')
  162. for cablepath in CablePath.objects.filter(_nodes__contains=instance.cable):
  163. # Remove the deleted CableTermination if it's one of the path's originating nodes
  164. if instance.termination in cablepath.origins:
  165. cablepath.origins.remove(instance.termination)
  166. # Clear _path on the removed origin to prevent stale connection display
  167. model.objects.filter(pk=instance.termination_id, _path=cablepath.pk).update(_path=None)
  168. cablepath.retrace()
  169. @receiver(post_save, sender=Interface)
  170. @receiver(post_save, sender=VMInterface)
  171. def update_mac_address_interface(instance, created, raw, **kwargs):
  172. """
  173. When creating a new Interface or VMInterface, check whether a MACAddress has been designated as its primary. If so,
  174. assign the MACAddress to the interface.
  175. """
  176. if created and not raw and instance.primary_mac_address:
  177. instance.primary_mac_address.assigned_object = instance
  178. instance.primary_mac_address.save()
  179. @receiver(post_save, sender=Location)
  180. @receiver(post_save, sender=Site)
  181. def sync_cached_scope_fields(instance, created, **kwargs):
  182. """
  183. Rebuild cached scope fields for all CachedScopeMixin-based models
  184. affected by a change in a Region, SiteGroup, Site, or Location.
  185. This method is safe to run for objects created in the past and does
  186. not rely on incremental updates. Cached fields are recomputed from
  187. authoritative relationships.
  188. """
  189. if created:
  190. return
  191. if isinstance(instance, Location):
  192. filters = {'_location': instance}
  193. elif isinstance(instance, Site):
  194. filters = {'_site': instance}
  195. else:
  196. return
  197. # These models are explicitly listed because they all subclass CachedScopeMixin
  198. # and therefore require their cached scope fields to be recomputed.
  199. for model in (Prefix, Cluster, WirelessLAN):
  200. qs = model.objects.filter(**filters)
  201. # Bulk update cached fields to avoid O(N) performance issues with large datasets.
  202. # This does not trigger post_save signals, avoiding spurious change log entries.
  203. objects_to_update = []
  204. for obj in qs:
  205. # Recompute cache using the same logic as save()
  206. obj.cache_related_objects()
  207. objects_to_update.append(obj)
  208. if objects_to_update:
  209. model.objects.bulk_update(
  210. objects_to_update,
  211. ['_location', '_site', '_site_group', '_region']
  212. )