Sapan Bhatia | 10b6256 | 2017-10-09 18:05:35 -0400 | [diff] [blame^] | 1 | |
| 2 | |
| 3 | # Copyright 2017-present Open Networking Foundation |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | from synchronizers.new_base.modelaccessor import * |
| 18 | from synchronizers.new_base.model_policies.model_policy_tenantwithcontainer import TenantWithContainerPolicy, LeastLoadedNodeScheduler |
| 19 | from synchronizers.new_base.exceptions import * |
| 20 | |
| 21 | class VMMETenantPolicy(TenantWithContainerPolicy): |
| 22 | model_name = "VMMETenant" |
| 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 VMMETenant.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 = VMMETenant.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("VMMETenant Instance %s is now unused -- deleting" % service_instance.instance) |
| 45 | self.delete_instance(service_instance, service_instance.instance) |
| 46 | else: |
| 47 | self.logger.info("VMMETenant Instance %s has %d other service instances attached" % (service_instance.instance, len(other_service_instances_this_instance))) |
| 48 | |
| 49 | def get_service(self, service_instance): |
| 50 | service_name = service_instance.owner.leaf_model_name |
| 51 | service_class = globals()[service_name] |
| 52 | return service_class.objects.get(id=service_instance.owner.id) |
| 53 | |
| 54 | def find_instance_for_instance_tag(self, instance_tag): |
| 55 | tags = Tag.objects.filter(name="instance_tag", value=instance_tag) |
| 56 | if tags: |
| 57 | return tags[0].content_object |
| 58 | return None |
| 59 | |
| 60 | def find_or_make_instance_for_instance_tag(self, service_instance): |
| 61 | instance_tag = self.get_instance_tag(service_instance) |
| 62 | instance = self.find_instance_for_instance_tag(instance_tag) |
| 63 | if instance: |
| 64 | if instance.no_sync: |
| 65 | # if no_sync is still set, then perhaps we failed while saving it and need to retry. |
| 66 | self.save_instance(service_instance, instance) |
| 67 | return instance |
| 68 | |
| 69 | desired_image = self.get_image(service_instance) |
| 70 | desired_flavor = self.get_flavor(service_instance) |
| 71 | |
| 72 | slice = service_instance.owner.slices.first() |
| 73 | |
| 74 | (node, parent) = LeastLoadedNodeScheduler(slice, label=None).pick() |
| 75 | |
| 76 | assert (slice is not None) |
| 77 | assert (node is not None) |
| 78 | assert (desired_image is not None) |
| 79 | assert (service_instance.creator is not None) |
| 80 | assert (node.site_deployment.deployment is not None) |
| 81 | assert (desired_image is not None) |
| 82 | |
| 83 | instance = Instance(slice=slice, |
| 84 | node=node, |
| 85 | image=desired_image, |
| 86 | creator=service_instance.creator, |
| 87 | deployment=node.site_deployment.deployment, |
| 88 | flavor=desired_flavor, |
| 89 | isolation=slice.default_isolation, |
| 90 | parent=parent) |
| 91 | |
| 92 | self.save_instance(service_instance, instance) |
| 93 | |
| 94 | return instance |
| 95 | |
| 96 | def manage_container(self, service_instance): |
| 97 | if service_instance.deleted: |
| 98 | return |
| 99 | |
| 100 | if service_instance.instance: |
| 101 | # We're good. |
| 102 | return |
| 103 | |
| 104 | instance = self.find_or_make_instance_for_instance_tag(service_instance) |
| 105 | service_instance.instance = instance |
| 106 | # TODO: possible for partial failure here? |
| 107 | service_instance.save() |
| 108 | |
| 109 | def delete_instance(self, service_instance, instance): |
| 110 | # delete the `instance_tag` tags |
| 111 | tags = Tag.objects.filter(service_id=service_instance.owner.id, content_type=instance.self_content_type_id, |
| 112 | object_id=instance.id, name="instance_tag") |
| 113 | for tag in tags: |
| 114 | tag.delete() |
| 115 | |
| 116 | tags = Tag.objects.filter(content_type=instance.self_content_type_id, object_id=instance.id, |
| 117 | name="vm_vrouter_tenant") |
| 118 | for tag in tags: |
| 119 | address_manager_instances = list(ServiceInstance.objects.filter(id=tag.value)) |
| 120 | tag.delete() |
| 121 | |
| 122 | # TODO: Potential partial failure |
| 123 | |
| 124 | for address_manager_instance in address_manager_instances: |
| 125 | self.logger.info("Deleting address_manager_instance %s" % address_manager_instance) |
| 126 | address_manager_instance.delete() |
| 127 | |
| 128 | instance.delete() |
| 129 | |
| 130 | def save_instance(self, service_instance, instance): |
| 131 | instance.no_sync = True # prevent instance from being synced until we're done with it |
| 132 | super(VMMETenantPolicy, self).save_instance(instance) |
| 133 | |
| 134 | try: |
| 135 | if instance.isolation in ["container", "container_vm"]: |
| 136 | raise Exception("Not supported") |
| 137 | |
| 138 | instance_tag = self.get_instance_tag(service_instance) |
| 139 | if instance_tag: |
| 140 | tags = Tag.objects.filter(name="instance_tag", value=instance_tag) |
| 141 | if not tags: |
| 142 | tag = Tag(service=service_instance.owner, content_type=instance.self_content_type_id, object_id=instance.id, name="instance_tag", value=str(instance_tag)) |
| 143 | tag.save() |
| 144 | |
| 145 | instance.no_sync = False # allow the synchronizer to run now |
| 146 | super(VMMETenantPolicy, self).save_instance(instance) |
| 147 | except: |
| 148 | # need to clean up any failures here |
| 149 | raise |
| 150 | |
| 151 | def get_instance_tag(self, service_instance): |
| 152 | return '%d'%service_instance.id |
| 153 | |
| 154 | def get_image(self, service_instance): |
| 155 | return service_instance.vmme_vendor.image |
| 156 | |
| 157 | def get_flavor(self, service_vendor): |
| 158 | return service_vendor.vmme_vendor.flavor |