models.py 8.7 KB

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