models.py 7.9 KB

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