blob: de459acf91bf0caf411394cbec672e1ef31e1b56 [file] [log] [blame]
Woojoong Kimeb8a7182018-01-09 12:54:38 -08001# 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.model_policies.model_policy_tenantwithcontainer import TenantWithContainerPolicy, LeastLoadedNodeScheduler
17from synchronizers.new_base.exceptions import *
18
19class HSSDBServiceInstancePolicy(TenantWithContainerPolicy):
20 model_name = "HSSDBServiceInstance"
21
22 def handle_create(self, service_instance):
23 return self.handle_update(service_instance)
24
25 def handle_update(self, service_instance):
26 if (service_instance.link_deleted_count>0) and (not service_instance.provided_links.exists()):
27 self.logger.info("The last provided link has been deleted -- self-destructing.")
28 self.handle_delete(service_instance)
29 if HSSDBServiceInstance.objects.filter(id=service_instance.id).exists():
30 service_instance.delete()
31 else:
32 self.logger.info("Tenant %s is already deleted" % service_instance)
33 return
34
35 self.manage_container(service_instance)
36
37 def handle_delete(self, service_instance):
38 if service_instance.instance and (not service_instance.instance.deleted):
39 all_service_instances_this_instance = HSSDBServiceInstance.objects.filter(instance_id=service_instance.instance.id)
40 other_service_instances_this_instance = [x for x in all_service_instances_this_instance if x.id != service_instance.id]
41 if (not other_service_instances_this_instance):
42 self.logger.info("HSSDBServiceInstance %s is now unused -- deleting" % service_instance.instance)
43 self.delete_instance(service_instance, service_instance.instance)
44 else:
45 self.logger.info("HSSDBServiceInstance %s has %d other service instances attached" % (service_instance.instance, len(other_service_instances_this_instance)))
46
47 def get_service(self, service_instance):
48 service_name = service_instance.owner.leaf_model_name
49 service_class = globals()[service_name]
50 return service_class.objects.get(id=service_instance.owner.id)
51
52 def find_instance_for_instance_tag(self, instance_tag):
53 tags = Tag.objects.filter(name="instance_tag", value=instance_tag)
54 if tags:
55 return tags[0].content_object
56 return None
57
58 def find_or_make_instance_for_instance_tag(self, service_instance):
59 instance_tag = self.get_instance_tag(service_instance)
60 instance = self.find_instance_for_instance_tag(instance_tag)
61 if instance:
62 if instance.no_sync:
63 # if no_sync is still set, then perhaps we failed while saving it and need to retry.
64 self.save_instance(service_instance, instance)
65 return instance
66
67 desired_image = self.get_image(service_instance)
68 desired_flavor = self.get_flavor(service_instance)
69
70 slice = service_instance.owner.slices.first()
71
72 (node, parent) = LeastLoadedNodeScheduler(slice, label=None).pick()
73
74 assert (slice is not None)
75 assert (node is not None)
76 assert (desired_image is not None)
77 assert (service_instance.creator is not None)
78 assert (node.site_deployment.deployment is not None)
79 assert (desired_image is not None)
80
81 instance = Instance(slice=slice,
82 node=node,
83 image=desired_image,
84 creator=service_instance.creator,
85 deployment=node.site_deployment.deployment,
86 flavor=desired_flavor,
87 isolation=slice.default_isolation,
88 parent=parent)
89
90 self.save_instance(service_instance, instance)
91
92 return instance
93
94 def manage_container(self, service_instance):
95 if service_instance.deleted:
96 return
97
98 if service_instance.instance:
99 # We're good.
100 return
101
102 instance = self.find_or_make_instance_for_instance_tag(service_instance)
103 service_instance.instance = instance
104 # TODO: possible for partial failure here?
105 service_instance.save()
106
107 def delete_instance(self, service_instance, instance):
108 # delete the `instance_tag` tags
109 tags = Tag.objects.filter(service_id=service_instance.owner.id, content_type=instance.self_content_type_id,
110 object_id=instance.id, name="instance_tag")
111 for tag in tags:
112 tag.delete()
113
114 tags = Tag.objects.filter(content_type=instance.self_content_type_id, object_id=instance.id,
115 name="vm_vrouter_tenant")
116 for tag in tags:
117 address_manager_instances = list(ServiceInstance.objects.filter(id=tag.value))
118 tag.delete()
119
120 # TODO: Potential partial failure
121
122 for address_manager_instance in address_manager_instances:
123 self.logger.info("Deleting address_manager_instance %s" % address_manager_instance)
124 address_manager_instance.delete()
125
126 instance.delete()
127
128 def save_instance(self, service_instance, instance):
129 instance.no_sync = True # prevent instance from being synced until we're done with it
130 super(HSSDBServiceInstancePolicy, self).save_instance(instance)
131
132 try:
133 if instance.isolation in ["container", "container_vm"]:
134 raise Exception("Not supported")
135
136 instance_tag = self.get_instance_tag(service_instance)
137 if instance_tag:
138 tags = Tag.objects.filter(name="instance_tag", value=instance_tag)
139 if not tags:
140 tag = Tag(service=service_instance.owner, content_type=instance.self_content_type_id, object_id=instance.id, name="instance_tag", value=str(instance_tag))
141 tag.save()
142
143 instance.no_sync = False # allow the synchronizer to run now
144 super(HSSDBServiceInstancePolicy, self).save_instance(instance)
145 except:
146 # need to clean up any failures here
147 raise
148
149 def get_instance_tag(self, service_instance):
150 return '%d'%service_instance.id
151
152 def get_image(self, service_instance):
153 return service_instance.hssdb_vendor.image
154
155 def get_flavor(self, service_vendor):
156 return service_vendor.hssdb_vendor.flavor