models.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. from django.contrib.contenttypes.fields import GenericRelation
  2. from django.db import models
  3. from django.urls import reverse
  4. from taggit.managers import TaggableManager
  5. from dcim.constants import CONNECTION_STATUS_CHOICES
  6. from dcim.fields import ASNField
  7. from dcim.models import CableTermination
  8. from extras.models import CustomFieldModel, ObjectChange, TaggedItem
  9. from extras.utils import extras_features
  10. from utilities.models import ChangeLoggedModel
  11. from utilities.utils import serialize_object
  12. from .choices import *
  13. from .querysets import CircuitQuerySet
  14. __all__ = (
  15. 'Circuit',
  16. 'CircuitTermination',
  17. 'CircuitType',
  18. 'Provider',
  19. )
  20. @extras_features('custom_fields', 'custom_links', 'graphs', 'export_templates', 'webhooks')
  21. class Provider(ChangeLoggedModel, CustomFieldModel):
  22. """
  23. Each Circuit belongs to a Provider. This is usually a telecommunications company or similar organization. This model
  24. stores information pertinent to the user's relationship with the Provider.
  25. """
  26. name = models.CharField(
  27. max_length=50,
  28. unique=True
  29. )
  30. slug = models.SlugField(
  31. unique=True
  32. )
  33. asn = ASNField(
  34. blank=True,
  35. null=True,
  36. verbose_name='ASN',
  37. help_text='32-bit autonomous system number'
  38. )
  39. account = models.CharField(
  40. max_length=30,
  41. blank=True,
  42. verbose_name='Account number'
  43. )
  44. portal_url = models.URLField(
  45. blank=True,
  46. verbose_name='Portal URL'
  47. )
  48. noc_contact = models.TextField(
  49. blank=True,
  50. verbose_name='NOC contact'
  51. )
  52. admin_contact = models.TextField(
  53. blank=True,
  54. verbose_name='Admin contact'
  55. )
  56. comments = models.TextField(
  57. blank=True
  58. )
  59. custom_field_values = GenericRelation(
  60. to='extras.CustomFieldValue',
  61. content_type_field='obj_type',
  62. object_id_field='obj_id'
  63. )
  64. tags = TaggableManager(through=TaggedItem)
  65. csv_headers = [
  66. 'name', 'slug', 'asn', 'account', 'portal_url', 'noc_contact', 'admin_contact', 'comments',
  67. ]
  68. clone_fields = [
  69. 'asn', 'account', 'portal_url', 'noc_contact', 'admin_contact',
  70. ]
  71. class Meta:
  72. ordering = ['name']
  73. def __str__(self):
  74. return self.name
  75. def get_absolute_url(self):
  76. return reverse('circuits:provider', args=[self.slug])
  77. def to_csv(self):
  78. return (
  79. self.name,
  80. self.slug,
  81. self.asn,
  82. self.account,
  83. self.portal_url,
  84. self.noc_contact,
  85. self.admin_contact,
  86. self.comments,
  87. )
  88. class CircuitType(ChangeLoggedModel):
  89. """
  90. Circuits can be organized by their functional role. For example, a user might wish to define CircuitTypes named
  91. "Long Haul," "Metro," or "Out-of-Band".
  92. """
  93. name = models.CharField(
  94. max_length=50,
  95. unique=True
  96. )
  97. slug = models.SlugField(
  98. unique=True
  99. )
  100. description = models.CharField(
  101. max_length=200,
  102. blank=True,
  103. )
  104. csv_headers = ['name', 'slug', 'description']
  105. class Meta:
  106. ordering = ['name']
  107. def __str__(self):
  108. return self.name
  109. def get_absolute_url(self):
  110. return "{}?type={}".format(reverse('circuits:circuit_list'), self.slug)
  111. def to_csv(self):
  112. return (
  113. self.name,
  114. self.slug,
  115. self.description,
  116. )
  117. @extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
  118. class Circuit(ChangeLoggedModel, CustomFieldModel):
  119. """
  120. A communications circuit connects two points. Each Circuit belongs to a Provider; Providers may have multiple
  121. circuits. Each circuit is also assigned a CircuitType and a Site. Circuit port speed and commit rate are measured
  122. in Kbps.
  123. """
  124. cid = models.CharField(
  125. max_length=50,
  126. verbose_name='Circuit ID'
  127. )
  128. provider = models.ForeignKey(
  129. to='circuits.Provider',
  130. on_delete=models.PROTECT,
  131. related_name='circuits'
  132. )
  133. type = models.ForeignKey(
  134. to='CircuitType',
  135. on_delete=models.PROTECT,
  136. related_name='circuits'
  137. )
  138. status = models.CharField(
  139. max_length=50,
  140. choices=CircuitStatusChoices,
  141. default=CircuitStatusChoices.STATUS_ACTIVE
  142. )
  143. tenant = models.ForeignKey(
  144. to='tenancy.Tenant',
  145. on_delete=models.PROTECT,
  146. related_name='circuits',
  147. blank=True,
  148. null=True
  149. )
  150. install_date = models.DateField(
  151. blank=True,
  152. null=True,
  153. verbose_name='Date installed'
  154. )
  155. commit_rate = models.PositiveIntegerField(
  156. blank=True,
  157. null=True,
  158. verbose_name='Commit rate (Kbps)')
  159. description = models.CharField(
  160. max_length=200,
  161. blank=True
  162. )
  163. comments = models.TextField(
  164. blank=True
  165. )
  166. custom_field_values = GenericRelation(
  167. to='extras.CustomFieldValue',
  168. content_type_field='obj_type',
  169. object_id_field='obj_id'
  170. )
  171. objects = CircuitQuerySet.as_manager()
  172. tags = TaggableManager(through=TaggedItem)
  173. csv_headers = [
  174. 'cid', 'provider', 'type', 'status', 'tenant', 'install_date', 'commit_rate', 'description', 'comments',
  175. ]
  176. clone_fields = [
  177. 'provider', 'type', 'status', 'tenant', 'install_date', 'commit_rate', 'description',
  178. ]
  179. STATUS_CLASS_MAP = {
  180. CircuitStatusChoices.STATUS_DEPROVISIONING: 'warning',
  181. CircuitStatusChoices.STATUS_ACTIVE: 'success',
  182. CircuitStatusChoices.STATUS_PLANNED: 'info',
  183. CircuitStatusChoices.STATUS_PROVISIONING: 'primary',
  184. CircuitStatusChoices.STATUS_OFFLINE: 'danger',
  185. CircuitStatusChoices.STATUS_DECOMMISSIONED: 'default',
  186. }
  187. class Meta:
  188. ordering = ['provider', 'cid']
  189. unique_together = ['provider', 'cid']
  190. def __str__(self):
  191. return self.cid
  192. def get_absolute_url(self):
  193. return reverse('circuits:circuit', args=[self.pk])
  194. def to_csv(self):
  195. return (
  196. self.cid,
  197. self.provider.name,
  198. self.type.name,
  199. self.get_status_display(),
  200. self.tenant.name if self.tenant else None,
  201. self.install_date,
  202. self.commit_rate,
  203. self.description,
  204. self.comments,
  205. )
  206. def get_status_class(self):
  207. return self.STATUS_CLASS_MAP.get(self.status)
  208. def _get_termination(self, side):
  209. for ct in self.terminations.all():
  210. if ct.term_side == side:
  211. return ct
  212. return None
  213. @property
  214. def termination_a(self):
  215. return self._get_termination('A')
  216. @property
  217. def termination_z(self):
  218. return self._get_termination('Z')
  219. class CircuitTermination(CableTermination):
  220. circuit = models.ForeignKey(
  221. to='circuits.Circuit',
  222. on_delete=models.CASCADE,
  223. related_name='terminations'
  224. )
  225. term_side = models.CharField(
  226. max_length=1,
  227. choices=CircuitTerminationSideChoices,
  228. verbose_name='Termination'
  229. )
  230. site = models.ForeignKey(
  231. to='dcim.Site',
  232. on_delete=models.PROTECT,
  233. related_name='circuit_terminations'
  234. )
  235. connected_endpoint = models.OneToOneField(
  236. to='dcim.Interface',
  237. on_delete=models.SET_NULL,
  238. related_name='+',
  239. blank=True,
  240. null=True
  241. )
  242. connection_status = models.NullBooleanField(
  243. choices=CONNECTION_STATUS_CHOICES,
  244. blank=True
  245. )
  246. port_speed = models.PositiveIntegerField(
  247. verbose_name='Port speed (Kbps)'
  248. )
  249. upstream_speed = models.PositiveIntegerField(
  250. blank=True,
  251. null=True,
  252. verbose_name='Upstream speed (Kbps)',
  253. help_text='Upstream speed, if different from port speed'
  254. )
  255. xconnect_id = models.CharField(
  256. max_length=50,
  257. blank=True,
  258. verbose_name='Cross-connect ID'
  259. )
  260. pp_info = models.CharField(
  261. max_length=100,
  262. blank=True,
  263. verbose_name='Patch panel/port(s)'
  264. )
  265. description = models.CharField(
  266. max_length=200,
  267. blank=True
  268. )
  269. class Meta:
  270. ordering = ['circuit', 'term_side']
  271. unique_together = ['circuit', 'term_side']
  272. def __str__(self):
  273. return 'Side {}'.format(self.get_term_side_display())
  274. def to_objectchange(self, action):
  275. # Annotate the parent Circuit
  276. try:
  277. related_object = self.circuit
  278. except Circuit.DoesNotExist:
  279. # Parent circuit has been deleted
  280. related_object = None
  281. return ObjectChange(
  282. changed_object=self,
  283. object_repr=str(self),
  284. action=action,
  285. related_object=related_object,
  286. object_data=serialize_object(self)
  287. )
  288. @property
  289. def parent(self):
  290. return self.circuit
  291. def get_peer_termination(self):
  292. peer_side = 'Z' if self.term_side == 'A' else 'A'
  293. try:
  294. return CircuitTermination.objects.prefetch_related('site').get(circuit=self.circuit, term_side=peer_side)
  295. except CircuitTermination.DoesNotExist:
  296. return None