blob: 5114871b8197e2ad35ef4a2df7991e1dec8394c2 [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
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
33class VOLTService(VOLTService_decl):
34 class Meta:
35 proxy = True
36
37 @staticmethod
38 def has_access_device(serial_number):
39 try:
40 ONUDevice.objects.get(serial_number=serial_number)
41 return True
42 except IndexError, e:
43 return False
44
Matteo Scandolo1dc711d2019-06-20 17:04:46 -070045 def get_olt_technology_from_unu_sn(self, onu_sn):
46 """
47 Return the technology assigned to an OLT Device given and ONU Serial Number
48 example usage:
49 volt = VOLTService.objects.first()
50 sn = volt.get_olt_technology_from_unu_sn("BRCM12345678")
51 # "XGSPON"
52
53 Arguments:
54 onu_sn {string} -- The ONU Serial Number
55
56 Returns:
57 string -- Technology
58 """
59 try:
60 onu = ONUDevice.objects.get(serial_number=onu_sn)
61 olt = onu.pon_port.olt_device
62 return olt.technology
63 except ObjectDoesNotExist:
64 raise XOSNotFound("Can't find OLT for %s" % onu_sn, serial_number=onu_sn)
65
66 def get_tech_profile(self, technology, profile_id):
67 """
68 Returns a Technology profiles or raise an Exception (DoesNotExist)
69 :param technology: string
70 :param profile_id: int
71 :return: TechnologyProfile
72 """
73 return TechnologyProfile.objects.get(technology=technology, profile_id=profile_id)
74
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070075class OLTDevice(OLTDevice_decl):
76 class Meta:
Matteo Scandolo2360fd92018-05-29 17:27:51 -070077 proxy = True
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070078
Matteo Scandolo2360fd92018-05-29 17:27:51 -070079 def get_volt_si(self):
80 return VOLTServiceInstance.objects.all()
81
Matteo Scandolo2ed64b92018-06-18 10:32:56 -070082 def save(self, *args, **kwargs):
83
84 if (self.host or self.port) and self.mac_address:
85 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))
86
87 super(OLTDevice, self).save(*args, **kwargs)
88
Matteo Scandolo2360fd92018-05-29 17:27:51 -070089 def delete(self, *args, **kwargs):
90
91 onus = []
92 pon_ports = self.pon_ports.all()
93 for port in pon_ports:
94 onus = onus + list(port.onu_devices.all())
95
96
97 if len(onus) > 0:
98 onus = [o.id for o in onus]
99
100 # find the ONUs used by VOLTServiceInstances
101 used_onus = [o.onu_device_id for o in self.get_volt_si()]
102
103 # find the intersection between the onus associated with this OLT and the used one
104 used_onus_to_delete = [o for o in onus if o in used_onus]
105
106 if len(used_onus_to_delete) > 0:
107 if hasattr(self, "device_id") and self.device_id:
108 item = self.device_id
109 elif hasattr(self, "name") and self.name:
110 item = self.name
111 else:
112 item = self.id
113 raise XOSValidationError('OLT "%s" can\'t be deleted as it has subscribers associated with its ONUs' % item)
114
115 super(OLTDevice, self).delete(*args, **kwargs)
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -0700116
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -0700117class ONUDevice(ONUDevice_decl):
118 class Meta:
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700119 proxy = True
120
Matteo Scandolo33fb1622018-06-19 18:54:35 -0700121 def delete(self, *args, **kwargs):
122
123 if len(self.volt_service_instances.all()) > 0:
124 raise XOSValidationError('ONU "%s" can\'t be deleted as it has subscribers associated with it' % self.serial_number)
125
126 super(ONUDevice, self).delete(*args, **kwargs)
127
Hardik Windlassda0d6c42019-05-13 16:00:01 +0000128class TechnologyProfile(TechnologyProfile_decl):
129 class Meta:
130 proxy = True
131
132 def save(self, *args, **kwargs):
133
134 caller_kind = None
135 if "caller_kind" in kwargs:
136 caller_kind = kwargs.get("caller_kind")
137
138 # only synchronizer is allowed to update the model
139 if not self.is_new and caller_kind != "synchronizer":
140 if not self.deleted:
141 existing = TechnologyProfile.objects.filter(id=self.id)
142 raise XOSValidationError('Modification operation is not allowed on Technology Profile [/%s/%s]. Delete it and add again' % (existing[0].technology, existing[0].profile_id))
143
144 # validate if technology profile value is valid JSON format string
145 if self.profile_value != None:
146 try:
147 tp_json_val = json.loads(self.profile_value)
148 except ValueError as e:
149 raise XOSValidationError('Technology Profile value not in valid JSON format')
150
151 super(TechnologyProfile, self).save(*args, **kwargs)
152