Woojoong Kim | 033b2ce | 2017-10-10 10:09:06 -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 | from synchronizers.new_base.modelaccessor import * |
| 17 | from synchronizers.new_base.model_policies.model_policy_tenantwithcontainer import TenantWithContainerPolicy, LeastLoadedNodeScheduler |
| 18 | from synchronizers.new_base.exceptions import * |
| 19 | |
| 20 | class VHSSTenantPolicy(TenantWithContainerPolicy): |
| 21 | model_name = "VHSSTenant" |
| 22 | |
| 23 | # Already defined in Super class |
| 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 VHSSTenant.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 = VHSSTenant.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("VHSSTenant Instance %s is now unused -- deleting" % service_instance.instance) |
| 45 | self.delete_instance(service_instance, service_instance.instance) |
| 46 | else: |
| 47 | self.logger.info("VHSSTenant Instance %s has %d other service instances attached" % |
| 48 | (service_instance.instance, len(other_service_instances_this_instance))) |
| 49 | |
| 50 | def get_service(self, service_instance): |
| 51 | service_name = service_instance.owner.leaf_model_name |
| 52 | service_class = globals()[service_name] |
| 53 | return service_class.objects.get(id=service_instance.owner.id) |
| 54 | |
| 55 | def find_instance_for_instance_tag(self, instance_tag): |
| 56 | tags = Tag.objects.filter(name="instance_tag", value=instance_tag) |
| 57 | if tags: |
| 58 | return tags[0].content_object |
| 59 | return None |
| 60 | |
| 61 | def find_or_make_instance_for_instance_tag(self, service_instance): |
| 62 | instance_tag = self.get_instance_tag(service_instance) |
| 63 | instance = self.find_instance_for_instance_tag(instance_tag) |
| 64 | if instance: |
| 65 | if instance.no_sync: |
| 66 | # if no_sync is still set, then perhaps we failed while saving it and need to retry. |
| 67 | self.save_instance(service_instance, instance) |
| 68 | return instance |
| 69 | |
| 70 | desired_image = self.get_image(service_instance) |
| 71 | desired_flavor = self.get_flavor(service_instance) |
| 72 | |
| 73 | slice = service_instance.owner.slices.first() |
| 74 | |
| 75 | (node, parent) = LeastLoadedNodeScheduler(slice, label=None).pick() |
| 76 | |
| 77 | assert (slice is not None) |
| 78 | assert (node is not None) |
| 79 | assert (desired_image is not None) |
| 80 | assert (service_instance.creator is not None) |
| 81 | assert (node.site_deployment.deployment is not None) |
| 82 | assert (desired_image is not None) |
| 83 | |
| 84 | instance = Instance(slice=slice, |
| 85 | node=node, |
| 86 | image=desired_image, |
| 87 | creator=service_instance.creator, |
| 88 | deployment=node.site_deployment.deployment, |
| 89 | flavor=desired_flavor, |
| 90 | isolation=slice.default_isolation, |
| 91 | parent=parent) |
| 92 | |
| 93 | self.save_instance(service_instance, instance) |
| 94 | |
| 95 | return instance |
| 96 | |
| 97 | def manage_container(self, service_instance): |
| 98 | if service_instance.deleted: |
| 99 | return |
| 100 | |
| 101 | if service_instance.instance: |
| 102 | # We're good. |
| 103 | return |
| 104 | |
| 105 | instance = self.find_or_make_instance_for_instance_tag(service_instance) |
| 106 | service_instance.instance = instance |
| 107 | # TODO: possible for partial failure here? |
| 108 | service_instance.save() |
| 109 | |
| 110 | def delete_instance(self, service_instance, instance): |
| 111 | # delete the `instance_tag` tags |
| 112 | tags = Tag.objects.filter(service_id=service_instance.owner.id, content_type=instance.self_content_type_id, |
| 113 | object_id=instance.id, name="instance_tag") |
| 114 | for tag in tags: |
| 115 | tag.delete() |
| 116 | |
| 117 | instance.delete() |
| 118 | |
| 119 | def save_instance(self, service_instance, instance): |
| 120 | instance.no_sync = True # prevent instance from being synced until we're done with it |
| 121 | super(VHSSTenantPolicy, self).save_instance(instance) |
| 122 | |
| 123 | try: |
| 124 | if instance.isolation in ["container", "container_vm"]: |
| 125 | raise Exception("Not supported") |
| 126 | |
| 127 | instance_tag = self.get_instance_tag(service_instance) |
| 128 | if instance_tag: |
| 129 | tags = Tag.objects.filter(name="instance_tag", value=instance_tag) |
| 130 | if not tags: |
| 131 | tag = Tag(service=service_instance.owner, content_type=instance.self_content_type_id, |
| 132 | object_id=instance.id, name="instance_tag", value=str(instance_tag)) |
| 133 | tag.save() |
| 134 | |
| 135 | instance.no_sync = False # allow the synchronizer to run now |
| 136 | super(VHSSTenantPolicy, self).save_instance(instance) |
| 137 | except: |
| 138 | # need to clean up any failures here |
| 139 | raise |
| 140 | |
| 141 | def get_instance_tag(self, service_instance): |
| 142 | return '%d' % service_instance.id |
| 143 | |
| 144 | def get_image(self, service_instance): |
| 145 | return service_instance.vhss_vendor.image |
| 146 | |
| 147 | def get_flavor(self, service_vendor): |
| 148 | return service_vendor.vhss_vendor.flavor |