Matteo Scandolo | 5e293c9 | 2017-08-08 13:05:23 -0700 | [diff] [blame] | 1 | |
| 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 Baker | 6ad0360 | 2017-06-01 17:34:31 -0700 | [diff] [blame] | 17 | from synchronizers.new_base.modelaccessor import * |
| 18 | from synchronizers.new_base.policy import Policy |
| 19 | |
| 20 | class 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 Baker | d7590f7 | 2017-07-18 11:37:22 -0700 | [diff] [blame] | 58 | vcpe = VSGTenant(owner=vsgServices[0]) |
Scott Baker | 6ad0360 | 2017-06-01 17:34:31 -0700 | [diff] [blame] | 59 | vcpe.creator = tenant.creator |
| 60 | vcpe.save() |
Scott Baker | d7590f7 | 2017-07-18 11:37:22 -0700 | [diff] [blame] | 61 | link = ServiceInstanceLink(provider_service_instance = vcpe, subscriber_service_instance = tenant) |
| 62 | link.save() |
Scott Baker | 6ad0360 | 2017-06-01 17:34:31 -0700 | [diff] [blame] | 63 | |
| 64 | def manage_subscriber(self, tenant): |
Scott Baker | d7590f7 | 2017-07-18 11:37:22 -0700 | [diff] [blame] | 65 | # 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 Baker | 6ad0360 | 2017-06-01 17:34:31 -0700 | [diff] [blame] | 71 | |
Scott Baker | d7590f7 | 2017-07-18 11:37:22 -0700 | [diff] [blame] | 72 | 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 Baker | 6ad0360 | 2017-06-01 17:34:31 -0700 | [diff] [blame] | 84 | |
| 85 | def cleanup_orphans(self, tenant): |
| 86 | # ensure vOLT only has one vCPE |
| 87 | cur_vcpe = tenant.vcpe |
Scott Baker | d7590f7 | 2017-07-18 11:37:22 -0700 | [diff] [blame] | 88 | |
| 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() |