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 | |
Matteo Scandolo | 5f894a1 | 2018-03-21 12:47:24 -0700 | [diff] [blame] | 16 | from synchronizers.new_base.modelaccessor import ServiceInstanceLink, model_accessor |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 17 | from synchronizers.new_base.policy import Policy |
| 18 | |
| 19 | class RCORDSubscriberPolicy(Policy): |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 20 | model_name = "RCORDSubscriber" |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 21 | |
| 22 | def handle_create(self, si): |
| 23 | return self.handle_update(si) |
| 24 | |
| 25 | def handle_update(self, si): |
| 26 | |
Matteo Scandolo | c322df5 | 2018-06-22 14:43:59 -0700 | [diff] [blame] | 27 | if si.status == "pre-provisioned": |
| 28 | self.logger.debug("MODEL_POLICY: Skipping chain creation as RCORDSubscriber %s is in 'pre-provisioned' state" % si.id) |
| 29 | return |
| 30 | |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 31 | chain = si.subscribed_links.all() |
| 32 | |
| 33 | # Already has a chain |
| 34 | if len(chain) > 0 and not si.is_new: |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 35 | self.logger.debug("MODEL_POLICY: RCORDSubscriber %s is already part of a chain" % si.id) |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 36 | return |
| 37 | |
| 38 | # if it does not have a chain, |
| 39 | # Find links to the next element in the service chain |
| 40 | # and create one |
| 41 | |
| 42 | links = si.owner.subscribed_dependencies.all() |
| 43 | |
| 44 | for link in links: |
Matteo Scandolo | 5f894a1 | 2018-03-21 12:47:24 -0700 | [diff] [blame] | 45 | si_class = link.provider_service.get_service_instance_class_name() |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 46 | self.logger.info("MODEL_POLICY: RCORDSubscriber %s creating %s" % (si, si_class)) |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 47 | |
Matteo Scandolo | 5f894a1 | 2018-03-21 12:47:24 -0700 | [diff] [blame] | 48 | eastbound_si_class = model_accessor.get_model_class(si_class) |
| 49 | eastbound_si = eastbound_si_class() |
| 50 | eastbound_si.owner_id = link.provider_service_id |
| 51 | eastbound_si.save() |
| 52 | link = ServiceInstanceLink(provider_service_instance=eastbound_si, subscriber_service_instance=si) |
| 53 | link.save() |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 54 | |
| 55 | def handle_delete(self, si): |
| 56 | pass |