blob: 943148834545905725c624d035f90c8a87d01363 [file] [log] [blame]
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -07001# 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 Scandolo6be6ee92018-05-24 15:07:51 -070015import random
16
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070017from core.models.xosbase import *
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070018from xos.exceptions import XOSValidationError
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070019
20from models_decl import VOLTService_decl
21from models_decl import VOLTServiceInstance_decl
22from models_decl import OLTDevice_decl
23from models_decl import PONPort_decl
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070024from models_decl import NNIPort_decl
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070025from models_decl import ONUDevice_decl
26
27class 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
40class VOLTServiceInstance(VOLTServiceInstance_decl):
41 class Meta:
42 proxy = True
43
44
45class OLTDevice(OLTDevice_decl):
46 class Meta:
47 proxy = True
48
49
50class PONPort(PONPort_decl):
51 class Meta:
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070052 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
87class NNIPort(NNIPort_decl):
88 class Meta:
89 proxy = True
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070090
91
92class ONUDevice(ONUDevice_decl):
93 class Meta:
94 proxy = True
95