Wei-Yu Chen | ba04359 | 2017-10-18 17:08:51 +0800 | [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 VSPGWUTenantPolicy(TenantWithContainerPolicy): |
| 22 | model_name = "VSPGWUTenant" |
Sapan Bhatia | f50fe4e | 2017-12-12 12:56:27 -0500 | [diff] [blame] | 23 | constrain_by_service_instance = True |
Wei-Yu Chen | ba04359 | 2017-10-18 17:08:51 +0800 | [diff] [blame] | 24 | |
| 25 | def handle_create(self, service_instance): |
| 26 | return self.handle_update(service_instance) |
| 27 | |
| 28 | def handle_update(self, service_instance): |
| 29 | if (service_instance.link_deleted_count>0) and (not service_instance.provided_links.exists()): |
| 30 | self.logger.info("The last provided link has been deleted -- self-destructing.") |
| 31 | self.handle_delete(service_instance) |
| 32 | if VSPGWUTenant.objects.filter(id=service_instance.id).exists(): |
| 33 | service_instance.delete() |
| 34 | else: |
| 35 | self.logger.info("Tenant %s is already deleted" % service_instance) |
| 36 | return |
| 37 | |
| 38 | self.manage_container(service_instance) |
| 39 | |
| 40 | def handle_delete(self, service_instance): |
| 41 | if service_instance.instance and (not service_instance.instance.deleted): |
| 42 | all_service_instances_this_instance = VSPGWUTenant.objects.filter(instance_id=service_instance.instance.id) |
| 43 | other_service_instances_this_instance = [x for x in all_service_instances_this_instance if x.id != service_instance.id] |
| 44 | if (not other_service_instances_this_instance): |
| 45 | self.logger.info("VSPGWUTenant Instance %s is now unused -- deleting" % service_instance.instance) |
| 46 | self.delete_instance(service_instance, service_instance.instance) |
| 47 | else: |
| 48 | self.logger.info("VSPGWUTenant Instance %s has %d other service instances attached" % (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) |
Andy Bavier | a2d772e | 2018-02-12 14:52:56 -0700 | [diff] [blame^] | 72 | node_label = service_instance.node_label |
| 73 | constrain_by_service_instance = self.constrain_by_service_instance |
Wei-Yu Chen | ba04359 | 2017-10-18 17:08:51 +0800 | [diff] [blame] | 74 | |
| 75 | slice = service_instance.owner.slices.first() |
| 76 | |
Andy Bavier | a2d772e | 2018-02-12 14:52:56 -0700 | [diff] [blame^] | 77 | scheduler = LeastLoadedNodeScheduler(slice, label=node_label, constrain_by_service_instance=constrain_by_service_instance) |
| 78 | (node, parent) = scheduler.pick() |
Wei-Yu Chen | ba04359 | 2017-10-18 17:08:51 +0800 | [diff] [blame] | 79 | |
| 80 | assert (slice is not None) |
| 81 | assert (node is not None) |
| 82 | assert (desired_image is not None) |
| 83 | assert (service_instance.creator is not None) |
| 84 | assert (node.site_deployment.deployment is not None) |
| 85 | assert (desired_image is not None) |
| 86 | |
| 87 | instance = Instance(slice=slice, |
| 88 | node=node, |
| 89 | image=desired_image, |
| 90 | creator=service_instance.creator, |
| 91 | deployment=node.site_deployment.deployment, |
| 92 | flavor=desired_flavor, |
| 93 | isolation=slice.default_isolation, |
| 94 | parent=parent) |
| 95 | |
| 96 | self.save_instance(service_instance, instance) |
| 97 | |
| 98 | return instance |
| 99 | |
| 100 | def manage_container(self, service_instance): |
| 101 | if service_instance.deleted: |
| 102 | return |
| 103 | |
| 104 | if service_instance.instance: |
| 105 | # We're good. |
| 106 | return |
| 107 | |
| 108 | instance = self.find_or_make_instance_for_instance_tag(service_instance) |
| 109 | service_instance.instance = instance |
| 110 | # TODO: possible for partial failure here? |
| 111 | service_instance.save() |
| 112 | |
| 113 | def delete_instance(self, service_instance, instance): |
| 114 | # delete the `instance_tag` tags |
| 115 | tags = Tag.objects.filter(service_id=service_instance.owner.id, content_type=instance.self_content_type_id, |
| 116 | object_id=instance.id, name="instance_tag") |
| 117 | for tag in tags: |
| 118 | tag.delete() |
| 119 | |
| 120 | tags = Tag.objects.filter(content_type=instance.self_content_type_id, object_id=instance.id, |
| 121 | name="vm_vrouter_tenant") |
| 122 | for tag in tags: |
| 123 | address_manager_instances = list(ServiceInstance.objects.filter(id=tag.value)) |
| 124 | tag.delete() |
| 125 | |
| 126 | # TODO: Potential partial failure |
| 127 | |
| 128 | for address_manager_instance in address_manager_instances: |
| 129 | self.logger.info("Deleting address_manager_instance %s" % address_manager_instance) |
| 130 | address_manager_instance.delete() |
| 131 | |
| 132 | instance.delete() |
| 133 | |
| 134 | def save_instance(self, service_instance, instance): |
| 135 | instance.no_sync = True # prevent instance from being synced until we're done with it |
| 136 | super(VSPGWUTenantPolicy, self).save_instance(instance) |
| 137 | |
| 138 | try: |
| 139 | if instance.isolation in ["container", "container_vm"]: |
| 140 | raise Exception("Not supported") |
| 141 | |
| 142 | instance_tag = self.get_instance_tag(service_instance) |
| 143 | if instance_tag: |
| 144 | tags = Tag.objects.filter(name="instance_tag", value=instance_tag) |
| 145 | if not tags: |
| 146 | tag = Tag(service=service_instance.owner, content_type=instance.self_content_type_id, object_id=instance.id, name="instance_tag", value=str(instance_tag)) |
| 147 | tag.save() |
| 148 | |
| 149 | instance.no_sync = False # allow the synchronizer to run now |
| 150 | super(VSPGWUTenantPolicy, self).save_instance(instance) |
| 151 | except: |
| 152 | # need to clean up any failures here |
| 153 | raise |
| 154 | |
| 155 | def get_instance_tag(self, service_instance): |
| 156 | return '%d'%service_instance.id |
| 157 | |
| 158 | def get_image(self, service_instance): |
| 159 | return service_instance.vspgwu_vendor.image |
| 160 | |
| 161 | def get_flavor(self, service_vendor): |
| 162 | return service_vendor.vspgwu_vendor.flavor |