2017-09-10 14:53:02 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
2017-01-15 23:01:18 +00:00
|
|
|
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
|
|
|
|
# se veut agnostique au réseau considéré, de manière à être installable en
|
|
|
|
# quelques clics.
|
|
|
|
#
|
|
|
|
# Copyright © 2017 Gabriel Détraz
|
2019-09-29 14:02:28 +00:00
|
|
|
# Copyright © 2017 Lara Kermarec
|
2017-01-15 23:01:18 +00:00
|
|
|
# Copyright © 2017 Augustin Lemesle
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2017-10-13 22:00:16 +00:00
|
|
|
"""
|
2020-04-29 13:00:47 +00:00
|
|
|
Definition of models for the 'topologie' app.
|
|
|
|
|
|
|
|
The following models are defined:
|
|
|
|
* stack (grouping switches): id, id_min, id_max and name
|
|
|
|
* switch: name, number of ports, related interface and machine (MAC
|
|
|
|
address, IP address etc.) (see machines.models.interface)
|
|
|
|
* port: related to a switch by foreign_key, number of the port, related
|
|
|
|
exclusively to another port, machine (server or AP) or room outlets
|
|
|
|
* room: list of outlets, name and comments about the plug's state
|
2017-10-13 22:00:16 +00:00
|
|
|
"""
|
2017-01-15 23:01:18 +00:00
|
|
|
|
2017-09-10 23:29:24 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-10-26 21:29:26 +00:00
|
|
|
import itertools
|
|
|
|
|
2016-07-03 01:12:41 +00:00
|
|
|
from django.db import models
|
2018-07-11 17:37:22 +00:00
|
|
|
from django.db.models.signals import post_save, post_delete
|
2018-05-21 19:13:44 +00:00
|
|
|
from django.utils.functional import cached_property
|
2019-08-07 00:09:57 +00:00
|
|
|
from django.core.cache import cache
|
2017-08-18 12:20:51 +00:00
|
|
|
from django.dispatch import receiver
|
2016-07-06 13:52:33 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2017-10-26 21:29:26 +00:00
|
|
|
from django.db import IntegrityError
|
|
|
|
from django.db import transaction
|
2018-05-26 22:36:25 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2017-10-26 21:29:26 +00:00
|
|
|
from reversion import revisions as reversion
|
2016-07-03 01:12:41 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
from preferences.models import OptionalTopologie, RadiusKey, SwitchManagementCred
|
|
|
|
from machines.models import Machine, regen, Role, MachineType, Ipv6List
|
2018-04-25 16:38:40 +00:00
|
|
|
from re2o.mixins import AclMixin, RevMixin
|
|
|
|
|
2018-04-14 00:20:44 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class Stack(AclMixin, RevMixin, models.Model):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Switch stack.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
name: the name of the stack.
|
|
|
|
stack_id: the ID of the stack, as a text chosen by the user.
|
|
|
|
details: the description to provide details about the stack.
|
|
|
|
member_id_min: the minimum switch ID in the stack.
|
|
|
|
member_id_max: the maximum switch ID in the stack.
|
|
|
|
"""
|
2017-08-17 22:17:56 +00:00
|
|
|
|
|
|
|
name = models.CharField(max_length=32, blank=True, null=True)
|
|
|
|
stack_id = models.CharField(max_length=32, unique=True)
|
|
|
|
details = models.CharField(max_length=255, blank=True, null=True)
|
2017-10-15 18:37:21 +00:00
|
|
|
member_id_min = models.PositiveIntegerField()
|
|
|
|
member_id_max = models.PositiveIntegerField()
|
2017-08-17 22:17:56 +00:00
|
|
|
|
2017-12-31 16:43:38 +00:00
|
|
|
class Meta:
|
2019-11-04 16:55:03 +00:00
|
|
|
permissions = (("view_stack", _("Can view a stack object")),)
|
2018-08-05 16:48:56 +00:00
|
|
|
verbose_name = _("switches stack")
|
|
|
|
verbose_name_plural = _("switches stacks")
|
2017-12-31 16:43:38 +00:00
|
|
|
|
2017-08-17 22:17:56 +00:00
|
|
|
def __str__(self):
|
|
|
|
return " ".join([self.name, self.stack_id])
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
2017-10-06 02:17:45 +00:00
|
|
|
self.clean()
|
2017-08-17 22:17:56 +00:00
|
|
|
if not self.name:
|
|
|
|
self.name = self.stack_id
|
|
|
|
super(Stack, self).save(*args, **kwargs)
|
|
|
|
|
|
|
|
def clean(self):
|
2020-05-30 09:38:02 +00:00
|
|
|
"""Check if id_max < id_min."""
|
2017-08-17 22:17:56 +00:00
|
|
|
if self.member_id_max < self.member_id_min:
|
2018-08-05 16:48:56 +00:00
|
|
|
raise ValidationError(
|
2019-11-16 14:14:22 +00:00
|
|
|
{"member_id_max": _("The maximum ID is less than the minimum ID.")}
|
2018-08-05 16:48:56 +00:00
|
|
|
)
|
2017-08-17 22:17:56 +00:00
|
|
|
|
2017-10-13 23:43:01 +00:00
|
|
|
|
2020-04-30 20:27:21 +00:00
|
|
|
class AccessPoint(Machine):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Wireless Access Point. Inherits from machines.interfaces.
|
2018-04-14 00:20:44 +00:00
|
|
|
|
2020-04-29 13:00:47 +00:00
|
|
|
Attributes:
|
|
|
|
location: the text to provide details about the AP's location.
|
2018-03-23 23:50:11 +00:00
|
|
|
"""
|
|
|
|
|
2018-04-14 00:20:44 +00:00
|
|
|
location = models.CharField(
|
2018-03-23 23:50:11 +00:00
|
|
|
max_length=255,
|
2019-11-16 14:14:22 +00:00
|
|
|
help_text=_("Details about the AP's location."),
|
2018-03-23 23:50:11 +00:00
|
|
|
blank=True,
|
2019-11-04 16:55:03 +00:00
|
|
|
null=True,
|
2018-03-23 23:50:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
2019-11-04 16:55:03 +00:00
|
|
|
permissions = (("view_accesspoint", _("Can view an access point object")),)
|
2018-08-05 16:48:56 +00:00
|
|
|
verbose_name = _("access point")
|
|
|
|
verbose_name_plural = _("access points")
|
2018-03-23 23:50:11 +00:00
|
|
|
|
2018-05-21 19:13:44 +00:00
|
|
|
def port(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Return the queryset of ports for this device."""
|
2019-11-04 16:55:03 +00:00
|
|
|
return Port.objects.filter(machine_interface__machine=self)
|
2018-05-21 19:13:44 +00:00
|
|
|
|
|
|
|
def switch(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Return the switch where this is plugged."""
|
2019-11-04 16:55:03 +00:00
|
|
|
return Switch.objects.filter(ports__machine_interface__machine=self)
|
2018-05-21 19:13:44 +00:00
|
|
|
|
|
|
|
def building(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Return the building of the AP/Server (building of the switches
|
|
|
|
connected to...).
|
2018-07-11 17:37:22 +00:00
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
return Building.objects.filter(switchbay__switch=self.switch())
|
2018-05-21 19:13:44 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def short_name(self):
|
|
|
|
return str(self.interface_set.first().domain.name)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def all_ap_in(cls, building_instance):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Get all the APs of the given building.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
building_instance: the building used to find APs.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The queryset of all APs in the given building.
|
|
|
|
"""
|
2018-07-11 17:37:22 +00:00
|
|
|
return cls.objects.filter(
|
|
|
|
interface__port__switch__switchbay__building=building_instance
|
|
|
|
)
|
2018-05-21 19:13:44 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.interface_set.first())
|
|
|
|
|
2020-05-01 10:23:41 +00:00
|
|
|
# We want to retrieve the default behaviour given by AclMixin rather
|
|
|
|
# than the one overwritten by Machine. If you are not familiar with
|
|
|
|
# the behaviour of `super`, please check https://docs.python.org/3/library/functions.html#super
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_instance(cls, *args, **kwargs):
|
|
|
|
return super(Machine, cls).get_instance(*args, **kwargs)
|
|
|
|
|
2020-04-30 20:27:21 +00:00
|
|
|
@classmethod
|
2020-05-01 10:23:41 +00:00
|
|
|
def can_create(cls, *args, **kwargs):
|
|
|
|
return super(Machine, cls).can_create(*args, **kwargs)
|
|
|
|
|
|
|
|
def can_edit(self, *args, **kwargs):
|
|
|
|
return super(Machine, self).can_edit(*args, **kwargs)
|
|
|
|
|
|
|
|
def can_delete(self, *args, **kwargs):
|
|
|
|
return super(Machine, self).can_delete(*args, **kwargs)
|
|
|
|
|
|
|
|
def can_view(self, *args, **kwargs):
|
|
|
|
return super(Machine, self).can_view(*args, **kwargs)
|
2020-04-30 20:27:21 +00:00
|
|
|
|
2018-05-21 19:13:44 +00:00
|
|
|
|
|
|
|
class Server(Machine):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Dummy class, to retrieve servers of a building, or get switch of a
|
|
|
|
server.
|
2018-07-11 17:37:22 +00:00
|
|
|
"""
|
2018-05-21 19:13:44 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
|
|
|
|
def port(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Return the queryset of ports for this device."""
|
2019-11-04 16:55:03 +00:00
|
|
|
return Port.objects.filter(machine_interface__machine=self)
|
2018-05-21 19:13:44 +00:00
|
|
|
|
|
|
|
def switch(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Return the switch where this is plugged."""
|
2019-11-04 16:55:03 +00:00
|
|
|
return Switch.objects.filter(ports__machine_interface__machine=self)
|
2018-05-21 19:13:44 +00:00
|
|
|
|
|
|
|
def building(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Return the building of the AP/Server (building of the switches
|
|
|
|
connected to...).
|
2018-07-11 17:37:22 +00:00
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
return Building.objects.filter(switchbay__switch=self.switch())
|
2018-05-21 19:13:44 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def short_name(self):
|
|
|
|
return str(self.interface_set.first().domain.name)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def all_server_in(cls, building_instance):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Get all the servers of the given building.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
building_instance: the building used to find servers.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The queryset of all servers in the given building.
|
|
|
|
"""
|
2018-07-11 17:37:22 +00:00
|
|
|
return cls.objects.filter(
|
|
|
|
interface__port__switch__switchbay__building=building_instance
|
|
|
|
).exclude(accesspoint__isnull=False)
|
2018-05-21 19:13:44 +00:00
|
|
|
|
2018-04-25 06:36:36 +00:00
|
|
|
def __str__(self):
|
|
|
|
return str(self.interface_set.first())
|
|
|
|
|
2020-05-01 10:23:41 +00:00
|
|
|
# We want to retrieve the default behaviour given by AclMixin rather
|
|
|
|
# than the one overwritten by Machine. If you are not familiar with
|
|
|
|
# the behaviour of `super`, please check https://docs.python.org/3/library/functions.html#super
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_instance(cls, *args, **kwargs):
|
|
|
|
return super(Machine, cls).get_instance(*args, **kwargs)
|
|
|
|
|
2020-04-30 20:27:21 +00:00
|
|
|
@classmethod
|
2020-05-01 10:23:41 +00:00
|
|
|
def can_create(cls, *args, **kwargs):
|
|
|
|
return super(Machine, cls).can_create(*args, **kwargs)
|
|
|
|
|
|
|
|
def can_edit(self, *args, **kwargs):
|
|
|
|
return super(Machine, self).can_edit(*args, **kwargs)
|
|
|
|
|
|
|
|
def can_delete(self, *args, **kwargs):
|
|
|
|
return super(Machine, self).can_delete(*args, **kwargs)
|
|
|
|
|
|
|
|
def can_view(self, *args, **kwargs):
|
|
|
|
return super(Machine, self).can_view(*args, **kwargs)
|
2018-03-23 23:50:11 +00:00
|
|
|
|
2020-04-30 20:27:21 +00:00
|
|
|
|
|
|
|
class Switch(Machine):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Switch.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
number: the number of ports of the switch.
|
|
|
|
stack: the stack the switch is a part of.
|
|
|
|
stack_member_id: the ID of the switch in the related stack.
|
|
|
|
model: the model of the switch.
|
|
|
|
switchbay: the bay in which the switch is located.
|
|
|
|
radius_key: the RADIUS key of the switch.
|
|
|
|
management_creds: the management credentials of the switch.
|
|
|
|
automatic_provision: whether automatic provision is enabled for the
|
|
|
|
switch.
|
|
|
|
"""
|
2016-11-01 01:14:06 +00:00
|
|
|
|
2019-11-16 14:14:22 +00:00
|
|
|
number = models.PositiveIntegerField(help_text=_("Number of ports."))
|
2017-10-13 22:00:16 +00:00
|
|
|
stack = models.ForeignKey(
|
2019-11-04 16:55:03 +00:00
|
|
|
"topologie.Stack", blank=True, null=True, on_delete=models.SET_NULL
|
2018-04-16 03:51:25 +00:00
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
stack_member_id = models.PositiveIntegerField(blank=True, null=True)
|
2017-10-26 03:07:11 +00:00
|
|
|
model = models.ForeignKey(
|
2019-11-04 16:55:03 +00:00
|
|
|
"topologie.ModelSwitch",
|
2017-10-26 03:07:11 +00:00
|
|
|
blank=True,
|
|
|
|
null=True,
|
2018-04-16 03:51:25 +00:00
|
|
|
on_delete=models.SET_NULL,
|
2019-11-16 14:14:22 +00:00
|
|
|
help_text=_("Switch model."),
|
2017-10-26 03:07:11 +00:00
|
|
|
)
|
2018-04-08 02:01:32 +00:00
|
|
|
switchbay = models.ForeignKey(
|
2019-11-04 16:55:03 +00:00
|
|
|
"topologie.SwitchBay", blank=True, null=True, on_delete=models.SET_NULL
|
2018-04-08 02:01:32 +00:00
|
|
|
)
|
2018-07-10 23:07:31 +00:00
|
|
|
radius_key = models.ForeignKey(
|
2019-11-04 16:55:03 +00:00
|
|
|
"preferences.RadiusKey",
|
2018-07-10 23:07:31 +00:00
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
on_delete=models.PROTECT,
|
2019-11-16 14:14:22 +00:00
|
|
|
help_text=_("RADIUS key of the switch."),
|
2018-07-10 23:07:31 +00:00
|
|
|
)
|
2018-07-11 00:19:29 +00:00
|
|
|
management_creds = models.ForeignKey(
|
2019-11-04 16:55:03 +00:00
|
|
|
"preferences.SwitchManagementCred",
|
2018-07-11 00:19:29 +00:00
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
on_delete=models.PROTECT,
|
2019-11-16 14:14:22 +00:00
|
|
|
help_text=_("Management credentials for the switch."),
|
2018-07-11 00:19:29 +00:00
|
|
|
)
|
2018-07-09 19:05:50 +00:00
|
|
|
automatic_provision = models.BooleanField(
|
2019-11-16 14:14:22 +00:00
|
|
|
default=False, help_text=_("Automatic provision for the switch.")
|
2018-07-09 19:05:50 +00:00
|
|
|
)
|
2019-01-03 00:02:02 +00:00
|
|
|
|
2017-08-17 22:17:56 +00:00
|
|
|
class Meta:
|
2019-11-04 16:55:03 +00:00
|
|
|
unique_together = ("stack", "stack_member_id")
|
|
|
|
permissions = (("view_switch", _("Can view a switch object")),)
|
2018-08-05 16:48:56 +00:00
|
|
|
verbose_name = _("switch")
|
|
|
|
verbose_name_plural = _("switches")
|
2016-07-03 01:12:41 +00:00
|
|
|
|
2017-08-17 22:17:56 +00:00
|
|
|
def clean(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Check if the stack member ID is in the range of the stack's IDs and
|
|
|
|
calls the clean of the parent class.
|
|
|
|
"""
|
2018-03-25 02:51:48 +00:00
|
|
|
super(Switch, self).clean()
|
2017-08-17 22:17:56 +00:00
|
|
|
if self.stack is not None:
|
|
|
|
if self.stack_member_id is not None:
|
2019-11-04 16:55:03 +00:00
|
|
|
if (self.stack_member_id > self.stack.member_id_max) or (
|
|
|
|
self.stack_member_id < self.stack.member_id_min
|
|
|
|
):
|
2017-10-13 23:43:01 +00:00
|
|
|
raise ValidationError(
|
2019-11-04 16:55:03 +00:00
|
|
|
{
|
|
|
|
"stack_member_id": _(
|
|
|
|
"The switch ID exceeds the"
|
|
|
|
" limits allowed by the stack."
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2017-08-17 22:17:56 +00:00
|
|
|
else:
|
2018-08-05 16:48:56 +00:00
|
|
|
raise ValidationError(
|
2019-11-16 14:14:22 +00:00
|
|
|
{"stack_member_id": _("The stack member ID can't be void.")}
|
2018-08-05 16:48:56 +00:00
|
|
|
)
|
2018-03-23 23:50:11 +00:00
|
|
|
|
2017-10-26 21:29:26 +00:00
|
|
|
def create_ports(self, begin, end):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Create ports for the switch if the values are consistent.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
begin: the number of the start port.
|
|
|
|
end: the number of the end port.
|
|
|
|
"""
|
2017-10-26 21:29:26 +00:00
|
|
|
if end < begin:
|
2019-11-16 14:14:22 +00:00
|
|
|
raise ValidationError(_("The end port is less than the start port."))
|
2018-12-30 13:27:41 +00:00
|
|
|
ports_to_create = range(begin, end + 1)
|
2019-11-04 16:55:03 +00:00
|
|
|
existing_ports = Port.objects.filter(switch=self.switch).values_list(
|
|
|
|
"port", flat=True
|
|
|
|
)
|
2018-12-29 23:18:35 +00:00
|
|
|
non_existing_ports = list(set(ports_to_create) - set(existing_ports))
|
|
|
|
|
|
|
|
if len(non_existing_ports) + existing_ports.count() > self.number:
|
2018-08-05 16:48:56 +00:00
|
|
|
raise ValidationError(_("This switch can't have that many ports."))
|
2018-12-29 23:18:35 +00:00
|
|
|
with transaction.atomic(), reversion.create_revision():
|
2019-11-16 14:14:22 +00:00
|
|
|
reversion.set_comment("Creation")
|
2019-11-04 16:55:03 +00:00
|
|
|
Port.objects.bulk_create(
|
|
|
|
[
|
|
|
|
Port(switch=self.switch, port=port_id)
|
|
|
|
for port_id in non_existing_ports
|
|
|
|
]
|
|
|
|
)
|
2017-08-17 22:17:56 +00:00
|
|
|
|
2018-04-07 18:45:29 +00:00
|
|
|
def main_interface(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Get the main interface of the switch (the management interface)."""
|
2019-11-04 16:55:03 +00:00
|
|
|
switch_iptype = OptionalTopologie.get_cached_value("switchs_ip_type")
|
2018-09-28 16:09:22 +00:00
|
|
|
if switch_iptype:
|
2019-11-04 16:55:03 +00:00
|
|
|
return (
|
|
|
|
self.interface_set.filter(machine_type__ip_type=switch_iptype).first()
|
|
|
|
or self.interface_set.first()
|
|
|
|
)
|
2018-04-07 18:45:29 +00:00
|
|
|
return self.interface_set.first()
|
|
|
|
|
2018-07-20 17:20:01 +00:00
|
|
|
@cached_property
|
|
|
|
def get_name(self):
|
2019-11-04 16:55:03 +00:00
|
|
|
return self.name or getattr(self.main_interface(), "domain", "Unknown")
|
2018-07-10 23:07:31 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def get_radius_key(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Get the RADIUS key object related to the switch."""
|
2018-07-10 23:07:31 +00:00
|
|
|
return self.radius_key or RadiusKey.objects.filter(default_switch=True).first()
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def get_radius_key_value(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Get the RADIUS key as a string, or None if there are no RADIUS key
|
|
|
|
related to the switch.
|
|
|
|
"""
|
2018-07-10 23:07:31 +00:00
|
|
|
if self.get_radius_key:
|
|
|
|
return self.get_radius_key.radius_key
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2019-10-10 15:55:59 +00:00
|
|
|
@cached_property
|
|
|
|
def get_radius_servers_objects(self):
|
2019-11-04 16:55:03 +00:00
|
|
|
return Role.all_interfaces_for_roletype("radius-server").filter(
|
|
|
|
machine_type__in=MachineType.objects.filter(
|
|
|
|
interface__in=self.interface_set.all()
|
|
|
|
)
|
|
|
|
)
|
2019-10-10 15:55:59 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def get_radius_servers(self):
|
|
|
|
def return_ips_dict(interfaces):
|
2019-11-04 16:55:03 +00:00
|
|
|
return {
|
|
|
|
"ipv4": [str(interface.ipv4) for interface in interfaces],
|
|
|
|
"ipv6": Ipv6List.objects.filter(interface__in=interfaces).values_list(
|
|
|
|
"ipv6", flat=True
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2019-10-10 15:55:59 +00:00
|
|
|
return return_ips_dict(self.get_radius_servers_objects)
|
|
|
|
|
2018-07-11 00:19:29 +00:00
|
|
|
@cached_property
|
|
|
|
def get_management_cred(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Get the management credentials objects of the switch."""
|
2019-11-04 16:55:03 +00:00
|
|
|
return (
|
|
|
|
self.management_creds
|
|
|
|
or SwitchManagementCred.objects.filter(default_switch=True).first()
|
|
|
|
)
|
2018-07-11 00:19:29 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def get_management_cred_value(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Get the management credentials as a dictionary, or None if there are
|
|
|
|
no management credentials related to the switch.
|
|
|
|
"""
|
2018-07-11 00:19:29 +00:00
|
|
|
if self.get_management_cred:
|
2019-11-04 16:55:03 +00:00
|
|
|
return {
|
|
|
|
"id": self.get_management_cred.management_id,
|
|
|
|
"pass": self.get_management_cred.management_pass,
|
|
|
|
}
|
2018-07-11 00:19:29 +00:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2018-07-09 22:44:22 +00:00
|
|
|
@cached_property
|
|
|
|
def rest_enabled(self):
|
2019-11-04 16:55:03 +00:00
|
|
|
return (
|
|
|
|
OptionalTopologie.get_cached_value("switchs_rest_management")
|
|
|
|
or self.automatic_provision
|
|
|
|
)
|
2018-07-09 22:44:22 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def web_management_enabled(self):
|
2019-11-04 16:55:03 +00:00
|
|
|
sw_management = OptionalTopologie.get_cached_value("switchs_web_management")
|
|
|
|
sw_management_ssl = OptionalTopologie.get_cached_value(
|
|
|
|
"switchs_web_management_ssl"
|
|
|
|
)
|
2018-07-09 22:44:22 +00:00
|
|
|
if sw_management_ssl:
|
|
|
|
return "ssl"
|
|
|
|
elif sw_management:
|
|
|
|
return "plain"
|
|
|
|
else:
|
|
|
|
return self.automatic_provision
|
|
|
|
|
2018-07-08 02:08:01 +00:00
|
|
|
@cached_property
|
|
|
|
def ipv4(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Get the IPv4 address of the switch's management interface."""
|
2018-07-08 02:08:01 +00:00
|
|
|
return str(self.main_interface().ipv4)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def ipv6(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Get the IPv6 address of the switch's management interface."""
|
2018-07-08 02:08:01 +00:00
|
|
|
return str(self.main_interface().ipv6().first())
|
2018-07-20 17:20:01 +00:00
|
|
|
|
2018-07-02 09:00:32 +00:00
|
|
|
@cached_property
|
2018-07-11 23:12:46 +00:00
|
|
|
def interfaces_subnet(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Get a dictionary of IPv4 addresses:subnets of all the switch's
|
|
|
|
interfaces.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
return dict(
|
|
|
|
(
|
|
|
|
str(interface.ipv4),
|
|
|
|
interface.machine_type.ip_type.ip_net_full_info
|
|
|
|
or interface.machine_type.ip_type.ip_set_full_info[0],
|
|
|
|
)
|
|
|
|
for interface in self.interface_set.all()
|
|
|
|
)
|
2018-07-02 09:00:32 +00:00
|
|
|
|
|
|
|
@cached_property
|
2018-07-11 23:12:46 +00:00
|
|
|
def interfaces6_subnet(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Get a dictionary of IPv6 addresses:subnets of all the switch's
|
|
|
|
interfaces.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
return dict(
|
|
|
|
(
|
|
|
|
str(interface.ipv6().first()),
|
|
|
|
interface.machine_type.ip_type.ip6_set_full_info,
|
|
|
|
)
|
|
|
|
for interface in self.interface_set.all()
|
|
|
|
)
|
2018-07-02 09:00:32 +00:00
|
|
|
|
2018-12-30 16:16:30 +00:00
|
|
|
@cached_property
|
|
|
|
def list_modules(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Get the list of dictionaries (rank, reference) of modules related to
|
|
|
|
the switch.
|
|
|
|
"""
|
2018-12-30 16:16:30 +00:00
|
|
|
modules = []
|
2019-11-04 16:55:03 +00:00
|
|
|
if getattr(self.model, "is_modular", None):
|
2018-12-30 16:16:30 +00:00
|
|
|
if self.model.is_itself_module:
|
|
|
|
modules.append((1, self.model.reference))
|
|
|
|
for module_of_self in self.moduleonswitch_set.all():
|
|
|
|
modules.append((module_of_self.slot, module_of_self.module.reference))
|
|
|
|
return modules
|
|
|
|
|
2019-07-20 23:25:04 +00:00
|
|
|
@cached_property
|
|
|
|
def get_dormitory(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Get the dormitory in which the switch is located."""
|
2019-07-20 23:25:04 +00:00
|
|
|
if self.switchbay:
|
|
|
|
return self.switchbay.building.dormitory
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2019-07-26 23:53:06 +00:00
|
|
|
@classmethod
|
|
|
|
def nothing_profile(cls):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Return default nothing port profile."""
|
2019-07-26 23:53:06 +00:00
|
|
|
nothing_profile, _created = PortProfile.objects.get_or_create(
|
2019-11-04 16:55:03 +00:00
|
|
|
profil_default="nothing", name="nothing", radius_type="NO"
|
2019-07-26 23:53:06 +00:00
|
|
|
)
|
|
|
|
return nothing_profile
|
|
|
|
|
|
|
|
def profile_type_or_nothing(self, profile_type):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Return the profile for a profile_type of this switch.
|
2019-09-10 17:11:14 +00:00
|
|
|
|
2020-04-29 13:00:47 +00:00
|
|
|
If it exists, return the defined default profile for a profile type on
|
|
|
|
the dormitory which the switch belongs.
|
|
|
|
Otherwise, return the nothing profile.
|
|
|
|
"""
|
2019-07-26 23:53:06 +00:00
|
|
|
profile_queryset = PortProfile.objects.filter(profil_default=profile_type)
|
|
|
|
if self.get_dormitory:
|
2019-11-04 16:55:03 +00:00
|
|
|
port_profile = (
|
|
|
|
profile_queryset.filter(on_dormitory=self.get_dormitory).first()
|
|
|
|
or profile_queryset.first()
|
|
|
|
)
|
2019-07-26 23:53:06 +00:00
|
|
|
else:
|
|
|
|
port_profile = profile_queryset.first()
|
2019-09-10 17:11:14 +00:00
|
|
|
return port_profile or Switch.nothing_profile()
|
2019-07-26 23:53:06 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def default_uplink_profile(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Default uplink profile for that switch -- in cache."""
|
2019-11-04 16:55:03 +00:00
|
|
|
return self.profile_type_or_nothing("uplink")
|
2019-07-26 23:53:06 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def default_access_point_profile(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Default AP profile for that switch -- in cache."""
|
2019-11-04 16:55:03 +00:00
|
|
|
return self.profile_type_or_nothing("access_point")
|
2019-07-26 23:53:06 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def default_room_profile(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Default room profile for that switch -- in cache."""
|
2019-11-04 16:55:03 +00:00
|
|
|
return self.profile_type_or_nothing("room")
|
2019-07-26 23:53:06 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def default_asso_machine_profile(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Default asso machine profile for that switch -- in cache."""
|
2019-11-04 16:55:03 +00:00
|
|
|
return self.profile_type_or_nothing("asso_machine")
|
2019-07-26 23:53:06 +00:00
|
|
|
|
2018-03-26 20:11:03 +00:00
|
|
|
def __str__(self):
|
2018-07-20 17:20:01 +00:00
|
|
|
return str(self.get_name)
|
2018-03-26 20:11:03 +00:00
|
|
|
|
2020-05-01 10:23:41 +00:00
|
|
|
# We want to retrieve the default behaviour given by AclMixin rather
|
|
|
|
# than the one overwritten by Machine. If you are not familiar with
|
|
|
|
# the behaviour of `super`, please check https://docs.python.org/3/library/functions.html#super
|
|
|
|
|
2020-04-30 20:27:21 +00:00
|
|
|
@classmethod
|
2020-05-01 10:23:41 +00:00
|
|
|
def get_instance(cls, *args, **kwargs):
|
|
|
|
return super(Machine, cls).get_instance(*args, **kwargs)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def can_create(cls, *args, **kwargs):
|
|
|
|
return super(Machine, cls).can_create(*args, **kwargs)
|
|
|
|
|
|
|
|
def can_edit(self, *args, **kwargs):
|
|
|
|
return super(Machine, self).can_edit(*args, **kwargs)
|
|
|
|
|
|
|
|
def can_delete(self, *args, **kwargs):
|
|
|
|
return super(Machine, self).can_delete(*args, **kwargs)
|
|
|
|
|
|
|
|
def can_view(self, *args, **kwargs):
|
|
|
|
return super(Machine, self).can_view(*args, **kwargs)
|
2020-04-30 20:27:21 +00:00
|
|
|
|
2017-10-13 23:43:01 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class ModelSwitch(AclMixin, RevMixin, models.Model):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Switch model.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
reference: the reference of the switch model.
|
|
|
|
commercial_name: the commercial name of the switch model.
|
|
|
|
constructor: the constructor of the switch model.
|
|
|
|
firmware: the firmware of the switch model.
|
|
|
|
is_modular: whether the switch model is modular.
|
|
|
|
is_itself_module: whether the switch is considered as a module.
|
|
|
|
"""
|
2018-08-05 16:48:56 +00:00
|
|
|
|
2017-10-26 03:07:11 +00:00
|
|
|
reference = models.CharField(max_length=255)
|
2019-11-04 16:55:03 +00:00
|
|
|
commercial_name = models.CharField(max_length=255, null=True, blank=True)
|
2017-10-26 03:07:11 +00:00
|
|
|
constructor = models.ForeignKey(
|
2019-11-04 16:55:03 +00:00
|
|
|
"topologie.ConstructorSwitch", on_delete=models.PROTECT
|
2018-07-08 20:28:10 +00:00
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
firmware = models.CharField(max_length=255, null=True, blank=True)
|
2018-12-30 16:04:21 +00:00
|
|
|
is_modular = models.BooleanField(
|
2019-11-04 16:55:03 +00:00
|
|
|
default=False, help_text=_("The switch model is modular.")
|
2019-01-03 00:02:02 +00:00
|
|
|
)
|
2018-12-30 16:04:21 +00:00
|
|
|
is_itself_module = models.BooleanField(
|
2019-11-04 16:55:03 +00:00
|
|
|
default=False, help_text=_("The switch is considered as a module.")
|
2019-01-03 00:02:02 +00:00
|
|
|
)
|
2017-10-26 03:07:11 +00:00
|
|
|
|
2017-12-31 16:43:38 +00:00
|
|
|
class Meta:
|
2019-11-04 16:55:03 +00:00
|
|
|
permissions = (("view_modelswitch", _("Can view a switch model object")),)
|
2018-08-05 16:48:56 +00:00
|
|
|
verbose_name = _("switch model")
|
|
|
|
verbose_name_plural = _("switch models")
|
2017-12-31 16:43:38 +00:00
|
|
|
|
2017-10-26 03:07:11 +00:00
|
|
|
def __str__(self):
|
2018-10-14 22:49:03 +00:00
|
|
|
if self.commercial_name:
|
2019-11-04 16:55:03 +00:00
|
|
|
return str(self.constructor) + " " + str(self.commercial_name)
|
2018-10-14 22:49:03 +00:00
|
|
|
else:
|
2019-11-04 16:55:03 +00:00
|
|
|
return str(self.constructor) + " " + self.reference
|
2017-10-26 03:07:11 +00:00
|
|
|
|
|
|
|
|
2018-12-30 16:04:21 +00:00
|
|
|
class ModuleSwitch(AclMixin, RevMixin, models.Model):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Switch module.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
reference: the reference of the switch module.
|
|
|
|
comment: the comment to describe the switch module.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2018-12-30 16:04:21 +00:00
|
|
|
reference = models.CharField(
|
|
|
|
max_length=255,
|
2019-11-16 14:14:22 +00:00
|
|
|
help_text=_("Reference of a module."),
|
|
|
|
verbose_name=_("module reference"),
|
2018-12-30 16:04:21 +00:00
|
|
|
)
|
|
|
|
comment = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
2019-11-16 14:14:22 +00:00
|
|
|
help_text=_("Comment."),
|
|
|
|
verbose_name=_("comment"),
|
2018-12-30 16:04:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
2019-11-04 16:55:03 +00:00
|
|
|
permissions = (("view_moduleswitch", _("Can view a switch module object")),)
|
2019-01-08 23:40:48 +00:00
|
|
|
verbose_name = _("switch module")
|
|
|
|
verbose_name_plural = _("switch modules")
|
2018-12-30 16:04:21 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.reference)
|
|
|
|
|
|
|
|
|
|
|
|
class ModuleOnSwitch(AclMixin, RevMixin, models.Model):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Link beetween module and switch.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
module: the switch module related to the link.
|
|
|
|
switch: the switch related to the link.
|
|
|
|
slot: the slot on the switch related to the link.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
|
|
|
module = models.ForeignKey("ModuleSwitch", on_delete=models.CASCADE)
|
|
|
|
switch = models.ForeignKey("Switch", on_delete=models.CASCADE)
|
2018-12-30 16:04:21 +00:00
|
|
|
slot = models.CharField(
|
2019-11-16 14:14:22 +00:00
|
|
|
max_length=15, help_text=_("Slot on switch."), verbose_name=_("slot")
|
2018-12-30 16:04:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
permissions = (
|
2019-11-04 16:55:03 +00:00
|
|
|
(
|
|
|
|
"view_moduleonswitch",
|
2019-11-16 14:14:22 +00:00
|
|
|
_("Can view a link between switch and module object"),
|
2019-11-04 16:55:03 +00:00
|
|
|
),
|
2018-12-30 16:04:21 +00:00
|
|
|
)
|
2019-01-08 23:40:48 +00:00
|
|
|
verbose_name = _("link between switch and module")
|
|
|
|
verbose_name_plural = _("links between switch and module")
|
2019-11-04 16:55:03 +00:00
|
|
|
unique_together = ["slot", "switch"]
|
2018-12-30 17:20:06 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2020-04-30 20:27:21 +00:00
|
|
|
return _("On slot %(slot)s of %(switch)s").format(
|
|
|
|
slot=str(self.slot), switch=str(self.switch)
|
|
|
|
)
|
2018-12-30 16:04:21 +00:00
|
|
|
|
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class ConstructorSwitch(AclMixin, RevMixin, models.Model):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Switch constructor.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
name: the name of the switch constructor.
|
|
|
|
"""
|
2018-08-05 16:48:56 +00:00
|
|
|
|
2017-10-26 03:07:11 +00:00
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
|
2017-12-31 16:43:38 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
2019-11-16 14:14:22 +00:00
|
|
|
("view_constructorswitch", _("Can view a switch constructor object")),
|
2017-12-31 16:43:38 +00:00
|
|
|
)
|
2018-08-05 16:48:56 +00:00
|
|
|
verbose_name = _("switch constructor")
|
2019-11-16 14:14:22 +00:00
|
|
|
verbose_name_plural = _("switch constructors")
|
2017-12-31 16:43:38 +00:00
|
|
|
|
2017-10-26 03:07:11 +00:00
|
|
|
def __str__(self):
|
2017-12-09 03:11:44 +00:00
|
|
|
return self.name
|
2017-10-26 03:07:11 +00:00
|
|
|
|
|
|
|
|
2018-04-07 18:45:29 +00:00
|
|
|
class SwitchBay(AclMixin, RevMixin, models.Model):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Switch bay.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
name: the name of the switch bay.
|
|
|
|
building: the building in which the switch bay is located.
|
|
|
|
info: the information to describe to switch bay.
|
|
|
|
"""
|
2018-08-05 16:48:56 +00:00
|
|
|
|
2018-04-07 18:45:29 +00:00
|
|
|
name = models.CharField(max_length=255)
|
2019-11-04 16:55:03 +00:00
|
|
|
building = models.ForeignKey("Building", on_delete=models.PROTECT)
|
|
|
|
info = models.CharField(max_length=255, blank=True, null=True)
|
2018-04-07 18:45:29 +00:00
|
|
|
|
|
|
|
class Meta:
|
2019-11-04 16:55:03 +00:00
|
|
|
permissions = (("view_switchbay", _("Can view a switch bay object")),)
|
2018-08-05 16:48:56 +00:00
|
|
|
verbose_name = _("switch bay")
|
|
|
|
verbose_name_plural = _("switch bays")
|
2018-04-07 18:45:29 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
2019-02-18 20:11:51 +00:00
|
|
|
class Dormitory(AclMixin, RevMixin, models.Model):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Dormitory.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
name: the name of the dormitory.
|
|
|
|
"""
|
2019-02-18 20:11:51 +00:00
|
|
|
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
|
|
|
|
class Meta:
|
2019-11-04 16:55:03 +00:00
|
|
|
permissions = (("view_dormitory", _("Can view a dormitory object")),)
|
2019-02-18 20:11:51 +00:00
|
|
|
verbose_name = _("dormitory")
|
|
|
|
verbose_name_plural = _("dormitories")
|
|
|
|
|
|
|
|
def all_ap_in(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Get all the APs in the dormitory."""
|
2019-02-18 20:11:51 +00:00
|
|
|
return AccessPoint.all_ap_in(self.building_set.all())
|
|
|
|
|
2019-08-07 00:09:57 +00:00
|
|
|
@classmethod
|
|
|
|
def is_multiple_dorms(cls):
|
2019-11-04 16:55:03 +00:00
|
|
|
multiple_dorms = cache.get("multiple_dorms")
|
2019-08-07 00:09:57 +00:00
|
|
|
if multiple_dorms:
|
|
|
|
return multiple_dorms
|
|
|
|
else:
|
2019-11-04 16:55:03 +00:00
|
|
|
return cache.get_or_set("multiple_dorms", cls.objects.count() > 1)
|
2019-08-07 00:09:57 +00:00
|
|
|
|
2019-02-18 20:11:51 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
2018-04-07 18:45:29 +00:00
|
|
|
class Building(AclMixin, RevMixin, models.Model):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Building.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
name: the name of the building.
|
|
|
|
dormitory: the dormitory of the building (a Dormitory can contain
|
|
|
|
multiple dormitories).
|
|
|
|
"""
|
2018-08-05 16:48:56 +00:00
|
|
|
|
2018-04-07 18:45:29 +00:00
|
|
|
name = models.CharField(max_length=255)
|
2019-11-04 16:55:03 +00:00
|
|
|
dormitory = models.ForeignKey("Dormitory", on_delete=models.PROTECT)
|
2018-04-07 18:45:29 +00:00
|
|
|
|
|
|
|
class Meta:
|
2019-11-04 16:55:03 +00:00
|
|
|
permissions = (("view_building", _("Can view a building object")),)
|
2018-08-05 16:48:56 +00:00
|
|
|
verbose_name = _("building")
|
|
|
|
verbose_name_plural = _("buildings")
|
2018-04-07 18:45:29 +00:00
|
|
|
|
2018-10-14 18:17:51 +00:00
|
|
|
def all_ap_in(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Get all the APs in the building."""
|
2018-10-14 18:17:51 +00:00
|
|
|
return AccessPoint.all_ap_in(self)
|
|
|
|
|
2019-08-06 15:46:13 +00:00
|
|
|
def get_name(self):
|
2019-08-07 00:09:57 +00:00
|
|
|
if Dormitory.is_multiple_dorms():
|
2019-02-18 20:11:51 +00:00
|
|
|
return self.dormitory.name + " : " + self.name
|
|
|
|
else:
|
|
|
|
return self.name
|
2018-04-07 18:45:29 +00:00
|
|
|
|
2019-08-06 15:46:13 +00:00
|
|
|
@cached_property
|
|
|
|
def cached_name(self):
|
2019-09-10 17:11:14 +00:00
|
|
|
return self.get_name()
|
2019-08-06 15:46:13 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.cached_name
|
|
|
|
|
2018-04-07 18:45:29 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class Port(AclMixin, RevMixin, models.Model):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Port of a switch.
|
|
|
|
|
|
|
|
A port is related exclusively to either:
|
|
|
|
* a room
|
|
|
|
* a machine, e.g. server
|
|
|
|
* another port
|
|
|
|
Behaviour according to the RADIUS mode:
|
|
|
|
* STRICT: connection only if the machine and room have access
|
|
|
|
* COMMON: check only the machine's state
|
|
|
|
* NO: accept only request coming from the port and set on the standard
|
|
|
|
VLAN.
|
|
|
|
* BLOQ: reject all requests.
|
|
|
|
The VLAN can be forced to override the general policy for VLAN setting.
|
|
|
|
This enables to force a port to a particular VLAN. It adds to the RADIUS
|
|
|
|
policy.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
switch: the switch to which the port belongs.
|
|
|
|
port: the port number on the switch for the Port object.
|
|
|
|
room: the room to which the port is related.
|
|
|
|
machine_interface: the machine to which the port is related
|
|
|
|
related: the other port to which is port is related.
|
|
|
|
custom_profile: the port profile of the port.
|
|
|
|
state: whether the port is active.
|
|
|
|
details: the details to describre the port.
|
|
|
|
"""
|
2017-10-13 22:00:16 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
switch = models.ForeignKey("Switch", related_name="ports", on_delete=models.CASCADE)
|
2017-10-15 18:37:21 +00:00
|
|
|
port = models.PositiveIntegerField()
|
2019-11-04 16:55:03 +00:00
|
|
|
room = models.ForeignKey("Room", on_delete=models.PROTECT, blank=True, null=True)
|
2017-10-13 22:00:16 +00:00
|
|
|
machine_interface = models.ForeignKey(
|
2019-11-04 16:55:03 +00:00
|
|
|
"machines.Interface", on_delete=models.SET_NULL, blank=True, null=True
|
2018-07-11 17:37:22 +00:00
|
|
|
)
|
2017-10-13 22:00:16 +00:00
|
|
|
related = models.OneToOneField(
|
2019-11-04 16:55:03 +00:00
|
|
|
"self", null=True, blank=True, related_name="related_port"
|
2018-07-11 17:37:22 +00:00
|
|
|
)
|
2018-06-30 22:17:24 +00:00
|
|
|
custom_profile = models.ForeignKey(
|
2019-11-04 16:55:03 +00:00
|
|
|
"PortProfile", on_delete=models.PROTECT, blank=True, null=True
|
2018-07-11 17:37:22 +00:00
|
|
|
)
|
2018-06-30 17:04:15 +00:00
|
|
|
state = models.BooleanField(
|
|
|
|
default=True,
|
2019-11-16 14:14:22 +00:00
|
|
|
help_text=_("Port state Active."),
|
|
|
|
verbose_name=_("port state Active"),
|
2018-06-30 17:04:15 +00:00
|
|
|
)
|
2016-07-06 19:50:15 +00:00
|
|
|
details = models.CharField(max_length=255, blank=True)
|
2016-07-03 12:02:46 +00:00
|
|
|
|
|
|
|
class Meta:
|
2019-11-04 16:55:03 +00:00
|
|
|
unique_together = ("switch", "port")
|
|
|
|
permissions = (("view_port", _("Can view a port object")),)
|
2018-08-05 16:48:56 +00:00
|
|
|
verbose_name = _("port")
|
|
|
|
verbose_name_plural = _("ports")
|
2016-07-06 13:52:33 +00:00
|
|
|
|
2018-06-30 15:29:00 +00:00
|
|
|
@cached_property
|
2018-07-08 02:08:01 +00:00
|
|
|
def pretty_name(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""More elaborated name for label on switch configuration."""
|
2018-07-08 02:08:01 +00:00
|
|
|
if self.related:
|
2019-01-08 23:40:48 +00:00
|
|
|
return _("Uplink: ") + self.related.switch.short_name
|
2018-07-08 02:08:01 +00:00
|
|
|
elif self.machine_interface:
|
2019-01-08 23:40:48 +00:00
|
|
|
return _("Machine: ") + str(self.machine_interface.domain)
|
2018-07-08 02:08:01 +00:00
|
|
|
elif self.room:
|
2019-01-08 23:40:48 +00:00
|
|
|
return _("Room: ") + str(self.room)
|
2018-07-08 02:08:01 +00:00
|
|
|
else:
|
2019-01-08 23:40:48 +00:00
|
|
|
return _("Unknown")
|
2018-07-08 02:08:01 +00:00
|
|
|
|
|
|
|
@cached_property
|
2018-10-03 20:17:36 +00:00
|
|
|
def get_port_profile(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Get the configuration profile for this port.
|
2018-06-30 16:19:02 +00:00
|
|
|
|
2020-04-29 13:00:47 +00:00
|
|
|
Returns:
|
|
|
|
The custom profile if it exists, else the default profile of the
|
|
|
|
dormitory if it exists, else the global default profile, else the
|
|
|
|
nothing profile.
|
|
|
|
"""
|
2018-06-30 22:17:24 +00:00
|
|
|
if self.custom_profile:
|
|
|
|
return self.custom_profile
|
2018-06-30 15:29:00 +00:00
|
|
|
elif self.related:
|
2019-07-26 23:53:06 +00:00
|
|
|
return self.switch.default_uplink_profile
|
2018-06-30 15:29:00 +00:00
|
|
|
elif self.machine_interface:
|
2019-11-04 16:55:03 +00:00
|
|
|
if hasattr(self.machine_interface.machine, "accesspoint"):
|
2019-07-26 23:53:06 +00:00
|
|
|
return self.switch.default_access_point_profile
|
2018-06-30 15:29:00 +00:00
|
|
|
else:
|
2019-07-26 23:53:06 +00:00
|
|
|
return self.switch.default_asso_machine_profile
|
2018-06-30 15:29:00 +00:00
|
|
|
elif self.room:
|
2019-07-26 23:53:06 +00:00
|
|
|
return self.switch.default_room_profile
|
2018-06-30 15:29:00 +00:00
|
|
|
else:
|
2019-09-10 17:11:14 +00:00
|
|
|
return Switch.nothing_profile()
|
2018-06-30 15:29:00 +00:00
|
|
|
|
2018-04-14 22:06:29 +00:00
|
|
|
@classmethod
|
|
|
|
def get_instance(cls, portid, *_args, **kwargs):
|
2019-11-04 16:55:03 +00:00
|
|
|
return (
|
|
|
|
cls.objects.select_related("machine_interface__domain__extension")
|
|
|
|
.select_related("machine_interface__machine__switch")
|
|
|
|
.select_related("room")
|
|
|
|
.select_related("related")
|
|
|
|
.prefetch_related("switch__interface_set__domain__extension")
|
|
|
|
.get(pk=portid)
|
|
|
|
)
|
2017-12-09 04:16:56 +00:00
|
|
|
|
2017-10-06 02:17:45 +00:00
|
|
|
def make_port_related(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Synchronise the related port with self."""
|
2017-10-06 02:17:45 +00:00
|
|
|
related_port = self.related
|
|
|
|
related_port.related = self
|
|
|
|
related_port.save()
|
2017-10-13 22:00:16 +00:00
|
|
|
|
2017-10-06 02:17:45 +00:00
|
|
|
def clean_port_related(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Delete the related relation on self."""
|
2017-10-06 02:17:45 +00:00
|
|
|
related_port = self.related_port
|
|
|
|
related_port.related = None
|
|
|
|
related_port.save()
|
|
|
|
|
2016-07-06 13:52:33 +00:00
|
|
|
def clean(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""
|
|
|
|
Check if the port is only related exclusively to either a room, a
|
|
|
|
machine or another port.
|
|
|
|
Check if the related port is not self and applies the relation to the
|
|
|
|
related port if the relation is correct.
|
|
|
|
Delete the relation if it points to self.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
if hasattr(self, "switch"):
|
2016-11-19 02:10:05 +00:00
|
|
|
if self.port > self.switch.number:
|
2018-04-14 22:06:29 +00:00
|
|
|
raise ValidationError(
|
2018-08-05 16:48:56 +00:00
|
|
|
_("The port can't exist, its number is too great.")
|
2018-04-14 22:06:29 +00:00
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
if (
|
|
|
|
self.room
|
|
|
|
and self.machine_interface
|
|
|
|
or self.room
|
|
|
|
and self.related
|
|
|
|
or self.machine_interface
|
|
|
|
and self.related
|
|
|
|
):
|
2018-04-14 22:06:29 +00:00
|
|
|
raise ValidationError(
|
2018-08-05 16:48:56 +00:00
|
|
|
_("Room, interface and related port are mutually exclusive.")
|
2018-04-14 22:06:29 +00:00
|
|
|
)
|
2017-10-13 22:00:16 +00:00
|
|
|
if self.related == self:
|
2018-08-05 16:48:56 +00:00
|
|
|
raise ValidationError(_("A port can't be related to itself."))
|
2016-07-06 13:52:33 +00:00
|
|
|
if self.related and not self.related.related:
|
|
|
|
if self.related.machine_interface or self.related.room:
|
2018-04-14 22:06:29 +00:00
|
|
|
raise ValidationError(
|
2019-11-04 16:55:03 +00:00
|
|
|
_(
|
|
|
|
"The related port is already used, please clear it"
|
|
|
|
" before creating the relation."
|
|
|
|
)
|
2018-04-14 22:06:29 +00:00
|
|
|
)
|
2016-07-06 13:52:33 +00:00
|
|
|
else:
|
2017-10-06 02:17:45 +00:00
|
|
|
self.make_port_related()
|
2019-11-04 16:55:03 +00:00
|
|
|
elif hasattr(self, "related_port"):
|
2017-10-06 02:17:45 +00:00
|
|
|
self.clean_port_related()
|
2016-07-03 01:12:41 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2016-07-03 12:02:46 +00:00
|
|
|
return str(self.switch) + " - " + str(self.port)
|
2016-07-03 01:12:41 +00:00
|
|
|
|
2017-10-13 23:43:01 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
class Room(AclMixin, RevMixin, models.Model):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Room.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
name: the name of the room.
|
|
|
|
details: the details describing the room.
|
|
|
|
building: the building in which the room is located.
|
|
|
|
"""
|
2016-11-01 01:14:06 +00:00
|
|
|
|
2019-02-18 20:11:51 +00:00
|
|
|
name = models.CharField(max_length=255)
|
2016-07-19 00:30:52 +00:00
|
|
|
details = models.CharField(max_length=255, blank=True)
|
2019-11-04 16:55:03 +00:00
|
|
|
building = models.ForeignKey("Building", on_delete=models.PROTECT)
|
2016-07-03 01:12:41 +00:00
|
|
|
|
2017-09-05 12:43:40 +00:00
|
|
|
class Meta:
|
2019-11-04 16:55:03 +00:00
|
|
|
ordering = ["building__name"]
|
|
|
|
permissions = (("view_room", _("Can view a room object")),)
|
2018-08-05 16:48:56 +00:00
|
|
|
verbose_name = _("room")
|
|
|
|
verbose_name_plural = _("rooms")
|
2019-11-04 16:55:03 +00:00
|
|
|
unique_together = ("name", "building")
|
2017-10-13 22:00:16 +00:00
|
|
|
|
2016-07-03 01:12:41 +00:00
|
|
|
def __str__(self):
|
2019-11-04 16:55:03 +00:00
|
|
|
return self.building.cached_name + " " + self.name
|
2016-07-03 12:02:46 +00:00
|
|
|
|
2017-10-13 23:43:01 +00:00
|
|
|
|
2018-06-26 16:49:19 +00:00
|
|
|
class PortProfile(AclMixin, RevMixin, models.Model):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Port profile.
|
|
|
|
|
|
|
|
Contains the information of the ports' configuration for a switch.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
name: the name of the port profile.
|
|
|
|
profil_default: the type of default profile (room, AP, uplink etc.).
|
|
|
|
on_dormitory: the dormitory with this default port profile.
|
|
|
|
vlan_untagged: the VLAN untagged of the port profile.
|
|
|
|
vlan_tagged: the VLAN(s) tagged of the port profile.
|
|
|
|
radius_type: the type of RADIUS authentication (inactive, MAC-address
|
|
|
|
or 802.1X) of the port profile.
|
|
|
|
radius_mode: the RADIUS mode of the port profile.
|
|
|
|
speed: the port speed limit of the port profile.
|
|
|
|
mac_limit: the MAC limit of the port profile.
|
|
|
|
flow_control: whether flow control is enabled.
|
|
|
|
dhcp_snooping: whether DHCP snooping is enabled.
|
|
|
|
dhcpv6_snooping: whether DHCPv6 snooping is enabled.
|
|
|
|
arp_protect: whether ARP protection is enabled.
|
|
|
|
ra_guard: whether RA guard is enabled.
|
|
|
|
loop_protect: whether loop protection is enabled.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
|
|
|
TYPES = (("NO", "NO"), ("802.1X", "802.1X"), ("MAC-radius", _("MAC-RADIUS")))
|
|
|
|
MODES = (("STRICT", "STRICT"), ("COMMON", "COMMON"))
|
2018-06-26 16:49:19 +00:00
|
|
|
SPEED = (
|
2019-11-04 16:55:03 +00:00
|
|
|
("10-half", "10-half"),
|
|
|
|
("100-half", "100-half"),
|
|
|
|
("10-full", "10-full"),
|
|
|
|
("100-full", "100-full"),
|
|
|
|
("1000-full", "1000-full"),
|
|
|
|
("auto", "auto"),
|
|
|
|
("auto-10", "auto-10"),
|
|
|
|
("auto-100", "auto-100"),
|
2018-07-11 17:37:22 +00:00
|
|
|
)
|
|
|
|
PROFIL_DEFAULT = (
|
2019-11-04 16:55:03 +00:00
|
|
|
("room", _("Room")),
|
|
|
|
("access_point", _("Access point")),
|
|
|
|
("uplink", _("Uplink")),
|
|
|
|
("asso_machine", _("Organisation machine")),
|
|
|
|
("nothing", _("Nothing")),
|
2018-07-11 17:37:22 +00:00
|
|
|
)
|
2019-11-16 14:14:22 +00:00
|
|
|
name = models.CharField(max_length=255, verbose_name=_("name"))
|
2018-06-26 16:49:19 +00:00
|
|
|
profil_default = models.CharField(
|
|
|
|
max_length=32,
|
|
|
|
choices=PROFIL_DEFAULT,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
2019-11-16 14:14:22 +00:00
|
|
|
verbose_name=_("default profile"),
|
2018-06-26 16:49:19 +00:00
|
|
|
)
|
2019-07-20 23:02:32 +00:00
|
|
|
on_dormitory = models.ForeignKey(
|
2019-11-04 16:55:03 +00:00
|
|
|
"topologie.Dormitory",
|
|
|
|
related_name="dormitory_ofprofil",
|
2019-07-20 23:02:32 +00:00
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
2019-11-16 14:14:22 +00:00
|
|
|
verbose_name=_("profile on dormitory"),
|
2019-07-20 23:02:32 +00:00
|
|
|
)
|
2018-05-26 22:36:25 +00:00
|
|
|
vlan_untagged = models.ForeignKey(
|
2019-11-04 16:55:03 +00:00
|
|
|
"machines.Vlan",
|
|
|
|
related_name="vlan_untagged",
|
2018-06-26 16:49:19 +00:00
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
2019-11-04 16:55:03 +00:00
|
|
|
verbose_name=_("VLAN untagged"),
|
2018-05-26 22:36:25 +00:00
|
|
|
)
|
|
|
|
vlan_tagged = models.ManyToManyField(
|
2019-11-04 16:55:03 +00:00
|
|
|
"machines.Vlan",
|
|
|
|
related_name="vlan_tagged",
|
2018-06-26 16:49:19 +00:00
|
|
|
blank=True,
|
2019-11-04 16:55:03 +00:00
|
|
|
verbose_name=_("VLAN(s) tagged"),
|
2018-06-26 16:49:19 +00:00
|
|
|
)
|
2018-05-26 22:36:25 +00:00
|
|
|
radius_type = models.CharField(
|
2018-06-26 16:49:19 +00:00
|
|
|
max_length=32,
|
|
|
|
choices=TYPES,
|
2020-04-30 20:27:21 +00:00
|
|
|
help_text=_("Type of RADIUS authentication: inactive, MAC-address or 802.1X."),
|
2019-11-04 16:55:03 +00:00
|
|
|
verbose_name=_("RADIUS type"),
|
2018-05-26 22:36:25 +00:00
|
|
|
)
|
|
|
|
radius_mode = models.CharField(
|
2018-06-26 16:49:19 +00:00
|
|
|
max_length=32,
|
|
|
|
choices=MODES,
|
2019-11-04 16:55:03 +00:00
|
|
|
default="COMMON",
|
|
|
|
help_text=_(
|
2019-11-16 14:14:22 +00:00
|
|
|
"In case of MAC-authentication: mode COMMON or STRICT on this port."
|
2019-11-04 16:55:03 +00:00
|
|
|
),
|
|
|
|
verbose_name=_("RADIUS mode"),
|
2018-06-26 16:49:19 +00:00
|
|
|
)
|
|
|
|
speed = models.CharField(
|
2019-11-16 14:14:22 +00:00
|
|
|
max_length=32, choices=SPEED, default="auto", help_text=_("Port speed limit.")
|
2018-06-26 16:49:19 +00:00
|
|
|
)
|
|
|
|
mac_limit = models.IntegerField(
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
2019-11-16 14:14:22 +00:00
|
|
|
help_text=_("Limit of MAC-address on this port."),
|
2019-11-04 16:55:03 +00:00
|
|
|
verbose_name=_("MAC limit"),
|
2018-06-26 16:49:19 +00:00
|
|
|
)
|
2019-11-16 14:14:22 +00:00
|
|
|
flow_control = models.BooleanField(default=False, help_text=_("Flow control."))
|
2018-06-26 16:49:19 +00:00
|
|
|
dhcp_snooping = models.BooleanField(
|
|
|
|
default=False,
|
2019-11-16 14:14:22 +00:00
|
|
|
help_text=_("Protect against rogue DHCP."),
|
2019-11-04 16:55:03 +00:00
|
|
|
verbose_name=_("DHCP snooping"),
|
2018-06-26 16:49:19 +00:00
|
|
|
)
|
|
|
|
dhcpv6_snooping = models.BooleanField(
|
|
|
|
default=False,
|
2019-11-16 14:14:22 +00:00
|
|
|
help_text=_("Protect against rogue DHCPv6."),
|
2019-11-04 16:55:03 +00:00
|
|
|
verbose_name=_("DHCPv6 snooping"),
|
2018-06-26 16:49:19 +00:00
|
|
|
)
|
|
|
|
arp_protect = models.BooleanField(
|
|
|
|
default=False,
|
2019-11-16 14:14:22 +00:00
|
|
|
help_text=_("Check if IP address is DHCP assigned."),
|
2019-11-04 16:55:03 +00:00
|
|
|
verbose_name=_("ARP protection"),
|
2018-05-26 22:36:25 +00:00
|
|
|
)
|
2018-06-26 16:49:19 +00:00
|
|
|
ra_guard = models.BooleanField(
|
|
|
|
default=False,
|
2019-11-16 14:14:22 +00:00
|
|
|
help_text=_("Protect against rogue RA."),
|
2019-11-04 16:55:03 +00:00
|
|
|
verbose_name=_("RA guard"),
|
2018-07-11 17:37:22 +00:00
|
|
|
)
|
2018-06-26 16:49:19 +00:00
|
|
|
loop_protect = models.BooleanField(
|
|
|
|
default=False,
|
2019-11-16 14:14:22 +00:00
|
|
|
help_text=_("Protect against loop."),
|
|
|
|
verbose_name=_("loop protection"),
|
2018-07-11 17:37:22 +00:00
|
|
|
)
|
2018-05-26 22:36:25 +00:00
|
|
|
|
|
|
|
class Meta:
|
2020-04-19 14:42:17 +00:00
|
|
|
permissions = (("view_portprofile", _("Can view a port profile object")),)
|
2018-08-05 16:48:56 +00:00
|
|
|
verbose_name = _("port profile")
|
|
|
|
verbose_name_plural = _("port profiles")
|
2019-11-04 16:55:03 +00:00
|
|
|
unique_together = ["on_dormitory", "profil_default"]
|
2018-05-26 22:36:25 +00:00
|
|
|
|
2018-07-11 17:37:22 +00:00
|
|
|
security_parameters_fields = [
|
2019-11-04 16:55:03 +00:00
|
|
|
"loop_protect",
|
|
|
|
"ra_guard",
|
|
|
|
"arp_protect",
|
|
|
|
"dhcpv6_snooping",
|
|
|
|
"dhcp_snooping",
|
|
|
|
"flow_control",
|
2018-07-11 17:37:22 +00:00
|
|
|
]
|
2018-06-26 23:29:40 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def security_parameters_enabled(self):
|
2018-07-11 17:37:22 +00:00
|
|
|
return [
|
|
|
|
parameter
|
|
|
|
for parameter in self.security_parameters_fields
|
|
|
|
if getattr(self, parameter)
|
|
|
|
]
|
2018-06-26 23:29:40 +00:00
|
|
|
|
2018-06-30 22:17:24 +00:00
|
|
|
@cached_property
|
|
|
|
def security_parameters_as_str(self):
|
2019-11-04 16:55:03 +00:00
|
|
|
return ",".join(self.security_parameters_enabled)
|
2018-06-30 22:17:24 +00:00
|
|
|
|
2019-07-20 23:02:32 +00:00
|
|
|
def clean(self):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Check that there is only one generic profile default."""
|
2019-07-20 23:02:32 +00:00
|
|
|
super(PortProfile, self).clean()
|
2019-11-04 16:55:03 +00:00
|
|
|
if (
|
|
|
|
self.profil_default
|
|
|
|
and not self.on_dormitory
|
|
|
|
and PortProfile.objects.exclude(id=self.id)
|
|
|
|
.filter(profil_default=self.profil_default)
|
|
|
|
.exclude(on_dormitory__isnull=False)
|
|
|
|
.exists()
|
|
|
|
):
|
2019-07-20 23:02:32 +00:00
|
|
|
raise ValidationError(
|
2019-11-04 16:55:03 +00:00
|
|
|
{
|
|
|
|
"profil_default": _(
|
2019-11-16 14:14:22 +00:00
|
|
|
"A default profile for all dormitories of that type already exists."
|
2019-11-04 16:55:03 +00:00
|
|
|
)
|
|
|
|
}
|
2019-07-20 23:02:32 +00:00
|
|
|
)
|
|
|
|
|
2018-05-26 22:36:25 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
2018-03-27 00:54:29 +00:00
|
|
|
@receiver(post_save, sender=AccessPoint)
|
2018-04-15 01:00:05 +00:00
|
|
|
def ap_post_save(**_kwargs):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Regenerate the AP names towards the controller."""
|
2019-11-04 16:55:03 +00:00
|
|
|
regen("unifi-ap-names")
|
2018-05-21 19:13:44 +00:00
|
|
|
regen("graph_topo")
|
2018-03-27 00:54:29 +00:00
|
|
|
|
2018-07-11 17:37:22 +00:00
|
|
|
|
2018-03-27 00:54:29 +00:00
|
|
|
@receiver(post_delete, sender=AccessPoint)
|
2018-04-15 01:00:05 +00:00
|
|
|
def ap_post_delete(**_kwargs):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Regenerate the AP names towards the controller."""
|
2019-11-04 16:55:03 +00:00
|
|
|
regen("unifi-ap-names")
|
2018-05-21 19:13:44 +00:00
|
|
|
regen("graph_topo")
|
2018-03-27 00:54:29 +00:00
|
|
|
|
2018-07-11 17:37:22 +00:00
|
|
|
|
2017-08-18 12:20:51 +00:00
|
|
|
@receiver(post_delete, sender=Stack)
|
2018-04-15 01:00:05 +00:00
|
|
|
def stack_post_delete(**_kwargs):
|
2020-04-29 13:00:47 +00:00
|
|
|
"""Empty the stack member ID of switches when a stack is deleted."""
|
2017-10-13 22:00:16 +00:00
|
|
|
Switch.objects.filter(stack=None).update(stack_member_id=None)
|
2018-05-21 19:13:44 +00:00
|
|
|
|
2018-07-11 17:37:22 +00:00
|
|
|
|
2018-05-21 19:13:44 +00:00
|
|
|
@receiver(post_save, sender=Port)
|
|
|
|
def port_post_save(**_kwargs):
|
|
|
|
regen("graph_topo")
|
|
|
|
|
2018-07-11 17:37:22 +00:00
|
|
|
|
2018-05-21 19:13:44 +00:00
|
|
|
@receiver(post_delete, sender=Port)
|
|
|
|
def port_post_delete(**_kwargs):
|
|
|
|
regen("graph_topo")
|
|
|
|
|
2018-07-11 17:37:22 +00:00
|
|
|
|
2018-05-21 19:13:44 +00:00
|
|
|
@receiver(post_save, sender=ModelSwitch)
|
|
|
|
def modelswitch_post_save(**_kwargs):
|
|
|
|
regen("graph_topo")
|
|
|
|
|
2018-07-11 17:37:22 +00:00
|
|
|
|
2018-05-21 19:13:44 +00:00
|
|
|
@receiver(post_delete, sender=ModelSwitch)
|
|
|
|
def modelswitch_post_delete(**_kwargs):
|
|
|
|
regen("graph_topo")
|
|
|
|
|
2018-07-11 17:37:22 +00:00
|
|
|
|
2018-05-21 19:13:44 +00:00
|
|
|
@receiver(post_save, sender=Building)
|
|
|
|
def building_post_save(**_kwargs):
|
|
|
|
regen("graph_topo")
|
|
|
|
|
2018-07-11 17:37:22 +00:00
|
|
|
|
2018-05-21 19:13:44 +00:00
|
|
|
@receiver(post_delete, sender=Building)
|
|
|
|
def building_post_delete(**_kwargs):
|
|
|
|
regen("graph_topo")
|
|
|
|
|
2018-07-11 17:37:22 +00:00
|
|
|
|
2018-05-21 19:13:44 +00:00
|
|
|
@receiver(post_save, sender=Switch)
|
|
|
|
def switch_post_save(**_kwargs):
|
|
|
|
regen("graph_topo")
|
|
|
|
|
2018-07-11 17:37:22 +00:00
|
|
|
|
2018-05-21 19:13:44 +00:00
|
|
|
@receiver(post_delete, sender=Switch)
|
|
|
|
def switch_post_delete(**_kwargs):
|
|
|
|
regen("graph_topo")
|