blob: 1321955c71db6d253c1601bfe58a68f5e5e04ff5 [file] [log] [blame]
Andy Bavier03df22b2017-08-30 14:46:02 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Matteo Scandolo2360fd92018-05-29 17:27:51 -070017from synchronizers.new_base.modelaccessor import VOLTServiceInstance, ServiceInstanceLink, ONUDevice, ServiceInstance, model_accessor
Andy Bavier03df22b2017-08-30 14:46:02 -070018from synchronizers.new_base.policy import Policy
19
Matteo Scandolod2458012018-03-01 13:40:36 -080020class VOLTServiceInstancePolicy(Policy):
21 model_name = "VOLTServiceInstance"
Andy Bavier03df22b2017-08-30 14:46:02 -070022
Matteo Scandolo2360fd92018-05-29 17:27:51 -070023 def handle_create(self, si):
24 return self.handle_update(si)
Andy Bavier03df22b2017-08-30 14:46:02 -070025
Matteo Scandolo2360fd92018-05-29 17:27:51 -070026 def handle_update(self, si):
Matteo Scandolof8692882018-02-26 15:30:08 -080027
Matteo Scandolo2360fd92018-05-29 17:27:51 -070028 if (si.link_deleted_count > 0) and (not si.provided_links.exists()):
Matteo Scandolof8692882018-02-26 15:30:08 -080029 # If this instance has no links pointing to it, delete
Matteo Scandolo2360fd92018-05-29 17:27:51 -070030 self.handle_delete(si)
31 if VOLTServiceInstance.objects.filter(id=si.id).exists():
32 si.delete()
Matteo Scandolof8692882018-02-26 15:30:08 -080033 return
34
Matteo Scandolo2360fd92018-05-29 17:27:51 -070035 self.create_eastbound_instance(si)
36 self.associate_onu_device(si)
Andy Bavier03df22b2017-08-30 14:46:02 -070037
Matteo Scandolo2360fd92018-05-29 17:27:51 -070038 def handle_delete(self, si):
Scott Baker697ad222017-09-18 16:30:18 -070039 pass
Scott Baker697ad222017-09-18 16:30:18 -070040
Matteo Scandolo89f16482018-03-12 16:12:53 -070041 def create_eastbound_instance(self, si):
Matteo Scandolo89f16482018-03-12 16:12:53 -070042 links = si.owner.subscribed_dependencies.all()
Matteo Scandolo89f16482018-03-12 16:12:53 -070043 for link in links:
Scott Bakerc50a3502018-08-27 11:36:36 -070044 # SEBA-216 prevent any attempt to create an ONOSServiceInstance
45 if "onos" in link.provider_service.name.lower():
46 continue
Matteo Scandolo7db85372018-05-22 15:26:55 -070047
Scott Baker59b1e072018-08-23 15:59:05 -070048 provider_service = link.provider_service.leaf_model
Matteo Scandolo89f16482018-03-12 16:12:53 -070049
Scott Baker59b1e072018-08-23 15:59:05 -070050 valid_provider_service_instance = provider_service.validate_links(si)
51 if not valid_provider_service_instance:
52 provider_service.acquire_service_instance(si)
Andy Bavier03df22b2017-08-30 14:46:02 -070053
Matteo Scandolo2360fd92018-05-29 17:27:51 -070054 def associate_onu_device(self, si):
Andy Bavier03df22b2017-08-30 14:46:02 -070055
Matteo Scandolo2360fd92018-05-29 17:27:51 -070056 self.logger.debug("MODEL_POLICY: attaching ONUDevice to VOLTServiceInstance %s" % si.id)
Andy Bavier03df22b2017-08-30 14:46:02 -070057
Matteo Scandolo2360fd92018-05-29 17:27:51 -070058 base_si = ServiceInstance.objects.get(id=si.id)
59 try:
60 onu_device_serial_number = base_si.get_westbound_service_instance_properties("onu_device")
61 except Exception as e:
62 raise Exception(
63 "VOLTServiceInstance %s has no westbound ServiceInstance specifying the onu_device, you need to manually specify it" % self.id)
Scott Baker697ad222017-09-18 16:30:18 -070064
Matteo Scandolo2360fd92018-05-29 17:27:51 -070065 try:
66 onu = ONUDevice.objects.get(serial_number=onu_device_serial_number)
67 except IndexError:
68 raise Exception("ONUDevice with serial number %s can't be found" % onu_device_serial_number)
Andy Bavier03df22b2017-08-30 14:46:02 -070069
Matteo Scandolo2360fd92018-05-29 17:27:51 -070070 si.onu_device_id = onu.id
71 si.save()