blob: b33bf1f8d21fdf7bfa82b147a2b7cfc7027482fe [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:
Matteo Scandolo7db85372018-05-22 15:26:55 -070057
Matteo Scandolo89f16482018-03-12 16:12:53 -070058 si_class = link.provider_service.get_service_instance_class_name()
59 self.logger.info("MODEL_POLICY: VOLTServiceInstance %s creating %s" % (si, si_class))
60
61 eastbound_si_class = model_accessor.get_model_class(si_class)
62 eastbound_si = eastbound_si_class()
Matteo Scandolo89f16482018-03-12 16:12:53 -070063 eastbound_si.owner_id = link.provider_service_id
64 eastbound_si.save()
65 link = ServiceInstanceLink(provider_service_instance=eastbound_si, subscriber_service_instance=si)
66 link.save()
Andy Bavier03df22b2017-08-30 14:46:02 -070067
Matteo Scandolo2360fd92018-05-29 17:27:51 -070068 def associate_onu_device(self, si):
Andy Bavier03df22b2017-08-30 14:46:02 -070069
Matteo Scandolo2360fd92018-05-29 17:27:51 -070070 self.logger.debug("MODEL_POLICY: attaching ONUDevice to VOLTServiceInstance %s" % si.id)
Andy Bavier03df22b2017-08-30 14:46:02 -070071
Matteo Scandolo2360fd92018-05-29 17:27:51 -070072 base_si = ServiceInstance.objects.get(id=si.id)
73 try:
74 onu_device_serial_number = base_si.get_westbound_service_instance_properties("onu_device")
75 except Exception as e:
76 raise Exception(
77 "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 -070078
Matteo Scandolo2360fd92018-05-29 17:27:51 -070079 try:
80 onu = ONUDevice.objects.get(serial_number=onu_device_serial_number)
81 except IndexError:
82 raise Exception("ONUDevice with serial number %s can't be found" % onu_device_serial_number)
Andy Bavier03df22b2017-08-30 14:46:02 -070083
Matteo Scandolo2360fd92018-05-29 17:27:51 -070084 si.onu_device_id = onu.id
85 si.save()