models.py 8.8 KB

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