Sapan Bhatia | 2810e03 | 2017-11-03 00:31:00 -0400 | [diff] [blame^] | 1 | # 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 | |
| 15 | from synchronizers.new_base.modelaccessor import * |
| 16 | from synchronizers.new_base.model_policies.model_policy_tenantwithcontainer import TenantWithContainerPolicy, LeastLoadedNodeScheduler |
| 17 | from synchronizers.new_base.exceptions import * |
| 18 | |
| 19 | from xosconfig import Config |
| 20 | from multistructlog import create_logger |
| 21 | |
| 22 | log = create_logger(Config().get('logging')) |
| 23 | blueprints = Config().get('blueprints') |
| 24 | |
| 25 | def service_of_service_instance(si): |
| 26 | if si.endswith('Tenant'): |
| 27 | return si[:-len('Tenant')] + 'Service' |
| 28 | elif si.endswith('ServiceInstance'): |
| 29 | return si[:-len('ServiceInstance')] + 'Service' |
| 30 | else: |
| 31 | raise Exception('Could not translate service instance into service: %s'%si) |
| 32 | |
| 33 | class VEPCServiceInstancePolicy(TenantWithContainerPolicy): |
| 34 | model_name = "VEPCServiceInstance" |
| 35 | |
| 36 | """TODO: Update the following to not be service-specific |
| 37 | This code assumes there is only one vendor installed |
| 38 | """ |
| 39 | def configure_service_instance(self, service_instance): |
| 40 | if service_instance.leaf_model_name == 'VSPGWUTenant': |
| 41 | vendor = VSPGWUVendor.objects.first() |
| 42 | if not vendor: |
| 43 | raise Exception('No VSPGWU vendors') |
| 44 | service_instance.vspgwu_vendor = vendor |
| 45 | elif service_instance.leaf_model_name == 'VSPGWCTenant': |
| 46 | vendor = VSPGWCVendor.objects.first() |
| 47 | if not vendor: |
| 48 | raise Exception('No VSPGWC vendors') |
| 49 | service_instance.vspgwc_vendor = vendor |
| 50 | |
| 51 | def child_service_instance_from_name(self, name): |
| 52 | service_instances = self.obj.child_serviceinstances.all() |
| 53 | |
| 54 | try: |
| 55 | service_instance = next(s for s in service_instances if s.leaf_model_name == name) |
| 56 | except StopIteration: |
| 57 | service_instance = None |
| 58 | |
| 59 | return service_instance |
| 60 | |
| 61 | def get_service_for_service_instance(self, si): |
| 62 | service = service_of_service_instance(si) |
| 63 | service_class = getattr(Slice().stub, service) |
| 64 | service_obj = service_class.objects.first() # There's only one service object |
| 65 | return service_obj |
| 66 | |
| 67 | def create_service_instance(si): |
| 68 | service = self.get_service_for_service_instance(si) |
| 69 | if not service: |
| 70 | raise Exception('No service object for %s'%service) |
| 71 | |
| 72 | si_class = getattr(Slice().stub, si) |
| 73 | s = si_class(owner = service, name = 'epc-' + si.lower()) |
| 74 | s.master_serviceinstance = self.obj |
| 75 | |
| 76 | self.configure_service_instance(s) |
| 77 | s.save() |
| 78 | return s |
| 79 | |
| 80 | def create_link(self, src, dst): |
| 81 | src_instance = self.child_service_instance_from_name(src) |
| 82 | if not src_instance: |
| 83 | src_instance = self.create_service_instance(src) |
| 84 | |
| 85 | dst_instance = self.child_service_instance_from_name(dst) |
| 86 | if not dst_instance: |
| 87 | dst_instance = self.create_service_instance(dst) |
| 88 | |
| 89 | src_service = self.get_service_for_service_instance(src) |
| 90 | dst_service = self.get_service_for_service_instance(dst) |
| 91 | |
| 92 | service_dependency = ServiceDependency.objects.filter(provider_service_id = dst_service.id, subscriber_service_id = src_service.id) |
| 93 | if not service_dependency: |
| 94 | service_dependency = ServiceDependency(provider_service = dst_service, subscriber_service = src_service) |
| 95 | service_dependency.save() |
| 96 | |
| 97 | service_instance_link = ServiceInstanceLink.objects.filter(provider_service_instance_id = dst_instance.id, subscriber_service_instance_id = src_instance.id) |
| 98 | if not service_instance_link: |
| 99 | service_instance_link = ServiceInstanceLink(provider_service_instance = dst_instance, subscriber_service_instance = src_instance) |
| 100 | service_instance_link.save() |
| 101 | |
| 102 | def recursive_create_links(self, blueprint, src): |
| 103 | for k, v in blueprint.iteritems(): |
| 104 | if src: |
| 105 | self.create_link(src, k) |
| 106 | |
| 107 | if isinstance(v, dict): |
| 108 | self.recursive_create_links(v, k) |
| 109 | else: |
| 110 | self.create_link(src, k) |
| 111 | |
| 112 | def create_child_services(self, service_instance): |
| 113 | self.obj = service_instance |
| 114 | # Create service graph based on blueprint |
| 115 | chosen_blueprint = service_instance.blueprint |
| 116 | try: |
| 117 | blueprint = next(b for b in blueprints if b['name'] == chosen_blueprint) |
| 118 | except StopIteration: |
| 119 | log.error('Chosen blueprint (%s) not found' % chosen_blueprint) |
| 120 | |
| 121 | self.recursive_create_links(blueprint['graph'], None) |
| 122 | |
| 123 | def handle_update(self, service_instance): |
| 124 | self.create_child_services(service_instance) |
| 125 | |
| 126 | if (service_instance.link_deleted_count > 0) and (not service_instance.provided_links.exists()): |
| 127 | self.logger.info( |
| 128 | "The last provided link has been deleted -- self-destructing.") |
| 129 | self.handle_delete(service_instance) |
| 130 | if VEPCServiceInstance.objects.filter(id=service_instance.id).exists(): |
| 131 | service_instance.delete() |
| 132 | else: |
| 133 | self.logger.info("Tenant %s is already deleted" % |
| 134 | service_instance) |
| 135 | return |
| 136 | |
| 137 | self.manage_container(service_instance) |
| 138 | |
| 139 | def handle_delete(self, service_instance): |
| 140 | if service_instance.instance and (not service_instance.instance.deleted): |
| 141 | all_service_instances_this_instance = VEPCServiceInstance.objects.filter( |
| 142 | instance_id=service_instance.instance.id) |
| 143 | other_service_instances_this_instance = [ |
| 144 | x for x in all_service_instances_this_instance if x.id != service_instance.id] |
| 145 | if (not other_service_instances_this_instance): |
| 146 | self.logger.info( |
| 147 | "VEPCServiceInstance Instance %s is now unused -- deleting" % service_instance.instance) |
| 148 | self.delete_instance( |
| 149 | service_instance, service_instance.instance) |
| 150 | else: |
| 151 | self.logger.info("VEPCServiceInstance Instance %s has %d other service instances attached" % ( |
| 152 | service_instance.instance, len(other_service_instances_this_instance))) |
| 153 | |
| 154 | def get_service(self, service_instance): |
| 155 | service_name = service_instance.owner.leaf_model_name |
| 156 | service_class = globals()[service_name] |
| 157 | return service_class.objects.get(id=service_instance.owner.id) |
| 158 | |
| 159 | def find_instance_for_instance_tag(self, instance_tag): |
| 160 | tags = Tag.objects.filter(name="instance_tag", value=instance_tag) |
| 161 | if tags: |
| 162 | return tags[0].content_object |
| 163 | return None |
| 164 | |
| 165 | def find_or_make_instance_for_instance_tag(self, service_instance): |
| 166 | instance_tag = self.get_instance_tag(service_instance) |
| 167 | instance = self.find_instance_for_instance_tag(instance_tag) |
| 168 | if instance: |
| 169 | if instance.no_sync: |
| 170 | # if no_sync is still set, then perhaps we failed while saving it and need to retry. |
| 171 | self.save_instance(service_instance, instance) |
| 172 | return instance |
| 173 | |
| 174 | desired_image = self.get_image(service_instance) |
| 175 | desired_flavor = self.get_flavor(service_instance) |
| 176 | |
| 177 | slice = service_instance.owner.slices.first() |
| 178 | |
| 179 | (node, parent) = LeastLoadedNodeScheduler(slice, label=None).pick() |
| 180 | |
| 181 | assert (slice is not None) |
| 182 | assert (node is not None) |
| 183 | assert (desired_image is not None) |
| 184 | assert (service_instance.creator is not None) |
| 185 | assert (node.site_deployment.deployment is not None) |
| 186 | assert (desired_image is not None) |
| 187 | |
| 188 | instance = Instance(slice=slice, |
| 189 | node=node, |
| 190 | image=desired_image, |
| 191 | creator=service_instance.creator, |
| 192 | deployment=node.site_deployment.deployment, |
| 193 | flavor=flavors[0], |
| 194 | isolation=slice.default_isolation, |
| 195 | parent=parent) |
| 196 | |
| 197 | self.save_instance(service_instance, instance) |
| 198 | |
| 199 | return instance |
| 200 | |
| 201 | def manage_container(self, service_instance): |
| 202 | if service_instance.deleted: |
| 203 | return |
| 204 | |
| 205 | if service_instance.instance: |
| 206 | # We're good. |
| 207 | return |
| 208 | |
| 209 | instance = self.find_or_make_instance_for_instance_tag( |
| 210 | service_instance) |
| 211 | service_instance.instance = instance |
| 212 | # TODO: possible for partial failure here? |
| 213 | service_instance.save() |
| 214 | |
| 215 | def delete_instance(self, service_instance, instance): |
| 216 | # delete the `instance_tag` tags |
| 217 | tags = Tag.objects.filter(service_id=service_instance.owner.id, content_type=instance.self_content_type_id, |
| 218 | object_id=instance.id, name="instance_tag") |
| 219 | for tag in tags: |
| 220 | tag.delete() |
| 221 | |
| 222 | tags = Tag.objects.filter(content_type=instance.self_content_type_id, object_id=instance.id, |
| 223 | name="vm_vrouter_tenant") |
| 224 | for tag in tags: |
| 225 | address_manager_instances = list( |
| 226 | ServiceInstance.objects.filter(id=tag.value)) |
| 227 | tag.delete() |
| 228 | |
| 229 | # TODO: Potential partial failure |
| 230 | |
| 231 | for address_manager_instance in address_manager_instances: |
| 232 | self.logger.info( |
| 233 | "Deleting address_manager_instance %s" % address_manager_instance) |
| 234 | address_manager_instance.delete() |
| 235 | |
| 236 | instance.delete() |
| 237 | |
| 238 | def save_instance(self, service_instance, instance): |
| 239 | instance.volumes = "/etc/dnsmasq.d,/etc/ufw" |
| 240 | instance.no_sync = True # prevent instance from being synced until we're done with it |
| 241 | super(VEPCServiceInstancePolicy, self).save_instance(instance) |
| 242 | |
| 243 | try: |
| 244 | if instance.isolation in ["container", "container_vm"]: |
| 245 | raise Exception("Not supported") |
| 246 | |
| 247 | instance_tag = self.get_instance_tag(service_instance) |
| 248 | |
| 249 | if instance_tag: |
| 250 | tags = Tag.objects.filter( |
| 251 | name="instance_tag", value=instance_tag) |
| 252 | if not tags: |
| 253 | tag = Tag(service=service_instance.owner, content_type=instance.self_content_type_id, |
| 254 | object_id=instance.id, name="instance_tag", value=str(instance_tag)) |
| 255 | tag.save() |
| 256 | |
| 257 | instance.no_sync = False # allow the synchronizer to run now |
| 258 | super(VEPCServiceInstancePolicy, self).save_instance(instance) |
| 259 | except: |
| 260 | # need to clean up any failures here |
| 261 | raise |
| 262 | |
| 263 | def get_instance_tag(self, service_instance): |
| 264 | return '%d'%service_instance.id |
| 265 | |
| 266 | def get_image(self, service_instance): |
| 267 | return None |
| 268 | |
| 269 | def get_flavor(self, service_instance): |
| 270 | raise None |
| 271 | |