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 |
| 22 | from models_decl import PONPort_decl |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 23 | from models_decl import NNIPort_decl |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 24 | from models_decl import ONUDevice_decl |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame^] | 25 | from models_decl import PONONUPort_decl |
| 26 | from models_decl import UNIPort_decl |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 27 | |
| 28 | class VOLTService(VOLTService_decl): |
| 29 | class Meta: |
| 30 | proxy = True |
| 31 | |
| 32 | @staticmethod |
| 33 | def has_access_device(serial_number): |
| 34 | try: |
| 35 | ONUDevice.objects.get(serial_number=serial_number) |
| 36 | return True |
| 37 | except IndexError, e: |
| 38 | return False |
| 39 | |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 40 | class VOLTServiceInstance(VOLTServiceInstance_decl): |
| 41 | class Meta: |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 42 | proxy = True |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 43 | |
| 44 | class OLTDevice(OLTDevice_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 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 48 | def get_volt_si(self): |
| 49 | return VOLTServiceInstance.objects.all() |
| 50 | |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 51 | def save(self, *args, **kwargs): |
| 52 | |
| 53 | if (self.host or self.port) and self.mac_address: |
| 54 | 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)) |
| 55 | |
| 56 | super(OLTDevice, self).save(*args, **kwargs) |
| 57 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 58 | def delete(self, *args, **kwargs): |
| 59 | |
| 60 | onus = [] |
| 61 | pon_ports = self.pon_ports.all() |
| 62 | for port in pon_ports: |
| 63 | onus = onus + list(port.onu_devices.all()) |
| 64 | |
| 65 | |
| 66 | if len(onus) > 0: |
| 67 | onus = [o.id for o in onus] |
| 68 | |
| 69 | # find the ONUs used by VOLTServiceInstances |
| 70 | used_onus = [o.onu_device_id for o in self.get_volt_si()] |
| 71 | |
| 72 | # find the intersection between the onus associated with this OLT and the used one |
| 73 | used_onus_to_delete = [o for o in onus if o in used_onus] |
| 74 | |
| 75 | if len(used_onus_to_delete) > 0: |
| 76 | if hasattr(self, "device_id") and self.device_id: |
| 77 | item = self.device_id |
| 78 | elif hasattr(self, "name") and self.name: |
| 79 | item = self.name |
| 80 | else: |
| 81 | item = self.id |
| 82 | raise XOSValidationError('OLT "%s" can\'t be deleted as it has subscribers associated with its ONUs' % item) |
| 83 | |
| 84 | super(OLTDevice, self).delete(*args, **kwargs) |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 85 | |
| 86 | class PONPort(PONPort_decl): |
| 87 | class Meta: |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 88 | proxy = True |
| 89 | |
| 90 | def generate_tag(self): |
| 91 | # NOTE this method will loop if available c_tags are ended |
| 92 | tag = random.randint(16, 4096) |
| 93 | if tag in self.get_used_s_tags(): |
| 94 | return self.generate_tag() |
| 95 | return tag |
| 96 | |
| 97 | def get_used_s_tags(self): |
| 98 | same_olt_device = OLTDevice.objects.filter(device_id=self.olt_device) |
| 99 | return [s.c_tag for s in same_olt_device] |
| 100 | |
| 101 | def save(self, *args, **kwargs): |
| 102 | # validate s_tag |
| 103 | if hasattr(self, 's_tag') and self.s_tag is not None: |
| 104 | is_update_with_same_tag = False |
| 105 | |
| 106 | if not self.is_new: |
| 107 | # if it is an update, but the tag is the same, skip validation |
| 108 | existing = PONPort.objects.filter(s_tag=self.s_tag) |
| 109 | |
| 110 | if len(existing) > 0 and existing[0].s_tag == self.s_tag and existing[0].id == self.id: |
| 111 | is_update_with_same_tag = True |
| 112 | |
| 113 | if self.s_tag in self.get_used_s_tags() and not is_update_with_same_tag: |
| 114 | raise XOSValidationError( |
| 115 | "The s_tag you specified (%s) has already been used on device %s" % (self.s_tag, self.onu_device)) |
| 116 | |
| 117 | if not hasattr(self, "s_tag") or self.s_tag is None: |
| 118 | self.s_tag = self.generate_tag() |
| 119 | |
| 120 | super(PONPort, self).save(*args, **kwargs) |
| 121 | |
| 122 | |
| 123 | class NNIPort(NNIPort_decl): |
| 124 | class Meta: |
| 125 | proxy = True |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 126 | |
| 127 | |
| 128 | class ONUDevice(ONUDevice_decl): |
| 129 | class Meta: |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame^] | 130 | proxy = True |
| 131 | |
| 132 | class PONONUPort(PONONUPort_decl): |
| 133 | class Meta: |
| 134 | proxy = True |
| 135 | |
| 136 | class UNIPort(UNIPort_decl): |
| 137 | class Meta: |
| 138 | proxy = True |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 139 | |