models.py 9.2 KB

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