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 | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 17 | from core.models.xosbase import * |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame^] | 18 | from xos.exceptions import XOSValidationError |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 19 | |
| 20 | from models_decl import VOLTService_decl |
| 21 | from models_decl import VOLTServiceInstance_decl |
| 22 | from models_decl import OLTDevice_decl |
| 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 |
| 26 | |
| 27 | class VOLTService(VOLTService_decl): |
| 28 | class Meta: |
| 29 | proxy = True |
| 30 | |
| 31 | @staticmethod |
| 32 | def has_access_device(serial_number): |
| 33 | try: |
| 34 | ONUDevice.objects.get(serial_number=serial_number) |
| 35 | return True |
| 36 | except IndexError, e: |
| 37 | return False |
| 38 | |
| 39 | |
| 40 | class VOLTServiceInstance(VOLTServiceInstance_decl): |
| 41 | class Meta: |
| 42 | proxy = True |
| 43 | |
| 44 | |
| 45 | class OLTDevice(OLTDevice_decl): |
| 46 | class Meta: |
| 47 | proxy = True |
| 48 | |
| 49 | |
| 50 | class PONPort(PONPort_decl): |
| 51 | class Meta: |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame^] | 52 | proxy = True |
| 53 | |
| 54 | def generate_tag(self): |
| 55 | # NOTE this method will loop if available c_tags are ended |
| 56 | tag = random.randint(16, 4096) |
| 57 | if tag in self.get_used_s_tags(): |
| 58 | return self.generate_tag() |
| 59 | return tag |
| 60 | |
| 61 | def get_used_s_tags(self): |
| 62 | same_olt_device = OLTDevice.objects.filter(device_id=self.olt_device) |
| 63 | return [s.c_tag for s in same_olt_device] |
| 64 | |
| 65 | def save(self, *args, **kwargs): |
| 66 | # validate s_tag |
| 67 | if hasattr(self, 's_tag') and self.s_tag is not None: |
| 68 | is_update_with_same_tag = False |
| 69 | |
| 70 | if not self.is_new: |
| 71 | # if it is an update, but the tag is the same, skip validation |
| 72 | existing = PONPort.objects.filter(s_tag=self.s_tag) |
| 73 | |
| 74 | if len(existing) > 0 and existing[0].s_tag == self.s_tag and existing[0].id == self.id: |
| 75 | is_update_with_same_tag = True |
| 76 | |
| 77 | if self.s_tag in self.get_used_s_tags() and not is_update_with_same_tag: |
| 78 | raise XOSValidationError( |
| 79 | "The s_tag you specified (%s) has already been used on device %s" % (self.s_tag, self.onu_device)) |
| 80 | |
| 81 | if not hasattr(self, "s_tag") or self.s_tag is None: |
| 82 | self.s_tag = self.generate_tag() |
| 83 | |
| 84 | super(PONPort, self).save(*args, **kwargs) |
| 85 | |
| 86 | |
| 87 | class NNIPort(NNIPort_decl): |
| 88 | class Meta: |
| 89 | proxy = True |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 90 | |
| 91 | |
| 92 | class ONUDevice(ONUDevice_decl): |
| 93 | class Meta: |
| 94 | proxy = True |
| 95 | |