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 | |
| 42 | vcpe = VSGTenant(provider_service=vsgServices[0], |
| 43 | subscriber_tenant=tenant) |
| 44 | vcpe.creator = tenant.creator |
| 45 | vcpe.save() |
| 46 | |
| 47 | def manage_subscriber(self, tenant): |
| 48 | if (tenant.subscriber_root is None): |
| 49 | # The vOLT is not connected to a Subscriber, so either find an |
| 50 | # existing subscriber with the same SSID, or autogenerate a new |
| 51 | # subscriber. |
| 52 | # |
| 53 | # TODO: This probably goes away when we rethink the ONOS-to-XOS |
| 54 | # vOLT API. |
| 55 | |
| 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 | tenant.subscriber_root = sub |
| 66 | tenant.save() |
| 67 | |
| 68 | def cleanup_orphans(self, tenant): |
| 69 | # ensure vOLT only has one vCPE |
| 70 | cur_vcpe = tenant.vcpe |
| 71 | subscribed_vcpes = VSGTenant.objects.filter(subscriber_tenant_id = tenant.id) |
| 72 | for vcpe in subscribed_vcpes: |
| 73 | if (not cur_vcpe) or (vcpe.id != cur_vcpe.id): |
| 74 | vcpe.delete() |