blob: cb260ed91478133d7d2a2bea92103493459c67aa [file] [log] [blame]
Matteo Scandoloc6230b42018-02-26 15:27:57 -08001# 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 Baker8b1a8852019-02-04 09:39:17 -080016from xossynchronizer.modelaccessor import ServiceInstanceLink, model_accessor
17from xossynchronizer.model_policies.policy import Policy
Matteo Scandoloc6230b42018-02-26 15:27:57 -080018
19class RCORDSubscriberPolicy(Policy):
Matteo Scandolod1707b32018-05-04 12:42:53 -070020 model_name = "RCORDSubscriber"
Matteo Scandoloc6230b42018-02-26 15:27:57 -080021
22 def handle_create(self, si):
23 return self.handle_update(si)
24
25 def handle_update(self, si):
26
Matteo Scandoloc322df52018-06-22 14:43:59 -070027 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 Scandoloc6230b42018-02-26 15:27:57 -080031 chain = si.subscribed_links.all()
32
33 # Already has a chain
Matteo Scandoloace9dd72018-08-16 16:12:43 -070034 if len(chain) > 0:
Matteo Scandolod1707b32018-05-04 12:42:53 -070035 self.logger.debug("MODEL_POLICY: RCORDSubscriber %s is already part of a chain" % si.id)
Matteo Scandoloace9dd72018-08-16 16:12:43 -070036 if si.status == "awaiting-auth" or si.status == "auth-failed" or si.status == "disabled":
37 # delete chain
38 self.logger.debug("MODEL_POLICY: deleting RCORDSubscriber chain from %s" % si.id, status=si.status)
39 for link in chain:
40 self.logger.debug("Removing link %s" % link.id, provider_service=link.provider_service_instance.leaf_model, subscriber_service=link.subscriber_service_instance.leaf_model)
41 link.delete()
42 link.provider_service_instance.leaf_model.delete()
Matteo Scandoloc6230b42018-02-26 15:27:57 -080043
Matteo Scandoloace9dd72018-08-16 16:12:43 -070044 else:
Matteo Scandolofab45ef2018-08-17 16:33:29 -070045 if si.status != "enabled":
46 self.logger.debug("MODEL_POLICY: NOT creating RCORDSubscriber chain for %s" % si.id, status=si.status)
47 else:
48 self.logger.debug("MODEL_POLICY: creating RCORDSubscriber chain for %s" % si.id, status=si.status)
49 # if it does not have a chain,
50 # Find links to the next element in the service chain
51 # and create one
52 links = si.owner.subscribed_dependencies.all()
Matteo Scandoloc6230b42018-02-26 15:27:57 -080053
Matteo Scandolofab45ef2018-08-17 16:33:29 -070054 for link in links:
55 si_class = link.provider_service.get_service_instance_class_name()
56 self.logger.info("MODEL_POLICY: RCORDSubscriber %s creating %s" % (si, si_class))
Matteo Scandoloc6230b42018-02-26 15:27:57 -080057
Matteo Scandolofab45ef2018-08-17 16:33:29 -070058 eastbound_si_class = model_accessor.get_model_class(si_class)
59 eastbound_si = eastbound_si_class()
60 eastbound_si.owner_id = link.provider_service_id
61 eastbound_si.save()
62 link = ServiceInstanceLink(provider_service_instance=eastbound_si, subscriber_service_instance=si)
63 link.save()
Matteo Scandoloc6230b42018-02-26 15:27:57 -080064
65 def handle_delete(self, si):
66 pass