models.py 6.9 KB

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