models.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. csv_headers = ['name', 'slug', 'asn', 'account', 'portal_url']
  47. class Meta:
  48. ordering = ['name']
  49. def __str__(self):
  50. return self.name
  51. def get_absolute_url(self):
  52. return reverse('circuits:provider', args=[self.slug])
  53. def to_csv(self):
  54. return csv_format([
  55. self.name,
  56. self.slug,
  57. self.asn,
  58. self.account,
  59. self.portal_url,
  60. ])
  61. @python_2_unicode_compatible
  62. class CircuitType(models.Model):
  63. """
  64. Circuits can be organized by their functional role. For example, a user might wish to define CircuitTypes named
  65. "Long Haul," "Metro," or "Out-of-Band".
  66. """
  67. name = models.CharField(max_length=50, unique=True)
  68. slug = models.SlugField(unique=True)
  69. class Meta:
  70. ordering = ['name']
  71. def __str__(self):
  72. return self.name
  73. def get_absolute_url(self):
  74. return "{}?type={}".format(reverse('circuits:circuit_list'), self.slug)
  75. @python_2_unicode_compatible
  76. class Circuit(CreatedUpdatedModel, CustomFieldModel):
  77. """
  78. A communications circuit connects two points. Each Circuit belongs to a Provider; Providers may have multiple
  79. circuits. Each circuit is also assigned a CircuitType and a Site. A Circuit may be terminated to a specific device
  80. interface, but this is not required. Circuit port speed and commit rate are measured in Kbps.
  81. """
  82. cid = models.CharField(max_length=50, verbose_name='Circuit ID')
  83. provider = models.ForeignKey('Provider', related_name='circuits', on_delete=models.PROTECT)
  84. type = models.ForeignKey('CircuitType', related_name='circuits', on_delete=models.PROTECT)
  85. tenant = models.ForeignKey(Tenant, related_name='circuits', blank=True, null=True, on_delete=models.PROTECT)
  86. install_date = models.DateField(blank=True, null=True, verbose_name='Date installed')
  87. commit_rate = models.PositiveIntegerField(blank=True, null=True, verbose_name='Commit rate (Kbps)')
  88. description = models.CharField(max_length=100, blank=True)
  89. comments = models.TextField(blank=True)
  90. custom_field_values = GenericRelation(CustomFieldValue, content_type_field='obj_type', object_id_field='obj_id')
  91. csv_headers = ['cid', 'provider', 'type', 'tenant', 'install_date', 'commit_rate', 'description']
  92. class Meta:
  93. ordering = ['provider', 'cid']
  94. unique_together = ['provider', 'cid']
  95. def __str__(self):
  96. return '{} {}'.format(self.provider, self.cid)
  97. def get_absolute_url(self):
  98. return reverse('circuits:circuit', args=[self.pk])
  99. def to_csv(self):
  100. return csv_format([
  101. self.cid,
  102. self.provider.name,
  103. self.type.name,
  104. self.tenant.name if self.tenant else None,
  105. self.install_date.isoformat() if self.install_date else None,
  106. self.commit_rate,
  107. self.description,
  108. ])
  109. def _get_termination(self, side):
  110. for ct in self.terminations.all():
  111. if ct.term_side == side:
  112. return ct
  113. return None
  114. @property
  115. def termination_a(self):
  116. return self._get_termination('A')
  117. @property
  118. def termination_z(self):
  119. return self._get_termination('Z')
  120. def commit_rate_human(self):
  121. return '' if not self.commit_rate else humanize_speed(self.commit_rate)
  122. commit_rate_human.admin_order_field = 'commit_rate'
  123. @python_2_unicode_compatible
  124. class CircuitTermination(models.Model):
  125. circuit = models.ForeignKey('Circuit', related_name='terminations', on_delete=models.CASCADE)
  126. term_side = models.CharField(max_length=1, choices=TERM_SIDE_CHOICES, verbose_name='Termination')
  127. site = models.ForeignKey('dcim.Site', related_name='circuit_terminations', on_delete=models.PROTECT)
  128. interface = models.OneToOneField(
  129. 'dcim.Interface', related_name='circuit_termination', blank=True, null=True, on_delete=models.PROTECT
  130. )
  131. port_speed = models.PositiveIntegerField(verbose_name='Port speed (Kbps)')
  132. upstream_speed = models.PositiveIntegerField(
  133. blank=True, null=True, verbose_name='Upstream speed (Kbps)',
  134. help_text='Upstream speed, if different from port speed'
  135. )
  136. xconnect_id = models.CharField(max_length=50, blank=True, verbose_name='Cross-connect ID')
  137. pp_info = models.CharField(max_length=100, blank=True, verbose_name='Patch panel/port(s)')
  138. class Meta:
  139. ordering = ['circuit', 'term_side']
  140. unique_together = ['circuit', 'term_side']
  141. def __str__(self):
  142. return '{} (Side {})'.format(self.circuit, self.get_term_side_display())
  143. def get_peer_termination(self):
  144. peer_side = 'Z' if self.term_side == 'A' else 'A'
  145. try:
  146. return CircuitTermination.objects.select_related('site').get(circuit=self.circuit, term_side=peer_side)
  147. except CircuitTermination.DoesNotExist:
  148. return None
  149. def port_speed_human(self):
  150. return humanize_speed(self.port_speed)
  151. port_speed_human.admin_order_field = 'port_speed'
  152. def upstream_speed_human(self):
  153. return '' if not self.upstream_speed else humanize_speed(self.upstream_speed)
  154. upstream_speed_human.admin_order_field = 'upstream_speed'