blob: 5cc435c517382db45af4f2a5d7322ce5335e62e7 [file] [log] [blame]
Andy Bavier0c564b32017-10-12 13:04:11 -07001
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
17from synchronizers.new_base.modelaccessor import *
18from synchronizers.new_base.model_policies.model_policy_tenantwithcontainer import TenantWithContainerPolicy
19
20class InternetEmulatorServiceInstancePolicy(TenantWithContainerPolicy):
21 model_name = "InternetEmulatorServiceInstance"
JianHao502dbfd2018-01-18 20:11:40 +080022 constrain_by_service_instance = True
23
24 def handle_create(self, service_instance):
25 return self.handle_update(service_instance)
26
27 def handle_update(self, service_instance):
28 if (service_instance.link_deleted_count>0) and (not service_instance.provided_links.exists()):
29 self.logger.info("The last provided link has been deleted -- self-destructing.")
30 self.handle_delete(service_instance)
31 if InternetEmulatorServiceInstance.objects.filter(id=service_instance.id).exists():
32 service_instance.delete()
33 else:
34 self.logger.info("Tenant %s is already deleted" % service_instance)
35 return
36
37 self.manage_container(service_instance)
38
39 def handle_delete(self, service_instance):
40 if service_instance.instance and (not service_instance.instance.deleted):
41 all_service_instances_this_instance = InternetEmulatorServiceInstance.objects.filter(instance_id=service_instance.instance.id)
42 other_service_instances_this_instance = [x for x in all_service_instances_this_instance if x.id != service_instance.id]
43 if (not other_service_instances_this_instance):
44 self.logger.info("InternetEmulatorServiceInstance Instance %s is now unused -- deleting" % service_instance.instance)
45 service_instance.instance.delete()
46 else:
47 self.logger.info("InternetEmulatorServiceInstance Instance %s has %d other service instances attached" % (service_instance.instance, len(other_service_instances_this_instance)))