blob: ef5ba4eb049761dc570015bc09106256defcf866 [file] [log] [blame]
Matteo Scandolo5e293c92017-08-08 13:05:23 -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
Scott Baker6ad03602017-06-01 17:34:31 -070017from 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):
32 if tenant.vcpe:
33 tenant.vcpe.delete()
34
35 def manage_vsg(self, tenant):
36 # Each VOLT object owns exactly one VCPE object
37
38 if tenant.deleted:
39 self.logger.info("MODEL_POLICY: volttenant %s deleted, deleting vsg" % tenant)
40 return
41
42 # Check to see if the wrong s-tag is set. This can only happen if the
43 # user changed the s-tag after the VoltTenant object was created.
44 if tenant.vcpe and tenant.vcpe.instance:
45 s_tags = Tag.objects.filter(content_type=tenant.vcpe.instance.self_content_type_id,
46 object_id=tenant.vcpe.instance.id, name="s_tag")
47 if s_tags and (s_tags[0].value != str(tenant.s_tag)):
48 self.logger.info("MODEL_POLICY: volttenant %s s_tag changed, deleting vsg" % tenant)
49 tenant.vcpe.delete()
50
51 if tenant.vcpe is None:
52 vsgServices = VSGService.objects.all()
53 if not vsgServices:
54 raise XOSConfigurationError("No VSG Services available")
55
56 self.logger.info("MODEL_POLICY: volttenant %s creating vsg" % tenant)
57
Scott Bakerd7590f72017-07-18 11:37:22 -070058 vcpe = VSGTenant(owner=vsgServices[0])
Scott Baker6ad03602017-06-01 17:34:31 -070059 vcpe.creator = tenant.creator
60 vcpe.save()
Scott Bakerd7590f72017-07-18 11:37:22 -070061 link = ServiceInstanceLink(provider_service_instance = vcpe, subscriber_service_instance = tenant)
62 link.save()
Scott Baker6ad03602017-06-01 17:34:31 -070063
64 def manage_subscriber(self, tenant):
Scott Bakerd7590f72017-07-18 11:37:22 -070065 # check for existing link to a root
66 links = tenant.provided_links.all()
67 for link in links:
68 roots = CordSubscriberRoot.objects.filter(id = link.subscriber_service_instance.id)
69 if roots:
70 return
Scott Baker6ad03602017-06-01 17:34:31 -070071
Scott Bakerd7590f72017-07-18 11:37:22 -070072 subs = CordSubscriberRoot.objects.filter(service_specific_id = tenant.service_specific_id)
73 if subs:
74 self.logger.info("MODEL_POLICY: volttenant %s using existing subscriber root" % tenant)
75 sub = subs[0]
76 else:
77 self.logger.info("MODEL_POLICY: volttenant %s creating new subscriber root" % tenant)
78 sub = CordSubscriberRoot(service_specific_id = tenant.service_specific_id,
79 name = "autogenerated-for-vOLT-%s" % tenant.id)
80 sub.save()
81
82 link = ServiceInstanceLink(provider_service_instance = tenant, subscriber_service_instance = sub)
83 link.save()
Scott Baker6ad03602017-06-01 17:34:31 -070084
85 def cleanup_orphans(self, tenant):
86 # ensure vOLT only has one vCPE
87 cur_vcpe = tenant.vcpe
Scott Bakerd7590f72017-07-18 11:37:22 -070088
89 links = tenant.subscribed_links.all()
90 for link in links:
91 vsgs = VSGTenant.objects.filter(id = link.provider_service_instance.id)
92 for vsg in vsgs:
93 if (not cur_vcpe) or (vsg.id != cur_vcpe.id):
94 vsg.delete()