Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 17 | from synchronizers.new_base.modelaccessor import VOLTServiceInstance, ServiceInstanceLink, ONUDevice, ServiceInstance, model_accessor |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 18 | from synchronizers.new_base.policy import Policy |
| 19 | |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 20 | class VOLTServiceInstancePolicy(Policy): |
| 21 | model_name = "VOLTServiceInstance" |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 22 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 23 | def handle_create(self, si): |
| 24 | return self.handle_update(si) |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 25 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 26 | def handle_update(self, si): |
Matteo Scandolo | f869288 | 2018-02-26 15:30:08 -0800 | [diff] [blame] | 27 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 28 | if (si.link_deleted_count > 0) and (not si.provided_links.exists()): |
Matteo Scandolo | f869288 | 2018-02-26 15:30:08 -0800 | [diff] [blame] | 29 | # If this instance has no links pointing to it, delete |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 30 | self.handle_delete(si) |
| 31 | if VOLTServiceInstance.objects.filter(id=si.id).exists(): |
| 32 | si.delete() |
Matteo Scandolo | f869288 | 2018-02-26 15:30:08 -0800 | [diff] [blame] | 33 | return |
| 34 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 35 | self.create_eastbound_instance(si) |
| 36 | self.associate_onu_device(si) |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 37 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 38 | def handle_delete(self, si): |
Scott Baker | 697ad22 | 2017-09-18 16:30:18 -0700 | [diff] [blame] | 39 | pass |
Scott Baker | 697ad22 | 2017-09-18 16:30:18 -0700 | [diff] [blame] | 40 | |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 41 | def create_eastbound_instance(self, si): |
Scott Baker | 697ad22 | 2017-09-18 16:30:18 -0700 | [diff] [blame] | 42 | |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 43 | chain = si.subscribed_links.all() |
Scott Baker | 697ad22 | 2017-09-18 16:30:18 -0700 | [diff] [blame] | 44 | |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 45 | # Already has a chain |
| 46 | if len(chain) > 0 and not si.is_new: |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 47 | self.logger.debug("MODEL_POLICY: VOLTServiceInstance %s is already part of a chain" % si.id) |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 48 | 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 Scandolo | 7db8537 | 2018-05-22 15:26:55 -0700 | [diff] [blame] | 57 | |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 58 | 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 Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 63 | 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 Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 67 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 68 | def associate_onu_device(self, si): |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 69 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 70 | self.logger.debug("MODEL_POLICY: attaching ONUDevice to VOLTServiceInstance %s" % si.id) |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 71 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 72 | 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 Baker | 697ad22 | 2017-09-18 16:30:18 -0700 | [diff] [blame] | 78 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 79 | 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 Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 83 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 84 | si.onu_device_id = onu.id |
| 85 | si.save() |