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 |
Hardik Windlass | da0d6c4 | 2019-05-13 16:00:01 +0000 | [diff] [blame^] | 28 | from models_decl import TechnologyProfile_decl |
| 29 | |
| 30 | import json |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 31 | |
| 32 | class VOLTService(VOLTService_decl): |
| 33 | class Meta: |
| 34 | proxy = True |
| 35 | |
| 36 | @staticmethod |
| 37 | def has_access_device(serial_number): |
| 38 | try: |
| 39 | ONUDevice.objects.get(serial_number=serial_number) |
| 40 | return True |
| 41 | except IndexError, e: |
| 42 | return False |
| 43 | |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 44 | class VOLTServiceInstance(VOLTServiceInstance_decl): |
| 45 | class Meta: |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 46 | proxy = True |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 47 | |
| 48 | class OLTDevice(OLTDevice_decl): |
| 49 | class Meta: |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 50 | proxy = True |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 51 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 52 | def get_volt_si(self): |
| 53 | return VOLTServiceInstance.objects.all() |
| 54 | |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 55 | def save(self, *args, **kwargs): |
| 56 | |
| 57 | if (self.host or self.port) and self.mac_address: |
| 58 | 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)) |
| 59 | |
| 60 | super(OLTDevice, self).save(*args, **kwargs) |
| 61 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 62 | def delete(self, *args, **kwargs): |
| 63 | |
| 64 | onus = [] |
| 65 | pon_ports = self.pon_ports.all() |
| 66 | for port in pon_ports: |
| 67 | onus = onus + list(port.onu_devices.all()) |
| 68 | |
| 69 | |
| 70 | if len(onus) > 0: |
| 71 | onus = [o.id for o in onus] |
| 72 | |
| 73 | # find the ONUs used by VOLTServiceInstances |
| 74 | used_onus = [o.onu_device_id for o in self.get_volt_si()] |
| 75 | |
| 76 | # find the intersection between the onus associated with this OLT and the used one |
| 77 | used_onus_to_delete = [o for o in onus if o in used_onus] |
| 78 | |
| 79 | if len(used_onus_to_delete) > 0: |
| 80 | if hasattr(self, "device_id") and self.device_id: |
| 81 | item = self.device_id |
| 82 | elif hasattr(self, "name") and self.name: |
| 83 | item = self.name |
| 84 | else: |
| 85 | item = self.id |
| 86 | raise XOSValidationError('OLT "%s" can\'t be deleted as it has subscribers associated with its ONUs' % item) |
| 87 | |
| 88 | super(OLTDevice, self).delete(*args, **kwargs) |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 89 | |
Scott Baker | 04c5ec3 | 2018-07-12 14:01:06 -0700 | [diff] [blame] | 90 | class PortBase(PortBase_decl): |
| 91 | class Meta: |
| 92 | proxy = True |
| 93 | |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 94 | class PONPort(PONPort_decl): |
| 95 | class Meta: |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 96 | proxy = True |
| 97 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 98 | class NNIPort(NNIPort_decl): |
| 99 | class Meta: |
| 100 | proxy = True |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 101 | |
| 102 | |
| 103 | class ONUDevice(ONUDevice_decl): |
| 104 | class Meta: |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 105 | proxy = True |
| 106 | |
Matteo Scandolo | 33fb162 | 2018-06-19 18:54:35 -0700 | [diff] [blame] | 107 | def delete(self, *args, **kwargs): |
| 108 | |
| 109 | if len(self.volt_service_instances.all()) > 0: |
| 110 | raise XOSValidationError('ONU "%s" can\'t be deleted as it has subscribers associated with it' % self.serial_number) |
| 111 | |
| 112 | super(ONUDevice, self).delete(*args, **kwargs) |
| 113 | |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 114 | class PONONUPort(PONONUPort_decl): |
| 115 | class Meta: |
| 116 | proxy = True |
| 117 | |
| 118 | class UNIPort(UNIPort_decl): |
| 119 | class Meta: |
| 120 | proxy = True |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 121 | |
Hardik Windlass | da0d6c4 | 2019-05-13 16:00:01 +0000 | [diff] [blame^] | 122 | class TechnologyProfile(TechnologyProfile_decl): |
| 123 | class Meta: |
| 124 | proxy = True |
| 125 | |
| 126 | def save(self, *args, **kwargs): |
| 127 | |
| 128 | caller_kind = None |
| 129 | if "caller_kind" in kwargs: |
| 130 | caller_kind = kwargs.get("caller_kind") |
| 131 | |
| 132 | # only synchronizer is allowed to update the model |
| 133 | if not self.is_new and caller_kind != "synchronizer": |
| 134 | if not self.deleted: |
| 135 | existing = TechnologyProfile.objects.filter(id=self.id) |
| 136 | raise XOSValidationError('Modification operation is not allowed on Technology Profile [/%s/%s]. Delete it and add again' % (existing[0].technology, existing[0].profile_id)) |
| 137 | |
| 138 | # validate if technology profile value is valid JSON format string |
| 139 | if self.profile_value != None: |
| 140 | try: |
| 141 | tp_json_val = json.loads(self.profile_value) |
| 142 | except ValueError as e: |
| 143 | raise XOSValidationError('Technology Profile value not in valid JSON format') |
| 144 | |
| 145 | super(TechnologyProfile, self).save(*args, **kwargs) |
| 146 | |