blob: 0756b45a2cc32d1c4cf66cc0ae19ce7326019e4c [file] [log] [blame]
Andy Bavier03df22b2017-08-30 14:46:02 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Matteo Scandoloca52aea2018-03-15 15:34:42 -070017from synchronizers.new_base.modelaccessor import VOLTServiceInstance, ServiceInstanceLink, model_accessor
Andy Bavier03df22b2017-08-30 14:46:02 -070018from synchronizers.new_base.policy import Policy
19
Matteo Scandolod2458012018-03-01 13:40:36 -080020class VOLTServiceInstancePolicy(Policy):
21 model_name = "VOLTServiceInstance"
Andy Bavier03df22b2017-08-30 14:46:02 -070022
23 def handle_create(self, tenant):
24 return self.handle_update(tenant)
25
26 def handle_update(self, tenant):
Matteo Scandolof8692882018-02-26 15:30:08 -080027
28 if (tenant.link_deleted_count > 0) and (not tenant.provided_links.exists()):
29 # If this instance has no links pointing to it, delete
30 self.handle_delete(tenant)
Matteo Scandolod2458012018-03-01 13:40:36 -080031 if VOLTServiceInstance.objects.filter(id=tenant.id).exists():
Matteo Scandolof8692882018-02-26 15:30:08 -080032 tenant.delete()
33 return
34
Andy Bavier03df22b2017-08-30 14:46:02 -070035 self.manage_vsg(tenant)
Andy Bavier03df22b2017-08-30 14:46:02 -070036 self.cleanup_orphans(tenant)
37
38 def handle_delete(self, tenant):
Scott Baker697ad222017-09-18 16:30:18 -070039 pass
40 # assume this is handled by ServiceInstanceLink being deleted
41 #if tenant.vcpe:
42 # tenant.vcpe.delete()
43
44 def get_current_vsg(self, tenant):
45 for link in ServiceInstanceLink.objects.filter(subscriber_service_instance_id = tenant.id):
46 # NOTE: Assumes the first (and only?) link is to a vsg
47 # cast from ServiceInstance to VSGTenant
48 return link.provider_service_instance.leaf_model
49 return None
50
Matteo Scandolo89f16482018-03-12 16:12:53 -070051 def create_eastbound_instance(self, si):
Scott Baker697ad222017-09-18 16:30:18 -070052
Matteo Scandolo89f16482018-03-12 16:12:53 -070053 chain = si.subscribed_links.all()
Scott Baker697ad222017-09-18 16:30:18 -070054
Matteo Scandolo89f16482018-03-12 16:12:53 -070055 # Already has a chain
56 if len(chain) > 0 and not si.is_new:
57 self.logger.debug("MODEL_POLICY: Subscriber %s is already part of a chain" % si.id)
58 return
59
60 # if it does not have a chain,
61 # Find links to the next element in the service chain
62 # and create one
63
64 links = si.owner.subscribed_dependencies.all()
65
66 for link in links:
67 si_class = link.provider_service.get_service_instance_class_name()
68 self.logger.info("MODEL_POLICY: VOLTServiceInstance %s creating %s" % (si, si_class))
69
70 eastbound_si_class = model_accessor.get_model_class(si_class)
71 eastbound_si = eastbound_si_class()
Matteo Scandolo89f16482018-03-12 16:12:53 -070072 eastbound_si.owner_id = link.provider_service_id
73 eastbound_si.save()
74 link = ServiceInstanceLink(provider_service_instance=eastbound_si, subscriber_service_instance=si)
75 link.save()
Andy Bavier03df22b2017-08-30 14:46:02 -070076
77 def manage_vsg(self, tenant):
78 # Each VOLT object owns exactly one VCPE object
79
80 if tenant.deleted:
Matteo Scandolod2458012018-03-01 13:40:36 -080081 self.logger.info("MODEL_POLICY: VOLTServiceInstance %s deleted, deleting vsg" % tenant)
Andy Bavier03df22b2017-08-30 14:46:02 -070082 return
83
Scott Baker697ad222017-09-18 16:30:18 -070084 cur_vsg = self.get_current_vsg(tenant)
85
Andy Bavier03df22b2017-08-30 14:46:02 -070086 # Check to see if the wrong s-tag is set. This can only happen if the
Matteo Scandolod2458012018-03-01 13:40:36 -080087 # user changed the s-tag after the VOLTServiceInstance object was created.
Scott Baker697ad222017-09-18 16:30:18 -070088 if cur_vsg and cur_vsg.instance:
89 s_tags = Tag.objects.filter(content_type=cur_vsg.instance.self_content_type_id,
90 object_id=cur_vsg.instance.id, name="s_tag")
Andy Bavier03df22b2017-08-30 14:46:02 -070091 if s_tags and (s_tags[0].value != str(tenant.s_tag)):
Matteo Scandolod2458012018-03-01 13:40:36 -080092 self.logger.info("MODEL_POLICY: VOLTServiceInstance %s s_tag changed, deleting vsg" % tenant)
Scott Baker697ad222017-09-18 16:30:18 -070093 cur_vsg.delete()
94 cur_vsg = None
Andy Bavier03df22b2017-08-30 14:46:02 -070095
Scott Baker697ad222017-09-18 16:30:18 -070096 if cur_vsg is None:
Matteo Scandolo89f16482018-03-12 16:12:53 -070097 self.create_eastbound_instance(tenant)
Andy Bavier03df22b2017-08-30 14:46:02 -070098
Andy Bavier03df22b2017-08-30 14:46:02 -070099 def cleanup_orphans(self, tenant):
100 # ensure vOLT only has one vCPE
Scott Baker697ad222017-09-18 16:30:18 -0700101 cur_vsg = self.get_current_vsg(tenant)
Andy Bavier03df22b2017-08-30 14:46:02 -0700102
103 links = tenant.subscribed_links.all()
104 for link in links:
Scott Baker697ad222017-09-18 16:30:18 -0700105 if (link.provider_service_instance_id != cur_vsg.id):
106 link.delete()