blob: ed1e06276c51db26fe3e689a0ba60b982e2a68d3 [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 Scandolo6be6ee92018-05-24 15:07:51 -070017from xos.exceptions import XOSValidationError
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070018
19from models_decl import VOLTService_decl
20from models_decl import VOLTServiceInstance_decl
21from models_decl import OLTDevice_decl
22from models_decl import PONPort_decl
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070023from models_decl import NNIPort_decl
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070024from models_decl import ONUDevice_decl
25
26class VOLTService(VOLTService_decl):
27 class Meta:
28 proxy = True
29
30 @staticmethod
31 def has_access_device(serial_number):
32 try:
33 ONUDevice.objects.get(serial_number=serial_number)
34 return True
35 except IndexError, e:
36 return False
37
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070038class VOLTServiceInstance(VOLTServiceInstance_decl):
39 class Meta:
Matteo Scandolo2360fd92018-05-29 17:27:51 -070040 proxy = True
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070041
42class OLTDevice(OLTDevice_decl):
43 class Meta:
Matteo Scandolo2360fd92018-05-29 17:27:51 -070044 proxy = True
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070045
Matteo Scandolo2360fd92018-05-29 17:27:51 -070046 def get_volt_si(self):
47 return VOLTServiceInstance.objects.all()
48
49 def delete(self, *args, **kwargs):
50
51 onus = []
52 pon_ports = self.pon_ports.all()
53 for port in pon_ports:
54 onus = onus + list(port.onu_devices.all())
55
56
57 if len(onus) > 0:
58 onus = [o.id for o in onus]
59
60 # find the ONUs used by VOLTServiceInstances
61 used_onus = [o.onu_device_id for o in self.get_volt_si()]
62
63 # find the intersection between the onus associated with this OLT and the used one
64 used_onus_to_delete = [o for o in onus if o in used_onus]
65
66 if len(used_onus_to_delete) > 0:
67 if hasattr(self, "device_id") and self.device_id:
68 item = self.device_id
69 elif hasattr(self, "name") and self.name:
70 item = self.name
71 else:
72 item = self.id
73 raise XOSValidationError('OLT "%s" can\'t be deleted as it has subscribers associated with its ONUs' % item)
74
75 super(OLTDevice, self).delete(*args, **kwargs)
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070076
77class PONPort(PONPort_decl):
78 class Meta:
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070079 proxy = True
80
81 def generate_tag(self):
82 # NOTE this method will loop if available c_tags are ended
83 tag = random.randint(16, 4096)
84 if tag in self.get_used_s_tags():
85 return self.generate_tag()
86 return tag
87
88 def get_used_s_tags(self):
89 same_olt_device = OLTDevice.objects.filter(device_id=self.olt_device)
90 return [s.c_tag for s in same_olt_device]
91
92 def save(self, *args, **kwargs):
93 # validate s_tag
94 if hasattr(self, 's_tag') and self.s_tag is not None:
95 is_update_with_same_tag = False
96
97 if not self.is_new:
98 # if it is an update, but the tag is the same, skip validation
99 existing = PONPort.objects.filter(s_tag=self.s_tag)
100
101 if len(existing) > 0 and existing[0].s_tag == self.s_tag and existing[0].id == self.id:
102 is_update_with_same_tag = True
103
104 if self.s_tag in self.get_used_s_tags() and not is_update_with_same_tag:
105 raise XOSValidationError(
106 "The s_tag you specified (%s) has already been used on device %s" % (self.s_tag, self.onu_device))
107
108 if not hasattr(self, "s_tag") or self.s_tag is None:
109 self.s_tag = self.generate_tag()
110
111 super(PONPort, self).save(*args, **kwargs)
112
113
114class NNIPort(NNIPort_decl):
115 class Meta:
116 proxy = True
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -0700117
118
119class ONUDevice(ONUDevice_decl):
120 class Meta:
121 proxy = True
122