blob: d5a3cc564b734753ee5a205c55eac31450617df4 [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 Scandoloe73cd442019-06-20 21:37:22 -070017from xos.exceptions import XOSValidationError, XOSNotFound
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070018
19from models_decl import VOLTService_decl
Scott Baker2e713b72019-06-11 16:41:37 -070020from models_decl import VOLTServiceInstance_decl, VOLTServiceInstance
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070021from models_decl import OLTDevice_decl
Scott Baker2e713b72019-06-11 16:41:37 -070022from models_decl import PortBase_decl, PortBase
23from models_decl import PONPort_decl, PONPort
24from models_decl import NNIPort_decl, NNIPort
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070025from models_decl import ONUDevice_decl
Scott Baker2e713b72019-06-11 16:41:37 -070026from models_decl import ANIPort_decl, ANIPort
27from models_decl import UNIPort_decl, UNIPort
Hardik Windlassda0d6c42019-05-13 16:00:01 +000028from models_decl import TechnologyProfile_decl
Matteo Scandolo1dc711d2019-06-20 17:04:46 -070029from django.core.exceptions import ObjectDoesNotExist
Hardik Windlassda0d6c42019-05-13 16:00:01 +000030
31import json
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070032
Matteo Scandolo045b04d2019-06-27 18:28:42 -070033
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070034class VOLTService(VOLTService_decl):
35 class Meta:
36 proxy = True
37
38 @staticmethod
39 def has_access_device(serial_number):
40 try:
41 ONUDevice.objects.get(serial_number=serial_number)
42 return True
43 except IndexError, e:
44 return False
45
Matteo Scandolo1dc711d2019-06-20 17:04:46 -070046 def get_olt_technology_from_unu_sn(self, onu_sn):
47 """
48 Return the technology assigned to an OLT Device given and ONU Serial Number
49 example usage:
50 volt = VOLTService.objects.first()
51 sn = volt.get_olt_technology_from_unu_sn("BRCM12345678")
52 # "XGSPON"
53
54 Arguments:
55 onu_sn {string} -- The ONU Serial Number
56
57 Returns:
58 string -- Technology
59 """
60 try:
61 onu = ONUDevice.objects.get(serial_number=onu_sn)
62 olt = onu.pon_port.olt_device
63 return olt.technology
64 except ObjectDoesNotExist:
Matteo Scandoloe73cd442019-06-20 21:37:22 -070065 raise XOSNotFound("Can't find OLT for %s" % onu_sn)
Matteo Scandolo1dc711d2019-06-20 17:04:46 -070066
67 def get_tech_profile(self, technology, profile_id):
68 """
69 Returns a Technology profiles or raise an Exception (DoesNotExist)
70 :param technology: string
71 :param profile_id: int
72 :return: TechnologyProfile
73 """
74 return TechnologyProfile.objects.get(technology=technology, profile_id=profile_id)
75
Matteo Scandolo045b04d2019-06-27 18:28:42 -070076
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070077class OLTDevice(OLTDevice_decl):
78 class Meta:
Matteo Scandolo2360fd92018-05-29 17:27:51 -070079 proxy = True
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070080
Matteo Scandolo2360fd92018-05-29 17:27:51 -070081 def get_volt_si(self):
82 return VOLTServiceInstance.objects.all()
83
Matteo Scandolo2ed64b92018-06-18 10:32:56 -070084 def save(self, *args, **kwargs):
85
86 if (self.host or self.port) and self.mac_address:
87 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))
88
89 super(OLTDevice, self).save(*args, **kwargs)
90
Matteo Scandolo2360fd92018-05-29 17:27:51 -070091 def delete(self, *args, **kwargs):
92
93 onus = []
94 pon_ports = self.pon_ports.all()
95 for port in pon_ports:
96 onus = onus + list(port.onu_devices.all())
97
98
99 if len(onus) > 0:
100 onus = [o.id for o in onus]
101
102 # find the ONUs used by VOLTServiceInstances
103 used_onus = [o.onu_device_id for o in self.get_volt_si()]
104
105 # find the intersection between the onus associated with this OLT and the used one
106 used_onus_to_delete = [o for o in onus if o in used_onus]
107
108 if len(used_onus_to_delete) > 0:
109 if hasattr(self, "device_id") and self.device_id:
110 item = self.device_id
111 elif hasattr(self, "name") and self.name:
112 item = self.name
113 else:
114 item = self.id
115 raise XOSValidationError('OLT "%s" can\'t be deleted as it has subscribers associated with its ONUs' % item)
116
117 super(OLTDevice, self).delete(*args, **kwargs)
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -0700118
Matteo Scandolo045b04d2019-06-27 18:28:42 -0700119
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -0700120class ONUDevice(ONUDevice_decl):
121 class Meta:
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700122 proxy = True
123
Matteo Scandolo33fb1622018-06-19 18:54:35 -0700124 def delete(self, *args, **kwargs):
125
126 if len(self.volt_service_instances.all()) > 0:
127 raise XOSValidationError('ONU "%s" can\'t be deleted as it has subscribers associated with it' % self.serial_number)
128
129 super(ONUDevice, self).delete(*args, **kwargs)
130
Matteo Scandolo045b04d2019-06-27 18:28:42 -0700131
Hardik Windlassda0d6c42019-05-13 16:00:01 +0000132class TechnologyProfile(TechnologyProfile_decl):
133 class Meta:
134 proxy = True
135
136 def save(self, *args, **kwargs):
137
138 caller_kind = None
139 if "caller_kind" in kwargs:
140 caller_kind = kwargs.get("caller_kind")
141
142 # only synchronizer is allowed to update the model
143 if not self.is_new and caller_kind != "synchronizer":
Matteo Scandolo045b04d2019-06-27 18:28:42 -0700144 if not self.deleted and len(self.diff.keys()) > 0:
Hardik Windlassda0d6c42019-05-13 16:00:01 +0000145 existing = TechnologyProfile.objects.filter(id=self.id)
146 raise XOSValidationError('Modification operation is not allowed on Technology Profile [/%s/%s]. Delete it and add again' % (existing[0].technology, existing[0].profile_id))
147
148 # validate if technology profile value is valid JSON format string
149 if self.profile_value != None:
150 try:
151 tp_json_val = json.loads(self.profile_value)
152 except ValueError as e:
153 raise XOSValidationError('Technology Profile value not in valid JSON format')
154
Matteo Scandolo2b4c8472019-06-26 18:06:47 -0700155 # TODO validate the tech profile (in the model), see errors like:
156 # num_gem_ports=tech_profile[TechProfile.NUM_GEM_PORTS],\nKeyError: \'num_gem_ports\''
157 # in File "/voltha/common/tech_profile/tech_profile.py", line 403, in _get_tech_profile
158
Hardik Windlassda0d6c42019-05-13 16:00:01 +0000159 super(TechnologyProfile, self).save(*args, **kwargs)
160