Scott Baker | 6ad0360 | 2017-06-01 17:34:31 -0700 | [diff] [blame] | 1 | from synchronizers.new_base.modelaccessor import * |
| 2 | from synchronizers.new_base.policy import Policy |
| 3 | |
| 4 | class 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 Baker | d7590f7 | 2017-07-18 11:37:22 -0700 | [diff] [blame] | 42 | vcpe = VSGTenant(owner=vsgServices[0]) |
Scott Baker | 6ad0360 | 2017-06-01 17:34:31 -0700 | [diff] [blame] | 43 | vcpe.creator = tenant.creator |
| 44 | vcpe.save() |
Scott Baker | d7590f7 | 2017-07-18 11:37:22 -0700 | [diff] [blame] | 45 | link = ServiceInstanceLink(provider_service_instance = vcpe, subscriber_service_instance = tenant) |
| 46 | link.save() |
Scott Baker | 6ad0360 | 2017-06-01 17:34:31 -0700 | [diff] [blame] | 47 | |
| 48 | def manage_subscriber(self, tenant): |
Scott Baker | d7590f7 | 2017-07-18 11:37:22 -0700 | [diff] [blame] | 49 | # 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 Baker | 6ad0360 | 2017-06-01 17:34:31 -0700 | [diff] [blame] | 55 | |
Scott Baker | d7590f7 | 2017-07-18 11:37:22 -0700 | [diff] [blame] | 56 | 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 Baker | 6ad0360 | 2017-06-01 17:34:31 -0700 | [diff] [blame] | 68 | |
| 69 | def cleanup_orphans(self, tenant): |
| 70 | # ensure vOLT only has one vCPE |
| 71 | cur_vcpe = tenant.vcpe |
Scott Baker | d7590f7 | 2017-07-18 11:37:22 -0700 | [diff] [blame] | 72 | |
| 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() |