blob: 48f0527918837bd71bd4f9383dcbcb0520aa7875 [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
Matteo Scandoloe982ca92018-03-01 13:40:57 -080017from synchronizers.new_base.modelaccessor import VSGServiceInstance, AddressManagerServiceInstance, VSGService, Tag, Flavor, Instance, Port, NetworkParameterType, NetworkParameter, ServiceInstance, model_accessor
Scott Baker96746882017-06-09 14:12:15 -070018from synchronizers.new_base.model_policies.model_policy_tenantwithcontainer import TenantWithContainerPolicy, LeastLoadedNodeScheduler
19from synchronizers.new_base.exceptions import *
Scott Baker216d20c2018-04-23 16:18:34 -070020from xosapi.orm import ORMGenericObjectNotFoundException
Scott Baker96746882017-06-09 14:12:15 -070021
Scott Baker645c0c52017-09-15 10:38:32 -070022class VSGServiceInstancePolicy(TenantWithContainerPolicy):
23 model_name = "VSGServiceInstance"
Scott Baker96746882017-06-09 14:12:15 -070024
Scott Baker645c0c52017-09-15 10:38:32 -070025 def handle_create(self, service_instance):
26 return self.handle_update(service_instance)
Scott Baker96746882017-06-09 14:12:15 -070027
Scott Baker645c0c52017-09-15 10:38:32 -070028 def handle_update(self, service_instance):
29 if (service_instance.link_deleted_count>0) and (not service_instance.provided_links.exists()):
Scott Baker0bbd7f22017-09-05 17:16:40 -070030 # if the last provided_link has just gone away, then self-destruct
Scott Baker645c0c52017-09-15 10:38:32 -070031 self.logger.info("The last provided link has been deleted -- self-destructing.")
Scott Bakerad310b92017-09-12 11:23:07 -070032 # TODO: We shouldn't have to call handle_delete ourselves. The model policy framework should handle this
Scott Baker645c0c52017-09-15 10:38:32 -070033 # for us, but it isn't. I think that's happening is that serviceinstance.delete() isn't setting a new
Scott Bakerad310b92017-09-12 11:23:07 -070034 # updated timestamp, since there's no way to pass `always_update_timestamp`, and therefore the
35 # policy framework doesn't know that the object has changed and needs new policies. For now, the
36 # workaround is to just call handle_delete ourselves.
Scott Baker645c0c52017-09-15 10:38:32 -070037 self.handle_delete(service_instance)
38 # Note that if we deleted the Instance in handle_delete, then django may have cascade-deleted the service
39 # instance by now. Thus we have to guard our delete, to check that the service instance still exists.
40 if VSGServiceInstance.objects.filter(id=service_instance.id).exists():
41 service_instance.delete()
Scott Bakerad310b92017-09-12 11:23:07 -070042 else:
Scott Baker645c0c52017-09-15 10:38:32 -070043 self.logger.info("Tenant %s is already deleted" % service_instance)
Scott Baker0bbd7f22017-09-05 17:16:40 -070044 return
45
Scott Baker645c0c52017-09-15 10:38:32 -070046 self.manage_container(service_instance)
47 self.manage_address_service_instance(service_instance)
48 self.cleanup_orphans(service_instance)
Scott Baker96746882017-06-09 14:12:15 -070049
Scott Baker645c0c52017-09-15 10:38:32 -070050 def handle_delete(self, service_instance):
51 if service_instance.instance and (not service_instance.instance.deleted):
52 all_service_instances_this_instance = VSGServiceInstance.objects.filter(instance_id=service_instance.instance.id)
53 other_service_instances_this_instance = [x for x in all_service_instances_this_instance if x.id != service_instance.id]
54 if (not other_service_instances_this_instance):
55 self.logger.info("VSG Instance %s is now unused -- deleting" % service_instance.instance)
56 self.delete_instance(service_instance, service_instance.instance)
Scott Bakerad310b92017-09-12 11:23:07 -070057 else:
Scott Baker645c0c52017-09-15 10:38:32 -070058 self.logger.info("VSG Instance %s has %d other service instances attached" % (service_instance.instance, len(other_service_instances_this_instance)))
Scott Baker96746882017-06-09 14:12:15 -070059
Scott Baker645c0c52017-09-15 10:38:32 -070060 def manage_address_service_instance(self, service_instance):
61 if service_instance.deleted:
Scott Baker96746882017-06-09 14:12:15 -070062 return
63
Scott Baker645c0c52017-09-15 10:38:32 -070064 if service_instance.address_service_instance is None:
65 address_service_instance = self.allocate_public_service_instance(address_pool_name="addresses_vsg", subscriber_tenant=service_instance)
Scott Bakeradd58dd2017-08-23 15:56:58 -070066 address_service_instance.save()
Scott Baker96746882017-06-09 14:12:15 -070067
Scott Baker645c0c52017-09-15 10:38:32 -070068 def cleanup_orphans(self, service_instance):
Scott Bakeradd58dd2017-08-23 15:56:58 -070069 # ensure vSG only has one AddressManagerServiceInstance
Scott Baker645c0c52017-09-15 10:38:32 -070070 cur_asi = service_instance.address_service_instance
71 for link in service_instance.subscribed_links.all():
Scott Baker80238f82017-07-18 16:01:10 -070072 # TODO: hardcoded dependency
Scott Bakeradd58dd2017-08-23 15:56:58 -070073 # cast from ServiceInstance to AddressManagerServiceInstance
74 asis = AddressManagerServiceInstance.objects.filter(id = link.provider_service_instance.id)
75 for asi in asis:
76 if (not cur_asi) or (asi.id != cur_asi.id):
77 asi.delete()
Scott Baker96746882017-06-09 14:12:15 -070078
Scott Baker645c0c52017-09-15 10:38:32 -070079 def get_vsg_service(self, service_instance):
80 return VSGService.objects.get(id=service_instance.owner.id)
Scott Baker96746882017-06-09 14:12:15 -070081
82 def find_instance_for_s_tag(self, s_tag):
83 tags = Tag.objects.filter(name="s_tag", value=s_tag)
Scott Baker216d20c2018-04-23 16:18:34 -070084 for tag in tags:
85 try:
86 return tag.content_object
87 except ORMGenericObjectNotFoundException:
88 self.logger.warning("Dangling Instance reference for s-tag %s. Deleting Tag object." % s_tag)
89 tag.delete()
Scott Baker96746882017-06-09 14:12:15 -070090
91 return None
92
Scott Baker645c0c52017-09-15 10:38:32 -070093 def find_or_make_instance_for_s_tag(self, service_instance):
94 instance = self.find_instance_for_s_tag(service_instance.volt.s_tag)
Scott Baker96746882017-06-09 14:12:15 -070095 if instance:
96 if instance.no_sync:
97 # if no_sync is still set, then perhaps we failed while saving it and need to retry.
Scott Baker645c0c52017-09-15 10:38:32 -070098 self.save_instance(service_instance, instance)
Scott Baker96746882017-06-09 14:12:15 -070099 return instance
100
Scott Baker645c0c52017-09-15 10:38:32 -0700101 desired_image = self.get_image(service_instance)
Scott Baker96746882017-06-09 14:12:15 -0700102
103 flavors = Flavor.objects.filter(name="m1.small")
104 if not flavors:
105 raise SynchronizerConfigurationError("No m1.small flavor")
106
Scott Baker645c0c52017-09-15 10:38:32 -0700107 slice = service_instance.owner.slices.first()
Scott Baker96746882017-06-09 14:12:15 -0700108
Scott Baker645c0c52017-09-15 10:38:32 -0700109 (node, parent) = LeastLoadedNodeScheduler(slice, label=self.get_vsg_service(service_instance).node_label).pick()
Scott Baker96746882017-06-09 14:12:15 -0700110
111 assert (slice is not None)
112 assert (node is not None)
113 assert (desired_image is not None)
Scott Baker96746882017-06-09 14:12:15 -0700114 assert (node.site_deployment.deployment is not None)
115 assert (desired_image is not None)
116
Scott Bakera6fa6ce2018-03-20 20:55:31 -0700117 assert(service_instance.volt)
118 assert(service_instance.volt.subscriber)
119 assert(service_instance.volt.subscriber.creator)
120
Scott Baker96746882017-06-09 14:12:15 -0700121 instance = Instance(slice=slice,
122 node=node,
123 image=desired_image,
Scott Bakera6fa6ce2018-03-20 20:55:31 -0700124 creator=service_instance.volt.subscriber.creator,
Scott Baker96746882017-06-09 14:12:15 -0700125 deployment=node.site_deployment.deployment,
126 flavor=flavors[0],
127 isolation=slice.default_isolation,
128 parent=parent)
129
Scott Baker645c0c52017-09-15 10:38:32 -0700130 self.save_instance(service_instance, instance)
Scott Baker96746882017-06-09 14:12:15 -0700131
132 return instance
133
Scott Baker645c0c52017-09-15 10:38:32 -0700134 def manage_container(self, service_instance):
135 if service_instance.deleted:
Scott Baker96746882017-06-09 14:12:15 -0700136 return
137
Scott Baker645c0c52017-09-15 10:38:32 -0700138 if not service_instance.volt:
Scott Baker96746882017-06-09 14:12:15 -0700139 raise SynchronizerConfigurationError("This VSG container has no volt")
140
Scott Baker645c0c52017-09-15 10:38:32 -0700141 if service_instance.instance:
Scott Baker96746882017-06-09 14:12:15 -0700142 # We're good.
143 return
144
Scott Baker645c0c52017-09-15 10:38:32 -0700145 instance = self.find_or_make_instance_for_s_tag(service_instance)
146 service_instance.instance = instance
Scott Baker96746882017-06-09 14:12:15 -0700147 # TODO: possible for partial failure here?
Scott Baker645c0c52017-09-15 10:38:32 -0700148 service_instance.save()
Scott Baker96746882017-06-09 14:12:15 -0700149
150 def find_or_make_port(self, instance, network, **kwargs):
151 port = Port.objects.filter(instance_id=instance.id, network_id=network.id)
152 if port:
153 port = port[0]
154 else:
155 port = Port(instance=instance, network=network, **kwargs)
156 port.save()
157 return port
158
Scott Baker645c0c52017-09-15 10:38:32 -0700159 def get_lan_network(self, service_instance, instance):
160 slice = service_instance.owner.slices.all()[0]
Scott Baker96746882017-06-09 14:12:15 -0700161 # there should only be one network private network, and its template should not be the management template
162 lan_networks = [x for x in slice.networks.all() if
163 x.template.visibility == "private" and (not "management" in x.template.name)]
164 if len(lan_networks) > 1:
165 raise SynchronizerProgrammingError("The vSG slice should only have one non-management private network")
166 if not lan_networks:
167 raise SynchronizerProgrammingError("No lan_network")
168 return lan_networks[0]
169
170 def port_set_parameter(self, port, name, value):
171 pt = NetworkParameterType.objects.get(name=name)
172 existing_params = NetworkParameter.objects.filter(parameter_id=pt.id, content_type=port.self_content_type_id, object_id=port.id)
173
174 if existing_params:
175 p = existing_params[0]
176 p.value = str(value)
177 p.save()
178 else:
179 p = NetworkParameter(parameter=pt, content_type=port.self_content_type_id, object_id=port.id, value=str(value))
180 p.save()
181
Scott Baker645c0c52017-09-15 10:38:32 -0700182 def delete_instance(self, service_instance, instance):
Scott Bakerad310b92017-09-12 11:23:07 -0700183 # delete the `s_tag` tags
Scott Baker645c0c52017-09-15 10:38:32 -0700184 tags = Tag.objects.filter(service_id=service_instance.owner.id, content_type=instance.self_content_type_id,
Scott Bakerad310b92017-09-12 11:23:07 -0700185 object_id=instance.id, name="s_tag")
186 for tag in tags:
187 tag.delete()
188
189 tags = Tag.objects.filter(content_type=instance.self_content_type_id, object_id=instance.id,
190 name="vm_vrouter_tenant")
191 for tag in tags:
192 address_manager_instances = list(ServiceInstance.objects.filter(id=tag.value))
193 tag.delete()
194
195 # TODO: Potential partial failure
196
197 for address_manager_instance in address_manager_instances:
198 self.logger.info("Deleting address_manager_instance %s" % address_manager_instance)
199 address_manager_instance.delete()
200
201 instance.delete()
202
Scott Baker645c0c52017-09-15 10:38:32 -0700203 def save_instance(self, service_instance, instance):
Scott Baker96746882017-06-09 14:12:15 -0700204 instance.volumes = "/etc/dnsmasq.d,/etc/ufw"
205 instance.no_sync = True # prevent instance from being synced until we're done with it
Scott Baker645c0c52017-09-15 10:38:32 -0700206 super(VSGServiceInstancePolicy, self).save_instance(instance)
Scott Baker96746882017-06-09 14:12:15 -0700207 try:
208 if instance.isolation in ["container", "container_vm"]:
209 raise Exception("Not supported")
210
211 if instance.isolation in ["vm"]:
Scott Baker645c0c52017-09-15 10:38:32 -0700212 lan_network = self.get_lan_network(service_instance, instance)
Scott Baker96746882017-06-09 14:12:15 -0700213 port = self.find_or_make_port(instance, lan_network)
Scott Baker645c0c52017-09-15 10:38:32 -0700214 self.port_set_parameter(port, "c_tag", service_instance.volt.c_tag)
215 self.port_set_parameter(port, "s_tag", service_instance.volt.s_tag)
216 self.port_set_parameter(port, "neutron_port_name", "stag-%s" % service_instance.volt.s_tag)
Scott Baker96746882017-06-09 14:12:15 -0700217 port.save()
218
219 # tag the instance with the s-tag, so we can easily find the
220 # instance later
Scott Baker645c0c52017-09-15 10:38:32 -0700221 if service_instance.volt and service_instance.volt.s_tag:
222 tags = Tag.objects.filter(name="s_tag", value=service_instance.volt.s_tag)
Scott Baker96746882017-06-09 14:12:15 -0700223 if not tags:
Scott Baker645c0c52017-09-15 10:38:32 -0700224 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 Baker96746882017-06-09 14:12:15 -0700225 tag.save()
226
227 # VTN-CORD needs a WAN address for the VM, so that the VM can
228 # be configured.
229 tags = Tag.objects.filter(content_type=instance.self_content_type_id, object_id=instance.id, name="vm_vrouter_tenant")
230 if not tags:
Scott Bakeradd58dd2017-08-23 15:56:58 -0700231 address_service_instance = self.allocate_public_service_instance(address_pool_name="addresses_vsg",
Scott Baker645c0c52017-09-15 10:38:32 -0700232 subscriber_service=service_instance.owner)
Scott Bakeradd58dd2017-08-23 15:56:58 -0700233 address_service_instance.set_attribute("tenant_for_instance_id", instance.id)
234 address_service_instance.save()
Scott Baker96746882017-06-09 14:12:15 -0700235 # TODO: potential partial failure
Scott Baker645c0c52017-09-15 10:38:32 -0700236 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 Baker96746882017-06-09 14:12:15 -0700237 tag.save()
238
239 instance.no_sync = False # allow the synchronizer to run now
Scott Baker645c0c52017-09-15 10:38:32 -0700240 super(VSGServiceInstancePolicy, self).save_instance(instance)
Scott Baker96746882017-06-09 14:12:15 -0700241 except:
242 # need to clean up any failures here
243 raise
244
245