Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [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 | class VEGTenantPolicy(TenantWithContainerPolicy): |
| 20 | model_name = "VEGTenant" |
| 21 | |
| 22 | def handle_create(self, tenant): |
| 23 | return self.handle_update(tenant) |
| 24 | |
| 25 | def handle_update(self, tenant): |
| 26 | self.manage_container(tenant) |
| 27 | self.manage_address_service_instance(tenant) |
| 28 | self.cleanup_orphans(tenant) |
| 29 | |
| 30 | def handle_delete(self, tenant): |
| 31 | if tenant.address_service_instance: |
| 32 | tenant.address_service_instance.delete() |
| 33 | |
| 34 | def manage_address_service_instance(self, tenant): |
| 35 | if tenant.deleted: |
| 36 | return |
| 37 | |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 38 | if tenant.vrouter is None: |
| 39 | vrouter = self.allocate_public_service_instance(address_pool_name="addresses_veg", subscriber_tenant=tenant) |
| 40 | vrouter.save() |
| 41 | |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 42 | def cleanup_orphans(self, tenant): |
| 43 | # ensure vEG only has one AddressManagerServiceInstance |
| 44 | cur_asi = tenant.address_service_instance |
| 45 | for link in tenant.subscribed_links.all(): |
| 46 | # TODO: hardcoded dependency |
| 47 | # cast from ServiceInstance to AddressManagerServiceInstance |
| 48 | asis = AddressManagerServiceInstance.objects.filter(id = link.provider_service_instance.id) |
| 49 | for asi in asis: |
| 50 | if (not cur_asi) or (asi.id != cur_asi.id): |
| 51 | asi.delete() |
| 52 | |
| 53 | def get_veg_service(self, tenant): |
| 54 | return VEGService.objects.get(id=tenant.owner.id) |
| 55 | |
| 56 | def find_instance_for_s_tag(self, s_tag): |
| 57 | tags = Tag.objects.filter(name="s_tag", value=s_tag) |
| 58 | if tags: |
| 59 | return tags[0].content_object |
| 60 | |
| 61 | return None |
| 62 | |
| 63 | def find_or_make_instance_for_s_tag(self, tenant, s_tag): |
| 64 | instance = self.find_instance_for_s_tag(tenant.volt.s_tag) |
| 65 | if instance: |
| 66 | if instance.no_sync: |
| 67 | # if no_sync is still set, then perhaps we failed while saving it and need to retry. |
| 68 | self.save_instance(tenant, instance) |
| 69 | return instance |
| 70 | |
| 71 | desired_image = self.get_image(tenant) |
| 72 | |
| 73 | flavors = Flavor.objects.filter(name="m1.small") |
| 74 | if not flavors: |
| 75 | raise SynchronizerConfigurationError("No m1.small flavor") |
| 76 | |
| 77 | slice = tenant.owner.slices.first() |
| 78 | |
| 79 | (node, parent) = LeastLoadedNodeScheduler(slice, label=self.get_veg_service(tenant).node_label).pick() |
| 80 | |
| 81 | assert (slice is not None) |
| 82 | assert (node is not None) |
| 83 | assert (desired_image is not None) |
| 84 | assert (tenant.creator is not None) |
| 85 | assert (node.site_deployment.deployment is not None) |
| 86 | assert (desired_image is not None) |
| 87 | |
| 88 | instance = Instance(slice=slice, |
| 89 | node=node, |
| 90 | image=desired_image, |
| 91 | creator=tenant.creator, |
| 92 | deployment=node.site_deployment.deployment, |
| 93 | flavor=flavors[0], |
| 94 | isolation=slice.default_isolation, |
| 95 | parent=parent) |
| 96 | |
| 97 | self.save_instance(tenant, instance) |
| 98 | |
| 99 | return instance |
| 100 | |
| 101 | def manage_container(self, tenant): |
| 102 | if tenant.deleted: |
| 103 | return |
| 104 | |
| 105 | if not tenant.volt: |
| 106 | raise SynchronizerConfigurationError("This VEG container has no volt") |
| 107 | |
| 108 | if tenant.instance: |
| 109 | # We're good. |
| 110 | return |
| 111 | |
| 112 | instance = self.find_or_make_instance_for_s_tag(tenant, tenant.volt.s_tag) |
| 113 | tenant.instance = instance |
| 114 | # TODO: possible for partial failure here? |
| 115 | tenant.save() |
| 116 | |
| 117 | def find_or_make_port(self, instance, network, **kwargs): |
| 118 | port = Port.objects.filter(instance_id=instance.id, network_id=network.id) |
| 119 | if port: |
| 120 | port = port[0] |
| 121 | else: |
| 122 | port = Port(instance=instance, network=network, **kwargs) |
| 123 | port.save() |
| 124 | return port |
| 125 | |
| 126 | def get_lan_network(self, tenant, instance): |
| 127 | slice = tenant.owner.slices.all()[0] |
| 128 | # there should only be one network private network, and its template should not be the management template |
| 129 | lan_networks = [x for x in slice.networks.all() if |
| 130 | x.template.visibility == "private" and (not "management" in x.template.name)] |
| 131 | if len(lan_networks) > 1: |
| 132 | raise SynchronizerProgrammingError("The vEG slice should only have one non-management private network") |
| 133 | if not lan_networks: |
| 134 | raise SynchronizerProgrammingError("No lan_network") |
| 135 | return lan_networks[0] |
| 136 | |
| 137 | def port_set_parameter(self, port, name, value): |
| 138 | pt = NetworkParameterType.objects.get(name=name) |
| 139 | existing_params = NetworkParameter.objects.filter(parameter_id=pt.id, content_type=port.self_content_type_id, object_id=port.id) |
| 140 | |
| 141 | if existing_params: |
| 142 | p = existing_params[0] |
| 143 | p.value = str(value) |
| 144 | p.save() |
| 145 | else: |
| 146 | p = NetworkParameter(parameter=pt, content_type=port.self_content_type_id, object_id=port.id, value=str(value)) |
| 147 | p.save() |
| 148 | |
| 149 | def save_instance(self, tenant, instance): |
| 150 | instance.volumes = "/etc/dnsmasq.d,/etc/ufw" |
| 151 | instance.no_sync = True # prevent instance from being synced until we're done with it |
| 152 | super(VEGTenantPolicy, self).save_instance(instance) |
| 153 | try: |
| 154 | if instance.isolation in ["container", "container_vm"]: |
| 155 | raise Exception("Not supported") |
| 156 | |
| 157 | if instance.isolation in ["vm"]: |
| 158 | lan_network = self.get_lan_network(tenant, instance) |
| 159 | port = self.find_or_make_port(instance, lan_network) |
| 160 | self.port_set_parameter(port, "c_tag", tenant.volt.c_tag) |
| 161 | self.port_set_parameter(port, "s_tag", tenant.volt.s_tag) |
| 162 | self.port_set_parameter(port, "neutron_port_name", "stag-%s" % tenant.volt.s_tag) |
| 163 | port.save() |
| 164 | |
| 165 | # tag the instance with the s-tag, so we can easily find the |
| 166 | # instance later |
| 167 | if tenant.volt and tenant.volt.s_tag: |
| 168 | tags = Tag.objects.filter(name="s_tag", value=tenant.volt.s_tag) |
| 169 | if not tags: |
| 170 | tag = Tag(service=tenant.owner, content_type=instance.self_content_type_id, object_id=instance.id, name="s_tag", value=str(tenant.volt.s_tag)) |
| 171 | tag.save() |
| 172 | |
| 173 | # VTN-CORD needs a WAN address for the VM, so that the VM can |
| 174 | # be configured. |
| 175 | tags = Tag.objects.filter(content_type=instance.self_content_type_id, object_id=instance.id, name="vm_vrouter_tenant") |
| 176 | if not tags: |
| 177 | address_service_instance = self.allocate_public_service_instance(address_pool_name="addresses_veg", |
| 178 | subscriber_service=tenant.owner) |
| 179 | address_service_instance.set_attribute("tenant_for_instance_id", instance.id) |
| 180 | address_service_instance.save() |
| 181 | # TODO: potential partial failure |
| 182 | tag = Tag(service=tenant.owner, content_type=instance.self_content_type_id, object_id=instance.id, name="vm_vrouter_tenant", value="%d" % address_service_instance.id) |
| 183 | tag.save() |
| 184 | |
| 185 | instance.no_sync = False # allow the synchronizer to run now |
| 186 | super(VEGTenantPolicy, self).save_instance(instance) |
| 187 | except: |
| 188 | # need to clean up any failures here |
| 189 | raise |
| 190 | |
| 191 | |