blob: 33b88777685068ae77a4f0908ebbd308cd9ee865 [file] [log] [blame]
Matteo Scandoloaca86652017-08-08 13:05:27 -07001
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 Baker96746882017-06-09 14:12:15 -070017from synchronizers.new_base.modelaccessor import *
18from synchronizers.new_base.model_policies.model_policy_tenantwithcontainer import TenantWithContainerPolicy, LeastLoadedNodeScheduler
19from synchronizers.new_base.exceptions import *
20
21class VSGTenantPolicy(TenantWithContainerPolicy):
22 model_name = "VSGTenant"
23
24 def handle_create(self, tenant):
25 return self.handle_update(tenant)
26
27 def handle_update(self, tenant):
Scott Baker0bbd7f22017-09-05 17:16:40 -070028 if (tenant.link_deleted_count>0) and (not tenant.provided_links.exists()):
29 # if the last provided_link has just gone away, then self-destruct
30 self.logger.info("The last provided link has been deleted -- self-destructing.");
Scott Bakerad310b92017-09-12 11:23:07 -070031 # TODO: We shouldn't have to call handle_delete ourselves. The model policy framework should handle this
32 # for us, but it isn't. I think that's happening is that tenant.delete() isn't setting a new
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.
36 self.handle_delete(tenant)
37 # Note that if we deleted the Instance in handle_delete, then django may have cascade-deleted the tenant
38 # by now. Thus we have to guard our delete, to check that the tenant still exists.
39 if VSGTenant.objects.filter(id=tenant.id).exists():
40 tenant.delete()
41 else:
42 self.logger.info("Tenant %s is already deleted" % tenant)
Scott Baker0bbd7f22017-09-05 17:16:40 -070043 return
44
Scott Baker96746882017-06-09 14:12:15 -070045 self.manage_container(tenant)
Scott Bakeradd58dd2017-08-23 15:56:58 -070046 self.manage_address_service_instance(tenant)
Scott Baker96746882017-06-09 14:12:15 -070047 self.cleanup_orphans(tenant)
48
49 def handle_delete(self, tenant):
Scott Bakerad310b92017-09-12 11:23:07 -070050 if tenant.instance and (not tenant.instance.deleted):
51 all_tenants_this_instance = VSGTenant.objects.filter(instance_id=tenant.instance.id)
52 other_tenants_this_instance = [x for x in all_tenants_this_instance if x.id != tenant.id]
53 if (not other_tenants_this_instance):
54 self.logger.info("VSG Instance %s is now unused -- deleting" % tenant.instance)
55 self.delete_instance(tenant, tenant.instance)
56 else:
57 self.logger.info("VSG Instance %s has %d other service instances attached" % (tenant.instance, len(other_tenants_this_instance)))
Scott Baker96746882017-06-09 14:12:15 -070058
Scott Bakeradd58dd2017-08-23 15:56:58 -070059 def manage_address_service_instance(self, tenant):
Scott Baker96746882017-06-09 14:12:15 -070060 if tenant.deleted:
61 return
62
Scott Bakeradd58dd2017-08-23 15:56:58 -070063 if tenant.address_service_instance is None:
64 address_service_instance = self.allocate_public_service_instance(address_pool_name="addresses_vsg", subscriber_tenant=tenant)
65 address_service_instance.save()
Scott Baker96746882017-06-09 14:12:15 -070066
67 def cleanup_orphans(self, tenant):
Scott Bakeradd58dd2017-08-23 15:56:58 -070068 # ensure vSG only has one AddressManagerServiceInstance
69 cur_asi = tenant.address_service_instance
Scott Baker80238f82017-07-18 16:01:10 -070070 for link in tenant.subscribed_links.all():
71 # TODO: hardcoded dependency
Scott Bakeradd58dd2017-08-23 15:56:58 -070072 # 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 Baker96746882017-06-09 14:12:15 -070077
78 def get_vsg_service(self, tenant):
Scott Baker80238f82017-07-18 16:01:10 -070079 return VSGService.objects.get(id=tenant.owner.id)
Scott Baker96746882017-06-09 14:12:15 -070080
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
88 def find_or_make_instance_for_s_tag(self, tenant, s_tag):
89 instance = self.find_instance_for_s_tag(tenant.volt.s_tag)
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.
93 self.save_instance(tenant, instance)
94 return instance
95
96 desired_image = self.get_image(tenant)
97
98 flavors = Flavor.objects.filter(name="m1.small")
99 if not flavors:
100 raise SynchronizerConfigurationError("No m1.small flavor")
101
Scott Baker80238f82017-07-18 16:01:10 -0700102 slice = tenant.owner.slices.first()
Scott Baker96746882017-06-09 14:12:15 -0700103
104 (node, parent) = LeastLoadedNodeScheduler(slice, label=self.get_vsg_service(tenant).node_label).pick()
105
106 assert (slice is not None)
107 assert (node is not None)
108 assert (desired_image is not None)
109 assert (tenant.creator is not None)
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,
116 creator=tenant.creator,
117 deployment=node.site_deployment.deployment,
118 flavor=flavors[0],
119 isolation=slice.default_isolation,
120 parent=parent)
121
122 self.save_instance(tenant, instance)
123
124 return instance
125
126 def manage_container(self, tenant):
127 if tenant.deleted:
128 return
129
130 if not tenant.volt:
131 raise SynchronizerConfigurationError("This VSG container has no volt")
132
133 if tenant.instance:
134 # We're good.
135 return
136
137 instance = self.find_or_make_instance_for_s_tag(tenant, tenant.volt.s_tag)
138 tenant.instance = instance
139 # TODO: possible for partial failure here?
140 tenant.save()
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
151 def get_lan_network(self, tenant, instance):
Scott Baker80238f82017-07-18 16:01:10 -0700152 slice = tenant.owner.slices.all()[0]
Scott Baker96746882017-06-09 14:12:15 -0700153 # 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 Bakerad310b92017-09-12 11:23:07 -0700174 def delete_instance(self, tenant, instance):
175 # delete the `s_tag` tags
176 tags = Tag.objects.filter(service_id=tenant.owner.id, content_type=instance.self_content_type_id,
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 Baker96746882017-06-09 14:12:15 -0700195 def save_instance(self, tenant, instance):
196 instance.volumes = "/etc/dnsmasq.d,/etc/ufw"
197 instance.no_sync = True # prevent instance from being synced until we're done with it
198 super(VSGTenantPolicy, self).save_instance(instance)
199 try:
200 if instance.isolation in ["container", "container_vm"]:
201 raise Exception("Not supported")
202
203 if instance.isolation in ["vm"]:
204 lan_network = self.get_lan_network(tenant, instance)
205 port = self.find_or_make_port(instance, lan_network)
206 self.port_set_parameter(port, "c_tag", tenant.volt.c_tag)
207 self.port_set_parameter(port, "s_tag", tenant.volt.s_tag)
208 self.port_set_parameter(port, "neutron_port_name", "stag-%s" % tenant.volt.s_tag)
209 port.save()
210
211 # tag the instance with the s-tag, so we can easily find the
212 # instance later
213 if tenant.volt and tenant.volt.s_tag:
214 tags = Tag.objects.filter(name="s_tag", value=tenant.volt.s_tag)
215 if not tags:
Scott Baker80238f82017-07-18 16:01:10 -0700216 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))
Scott Baker96746882017-06-09 14:12:15 -0700217 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 Bakeradd58dd2017-08-23 15:56:58 -0700223 address_service_instance = self.allocate_public_service_instance(address_pool_name="addresses_vsg",
224 subscriber_service=tenant.owner)
225 address_service_instance.set_attribute("tenant_for_instance_id", instance.id)
226 address_service_instance.save()
Scott Baker96746882017-06-09 14:12:15 -0700227 # TODO: potential partial failure
Scott Bakeradd58dd2017-08-23 15:56:58 -0700228 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)
Scott Baker96746882017-06-09 14:12:15 -0700229 tag.save()
230
231 instance.no_sync = False # allow the synchronizer to run now
232 super(VSGTenantPolicy, self).save_instance(instance)
233 except:
234 # need to clean up any failures here
235 raise
236
237