circuits.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. from django.contrib.contenttypes.fields import GenericRelation
  2. from django.core.exceptions import ValidationError
  3. from django.db import models
  4. from django.urls import reverse
  5. from django.utils.translation import gettext_lazy as _
  6. from circuits.choices import *
  7. from dcim.models import CabledObjectModel
  8. from netbox.models import (
  9. ChangeLoggedModel, CustomFieldsMixin, CustomLinksMixin, OrganizationalModel, PrimaryModel, TagsMixin,
  10. )
  11. __all__ = (
  12. 'Circuit',
  13. 'CircuitTermination',
  14. 'CircuitType',
  15. )
  16. class CircuitType(OrganizationalModel):
  17. """
  18. Circuits can be organized by their functional role. For example, a user might wish to define CircuitTypes named
  19. "Long Haul," "Metro," or "Out-of-Band".
  20. """
  21. def get_absolute_url(self):
  22. return reverse('circuits:circuittype', args=[self.pk])
  23. class Circuit(PrimaryModel):
  24. """
  25. A communications circuit connects two points. Each Circuit belongs to a Provider; Providers may have multiple
  26. circuits. Each circuit is also assigned a CircuitType and a Site, and may optionally be assigned to a particular
  27. ProviderAccount. Circuit port speed and commit rate are measured in Kbps.
  28. """
  29. cid = models.CharField(
  30. max_length=100,
  31. verbose_name=_('circuit ID'),
  32. help_text=_('Unique circuit ID')
  33. )
  34. provider = models.ForeignKey(
  35. to='circuits.Provider',
  36. on_delete=models.PROTECT,
  37. related_name='circuits'
  38. )
  39. provider_account = models.ForeignKey(
  40. to='circuits.ProviderAccount',
  41. on_delete=models.PROTECT,
  42. related_name='circuits',
  43. blank=True,
  44. null=True
  45. )
  46. type = models.ForeignKey(
  47. to='CircuitType',
  48. on_delete=models.PROTECT,
  49. related_name='circuits'
  50. )
  51. status = models.CharField(
  52. verbose_name=_('status'),
  53. max_length=50,
  54. choices=CircuitStatusChoices,
  55. default=CircuitStatusChoices.STATUS_ACTIVE
  56. )
  57. tenant = models.ForeignKey(
  58. to='tenancy.Tenant',
  59. on_delete=models.PROTECT,
  60. related_name='circuits',
  61. blank=True,
  62. null=True
  63. )
  64. install_date = models.DateField(
  65. blank=True,
  66. null=True,
  67. verbose_name=_('installed')
  68. )
  69. termination_date = models.DateField(
  70. blank=True,
  71. null=True,
  72. verbose_name=_('terminates')
  73. )
  74. commit_rate = models.PositiveIntegerField(
  75. blank=True,
  76. null=True,
  77. verbose_name=_('commit rate (Kbps)'),
  78. help_text=_("Committed rate")
  79. )
  80. # Generic relations
  81. contacts = GenericRelation(
  82. to='tenancy.ContactAssignment'
  83. )
  84. images = GenericRelation(
  85. to='extras.ImageAttachment'
  86. )
  87. # Cache associated CircuitTerminations
  88. termination_a = models.ForeignKey(
  89. to='circuits.CircuitTermination',
  90. on_delete=models.SET_NULL,
  91. related_name='+',
  92. editable=False,
  93. blank=True,
  94. null=True
  95. )
  96. termination_z = models.ForeignKey(
  97. to='circuits.CircuitTermination',
  98. on_delete=models.SET_NULL,
  99. related_name='+',
  100. editable=False,
  101. blank=True,
  102. null=True
  103. )
  104. clone_fields = (
  105. 'provider', 'provider_account', 'type', 'status', 'tenant', 'install_date', 'termination_date', 'commit_rate',
  106. 'description',
  107. )
  108. prerequisite_models = (
  109. 'circuits.CircuitType',
  110. 'circuits.Provider',
  111. )
  112. class Meta:
  113. ordering = ['provider', 'provider_account', 'cid']
  114. constraints = (
  115. models.UniqueConstraint(
  116. fields=('provider', 'cid'),
  117. name='%(app_label)s_%(class)s_unique_provider_cid'
  118. ),
  119. models.UniqueConstraint(
  120. fields=('provider_account', 'cid'),
  121. name='%(app_label)s_%(class)s_unique_provideraccount_cid'
  122. ),
  123. )
  124. def __str__(self):
  125. return self.cid
  126. def get_absolute_url(self):
  127. return reverse('circuits:circuit', args=[self.pk])
  128. def get_status_color(self):
  129. return CircuitStatusChoices.colors.get(self.status)
  130. def clean(self):
  131. super().clean()
  132. if self.provider_account and self.provider != self.provider_account.provider:
  133. raise ValidationError({'provider_account': "The assigned account must belong to the assigned provider."})
  134. class CircuitTermination(
  135. CustomFieldsMixin,
  136. CustomLinksMixin,
  137. TagsMixin,
  138. ChangeLoggedModel,
  139. CabledObjectModel
  140. ):
  141. circuit = models.ForeignKey(
  142. to='circuits.Circuit',
  143. on_delete=models.CASCADE,
  144. related_name='terminations'
  145. )
  146. term_side = models.CharField(
  147. max_length=1,
  148. choices=CircuitTerminationSideChoices,
  149. verbose_name=_('termination')
  150. )
  151. site = models.ForeignKey(
  152. to='dcim.Site',
  153. on_delete=models.PROTECT,
  154. related_name='circuit_terminations',
  155. blank=True,
  156. null=True
  157. )
  158. provider_network = models.ForeignKey(
  159. to='circuits.ProviderNetwork',
  160. on_delete=models.PROTECT,
  161. related_name='circuit_terminations',
  162. blank=True,
  163. null=True
  164. )
  165. port_speed = models.PositiveIntegerField(
  166. verbose_name=_('port speed (Kbps)'),
  167. blank=True,
  168. null=True,
  169. help_text=_('Physical circuit speed')
  170. )
  171. upstream_speed = models.PositiveIntegerField(
  172. blank=True,
  173. null=True,
  174. verbose_name=_('upstream speed (Kbps)'),
  175. help_text=_('Upstream speed, if different from port speed')
  176. )
  177. xconnect_id = models.CharField(
  178. max_length=50,
  179. blank=True,
  180. verbose_name=_('cross-connect ID'),
  181. help_text=_('ID of the local cross-connect')
  182. )
  183. pp_info = models.CharField(
  184. max_length=100,
  185. blank=True,
  186. verbose_name=_('patch panel/port(s)'),
  187. help_text=_('Patch panel ID and port number(s)')
  188. )
  189. description = models.CharField(
  190. verbose_name=_('description'),
  191. max_length=200,
  192. blank=True
  193. )
  194. class Meta:
  195. ordering = ['circuit', 'term_side']
  196. constraints = (
  197. models.UniqueConstraint(
  198. fields=('circuit', 'term_side'),
  199. name='%(app_label)s_%(class)s_unique_circuit_term_side'
  200. ),
  201. )
  202. def __str__(self):
  203. return f'{self.circuit}: Termination {self.term_side}'
  204. def get_absolute_url(self):
  205. return self.circuit.get_absolute_url()
  206. def clean(self):
  207. super().clean()
  208. # Must define either site *or* provider network
  209. if self.site is None and self.provider_network is None:
  210. raise ValidationError("A circuit termination must attach to either a site or a provider network.")
  211. if self.site and self.provider_network:
  212. raise ValidationError("A circuit termination cannot attach to both a site and a provider network.")
  213. def to_objectchange(self, action):
  214. objectchange = super().to_objectchange(action)
  215. objectchange.related_object = self.circuit
  216. return objectchange
  217. @property
  218. def parent_object(self):
  219. return self.circuit
  220. def get_peer_termination(self):
  221. peer_side = 'Z' if self.term_side == 'A' else 'A'
  222. try:
  223. return CircuitTermination.objects.prefetch_related('site').get(
  224. circuit=self.circuit,
  225. term_side=peer_side
  226. )
  227. except CircuitTermination.DoesNotExist:
  228. return None