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 | 6f5c896 | 2018-03-01 13:40:49 -0800 | [diff] [blame^] | 16 | from synchronizers.new_base.modelaccessor import VOLTServiceInstance, ServiceInstanceLink |
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): |
| 20 | model_name = "CordSubscriberRoot" |
| 21 | |
| 22 | def handle_create(self, si): |
| 23 | return self.handle_update(si) |
| 24 | |
| 25 | def handle_update(self, si): |
| 26 | |
| 27 | chain = si.subscribed_links.all() |
| 28 | |
| 29 | # Already has a chain |
| 30 | if len(chain) > 0 and not si.is_new: |
| 31 | self.logger.debug("MODEL_POLICY: Subscriber %s is already part of a chain" % si.id) |
| 32 | return |
| 33 | |
| 34 | # if it does not have a chain, |
| 35 | # Find links to the next element in the service chain |
| 36 | # and create one |
| 37 | |
| 38 | links = si.owner.subscribed_dependencies.all() |
| 39 | |
| 40 | for link in links: |
| 41 | ps = link.provider_service.leaf_model |
| 42 | |
| 43 | # FIXME we should use get_service_instance_class here to support the general case. |
| 44 | # we don't know what the next service in the chain will be |
| 45 | |
| 46 | if ps.model_name is "VOLTService": |
Matteo Scandolo | 6f5c896 | 2018-03-01 13:40:49 -0800 | [diff] [blame^] | 47 | volt = VOLTServiceInstance(name="volt-for-subscriber-%s" % si.id) |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 48 | volt.save() |
| 49 | |
| 50 | si_link = ServiceInstanceLink( |
| 51 | provider_service_instance=volt, |
| 52 | subscriber_service_instance=si |
| 53 | ) |
| 54 | si_link.save() |
| 55 | |
| 56 | print links |
| 57 | |
| 58 | |
| 59 | def handle_delete(self, si): |
| 60 | pass |