Matteo Scandolo | aca8665 | 2017-08-08 13:05:27 -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 | |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 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 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 21 | class VSGServiceInstancePolicy(TenantWithContainerPolicy): |
| 22 | model_name = "VSGServiceInstance" |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 23 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 24 | def handle_create(self, service_instance): |
| 25 | return self.handle_update(service_instance) |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 26 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 27 | def handle_update(self, service_instance): |
| 28 | if (service_instance.link_deleted_count>0) and (not service_instance.provided_links.exists()): |
Scott Baker | 0bbd7f2 | 2017-09-05 17:16:40 -0700 | [diff] [blame] | 29 | # if the last provided_link has just gone away, then self-destruct |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 30 | self.logger.info("The last provided link has been deleted -- self-destructing.") |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 31 | # TODO: We shouldn't have to call handle_delete ourselves. The model policy framework should handle this |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 32 | # for us, but it isn't. I think that's happening is that serviceinstance.delete() isn't setting a new |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 33 | # updated timestamp, since there's no way to pass `always_update_timestamp`, and therefore the |
| 34 | # policy framework doesn't know that the object has changed and needs new policies. For now, the |
| 35 | # workaround is to just call handle_delete ourselves. |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 36 | self.handle_delete(service_instance) |
| 37 | # Note that if we deleted the Instance in handle_delete, then django may have cascade-deleted the service |
| 38 | # instance by now. Thus we have to guard our delete, to check that the service instance still exists. |
| 39 | if VSGServiceInstance.objects.filter(id=service_instance.id).exists(): |
| 40 | service_instance.delete() |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 41 | else: |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 42 | self.logger.info("Tenant %s is already deleted" % service_instance) |
Scott Baker | 0bbd7f2 | 2017-09-05 17:16:40 -0700 | [diff] [blame] | 43 | return |
| 44 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 45 | self.manage_container(service_instance) |
| 46 | self.manage_address_service_instance(service_instance) |
| 47 | self.cleanup_orphans(service_instance) |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 48 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 49 | def handle_delete(self, service_instance): |
| 50 | if service_instance.instance and (not service_instance.instance.deleted): |
| 51 | all_service_instances_this_instance = VSGServiceInstance.objects.filter(instance_id=service_instance.instance.id) |
| 52 | other_service_instances_this_instance = [x for x in all_service_instances_this_instance if x.id != service_instance.id] |
| 53 | if (not other_service_instances_this_instance): |
| 54 | self.logger.info("VSG Instance %s is now unused -- deleting" % service_instance.instance) |
| 55 | self.delete_instance(service_instance, service_instance.instance) |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 56 | else: |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 57 | self.logger.info("VSG Instance %s has %d other service instances attached" % (service_instance.instance, len(other_service_instances_this_instance))) |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 58 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 59 | def manage_address_service_instance(self, service_instance): |
| 60 | if service_instance.deleted: |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 61 | return |
| 62 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 63 | if service_instance.address_service_instance is None: |
| 64 | address_service_instance = self.allocate_public_service_instance(address_pool_name="addresses_vsg", subscriber_tenant=service_instance) |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 65 | address_service_instance.save() |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 66 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 67 | def cleanup_orphans(self, service_instance): |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 68 | # ensure vSG only has one AddressManagerServiceInstance |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 69 | cur_asi = service_instance.address_service_instance |
| 70 | for link in service_instance.subscribed_links.all(): |
Scott Baker | 80238f8 | 2017-07-18 16:01:10 -0700 | [diff] [blame] | 71 | # TODO: hardcoded dependency |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 72 | # cast from ServiceInstance to AddressManagerServiceInstance |
| 73 | asis = AddressManagerServiceInstance.objects.filter(id = link.provider_service_instance.id) |
| 74 | for asi in asis: |
| 75 | if (not cur_asi) or (asi.id != cur_asi.id): |
| 76 | asi.delete() |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 77 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 78 | def get_vsg_service(self, service_instance): |
| 79 | return VSGService.objects.get(id=service_instance.owner.id) |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 80 | |
| 81 | def find_instance_for_s_tag(self, s_tag): |
| 82 | tags = Tag.objects.filter(name="s_tag", value=s_tag) |
| 83 | if tags: |
| 84 | return tags[0].content_object |
| 85 | |
| 86 | return None |
| 87 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 88 | def find_or_make_instance_for_s_tag(self, service_instance): |
| 89 | instance = self.find_instance_for_s_tag(service_instance.volt.s_tag) |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 90 | if instance: |
| 91 | if instance.no_sync: |
| 92 | # if no_sync is still set, then perhaps we failed while saving it and need to retry. |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 93 | self.save_instance(service_instance, instance) |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 94 | return instance |
| 95 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 96 | desired_image = self.get_image(service_instance) |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 97 | |
| 98 | flavors = Flavor.objects.filter(name="m1.small") |
| 99 | if not flavors: |
| 100 | raise SynchronizerConfigurationError("No m1.small flavor") |
| 101 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 102 | slice = service_instance.owner.slices.first() |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 103 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 104 | (node, parent) = LeastLoadedNodeScheduler(slice, label=self.get_vsg_service(service_instance).node_label).pick() |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 105 | |
| 106 | assert (slice is not None) |
| 107 | assert (node is not None) |
| 108 | assert (desired_image is not None) |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 109 | assert (service_instance.creator is not None) |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 110 | assert (node.site_deployment.deployment is not None) |
| 111 | assert (desired_image is not None) |
| 112 | |
| 113 | instance = Instance(slice=slice, |
| 114 | node=node, |
| 115 | image=desired_image, |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 116 | creator=service_instance.creator, |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 117 | deployment=node.site_deployment.deployment, |
| 118 | flavor=flavors[0], |
| 119 | isolation=slice.default_isolation, |
| 120 | parent=parent) |
| 121 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 122 | self.save_instance(service_instance, instance) |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 123 | |
| 124 | return instance |
| 125 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 126 | def manage_container(self, service_instance): |
| 127 | if service_instance.deleted: |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 128 | return |
| 129 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 130 | if not service_instance.volt: |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 131 | raise SynchronizerConfigurationError("This VSG container has no volt") |
| 132 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 133 | if service_instance.instance: |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 134 | # We're good. |
| 135 | return |
| 136 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 137 | instance = self.find_or_make_instance_for_s_tag(service_instance) |
| 138 | service_instance.instance = instance |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 139 | # TODO: possible for partial failure here? |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 140 | service_instance.save() |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 141 | |
| 142 | def find_or_make_port(self, instance, network, **kwargs): |
| 143 | port = Port.objects.filter(instance_id=instance.id, network_id=network.id) |
| 144 | if port: |
| 145 | port = port[0] |
| 146 | else: |
| 147 | port = Port(instance=instance, network=network, **kwargs) |
| 148 | port.save() |
| 149 | return port |
| 150 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 151 | def get_lan_network(self, service_instance, instance): |
| 152 | slice = service_instance.owner.slices.all()[0] |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 153 | # there should only be one network private network, and its template should not be the management template |
| 154 | lan_networks = [x for x in slice.networks.all() if |
| 155 | x.template.visibility == "private" and (not "management" in x.template.name)] |
| 156 | if len(lan_networks) > 1: |
| 157 | raise SynchronizerProgrammingError("The vSG slice should only have one non-management private network") |
| 158 | if not lan_networks: |
| 159 | raise SynchronizerProgrammingError("No lan_network") |
| 160 | return lan_networks[0] |
| 161 | |
| 162 | def port_set_parameter(self, port, name, value): |
| 163 | pt = NetworkParameterType.objects.get(name=name) |
| 164 | existing_params = NetworkParameter.objects.filter(parameter_id=pt.id, content_type=port.self_content_type_id, object_id=port.id) |
| 165 | |
| 166 | if existing_params: |
| 167 | p = existing_params[0] |
| 168 | p.value = str(value) |
| 169 | p.save() |
| 170 | else: |
| 171 | p = NetworkParameter(parameter=pt, content_type=port.self_content_type_id, object_id=port.id, value=str(value)) |
| 172 | p.save() |
| 173 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 174 | def delete_instance(self, service_instance, instance): |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 175 | # delete the `s_tag` tags |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 176 | tags = Tag.objects.filter(service_id=service_instance.owner.id, content_type=instance.self_content_type_id, |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 177 | object_id=instance.id, name="s_tag") |
| 178 | for tag in tags: |
| 179 | tag.delete() |
| 180 | |
| 181 | tags = Tag.objects.filter(content_type=instance.self_content_type_id, object_id=instance.id, |
| 182 | name="vm_vrouter_tenant") |
| 183 | for tag in tags: |
| 184 | address_manager_instances = list(ServiceInstance.objects.filter(id=tag.value)) |
| 185 | tag.delete() |
| 186 | |
| 187 | # TODO: Potential partial failure |
| 188 | |
| 189 | for address_manager_instance in address_manager_instances: |
| 190 | self.logger.info("Deleting address_manager_instance %s" % address_manager_instance) |
| 191 | address_manager_instance.delete() |
| 192 | |
| 193 | instance.delete() |
| 194 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 195 | def save_instance(self, service_instance, instance): |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 196 | instance.volumes = "/etc/dnsmasq.d,/etc/ufw" |
| 197 | instance.no_sync = True # prevent instance from being synced until we're done with it |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 198 | super(VSGServiceInstancePolicy, self).save_instance(instance) |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 199 | try: |
| 200 | if instance.isolation in ["container", "container_vm"]: |
| 201 | raise Exception("Not supported") |
| 202 | |
| 203 | if instance.isolation in ["vm"]: |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 204 | lan_network = self.get_lan_network(service_instance, instance) |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 205 | port = self.find_or_make_port(instance, lan_network) |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 206 | self.port_set_parameter(port, "c_tag", service_instance.volt.c_tag) |
| 207 | self.port_set_parameter(port, "s_tag", service_instance.volt.s_tag) |
| 208 | self.port_set_parameter(port, "neutron_port_name", "stag-%s" % service_instance.volt.s_tag) |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 209 | port.save() |
| 210 | |
| 211 | # tag the instance with the s-tag, so we can easily find the |
| 212 | # instance later |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 213 | if service_instance.volt and service_instance.volt.s_tag: |
| 214 | tags = Tag.objects.filter(name="s_tag", value=service_instance.volt.s_tag) |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 215 | if not tags: |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 216 | tag = Tag(service=service_instance.owner, content_type=instance.self_content_type_id, object_id=instance.id, name="s_tag", value=str(service_instance.volt.s_tag)) |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 217 | tag.save() |
| 218 | |
| 219 | # VTN-CORD needs a WAN address for the VM, so that the VM can |
| 220 | # be configured. |
| 221 | tags = Tag.objects.filter(content_type=instance.self_content_type_id, object_id=instance.id, name="vm_vrouter_tenant") |
| 222 | if not tags: |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 223 | address_service_instance = self.allocate_public_service_instance(address_pool_name="addresses_vsg", |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 224 | subscriber_service=service_instance.owner) |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 225 | address_service_instance.set_attribute("tenant_for_instance_id", instance.id) |
| 226 | address_service_instance.save() |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 227 | # TODO: potential partial failure |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 228 | tag = Tag(service=service_instance.owner, content_type=instance.self_content_type_id, object_id=instance.id, name="vm_vrouter_tenant", value="%d" % address_service_instance.id) |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 229 | tag.save() |
| 230 | |
| 231 | instance.no_sync = False # allow the synchronizer to run now |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 232 | super(VSGServiceInstancePolicy, self).save_instance(instance) |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 233 | except: |
| 234 | # need to clean up any failures here |
| 235 | raise |
| 236 | |
| 237 | |