models.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. from __future__ import unicode_literals
  2. from django.contrib.contenttypes.fields import GenericRelation
  3. from django.db import models
  4. from django.urls import reverse
  5. from django.utils.encoding import python_2_unicode_compatible
  6. from dcim.constants import STATUS_CLASSES
  7. from dcim.fields import ASNField
  8. from extras.models import CustomFieldModel, CustomFieldValue
  9. from tenancy.models import Tenant
  10. from utilities.models import CreatedUpdatedModel
  11. from .constants import CIRCUIT_STATUS_ACTIVE, CIRCUIT_STATUS_CHOICES, TERM_SIDE_CHOICES
  12. @python_2_unicode_compatible
  13. class Provider(CreatedUpdatedModel, CustomFieldModel):
  14. """
  15. Each Circuit belongs to a Provider. This is usually a telecommunications company or similar organization. This model
  16. stores information pertinent to the user's relationship with the Provider.
  17. """
  18. name = models.CharField(max_length=50, unique=True)
  19. slug = models.SlugField(unique=True)
  20. asn = ASNField(blank=True, null=True, verbose_name='ASN')
  21. account = models.CharField(max_length=30, blank=True, verbose_name='Account number')
  22. portal_url = models.URLField(blank=True, verbose_name='Portal')
  23. noc_contact = models.TextField(blank=True, verbose_name='NOC contact')
  24. admin_contact = models.TextField(blank=True, verbose_name='Admin contact')
  25. comments = models.TextField(blank=True)
  26. custom_field_values = GenericRelation(CustomFieldValue, content_type_field='obj_type', object_id_field='obj_id')
  27. csv_headers = ['name', 'slug', 'asn', 'account', 'portal_url', 'noc_contact', 'admin_contact', 'comments']
  28. class Meta:
  29. ordering = ['name']
  30. def __str__(self):
  31. return self.name
  32. def get_absolute_url(self):
  33. return reverse('circuits:provider', args=[self.slug])
  34. def to_csv(self):
  35. return (
  36. self.name,
  37. self.slug,
  38. self.asn,
  39. self.account,
  40. self.portal_url,
  41. self.noc_contact,
  42. self.admin_contact,
  43. self.comments,
  44. )
  45. @python_2_unicode_compatible
  46. class CircuitType(models.Model):
  47. """
  48. Circuits can be organized by their functional role. For example, a user might wish to define CircuitTypes named
  49. "Long Haul," "Metro," or "Out-of-Band".
  50. """
  51. name = models.CharField(max_length=50, unique=True)
  52. slug = models.SlugField(unique=True)
  53. csv_headers = ['name', 'slug']
  54. class Meta:
  55. ordering = ['name']
  56. def __str__(self):
  57. return self.name
  58. def get_absolute_url(self):
  59. return "{}?type={}".format(reverse('circuits:circuit_list'), self.slug)
  60. def to_csv(self):
  61. return (
  62. self.name,
  63. self.slug,
  64. )
  65. @python_2_unicode_compatible
  66. class Circuit(CreatedUpdatedModel, CustomFieldModel):
  67. """
  68. A communications circuit connects two points. Each Circuit belongs to a Provider; Providers may have multiple
  69. circuits. Each circuit is also assigned a CircuitType and a Site. A Circuit may be terminated to a specific device
  70. interface, but this is not required. Circuit port speed and commit rate are measured in Kbps.
  71. """
  72. cid = models.CharField(max_length=50, verbose_name='Circuit ID')
  73. provider = models.ForeignKey('Provider', related_name='circuits', on_delete=models.PROTECT)
  74. type = models.ForeignKey('CircuitType', related_name='circuits', on_delete=models.PROTECT)
  75. status = models.PositiveSmallIntegerField(choices=CIRCUIT_STATUS_CHOICES, default=CIRCUIT_STATUS_ACTIVE)
  76. tenant = models.ForeignKey(Tenant, related_name='circuits', blank=True, null=True, on_delete=models.PROTECT)
  77. install_date = models.DateField(blank=True, null=True, verbose_name='Date installed')
  78. commit_rate = models.PositiveIntegerField(blank=True, null=True, verbose_name='Commit rate (Kbps)')
  79. description = models.CharField(max_length=100, blank=True)
  80. comments = models.TextField(blank=True)
  81. custom_field_values = GenericRelation(CustomFieldValue, content_type_field='obj_type', object_id_field='obj_id')
  82. csv_headers = [
  83. 'cid', 'provider', 'type', 'status', 'tenant', 'install_date', 'commit_rate', 'description', 'comments',
  84. ]
  85. class Meta:
  86. ordering = ['provider', 'cid']
  87. unique_together = ['provider', 'cid']
  88. def __str__(self):
  89. return '{} {}'.format(self.provider, self.cid)
  90. def get_absolute_url(self):
  91. return reverse('circuits:circuit', args=[self.pk])
  92. def to_csv(self):
  93. return (
  94. self.cid,
  95. self.provider.name,
  96. self.type.name,
  97. self.get_status_display(),
  98. self.tenant.name if self.tenant else None,
  99. self.install_date,
  100. self.commit_rate,
  101. self.description,
  102. self.comments,
  103. )
  104. def get_status_class(self):
  105. return STATUS_CLASSES[self.status]
  106. def _get_termination(self, side):
  107. for ct in self.terminations.all():
  108. if ct.term_side == side:
  109. return ct
  110. return None
  111. @property
  112. def termination_a(self):
  113. return self._get_termination('A')
  114. @property
  115. def termination_z(self):
  116. return self._get_termination('Z')
  117. @python_2_unicode_compatible
  118. class CircuitTermination(models.Model):
  119. circuit = models.ForeignKey('Circuit', related_name='terminations', on_delete=models.CASCADE)
  120. term_side = models.CharField(max_length=1, choices=TERM_SIDE_CHOICES, verbose_name='Termination')
  121. site = models.ForeignKey('dcim.Site', related_name='circuit_terminations', on_delete=models.PROTECT)
  122. interface = models.OneToOneField(
  123. 'dcim.Interface', related_name='circuit_termination', blank=True, null=True, on_delete=models.PROTECT
  124. )
  125. port_speed = models.PositiveIntegerField(verbose_name='Port speed (Kbps)')
  126. upstream_speed = models.PositiveIntegerField(
  127. blank=True, null=True, verbose_name='Upstream speed (Kbps)',
  128. help_text='Upstream speed, if different from port speed'
  129. )
  130. xconnect_id = models.CharField(max_length=50, blank=True, verbose_name='Cross-connect ID')
  131. pp_info = models.CharField(max_length=100, blank=True, verbose_name='Patch panel/port(s)')
  132. class Meta:
  133. ordering = ['circuit', 'term_side']
  134. unique_together = ['circuit', 'term_side']
  135. def __str__(self):
  136. return '{} (Side {})'.format(self.circuit, self.get_term_side_display())
  137. def get_peer_termination(self):
  138. peer_side = 'Z' if self.term_side == 'A' else 'A'
  139. try:
  140. return CircuitTermination.objects.select_related('site').get(circuit=self.circuit, term_side=peer_side)
  141. except CircuitTermination.DoesNotExist:
  142. return None