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 | |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 17 | from xossynchronizer.modelaccessor import VOLTServiceInstance, ServiceInstanceLink, ONUDevice, ServiceInstance, model_accessor |
| 18 | from xossynchronizer.model_policies.policy import Policy |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 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): |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 42 | links = si.owner.subscribed_dependencies.all() |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 43 | for link in links: |
Scott Baker | c50a350 | 2018-08-27 11:36:36 -0700 | [diff] [blame] | 44 | # SEBA-216 prevent any attempt to create an ONOSServiceInstance |
| 45 | if "onos" in link.provider_service.name.lower(): |
| 46 | continue |
Matteo Scandolo | 7db8537 | 2018-05-22 15:26:55 -0700 | [diff] [blame] | 47 | |
Scott Baker | 59b1e07 | 2018-08-23 15:59:05 -0700 | [diff] [blame] | 48 | provider_service = link.provider_service.leaf_model |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 49 | |
Scott Baker | 59b1e07 | 2018-08-23 15:59:05 -0700 | [diff] [blame] | 50 | valid_provider_service_instance = provider_service.validate_links(si) |
| 51 | if not valid_provider_service_instance: |
| 52 | provider_service.acquire_service_instance(si) |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 53 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 54 | def associate_onu_device(self, si): |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 55 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 56 | self.logger.debug("MODEL_POLICY: attaching ONUDevice to VOLTServiceInstance %s" % si.id) |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 57 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 58 | 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 Baker | 697ad22 | 2017-09-18 16:30:18 -0700 | [diff] [blame] | 64 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 65 | 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 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 | si.onu_device_id = onu.id |
Andy Bavier | 00c573c | 2019-02-08 16:19:11 -0700 | [diff] [blame] | 71 | si.save_changed_fields() |