Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | |
Scott Baker | 8b1a885 | 2019-02-04 09:39:17 -0800 | [diff] [blame] | 16 | from xossynchronizer.modelaccessor import ServiceInstanceLink, model_accessor |
| 17 | from xossynchronizer.model_policies.policy import Policy |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 18 | |
Scott Baker | b167151 | 2019-04-01 19:16:54 -0700 | [diff] [blame^] | 19 | |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 20 | class RCORDSubscriberPolicy(Policy): |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 21 | model_name = "RCORDSubscriber" |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 22 | |
| 23 | def handle_create(self, si): |
| 24 | return self.handle_update(si) |
| 25 | |
| 26 | def handle_update(self, si): |
| 27 | |
Matteo Scandolo | c322df5 | 2018-06-22 14:43:59 -0700 | [diff] [blame] | 28 | if si.status == "pre-provisioned": |
Scott Baker | b167151 | 2019-04-01 19:16:54 -0700 | [diff] [blame^] | 29 | self.logger.debug( |
| 30 | "MODEL_POLICY: Skipping chain creation as RCORDSubscriber %s is in 'pre-provisioned' state" % |
| 31 | si.id) |
Matteo Scandolo | c322df5 | 2018-06-22 14:43:59 -0700 | [diff] [blame] | 32 | return |
| 33 | |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 34 | chain = si.subscribed_links.all() |
| 35 | |
| 36 | # Already has a chain |
Matteo Scandolo | ace9dd7 | 2018-08-16 16:12:43 -0700 | [diff] [blame] | 37 | if len(chain) > 0: |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 38 | self.logger.debug("MODEL_POLICY: RCORDSubscriber %s is already part of a chain" % si.id) |
Matteo Scandolo | ace9dd7 | 2018-08-16 16:12:43 -0700 | [diff] [blame] | 39 | if si.status == "awaiting-auth" or si.status == "auth-failed" or si.status == "disabled": |
| 40 | # delete chain |
| 41 | self.logger.debug("MODEL_POLICY: deleting RCORDSubscriber chain from %s" % si.id, status=si.status) |
| 42 | for link in chain: |
Scott Baker | b167151 | 2019-04-01 19:16:54 -0700 | [diff] [blame^] | 43 | self.logger.debug("Removing link %s" % link.id, |
| 44 | provider_service=link.provider_service_instance.leaf_model, |
| 45 | subscriber_service=link.subscriber_service_instance.leaf_model) |
Matteo Scandolo | ace9dd7 | 2018-08-16 16:12:43 -0700 | [diff] [blame] | 46 | link.delete() |
| 47 | link.provider_service_instance.leaf_model.delete() |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 48 | |
Matteo Scandolo | ace9dd7 | 2018-08-16 16:12:43 -0700 | [diff] [blame] | 49 | else: |
Matteo Scandolo | fab45ef | 2018-08-17 16:33:29 -0700 | [diff] [blame] | 50 | if si.status != "enabled": |
| 51 | self.logger.debug("MODEL_POLICY: NOT creating RCORDSubscriber chain for %s" % si.id, status=si.status) |
| 52 | else: |
| 53 | self.logger.debug("MODEL_POLICY: creating RCORDSubscriber chain for %s" % si.id, status=si.status) |
| 54 | # if it does not have a chain, |
| 55 | # Find links to the next element in the service chain |
| 56 | # and create one |
| 57 | links = si.owner.subscribed_dependencies.all() |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 58 | |
Matteo Scandolo | fab45ef | 2018-08-17 16:33:29 -0700 | [diff] [blame] | 59 | for link in links: |
| 60 | si_class = link.provider_service.get_service_instance_class_name() |
| 61 | self.logger.info("MODEL_POLICY: RCORDSubscriber %s creating %s" % (si, si_class)) |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 62 | |
Matteo Scandolo | fab45ef | 2018-08-17 16:33:29 -0700 | [diff] [blame] | 63 | eastbound_si_class = model_accessor.get_model_class(si_class) |
| 64 | eastbound_si = eastbound_si_class() |
| 65 | eastbound_si.owner_id = link.provider_service_id |
| 66 | eastbound_si.save() |
| 67 | link = ServiceInstanceLink(provider_service_instance=eastbound_si, subscriber_service_instance=si) |
| 68 | link.save() |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 69 | |
| 70 | def handle_delete(self, si): |
| 71 | pass |