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