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 | |
| 28 | chain = si.subscribed_links.all() |
| 29 | |
| 30 | # Already has a chain |
Matteo Scandolo | c775690 | 2019-05-31 16:04:05 -0700 | [diff] [blame^] | 31 | if si.status != "enabled" and len(chain) > 0: |
| 32 | # delete chain |
| 33 | self.logger.debug("MODEL_POLICY: deleting RCORDSubscriber chain from %s" % si.id, status=si.status) |
| 34 | for link in chain: |
| 35 | self.logger.debug("Removing link %s" % link.id, |
| 36 | provider_service=link.provider_service_instance.leaf_model, |
| 37 | subscriber_service=link.subscriber_service_instance.leaf_model) |
| 38 | link.delete() |
| 39 | link.provider_service_instance.leaf_model.delete() |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 40 | |
Matteo Scandolo | c775690 | 2019-05-31 16:04:05 -0700 | [diff] [blame^] | 41 | elif si.status == "enabled": |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 42 | |
Matteo Scandolo | c775690 | 2019-05-31 16:04:05 -0700 | [diff] [blame^] | 43 | self.logger.debug("MODEL_POLICY: creating RCORDSubscriber chain for %s" % si.id, status=si.status) |
| 44 | # if it does not have a chain, |
| 45 | # Find links to the next element in the service chain |
| 46 | # and create one |
| 47 | links = si.owner.subscribed_dependencies.all() |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 48 | |
Matteo Scandolo | c775690 | 2019-05-31 16:04:05 -0700 | [diff] [blame^] | 49 | for link in links: |
| 50 | si_class = link.provider_service.get_service_instance_class_name() |
| 51 | self.logger.info("MODEL_POLICY: RCORDSubscriber %s creating %s" % (si, si_class)) |
| 52 | |
| 53 | provider_service = link.provider_service.leaf_model |
| 54 | |
| 55 | valid_provider_service_instance = provider_service.validate_links(si) |
| 56 | if not valid_provider_service_instance: |
| 57 | provider_service.acquire_service_instance(si) |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 58 | |
| 59 | def handle_delete(self, si): |
| 60 | pass |