blob: 3dcefae89a325a81bfcc2ad7719021e101565b92 [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
Matteo Scandolo2ed64b92018-06-18 10:32:56 -070049 def save(self, *args, **kwargs):
50
51 if (self.host or self.port) and self.mac_address:
52 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))
53
54 super(OLTDevice, self).save(*args, **kwargs)
55
Matteo Scandolo2360fd92018-05-29 17:27:51 -070056 def delete(self, *args, **kwargs):
57
58 onus = []
59 pon_ports = self.pon_ports.all()
60 for port in pon_ports:
61 onus = onus + list(port.onu_devices.all())
62
63
64 if len(onus) > 0:
65 onus = [o.id for o in onus]
66
67 # find the ONUs used by VOLTServiceInstances
68 used_onus = [o.onu_device_id for o in self.get_volt_si()]
69
70 # find the intersection between the onus associated with this OLT and the used one
71 used_onus_to_delete = [o for o in onus if o in used_onus]
72
73 if len(used_onus_to_delete) > 0:
74 if hasattr(self, "device_id") and self.device_id:
75 item = self.device_id
76 elif hasattr(self, "name") and self.name:
77 item = self.name
78 else:
79 item = self.id
80 raise XOSValidationError('OLT "%s" can\'t be deleted as it has subscribers associated with its ONUs' % item)
81
82 super(OLTDevice, self).delete(*args, **kwargs)
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070083
84class PONPort(PONPort_decl):
85 class Meta:
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070086 proxy = True
87
88 def generate_tag(self):
89 # NOTE this method will loop if available c_tags are ended
90 tag = random.randint(16, 4096)
91 if tag in self.get_used_s_tags():
92 return self.generate_tag()
93 return tag
94
95 def get_used_s_tags(self):
96 same_olt_device = OLTDevice.objects.filter(device_id=self.olt_device)
97 return [s.c_tag for s in same_olt_device]
98
99 def save(self, *args, **kwargs):
100 # validate s_tag
101 if hasattr(self, 's_tag') and self.s_tag is not None:
102 is_update_with_same_tag = False
103
104 if not self.is_new:
105 # if it is an update, but the tag is the same, skip validation
106 existing = PONPort.objects.filter(s_tag=self.s_tag)
107
108 if len(existing) > 0 and existing[0].s_tag == self.s_tag and existing[0].id == self.id:
109 is_update_with_same_tag = True
110
111 if self.s_tag in self.get_used_s_tags() and not is_update_with_same_tag:
112 raise XOSValidationError(
113 "The s_tag you specified (%s) has already been used on device %s" % (self.s_tag, self.onu_device))
114
115 if not hasattr(self, "s_tag") or self.s_tag is None:
116 self.s_tag = self.generate_tag()
117
118 super(PONPort, self).save(*args, **kwargs)
119
120
121class NNIPort(NNIPort_decl):
122 class Meta:
123 proxy = True
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -0700124
125
126class ONUDevice(ONUDevice_decl):
127 class Meta:
128 proxy = True
129