blob: 8d020912743bfb466fa0728586e2fc1bd76430da [file] [log] [blame]
Scott Baker6ad03602017-06-01 17:34:31 -07001from synchronizers.new_base.modelaccessor import *
2from synchronizers.new_base.policy import Policy
3
4class VOLTTenantPolicy(Policy):
5 model_name = "VOLTTenant"
6
7 def handle_create(self, tenant):
8 return self.handle_update(tenant)
9
10 def handle_update(self, tenant):
11 self.manage_vsg(tenant)
12 self.manage_subscriber(tenant)
13 self.cleanup_orphans(tenant)
14
15 def handle_delete(self, tenant):
16 if tenant.vcpe:
17 tenant.vcpe.delete()
18
19 def manage_vsg(self, tenant):
20 # Each VOLT object owns exactly one VCPE object
21
22 if tenant.deleted:
23 self.logger.info("MODEL_POLICY: volttenant %s deleted, deleting vsg" % tenant)
24 return
25
26 # Check to see if the wrong s-tag is set. This can only happen if the
27 # user changed the s-tag after the VoltTenant object was created.
28 if tenant.vcpe and tenant.vcpe.instance:
29 s_tags = Tag.objects.filter(content_type=tenant.vcpe.instance.self_content_type_id,
30 object_id=tenant.vcpe.instance.id, name="s_tag")
31 if s_tags and (s_tags[0].value != str(tenant.s_tag)):
32 self.logger.info("MODEL_POLICY: volttenant %s s_tag changed, deleting vsg" % tenant)
33 tenant.vcpe.delete()
34
35 if tenant.vcpe is None:
36 vsgServices = VSGService.objects.all()
37 if not vsgServices:
38 raise XOSConfigurationError("No VSG Services available")
39
40 self.logger.info("MODEL_POLICY: volttenant %s creating vsg" % tenant)
41
Scott Bakerd7590f72017-07-18 11:37:22 -070042 vcpe = VSGTenant(owner=vsgServices[0])
Scott Baker6ad03602017-06-01 17:34:31 -070043 vcpe.creator = tenant.creator
44 vcpe.save()
Scott Bakerd7590f72017-07-18 11:37:22 -070045 link = ServiceInstanceLink(provider_service_instance = vcpe, subscriber_service_instance = tenant)
46 link.save()
Scott Baker6ad03602017-06-01 17:34:31 -070047
48 def manage_subscriber(self, tenant):
Scott Bakerd7590f72017-07-18 11:37:22 -070049 # check for existing link to a root
50 links = tenant.provided_links.all()
51 for link in links:
52 roots = CordSubscriberRoot.objects.filter(id = link.subscriber_service_instance.id)
53 if roots:
54 return
Scott Baker6ad03602017-06-01 17:34:31 -070055
Scott Bakerd7590f72017-07-18 11:37:22 -070056 subs = CordSubscriberRoot.objects.filter(service_specific_id = tenant.service_specific_id)
57 if subs:
58 self.logger.info("MODEL_POLICY: volttenant %s using existing subscriber root" % tenant)
59 sub = subs[0]
60 else:
61 self.logger.info("MODEL_POLICY: volttenant %s creating new subscriber root" % tenant)
62 sub = CordSubscriberRoot(service_specific_id = tenant.service_specific_id,
63 name = "autogenerated-for-vOLT-%s" % tenant.id)
64 sub.save()
65
66 link = ServiceInstanceLink(provider_service_instance = tenant, subscriber_service_instance = sub)
67 link.save()
Scott Baker6ad03602017-06-01 17:34:31 -070068
69 def cleanup_orphans(self, tenant):
70 # ensure vOLT only has one vCPE
71 cur_vcpe = tenant.vcpe
Scott Bakerd7590f72017-07-18 11:37:22 -070072
73 links = tenant.subscribed_links.all()
74 for link in links:
75 vsgs = VSGTenant.objects.filter(id = link.provider_service_instance.id)
76 for vsg in vsgs:
77 if (not cur_vcpe) or (vsg.id != cur_vcpe.id):
78 vsg.delete()