123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- from __future__ import unicode_literals
- from django.contrib.contenttypes.fields import GenericRelation
- from django.db import models
- from django.urls import reverse
- from django.utils.encoding import python_2_unicode_compatible
- from dcim.constants import STATUS_CLASSES
- from dcim.fields import ASNField
- from extras.models import CustomFieldModel, CustomFieldValue
- from tenancy.models import Tenant
- from utilities.models import CreatedUpdatedModel
- from .constants import CIRCUIT_STATUS_ACTIVE, CIRCUIT_STATUS_CHOICES, TERM_SIDE_CHOICES
- @python_2_unicode_compatible
- class Provider(CreatedUpdatedModel, CustomFieldModel):
- """
- Each Circuit belongs to a Provider. This is usually a telecommunications company or similar organization. This model
- stores information pertinent to the user's relationship with the Provider.
- """
- name = models.CharField(max_length=50, unique=True)
- slug = models.SlugField(unique=True)
- asn = ASNField(blank=True, null=True, verbose_name='ASN')
- account = models.CharField(max_length=30, blank=True, verbose_name='Account number')
- portal_url = models.URLField(blank=True, verbose_name='Portal')
- noc_contact = models.TextField(blank=True, verbose_name='NOC contact')
- admin_contact = models.TextField(blank=True, verbose_name='Admin contact')
- comments = models.TextField(blank=True)
- custom_field_values = GenericRelation(CustomFieldValue, content_type_field='obj_type', object_id_field='obj_id')
- csv_headers = ['name', 'slug', 'asn', 'account', 'portal_url', 'noc_contact', 'admin_contact', 'comments']
- class Meta:
- ordering = ['name']
- def __str__(self):
- return self.name
- def get_absolute_url(self):
- return reverse('circuits:provider', args=[self.slug])
- def to_csv(self):
- return (
- self.name,
- self.slug,
- self.asn,
- self.account,
- self.portal_url,
- self.noc_contact,
- self.admin_contact,
- self.comments,
- )
- @python_2_unicode_compatible
- class CircuitType(models.Model):
- """
- Circuits can be organized by their functional role. For example, a user might wish to define CircuitTypes named
- "Long Haul," "Metro," or "Out-of-Band".
- """
- name = models.CharField(max_length=50, unique=True)
- slug = models.SlugField(unique=True)
- csv_headers = ['name', 'slug']
- class Meta:
- ordering = ['name']
- def __str__(self):
- return self.name
- def get_absolute_url(self):
- return "{}?type={}".format(reverse('circuits:circuit_list'), self.slug)
- def to_csv(self):
- return (
- self.name,
- self.slug,
- )
- @python_2_unicode_compatible
- class Circuit(CreatedUpdatedModel, CustomFieldModel):
- """
- A communications circuit connects two points. Each Circuit belongs to a Provider; Providers may have multiple
- circuits. Each circuit is also assigned a CircuitType and a Site. A Circuit may be terminated to a specific device
- interface, but this is not required. Circuit port speed and commit rate are measured in Kbps.
- """
- cid = models.CharField(max_length=50, verbose_name='Circuit ID')
- provider = models.ForeignKey('Provider', related_name='circuits', on_delete=models.PROTECT)
- type = models.ForeignKey('CircuitType', related_name='circuits', on_delete=models.PROTECT)
- status = models.PositiveSmallIntegerField(choices=CIRCUIT_STATUS_CHOICES, default=CIRCUIT_STATUS_ACTIVE)
- tenant = models.ForeignKey(Tenant, related_name='circuits', blank=True, null=True, on_delete=models.PROTECT)
- install_date = models.DateField(blank=True, null=True, verbose_name='Date installed')
- commit_rate = models.PositiveIntegerField(blank=True, null=True, verbose_name='Commit rate (Kbps)')
- description = models.CharField(max_length=100, blank=True)
- comments = models.TextField(blank=True)
- custom_field_values = GenericRelation(CustomFieldValue, content_type_field='obj_type', object_id_field='obj_id')
- csv_headers = [
- 'cid', 'provider', 'type', 'status', 'tenant', 'install_date', 'commit_rate', 'description', 'comments',
- ]
- class Meta:
- ordering = ['provider', 'cid']
- unique_together = ['provider', 'cid']
- def __str__(self):
- return '{} {}'.format(self.provider, self.cid)
- def get_absolute_url(self):
- return reverse('circuits:circuit', args=[self.pk])
- def to_csv(self):
- return (
- self.cid,
- self.provider.name,
- self.type.name,
- self.get_status_display(),
- self.tenant.name if self.tenant else None,
- self.install_date,
- self.commit_rate,
- self.description,
- self.comments,
- )
- def get_status_class(self):
- return STATUS_CLASSES[self.status]
- def _get_termination(self, side):
- for ct in self.terminations.all():
- if ct.term_side == side:
- return ct
- return None
- @property
- def termination_a(self):
- return self._get_termination('A')
- @property
- def termination_z(self):
- return self._get_termination('Z')
- @python_2_unicode_compatible
- class CircuitTermination(models.Model):
- circuit = models.ForeignKey('Circuit', related_name='terminations', on_delete=models.CASCADE)
- term_side = models.CharField(max_length=1, choices=TERM_SIDE_CHOICES, verbose_name='Termination')
- site = models.ForeignKey('dcim.Site', related_name='circuit_terminations', on_delete=models.PROTECT)
- interface = models.OneToOneField(
- 'dcim.Interface', related_name='circuit_termination', blank=True, null=True, on_delete=models.PROTECT
- )
- port_speed = models.PositiveIntegerField(verbose_name='Port speed (Kbps)')
- upstream_speed = models.PositiveIntegerField(
- blank=True, null=True, verbose_name='Upstream speed (Kbps)',
- help_text='Upstream speed, if different from port speed'
- )
- xconnect_id = models.CharField(max_length=50, blank=True, verbose_name='Cross-connect ID')
- pp_info = models.CharField(max_length=100, blank=True, verbose_name='Patch panel/port(s)')
- class Meta:
- ordering = ['circuit', 'term_side']
- unique_together = ['circuit', 'term_side']
- def __str__(self):
- return '{} (Side {})'.format(self.circuit, self.get_term_side_display())
- def get_peer_termination(self):
- peer_side = 'Z' if self.term_side == 'A' else 'A'
- try:
- return CircuitTermination.objects.select_related('site').get(circuit=self.circuit, term_side=peer_side)
- except CircuitTermination.DoesNotExist:
- return None
|