blob: 6800e8171286e8d6edf9daad4229aa73d3ab00ca [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 *
20
Scott Baker645c0c52017-09-15 10:38:32 -070021class VSGServiceInstancePolicy(TenantWithContainerPolicy):
22 model_name = "VSGServiceInstance"
Scott Baker96746882017-06-09 14:12:15 -070023
Scott Baker645c0c52017-09-15 10:38:32 -070024 def handle_create(self, service_instance):
25 return self.handle_update(service_instance)
Scott Baker96746882017-06-09 14:12:15 -070026
Scott Baker645c0c52017-09-15 10:38:32 -070027 def handle_update(self, service_instance):
28 if (service_instance.link_deleted_count>0) and (not service_instance.provided_links.exists()):
Scott Baker0bbd7f22017-09-05 17:16:40 -070029 # if the last provided_link has just gone away, then self-destruct
Scott Baker645c0c52017-09-15 10:38:32 -070030 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
Scott Baker645c0c52017-09-15 10:38:32 -070032 # 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 -070033 # 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 Baker645c0c52017-09-15 10:38:32 -070036 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 Bakerad310b92017-09-12 11:23:07 -070041 else:
Scott Baker645c0c52017-09-15 10:38:32 -070042 self.logger.info("Tenant %s is already deleted" % service_instance)
Scott Baker0bbd7f22017-09-05 17:16:40 -070043 return
44
Scott Baker645c0c52017-09-15 10:38:32 -070045 self.manage_container(service_instance)
46 self.manage_address_service_instance(service_instance)
47 self.cleanup_orphans(service_instance)
Scott Baker96746882017-06-09 14:12:15 -070048
Scott Baker645c0c52017-09-15 10:38:32 -070049 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 Bakerad310b92017-09-12 11:23:07 -070056 else:
Scott Baker645c0c52017-09-15 10:38:32 -070057 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 -070058
Scott Baker645c0c52017-09-15 10:38:32 -070059 def manage_address_service_instance(self, service_instance):
60 if service_instance.deleted:
Scott Baker96746882017-06-09 14:12:15 -070061 return
62
Scott Baker645c0c52017-09-15 10:38:32 -070063 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 Bakeradd58dd2017-08-23 15:56:58 -070065 address_service_instance.save()
Scott Baker96746882017-06-09 14:12:15 -070066
Scott Baker645c0c52017-09-15 10:38:32 -070067 def cleanup_orphans(self, service_instance):
Scott Bakeradd58dd2017-08-23 15:56:58 -070068 # ensure vSG only has one AddressManagerServiceInstance
Scott Baker645c0c52017-09-15 10:38:32 -070069 cur_asi = service_instance.address_service_instance
70 for link in service_instance.subscribed_links.all():
Scott Baker80238f82017-07-18 16:01:10 -070071 # 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
Scott Baker645c0c52017-09-15 10:38:32 -070078 def get_vsg_service(self, service_instance):
79 return VSGService.objects.get(id=service_instance.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
Scott Baker645c0c52017-09-15 10:38:32 -070088 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 Baker96746882017-06-09 14:12:15 -070090 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 Baker645c0c52017-09-15 10:38:32 -070093 self.save_instance(service_instance, instance)
Scott Baker96746882017-06-09 14:12:15 -070094 return instance
95
Scott Baker645c0c52017-09-15 10:38:32 -070096 desired_image = self.get_image(service_instance)
Scott Baker96746882017-06-09 14:12:15 -070097
98 flavors = Flavor.objects.filter(name="m1.small")
99 if not flavors:
100 raise SynchronizerConfigurationError("No m1.small flavor")
101
Scott Baker645c0c52017-09-15 10:38:32 -0700102 slice = service_instance.owner.slices.first()
Scott Baker96746882017-06-09 14:12:15 -0700103
Scott Baker645c0c52017-09-15 10:38:32 -0700104 (node, parent) = LeastLoadedNodeScheduler(slice, label=self.get_vsg_service(service_instance).node_label).pick()
Scott Baker96746882017-06-09 14:12:15 -0700105
106 assert (slice is not None)
107 assert (node is not None)
108 assert (desired_image is not None)
Scott Baker96746882017-06-09 14:12:15 -0700109 assert (node.site_deployment.deployment is not None)
110 assert (desired_image is not None)
111
Scott Bakera6fa6ce2018-03-20 20:55:31 -0700112 assert(service_instance.volt)
113 assert(service_instance.volt.subscriber)
114 assert(service_instance.volt.subscriber.creator)
115
Scott Baker96746882017-06-09 14:12:15 -0700116 instance = Instance(slice=slice,
117 node=node,
118 image=desired_image,
Scott Bakera6fa6ce2018-03-20 20:55:31 -0700119 creator=service_instance.volt.subscriber.creator,
Scott Baker96746882017-06-09 14:12:15 -0700120 deployment=node.site_deployment.deployment,
121 flavor=flavors[0],
122 isolation=slice.default_isolation,
123 parent=parent)
124
Scott Baker645c0c52017-09-15 10:38:32 -0700125 self.save_instance(service_instance, instance)
Scott Baker96746882017-06-09 14:12:15 -0700126
127 return instance
128
Scott Baker645c0c52017-09-15 10:38:32 -0700129 def manage_container(self, service_instance):
130 if service_instance.deleted:
Scott Baker96746882017-06-09 14:12:15 -0700131 return
132
Scott Baker645c0c52017-09-15 10:38:32 -0700133 if not service_instance.volt:
Scott Baker96746882017-06-09 14:12:15 -0700134 raise SynchronizerConfigurationError("This VSG container has no volt")
135
Scott Baker645c0c52017-09-15 10:38:32 -0700136 if service_instance.instance:
Scott Baker96746882017-06-09 14:12:15 -0700137 # We're good.
138 return
139
Scott Baker645c0c52017-09-15 10:38:32 -0700140 instance = self.find_or_make_instance_for_s_tag(service_instance)
141 service_instance.instance = instance
Scott Baker96746882017-06-09 14:12:15 -0700142 # TODO: possible for partial failure here?
Scott Baker645c0c52017-09-15 10:38:32 -0700143 service_instance.save()
Scott Baker96746882017-06-09 14:12:15 -0700144
145 def find_or_make_port(self, instance, network, **kwargs):
146 port = Port.objects.filter(instance_id=instance.id, network_id=network.id)
147 if port:
148 port = port[0]
149 else:
150 port = Port(instance=instance, network=network, **kwargs)
151 port.save()
152 return port
153
Scott Baker645c0c52017-09-15 10:38:32 -0700154 def get_lan_network(self, service_instance, instance):
155 slice = service_instance.owner.slices.all()[0]
Scott Baker96746882017-06-09 14:12:15 -0700156 # there should only be one network private network, and its template should not be the management template
157 lan_networks = [x for x in slice.networks.all() if
158 x.template.visibility == "private" and (not "management" in x.template.name)]
159 if len(lan_networks) > 1:
160 raise SynchronizerProgrammingError("The vSG slice should only have one non-management private network")
161 if not lan_networks:
162 raise SynchronizerProgrammingError("No lan_network")
163 return lan_networks[0]
164
165 def port_set_parameter(self, port, name, value):
166 pt = NetworkParameterType.objects.get(name=name)
167 existing_params = NetworkParameter.objects.filter(parameter_id=pt.id, content_type=port.self_content_type_id, object_id=port.id)
168
169 if existing_params:
170 p = existing_params[0]
171 p.value = str(value)
172 p.save()
173 else:
174 p = NetworkParameter(parameter=pt, content_type=port.self_content_type_id, object_id=port.id, value=str(value))
175 p.save()
176
Scott Baker645c0c52017-09-15 10:38:32 -0700177 def delete_instance(self, service_instance, instance):
Scott Bakerad310b92017-09-12 11:23:07 -0700178 # delete the `s_tag` tags
Scott Baker645c0c52017-09-15 10:38:32 -0700179 tags = Tag.objects.filter(service_id=service_instance.owner.id, content_type=instance.self_content_type_id,
Scott Bakerad310b92017-09-12 11:23:07 -0700180 object_id=instance.id, name="s_tag")
181 for tag in tags:
182 tag.delete()
183
184 tags = Tag.objects.filter(content_type=instance.self_content_type_id, object_id=instance.id,
185 name="vm_vrouter_tenant")
186 for tag in tags:
187 address_manager_instances = list(ServiceInstance.objects.filter(id=tag.value))
188 tag.delete()
189
190 # TODO: Potential partial failure
191
192 for address_manager_instance in address_manager_instances:
193 self.logger.info("Deleting address_manager_instance %s" % address_manager_instance)
194 address_manager_instance.delete()
195
196 instance.delete()
197
Scott Baker645c0c52017-09-15 10:38:32 -0700198 def save_instance(self, service_instance, instance):
Scott Baker96746882017-06-09 14:12:15 -0700199 instance.volumes = "/etc/dnsmasq.d,/etc/ufw"
200 instance.no_sync = True # prevent instance from being synced until we're done with it
Scott Baker645c0c52017-09-15 10:38:32 -0700201 super(VSGServiceInstancePolicy, self).save_instance(instance)
Scott Baker96746882017-06-09 14:12:15 -0700202 try:
203 if instance.isolation in ["container", "container_vm"]:
204 raise Exception("Not supported")
205
206 if instance.isolation in ["vm"]:
Scott Baker645c0c52017-09-15 10:38:32 -0700207 lan_network = self.get_lan_network(service_instance, instance)
Scott Baker96746882017-06-09 14:12:15 -0700208 port = self.find_or_make_port(instance, lan_network)
Scott Baker645c0c52017-09-15 10:38:32 -0700209 self.port_set_parameter(port, "c_tag", service_instance.volt.c_tag)
210 self.port_set_parameter(port, "s_tag", service_instance.volt.s_tag)
211 self.port_set_parameter(port, "neutron_port_name", "stag-%s" % service_instance.volt.s_tag)
Scott Baker96746882017-06-09 14:12:15 -0700212 port.save()
213
214 # tag the instance with the s-tag, so we can easily find the
215 # instance later
Scott Baker645c0c52017-09-15 10:38:32 -0700216 if service_instance.volt and service_instance.volt.s_tag:
217 tags = Tag.objects.filter(name="s_tag", value=service_instance.volt.s_tag)
Scott Baker96746882017-06-09 14:12:15 -0700218 if not tags:
Scott Baker645c0c52017-09-15 10:38:32 -0700219 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 -0700220 tag.save()
221
222 # VTN-CORD needs a WAN address for the VM, so that the VM can
223 # be configured.
224 tags = Tag.objects.filter(content_type=instance.self_content_type_id, object_id=instance.id, name="vm_vrouter_tenant")
225 if not tags:
Scott Bakeradd58dd2017-08-23 15:56:58 -0700226 address_service_instance = self.allocate_public_service_instance(address_pool_name="addresses_vsg",
Scott Baker645c0c52017-09-15 10:38:32 -0700227 subscriber_service=service_instance.owner)
Scott Bakeradd58dd2017-08-23 15:56:58 -0700228 address_service_instance.set_attribute("tenant_for_instance_id", instance.id)
229 address_service_instance.save()
Scott Baker96746882017-06-09 14:12:15 -0700230 # TODO: potential partial failure
Scott Baker645c0c52017-09-15 10:38:32 -0700231 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 -0700232 tag.save()
233
234 instance.no_sync = False # allow the synchronizer to run now
Scott Baker645c0c52017-09-15 10:38:32 -0700235 super(VSGServiceInstancePolicy, self).save_instance(instance)
Scott Baker96746882017-06-09 14:12:15 -0700236 except:
237 # need to clean up any failures here
238 raise
239
240