models.py 6.9 KB

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