Andy Bavier | 03df22b | 2017-08-30 14:46:02 -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 | |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 17 | from synchronizers.new_base.modelaccessor import VOLTServiceInstance, ServiceInstanceLink, VSGService, VSGServiceInstance, model_accessor |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 18 | from synchronizers.new_base.policy import Policy |
| 19 | |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 20 | class VOLTServiceInstancePolicy(Policy): |
| 21 | model_name = "VOLTServiceInstance" |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 22 | |
| 23 | def handle_create(self, tenant): |
| 24 | return self.handle_update(tenant) |
| 25 | |
| 26 | def handle_update(self, tenant): |
Matteo Scandolo | f869288 | 2018-02-26 15:30:08 -0800 | [diff] [blame] | 27 | |
| 28 | if (tenant.link_deleted_count > 0) and (not tenant.provided_links.exists()): |
| 29 | # If this instance has no links pointing to it, delete |
| 30 | self.handle_delete(tenant) |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 31 | if VOLTServiceInstance.objects.filter(id=tenant.id).exists(): |
Matteo Scandolo | f869288 | 2018-02-26 15:30:08 -0800 | [diff] [blame] | 32 | tenant.delete() |
| 33 | return |
| 34 | |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 35 | self.manage_vsg(tenant) |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 36 | self.cleanup_orphans(tenant) |
| 37 | |
| 38 | def handle_delete(self, tenant): |
Scott Baker | 697ad22 | 2017-09-18 16:30:18 -0700 | [diff] [blame] | 39 | pass |
| 40 | # assume this is handled by ServiceInstanceLink being deleted |
| 41 | #if tenant.vcpe: |
| 42 | # tenant.vcpe.delete() |
| 43 | |
| 44 | def get_current_vsg(self, tenant): |
| 45 | for link in ServiceInstanceLink.objects.filter(subscriber_service_instance_id = tenant.id): |
| 46 | # NOTE: Assumes the first (and only?) link is to a vsg |
| 47 | # cast from ServiceInstance to VSGTenant |
| 48 | return link.provider_service_instance.leaf_model |
| 49 | return None |
| 50 | |
| 51 | def create_vsg(self, tenant): |
| 52 | vsgServices = VSGService.objects.all() |
| 53 | if not vsgServices: |
| 54 | raise XOSConfigurationError("No VSG Services available") |
| 55 | |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 56 | self.logger.info("MODEL_POLICY: VOLTServiceInstance %s creating vsg" % tenant) |
Scott Baker | 697ad22 | 2017-09-18 16:30:18 -0700 | [diff] [blame] | 57 | |
| 58 | cur_vsg = VSGServiceInstance(owner=vsgServices[0]) |
| 59 | cur_vsg.creator = tenant.creator |
| 60 | cur_vsg.save() |
| 61 | link = ServiceInstanceLink(provider_service_instance=cur_vsg, subscriber_service_instance=tenant) |
| 62 | link.save() |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 63 | |
| 64 | def manage_vsg(self, tenant): |
| 65 | # Each VOLT object owns exactly one VCPE object |
| 66 | |
| 67 | if tenant.deleted: |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 68 | self.logger.info("MODEL_POLICY: VOLTServiceInstance %s deleted, deleting vsg" % tenant) |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 69 | return |
| 70 | |
Scott Baker | 697ad22 | 2017-09-18 16:30:18 -0700 | [diff] [blame] | 71 | cur_vsg = self.get_current_vsg(tenant) |
| 72 | |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 73 | # Check to see if the wrong s-tag is set. This can only happen if the |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 74 | # user changed the s-tag after the VOLTServiceInstance object was created. |
Scott Baker | 697ad22 | 2017-09-18 16:30:18 -0700 | [diff] [blame] | 75 | if cur_vsg and cur_vsg.instance: |
| 76 | s_tags = Tag.objects.filter(content_type=cur_vsg.instance.self_content_type_id, |
| 77 | object_id=cur_vsg.instance.id, name="s_tag") |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 78 | if s_tags and (s_tags[0].value != str(tenant.s_tag)): |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 79 | self.logger.info("MODEL_POLICY: VOLTServiceInstance %s s_tag changed, deleting vsg" % tenant) |
Scott Baker | 697ad22 | 2017-09-18 16:30:18 -0700 | [diff] [blame] | 80 | cur_vsg.delete() |
| 81 | cur_vsg = None |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 82 | |
Scott Baker | 697ad22 | 2017-09-18 16:30:18 -0700 | [diff] [blame] | 83 | if cur_vsg is None: |
| 84 | self.create_vsg(tenant) |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 85 | |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 86 | def cleanup_orphans(self, tenant): |
| 87 | # ensure vOLT only has one vCPE |
Scott Baker | 697ad22 | 2017-09-18 16:30:18 -0700 | [diff] [blame] | 88 | cur_vsg = self.get_current_vsg(tenant) |
Andy Bavier | 03df22b | 2017-08-30 14:46:02 -0700 | [diff] [blame] | 89 | |
| 90 | links = tenant.subscribed_links.all() |
| 91 | for link in links: |
Scott Baker | 697ad22 | 2017-09-18 16:30:18 -0700 | [diff] [blame] | 92 | if (link.provider_service_instance_id != cur_vsg.id): |
| 93 | link.delete() |