Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 15 | import random |
| 16 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 17 | from xos.exceptions import XOSValidationError |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 18 | |
| 19 | from models_decl import VOLTService_decl |
| 20 | from models_decl import VOLTServiceInstance_decl |
| 21 | from models_decl import OLTDevice_decl |
Scott Baker | 04c5ec3 | 2018-07-12 14:01:06 -0700 | [diff] [blame] | 22 | from models_decl import PortBase_decl |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 23 | from models_decl import PONPort_decl |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 24 | from models_decl import NNIPort_decl |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 25 | from models_decl import ONUDevice_decl |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 26 | from models_decl import PONONUPort_decl |
| 27 | from models_decl import UNIPort_decl |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 28 | |
| 29 | class VOLTService(VOLTService_decl): |
| 30 | class Meta: |
| 31 | proxy = True |
| 32 | |
| 33 | @staticmethod |
| 34 | def has_access_device(serial_number): |
| 35 | try: |
| 36 | ONUDevice.objects.get(serial_number=serial_number) |
| 37 | return True |
| 38 | except IndexError, e: |
| 39 | return False |
| 40 | |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 41 | class VOLTServiceInstance(VOLTServiceInstance_decl): |
| 42 | class Meta: |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 43 | proxy = True |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 44 | |
| 45 | class OLTDevice(OLTDevice_decl): |
| 46 | class Meta: |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 47 | proxy = True |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 48 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 49 | def get_volt_si(self): |
| 50 | return VOLTServiceInstance.objects.all() |
| 51 | |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 52 | def save(self, *args, **kwargs): |
| 53 | |
| 54 | if (self.host or self.port) and self.mac_address: |
| 55 | raise XOSValidationError("You can't specify both host/port and mac_address for OLTDevice [host=%s, port=%s, mac_address=%s]" % (self.host, self.port, self.mac_address)) |
| 56 | |
| 57 | super(OLTDevice, self).save(*args, **kwargs) |
| 58 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 59 | def delete(self, *args, **kwargs): |
| 60 | |
| 61 | onus = [] |
| 62 | pon_ports = self.pon_ports.all() |
| 63 | for port in pon_ports: |
| 64 | onus = onus + list(port.onu_devices.all()) |
| 65 | |
| 66 | |
| 67 | if len(onus) > 0: |
| 68 | onus = [o.id for o in onus] |
| 69 | |
| 70 | # find the ONUs used by VOLTServiceInstances |
| 71 | used_onus = [o.onu_device_id for o in self.get_volt_si()] |
| 72 | |
| 73 | # find the intersection between the onus associated with this OLT and the used one |
| 74 | used_onus_to_delete = [o for o in onus if o in used_onus] |
| 75 | |
| 76 | if len(used_onus_to_delete) > 0: |
| 77 | if hasattr(self, "device_id") and self.device_id: |
| 78 | item = self.device_id |
| 79 | elif hasattr(self, "name") and self.name: |
| 80 | item = self.name |
| 81 | else: |
| 82 | item = self.id |
| 83 | raise XOSValidationError('OLT "%s" can\'t be deleted as it has subscribers associated with its ONUs' % item) |
| 84 | |
| 85 | super(OLTDevice, self).delete(*args, **kwargs) |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 86 | |
Scott Baker | 04c5ec3 | 2018-07-12 14:01:06 -0700 | [diff] [blame] | 87 | class PortBase(PortBase_decl): |
| 88 | class Meta: |
| 89 | proxy = True |
| 90 | |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 91 | class PONPort(PONPort_decl): |
| 92 | class Meta: |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 93 | proxy = True |
| 94 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 95 | class NNIPort(NNIPort_decl): |
| 96 | class Meta: |
| 97 | proxy = True |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 98 | |
| 99 | |
| 100 | class ONUDevice(ONUDevice_decl): |
| 101 | class Meta: |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 102 | proxy = True |
| 103 | |
Matteo Scandolo | 33fb162 | 2018-06-19 18:54:35 -0700 | [diff] [blame] | 104 | def delete(self, *args, **kwargs): |
| 105 | |
| 106 | if len(self.volt_service_instances.all()) > 0: |
| 107 | raise XOSValidationError('ONU "%s" can\'t be deleted as it has subscribers associated with it' % self.serial_number) |
| 108 | |
| 109 | super(ONUDevice, self).delete(*args, **kwargs) |
| 110 | |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 111 | class PONONUPort(PONONUPort_decl): |
| 112 | class Meta: |
| 113 | proxy = True |
| 114 | |
| 115 | class UNIPort(UNIPort_decl): |
| 116 | class Meta: |
| 117 | proxy = True |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 118 | |