blob: 227efa9e317424bfb167910bd2405448e8e451a1 [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):
Scott Baker697ad222017-09-18 16:30:18 -070042
Matteo Scandolo89f16482018-03-12 16:12:53 -070043 chain = si.subscribed_links.all()
Scott Baker697ad222017-09-18 16:30:18 -070044
Matteo Scandolo89f16482018-03-12 16:12:53 -070045 # Already has a chain
46 if len(chain) > 0 and not si.is_new:
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070047 self.logger.debug("MODEL_POLICY: VOLTServiceInstance %s is already part of a chain" % si.id)
Matteo Scandolo89f16482018-03-12 16:12:53 -070048 return
49
50 # if it does not have a chain,
51 # Find links to the next element in the service chain
52 # and create one
53
54 links = si.owner.subscribed_dependencies.all()
55
56 for link in links:
Scott Bakerc50a3502018-08-27 11:36:36 -070057 # SEBA-216 prevent any attempt to create an ONOSServiceInstance
58 if "onos" in link.provider_service.name.lower():
59 continue
Matteo Scandolo7db85372018-05-22 15:26:55 -070060
Matteo Scandolo89f16482018-03-12 16:12:53 -070061 si_class = link.provider_service.get_service_instance_class_name()
62 self.logger.info("MODEL_POLICY: VOLTServiceInstance %s creating %s" % (si, si_class))
63
64 eastbound_si_class = model_accessor.get_model_class(si_class)
65 eastbound_si = eastbound_si_class()
Matteo Scandolo89f16482018-03-12 16:12:53 -070066 eastbound_si.owner_id = link.provider_service_id
67 eastbound_si.save()
68 link = ServiceInstanceLink(provider_service_instance=eastbound_si, subscriber_service_instance=si)
69 link.save()
Andy Bavier03df22b2017-08-30 14:46:02 -070070
Matteo Scandolo2360fd92018-05-29 17:27:51 -070071 def associate_onu_device(self, si):
Andy Bavier03df22b2017-08-30 14:46:02 -070072
Matteo Scandolo2360fd92018-05-29 17:27:51 -070073 self.logger.debug("MODEL_POLICY: attaching ONUDevice to VOLTServiceInstance %s" % si.id)
Andy Bavier03df22b2017-08-30 14:46:02 -070074
Matteo Scandolo2360fd92018-05-29 17:27:51 -070075 base_si = ServiceInstance.objects.get(id=si.id)
76 try:
77 onu_device_serial_number = base_si.get_westbound_service_instance_properties("onu_device")
78 except Exception as e:
79 raise Exception(
80 "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 -070081
Matteo Scandolo2360fd92018-05-29 17:27:51 -070082 try:
83 onu = ONUDevice.objects.get(serial_number=onu_device_serial_number)
84 except IndexError:
85 raise Exception("ONUDevice with serial number %s can't be found" % onu_device_serial_number)
Andy Bavier03df22b2017-08-30 14:46:02 -070086
Matteo Scandolo2360fd92018-05-29 17:27:51 -070087 si.onu_device_id = onu.id
88 si.save()