blob: 32bc0368355b3d2dcbb3a9ff90f653443a5df8a6 [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
17from synchronizers.new_base.modelaccessor import *
18from synchronizers.new_base.policy import Policy
19
20class VOLTTenantPolicy(Policy):
21 model_name = "VOLTTenant"
22
23 def handle_create(self, tenant):
24 return self.handle_update(tenant)
25
26 def handle_update(self, tenant):
27 self.manage_vsg(tenant)
28 self.manage_subscriber(tenant)
29 self.cleanup_orphans(tenant)
30
31 def handle_delete(self, tenant):
Scott Baker697ad222017-09-18 16:30:18 -070032 pass
33 # assume this is handled by ServiceInstanceLink being deleted
34 #if tenant.vcpe:
35 # tenant.vcpe.delete()
36
37 def get_current_vsg(self, tenant):
38 for link in ServiceInstanceLink.objects.filter(subscriber_service_instance_id = tenant.id):
39 # NOTE: Assumes the first (and only?) link is to a vsg
40 # cast from ServiceInstance to VSGTenant
41 return link.provider_service_instance.leaf_model
42 return None
43
44 def create_vsg(self, tenant):
45 vsgServices = VSGService.objects.all()
46 if not vsgServices:
47 raise XOSConfigurationError("No VSG Services available")
48
49 self.logger.info("MODEL_POLICY: volttenant %s creating vsg" % tenant)
50
51 cur_vsg = VSGServiceInstance(owner=vsgServices[0])
52 cur_vsg.creator = tenant.creator
53 cur_vsg.save()
54 link = ServiceInstanceLink(provider_service_instance=cur_vsg, subscriber_service_instance=tenant)
55 link.save()
Andy Bavier03df22b2017-08-30 14:46:02 -070056
57 def manage_vsg(self, tenant):
58 # Each VOLT object owns exactly one VCPE object
59
60 if tenant.deleted:
61 self.logger.info("MODEL_POLICY: volttenant %s deleted, deleting vsg" % tenant)
62 return
63
Scott Baker697ad222017-09-18 16:30:18 -070064 cur_vsg = self.get_current_vsg(tenant)
65
Andy Bavier03df22b2017-08-30 14:46:02 -070066 # Check to see if the wrong s-tag is set. This can only happen if the
67 # user changed the s-tag after the VoltTenant object was created.
Scott Baker697ad222017-09-18 16:30:18 -070068 if cur_vsg and cur_vsg.instance:
69 s_tags = Tag.objects.filter(content_type=cur_vsg.instance.self_content_type_id,
70 object_id=cur_vsg.instance.id, name="s_tag")
Andy Bavier03df22b2017-08-30 14:46:02 -070071 if s_tags and (s_tags[0].value != str(tenant.s_tag)):
72 self.logger.info("MODEL_POLICY: volttenant %s s_tag changed, deleting vsg" % tenant)
Scott Baker697ad222017-09-18 16:30:18 -070073 cur_vsg.delete()
74 cur_vsg = None
Andy Bavier03df22b2017-08-30 14:46:02 -070075
Scott Baker697ad222017-09-18 16:30:18 -070076 if cur_vsg is None:
77 self.create_vsg(tenant)
Andy Bavier03df22b2017-08-30 14:46:02 -070078
79 def manage_subscriber(self, tenant):
80 # check for existing link to a root
81 links = tenant.provided_links.all()
82 for link in links:
83 roots = CordSubscriberRoot.objects.filter(id = link.subscriber_service_instance.id)
84 if roots:
85 return
86
87 subs = CordSubscriberRoot.objects.filter(service_specific_id = tenant.service_specific_id)
88 if subs:
89 self.logger.info("MODEL_POLICY: volttenant %s using existing subscriber root" % tenant)
90 sub = subs[0]
91 else:
92 self.logger.info("MODEL_POLICY: volttenant %s creating new subscriber root" % tenant)
93 sub = CordSubscriberRoot(service_specific_id = tenant.service_specific_id,
94 name = "autogenerated-for-vOLT-%s" % tenant.id)
95 sub.save()
96
97 link = ServiceInstanceLink(provider_service_instance = tenant, subscriber_service_instance = sub)
98 link.save()
99
100 def cleanup_orphans(self, tenant):
101 # ensure vOLT only has one vCPE
Scott Baker697ad222017-09-18 16:30:18 -0700102 cur_vsg = self.get_current_vsg(tenant)
Andy Bavier03df22b2017-08-30 14:46:02 -0700103
104 links = tenant.subscribed_links.all()
105 for link in links:
Scott Baker697ad222017-09-18 16:30:18 -0700106 if (link.provider_service_instance_id != cur_vsg.id):
107 link.delete()