models.py 9.3 KB

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