blob: 9dec2c933838d5795e164e37c6f87572ba000b5f [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
Scott Baker04c5ec32018-07-12 14:01:06 -070022from models_decl import PortBase_decl
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070023from 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
Matteo Scandolo915c7b82019-05-09 16:26:12 -070026from models_decl import ANIPort_decl
Matteo Scandolod8ed60e2018-06-18 17:00:57 -070027from models_decl import UNIPort_decl
Hardik Windlassda0d6c42019-05-13 16:00:01 +000028from models_decl import TechnologyProfile_decl
29
30import json
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070031
32class VOLTService(VOLTService_decl):
33 class Meta:
34 proxy = True
35
36 @staticmethod
37 def has_access_device(serial_number):
38 try:
39 ONUDevice.objects.get(serial_number=serial_number)
40 return True
41 except IndexError, e:
42 return False
43
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070044class OLTDevice(OLTDevice_decl):
45 class Meta:
Matteo Scandolo2360fd92018-05-29 17:27:51 -070046 proxy = True
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070047
Matteo Scandolo2360fd92018-05-29 17:27:51 -070048 def get_volt_si(self):
49 return VOLTServiceInstance.objects.all()
50
Matteo Scandolo2ed64b92018-06-18 10:32:56 -070051 def save(self, *args, **kwargs):
52
53 if (self.host or self.port) and self.mac_address:
54 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))
55
56 super(OLTDevice, self).save(*args, **kwargs)
57
Matteo Scandolo2360fd92018-05-29 17:27:51 -070058 def delete(self, *args, **kwargs):
59
60 onus = []
61 pon_ports = self.pon_ports.all()
62 for port in pon_ports:
63 onus = onus + list(port.onu_devices.all())
64
65
66 if len(onus) > 0:
67 onus = [o.id for o in onus]
68
69 # find the ONUs used by VOLTServiceInstances
70 used_onus = [o.onu_device_id for o in self.get_volt_si()]
71
72 # find the intersection between the onus associated with this OLT and the used one
73 used_onus_to_delete = [o for o in onus if o in used_onus]
74
75 if len(used_onus_to_delete) > 0:
76 if hasattr(self, "device_id") and self.device_id:
77 item = self.device_id
78 elif hasattr(self, "name") and self.name:
79 item = self.name
80 else:
81 item = self.id
82 raise XOSValidationError('OLT "%s" can\'t be deleted as it has subscribers associated with its ONUs' % item)
83
84 super(OLTDevice, self).delete(*args, **kwargs)
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070085
Matteo Scandoloe2cb8a42018-05-18 16:30:06 -070086class ONUDevice(ONUDevice_decl):
87 class Meta:
Matteo Scandolod8ed60e2018-06-18 17:00:57 -070088 proxy = True
89
Matteo Scandolo33fb1622018-06-19 18:54:35 -070090 def delete(self, *args, **kwargs):
91
92 if len(self.volt_service_instances.all()) > 0:
93 raise XOSValidationError('ONU "%s" can\'t be deleted as it has subscribers associated with it' % self.serial_number)
94
95 super(ONUDevice, self).delete(*args, **kwargs)
96
Hardik Windlassda0d6c42019-05-13 16:00:01 +000097class TechnologyProfile(TechnologyProfile_decl):
98 class Meta:
99 proxy = True
100
101 def save(self, *args, **kwargs):
102
103 caller_kind = None
104 if "caller_kind" in kwargs:
105 caller_kind = kwargs.get("caller_kind")
106
107 # only synchronizer is allowed to update the model
108 if not self.is_new and caller_kind != "synchronizer":
109 if not self.deleted:
110 existing = TechnologyProfile.objects.filter(id=self.id)
111 raise XOSValidationError('Modification operation is not allowed on Technology Profile [/%s/%s]. Delete it and add again' % (existing[0].technology, existing[0].profile_id))
112
113 # validate if technology profile value is valid JSON format string
114 if self.profile_value != None:
115 try:
116 tp_json_val = json.loads(self.profile_value)
117 except ValueError as e:
118 raise XOSValidationError('Technology Profile value not in valid JSON format')
119
120 super(TechnologyProfile, self).save(*args, **kwargs)
121