1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

167 lines
6.2 KiB
Python
Raw Normal View History

2017-05-24 11:33:11 -04:00
from __future__ import unicode_literals
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
from django.urls import reverse
from django.utils.encoding import python_2_unicode_compatible
2016-03-01 11:23:03 -05:00
from dcim.fields import ASNField
2016-08-22 13:20:30 -04:00
from extras.models import CustomFieldModel, CustomFieldValue
2016-07-26 15:42:26 -04:00
from tenancy.models import Tenant
from utilities.models import CreatedUpdatedModel
from .constants import *
@python_2_unicode_compatible
2016-08-15 15:24:23 -04:00
class Provider(CreatedUpdatedModel, 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
"""
name = models.CharField(max_length=50, unique=True)
slug = models.SlugField(unique=True)
asn = ASNField(blank=True, null=True, verbose_name='ASN')
2016-03-01 11:23:03 -05:00
account = models.CharField(max_length=30, blank=True, verbose_name='Account number')
portal_url = models.URLField(blank=True, verbose_name='Portal')
2016-06-21 12:45:02 -04:00
noc_contact = models.TextField(blank=True, verbose_name='NOC contact')
admin_contact = models.TextField(blank=True, verbose_name='Admin contact')
2016-03-01 11:23:03 -05:00
comments = models.TextField(blank=True)
2016-08-22 13:20:30 -04:00
custom_field_values = GenericRelation(CustomFieldValue, content_type_field='obj_type', object_id_field='obj_id')
2016-03-01 11:23:03 -05:00
csv_headers = ['name', 'slug', 'asn', 'account', 'portal_url', 'noc_contact', 'admin_contact', 'comments']
2016-03-01 11:23:03 -05:00
class Meta:
ordering = ['name']
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])
def to_csv(self):
2018-02-02 11:34:31 -05:00
return (
self.name,
self.slug,
self.asn,
self.account,
self.portal_url,
self.noc_contact,
self.admin_contact,
self.comments,
2018-02-02 11:34:31 -05:00
)
2016-03-01 11:23:03 -05:00
@python_2_unicode_compatible
2016-03-01 11:23:03 -05:00
class CircuitType(models.Model):
"""
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
"""
name = models.CharField(max_length=50, unique=True)
slug = models.SlugField(unique=True)
csv_headers = ['name', 'slug']
2016-03-01 11:23:03 -05:00
class Meta:
ordering = ['name']
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)
def to_csv(self):
return (
self.name,
self.slug,
)
2016-03-01 11:23:03 -05:00
@python_2_unicode_compatible
2016-08-15 15:24:23 -04:00
class Circuit(CreatedUpdatedModel, 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
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.
2016-03-01 11:23:03 -05:00
"""
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)
2016-07-26 15:42:26 -04:00
tenant = models.ForeignKey(Tenant, related_name='circuits', blank=True, null=True, on_delete=models.PROTECT)
2016-03-01 11:23:03 -05:00
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)
2016-03-01 11:23:03 -05:00
comments = models.TextField(blank=True)
2016-08-22 13:20:30 -04:00
custom_field_values = GenericRelation(CustomFieldValue, content_type_field='obj_type', object_id_field='obj_id')
2016-03-01 11:23:03 -05:00
csv_headers = ['cid', 'provider', 'type', 'tenant', 'install_date', 'commit_rate', 'description', 'comments']
2016-03-01 11:23:03 -05:00
class Meta:
ordering = ['provider', 'cid']
unique_together = ['provider', 'cid']
def __str__(self):
2017-05-24 11:33:11 -04:00
return '{} {}'.format(self.provider, self.cid)
2016-03-01 11:23:03 -05:00
def get_absolute_url(self):
return reverse('circuits:circuit', args=[self.pk])
def to_csv(self):
2018-02-02 11:34:31 -05:00
return (
self.cid,
self.provider.name,
self.type.name,
self.tenant.name if self.tenant else None,
2018-02-02 11:34:31 -05:00
self.install_date,
self.commit_rate,
self.description,
self.comments,
2018-02-02 11:34:31 -05:00
)
2016-06-20 15:58:18 -04: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')
@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):
2017-05-24 11:33:11 -04:00
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