2016-08-22 13:20:30 -04:00
|
|
|
from django.contrib.contenttypes.fields import GenericRelation
|
2016-03-01 11:23:03 -05:00
|
|
|
from django.db import models
|
2017-04-05 14:40:25 -04:00
|
|
|
from django.urls import reverse
|
2018-05-10 12:53:11 -04:00
|
|
|
from taggit.managers import TaggableManager
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2019-11-21 21:28:59 -05:00
|
|
|
from dcim.constants import CONNECTION_STATUS_CHOICES
|
2016-07-13 15:30:15 -04:00
|
|
|
from dcim.fields import ASNField
|
2018-10-30 14:27:45 -04:00
|
|
|
from dcim.models import CableTermination
|
2019-02-20 03:52:47 -05:00
|
|
|
from extras.models import CustomFieldModel, ObjectChange, TaggedItem
|
2018-06-13 15:40:16 -04:00
|
|
|
from utilities.models import ChangeLoggedModel
|
2018-07-12 13:46:30 -04:00
|
|
|
from utilities.utils import serialize_object
|
2019-11-07 11:10:46 -05:00
|
|
|
from .choices import *
|
2016-12-14 13:47:22 -05:00
|
|
|
|
|
|
|
|
2020-01-14 12:01:23 -05:00
|
|
|
__all__ = (
|
|
|
|
'Circuit',
|
|
|
|
'CircuitTermination',
|
|
|
|
'CircuitType',
|
|
|
|
'Provider',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-06-13 15:40:16 -04:00
|
|
|
class Provider(ChangeLoggedModel, CustomFieldModel):
|
2016-03-01 11:23:03 -05:00
|
|
|
"""
|
2016-06-21 12:45:02 -04:00
|
|
|
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.
|
2016-03-01 11:23:03 -05:00
|
|
|
"""
|
2018-03-30 13:57:26 -04:00
|
|
|
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(
|
|
|
|
to='extras.CustomFieldValue',
|
|
|
|
content_type_field='obj_type',
|
|
|
|
object_id_field='obj_id'
|
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2019-02-20 03:52:47 -05:00
|
|
|
tags = TaggableManager(through=TaggedItem)
|
2018-05-10 12:53:11 -04:00
|
|
|
|
2019-12-06 16:13:52 -05:00
|
|
|
csv_headers = [
|
|
|
|
'name', 'slug', 'asn', 'account', 'portal_url', 'noc_contact', 'admin_contact', 'comments',
|
|
|
|
]
|
|
|
|
clone_fields = [
|
|
|
|
'asn', 'account', 'portal_url', 'noc_contact', 'admin_contact',
|
|
|
|
]
|
2018-05-30 11:19:10 -04:00
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
class Meta:
|
|
|
|
ordering = ['name']
|
|
|
|
|
2017-01-23 22:44:29 +01:00
|
|
|
def __str__(self):
|
2016-03-01 11:23:03 -05:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('circuits:provider', args=[self.slug])
|
|
|
|
|
2016-06-16 13:57:43 -04:00
|
|
|
def to_csv(self):
|
2018-02-02 11:34:31 -05:00
|
|
|
return (
|
2016-06-16 13:57:43 -04:00
|
|
|
self.name,
|
|
|
|
self.slug,
|
2017-01-04 10:47:00 -05:00
|
|
|
self.asn,
|
2016-06-16 13:57:43 -04:00
|
|
|
self.account,
|
|
|
|
self.portal_url,
|
2018-02-02 14:26:16 -05:00
|
|
|
self.noc_contact,
|
|
|
|
self.admin_contact,
|
|
|
|
self.comments,
|
2018-02-02 11:34:31 -05:00
|
|
|
)
|
2016-06-16 13:57:43 -04:00
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2018-06-13 15:40:16 -04:00
|
|
|
class CircuitType(ChangeLoggedModel):
|
2016-03-01 11:23:03 -05:00
|
|
|
"""
|
2017-01-04 10:47:00 -05:00
|
|
|
Circuits can be organized by their functional role. For example, a user might wish to define CircuitTypes named
|
2016-06-21 12:45:02 -04:00
|
|
|
"Long Haul," "Metro," or "Out-of-Band".
|
2016-03-01 11:23:03 -05:00
|
|
|
"""
|
2018-03-30 13:57:26 -04:00
|
|
|
name = models.CharField(
|
|
|
|
max_length=50,
|
|
|
|
unique=True
|
|
|
|
)
|
|
|
|
slug = models.SlugField(
|
|
|
|
unique=True
|
|
|
|
)
|
2019-12-10 13:24:02 -05:00
|
|
|
description = models.CharField(
|
|
|
|
max_length=100,
|
|
|
|
blank=True,
|
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2019-12-10 13:24:02 -05:00
|
|
|
csv_headers = ['name', 'slug', 'description']
|
2018-02-02 14:26:16 -05:00
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
class Meta:
|
|
|
|
ordering = ['name']
|
|
|
|
|
2017-01-23 22:44:29 +01:00
|
|
|
def __str__(self):
|
2016-03-01 11:23:03 -05:00
|
|
|
return self.name
|
|
|
|
|
2016-05-13 12:44:03 -04:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return "{}?type={}".format(reverse('circuits:circuit_list'), self.slug)
|
|
|
|
|
2018-02-02 14:26:16 -05:00
|
|
|
def to_csv(self):
|
|
|
|
return (
|
|
|
|
self.name,
|
|
|
|
self.slug,
|
2019-12-10 13:24:02 -05:00
|
|
|
self.description,
|
2018-02-02 14:26:16 -05:00
|
|
|
)
|
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2018-06-13 15:40:16 -04:00
|
|
|
class Circuit(ChangeLoggedModel, CustomFieldModel):
|
2016-03-01 11:23:03 -05:00
|
|
|
"""
|
2016-06-21 12:45:02 -04:00
|
|
|
A communications circuit connects two points. Each Circuit belongs to a Provider; Providers may have multiple
|
2018-10-30 12:16:22 -04:00
|
|
|
circuits. Each circuit is also assigned a CircuitType and a Site. Circuit port speed and commit rate are measured
|
|
|
|
in Kbps.
|
2016-03-01 11:23:03 -05:00
|
|
|
"""
|
2018-03-30 13:57:26 -04:00
|
|
|
cid = models.CharField(
|
|
|
|
max_length=50,
|
|
|
|
verbose_name='Circuit ID'
|
|
|
|
)
|
|
|
|
provider = models.ForeignKey(
|
|
|
|
to='circuits.Provider',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name='circuits'
|
|
|
|
)
|
|
|
|
type = models.ForeignKey(
|
|
|
|
to='CircuitType',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name='circuits'
|
|
|
|
)
|
2019-11-07 11:10:46 -05:00
|
|
|
status = models.CharField(
|
|
|
|
max_length=50,
|
|
|
|
choices=CircuitStatusChoices,
|
|
|
|
default=CircuitStatusChoices.STATUS_ACTIVE
|
2018-03-30 13:57:26 -04:00
|
|
|
)
|
|
|
|
tenant = models.ForeignKey(
|
|
|
|
to='tenancy.Tenant',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name='circuits',
|
|
|
|
blank=True,
|
|
|
|
null=True
|
|
|
|
)
|
|
|
|
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(
|
|
|
|
to='extras.CustomFieldValue',
|
|
|
|
content_type_field='obj_type',
|
|
|
|
object_id_field='obj_id'
|
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2019-02-20 03:52:47 -05:00
|
|
|
tags = TaggableManager(through=TaggedItem)
|
2018-05-10 12:53:11 -04:00
|
|
|
|
2018-02-06 14:58:11 -05:00
|
|
|
csv_headers = [
|
|
|
|
'cid', 'provider', 'type', 'status', 'tenant', 'install_date', 'commit_rate', 'description', 'comments',
|
|
|
|
]
|
2019-12-06 16:13:52 -05:00
|
|
|
clone_fields = [
|
|
|
|
'provider', 'type', 'status', 'tenant', 'install_date', 'commit_rate', 'description',
|
|
|
|
]
|
2017-06-09 16:24:59 -04:00
|
|
|
|
2019-11-07 11:10:46 -05:00
|
|
|
STATUS_CLASS_MAP = {
|
|
|
|
CircuitStatusChoices.STATUS_DEPROVISIONING: 'warning',
|
|
|
|
CircuitStatusChoices.STATUS_ACTIVE: 'success',
|
|
|
|
CircuitStatusChoices.STATUS_PLANNED: 'info',
|
|
|
|
CircuitStatusChoices.STATUS_PROVISIONING: 'primary',
|
|
|
|
CircuitStatusChoices.STATUS_OFFLINE: 'danger',
|
|
|
|
CircuitStatusChoices.STATUS_DECOMMISSIONED: 'default',
|
|
|
|
}
|
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
class Meta:
|
|
|
|
ordering = ['provider', 'cid']
|
|
|
|
unique_together = ['provider', 'cid']
|
|
|
|
|
2017-01-23 22:44:29 +01:00
|
|
|
def __str__(self):
|
2018-12-11 11:15:45 -05:00
|
|
|
return self.cid
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('circuits:circuit', args=[self.pk])
|
2016-06-16 13:57:43 -04:00
|
|
|
|
|
|
|
def to_csv(self):
|
2018-02-02 11:34:31 -05:00
|
|
|
return (
|
2016-06-16 13:57:43 -04:00
|
|
|
self.cid,
|
|
|
|
self.provider.name,
|
|
|
|
self.type.name,
|
2018-02-06 14:06:05 -05:00
|
|
|
self.get_status_display(),
|
2017-01-04 10:47:00 -05:00
|
|
|
self.tenant.name if self.tenant else None,
|
2018-02-02 11:34:31 -05:00
|
|
|
self.install_date,
|
2017-01-04 10:47:00 -05:00
|
|
|
self.commit_rate,
|
2017-01-17 15:18:03 -05:00
|
|
|
self.description,
|
2018-02-02 14:26:16 -05:00
|
|
|
self.comments,
|
2018-02-02 11:34:31 -05:00
|
|
|
)
|
2016-06-20 15:58:18 -04:00
|
|
|
|
2018-02-06 14:06:05 -05:00
|
|
|
def get_status_class(self):
|
2019-11-07 11:10:46 -05:00
|
|
|
return self.STATUS_CLASS_MAP.get(self.status)
|
2018-02-06 14:06:05 -05:00
|
|
|
|
2016-12-14 13:47:22 -05:00
|
|
|
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')
|
|
|
|
|
|
|
|
|
2018-10-30 14:27:45 -04:00
|
|
|
class CircuitTermination(CableTermination):
|
2018-03-30 13:57:26 -04:00
|
|
|
circuit = models.ForeignKey(
|
|
|
|
to='circuits.Circuit',
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name='terminations'
|
|
|
|
)
|
|
|
|
term_side = models.CharField(
|
|
|
|
max_length=1,
|
2019-11-21 21:28:59 -05:00
|
|
|
choices=CircuitTerminationSideChoices,
|
2018-03-30 13:57:26 -04:00
|
|
|
verbose_name='Termination'
|
|
|
|
)
|
|
|
|
site = models.ForeignKey(
|
|
|
|
to='dcim.Site',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name='circuit_terminations'
|
|
|
|
)
|
2018-10-30 12:16:22 -04:00
|
|
|
connected_endpoint = models.OneToOneField(
|
2018-03-30 13:57:26 -04:00
|
|
|
to='dcim.Interface',
|
2018-10-30 12:16:22 -04:00
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
related_name='+',
|
2018-03-30 13:57:26 -04:00
|
|
|
blank=True,
|
|
|
|
null=True
|
|
|
|
)
|
2018-10-30 12:16:22 -04:00
|
|
|
connection_status = models.NullBooleanField(
|
|
|
|
choices=CONNECTION_STATUS_CHOICES,
|
2018-11-19 12:37:53 -05:00
|
|
|
blank=True
|
2018-10-30 12:16:22 -04:00
|
|
|
)
|
2018-03-30 13:57:26 -04:00
|
|
|
port_speed = models.PositiveIntegerField(
|
|
|
|
verbose_name='Port speed (Kbps)'
|
2017-04-05 14:40:25 -04:00
|
|
|
)
|
2017-04-19 13:19:30 -04:00
|
|
|
upstream_speed = models.PositiveIntegerField(
|
2018-03-30 13:57:26 -04:00
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
verbose_name='Upstream speed (Kbps)',
|
2017-04-19 13:19:30 -04:00
|
|
|
help_text='Upstream speed, if different from port speed'
|
|
|
|
)
|
2018-03-30 13:57:26 -04:00
|
|
|
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)'
|
|
|
|
)
|
2018-11-05 13:53:22 -05:00
|
|
|
description = models.CharField(
|
|
|
|
max_length=100,
|
|
|
|
blank=True
|
|
|
|
)
|
2016-12-14 13:47:22 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['circuit', 'term_side']
|
|
|
|
unique_together = ['circuit', 'term_side']
|
|
|
|
|
2017-01-23 22:44:29 +01:00
|
|
|
def __str__(self):
|
2018-11-01 14:14:31 -04:00
|
|
|
return 'Side {}'.format(self.get_term_side_display())
|
2016-12-14 13:47:22 -05:00
|
|
|
|
2019-08-26 16:52:05 -04:00
|
|
|
def to_objectchange(self, action):
|
|
|
|
# Annotate the parent Circuit
|
2019-05-29 17:17:06 -04:00
|
|
|
try:
|
|
|
|
related_object = self.circuit
|
|
|
|
except Circuit.DoesNotExist:
|
|
|
|
# Parent circuit has been deleted
|
|
|
|
related_object = None
|
2019-08-26 16:52:05 -04:00
|
|
|
|
|
|
|
return ObjectChange(
|
2018-07-12 13:46:30 -04:00
|
|
|
changed_object=self,
|
2019-08-26 16:52:05 -04:00
|
|
|
object_repr=str(self),
|
2018-07-12 13:46:30 -04:00
|
|
|
action=action,
|
2019-08-26 16:52:05 -04:00
|
|
|
related_object=related_object,
|
2018-07-12 13:46:30 -04:00
|
|
|
object_data=serialize_object(self)
|
2019-08-26 16:52:05 -04:00
|
|
|
)
|
2018-07-12 13:46:30 -04:00
|
|
|
|
2019-01-31 12:21:43 -05:00
|
|
|
@property
|
|
|
|
def parent(self):
|
|
|
|
return self.circuit
|
|
|
|
|
2016-12-14 13:47:22 -05:00
|
|
|
def get_peer_termination(self):
|
|
|
|
peer_side = 'Z' if self.term_side == 'A' else 'A'
|
|
|
|
try:
|
2019-08-19 01:53:39 -04:00
|
|
|
return CircuitTermination.objects.prefetch_related('site').get(circuit=self.circuit, term_side=peer_side)
|
2016-12-14 13:47:22 -05:00
|
|
|
except CircuitTermination.DoesNotExist:
|
|
|
|
return None
|