blob: 9af1b80a93b1b05a45218a376df6913aedee412c [file] [log] [blame]
Matteo Scandolo23ccdfb2017-10-05 17:52:13 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License
14
15from synchronizers.new_base.modelaccessor import *
16from synchronizers.new_base.policy import Policy
17from synchronizers.new_base.model_policies.model_policy_tenantwithcontainer import TenantWithContainerPolicy
18from synchronizers.new_base.exceptions import *
19
20class VENBServiceInstancePolicy(TenantWithContainerPolicy):
Sapan Bhatia0f092e52017-12-01 10:18:14 -050021 model_name = "VENBServiceInstance"
22
23 def handle_create(self, service_instance):
24 return self.handle_update(service_instance)
25
26 def handle_update(self, service_instance):
27 if (service_instance.link_deleted_count>0) and (not service_instance.provided_links.exists()):
28 self.logger.info("The last provided link has been deleted -- self-destructing.")
29 self.handle_delete(service_instance)
30 if VENBServiceInstance.objects.filter(id=service_instance.id).exists():
31 service_instance.delete()
32 else:
33 self.logger.info("Tenant %s is already deleted" % service_instance)
34 return
35
36 self.manage_container(service_instance)
37
38 def handle_delete(self, service_instance):
39 if service_instance.instance and (not service_instance.instance.deleted):
40 all_service_instances_this_instance = VENBServiceInstance.objects.filter(instance_id=service_instance.instance.id)
41 other_service_instances_this_instance = [x for x in all_service_instances_this_instance if x.id != service_instance.id]
42 if (not other_service_instances_this_instance):
43 self.logger.info("VENBServiceInstance Instance %s is now unused -- deleting" % service_instance.instance)
44 service_instance.instance.delete()
45 else:
46 self.logger.info("VENBServiceInstance Instance %s has %d other service instances attached" % (service_instance.instance, len(other_service_instances_this_instance)))
47