blob: e5a0562c99ba145a71033242f0171bdc0f0e3b2c [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
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 Baker645c0c52017-09-15 10:38:32 -0700109 assert (service_instance.creator is not None)
Scott Baker96746882017-06-09 14:12:15 -0700110 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,
Scott Baker645c0c52017-09-15 10:38:32 -0700116 creator=service_instance.creator,
Scott Baker96746882017-06-09 14:12:15 -0700117 deployment=node.site_deployment.deployment,
118 flavor=flavors[0],
119 isolation=slice.default_isolation,
120 parent=parent)
121
Scott Baker645c0c52017-09-15 10:38:32 -0700122 self.save_instance(service_instance, instance)
Scott Baker96746882017-06-09 14:12:15 -0700123
124 return instance
125
Scott Baker645c0c52017-09-15 10:38:32 -0700126 def manage_container(self, service_instance):
127 if service_instance.deleted:
Scott Baker96746882017-06-09 14:12:15 -0700128 return
129
Scott Baker645c0c52017-09-15 10:38:32 -0700130 if not service_instance.volt:
Scott Baker96746882017-06-09 14:12:15 -0700131 raise SynchronizerConfigurationError("This VSG container has no volt")
132
Scott Baker645c0c52017-09-15 10:38:32 -0700133 if service_instance.instance:
Scott Baker96746882017-06-09 14:12:15 -0700134 # We're good.
135 return
136
Scott Baker645c0c52017-09-15 10:38:32 -0700137 instance = self.find_or_make_instance_for_s_tag(service_instance)
138 service_instance.instance = instance
Scott Baker96746882017-06-09 14:12:15 -0700139 # TODO: possible for partial failure here?
Scott Baker645c0c52017-09-15 10:38:32 -0700140 service_instance.save()
Scott Baker96746882017-06-09 14:12:15 -0700141
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
Scott Baker645c0c52017-09-15 10:38:32 -0700151 def get_lan_network(self, service_instance, instance):
152 slice = service_instance.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 Baker645c0c52017-09-15 10:38:32 -0700174 def delete_instance(self, service_instance, instance):
Scott Bakerad310b92017-09-12 11:23:07 -0700175 # delete the `s_tag` tags
Scott Baker645c0c52017-09-15 10:38:32 -0700176 tags = Tag.objects.filter(service_id=service_instance.owner.id, content_type=instance.self_content_type_id,
Scott Bakerad310b92017-09-12 11:23:07 -0700177 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 Baker645c0c52017-09-15 10:38:32 -0700195 def save_instance(self, service_instance, instance):
Scott Baker96746882017-06-09 14:12:15 -0700196 instance.volumes = "/etc/dnsmasq.d,/etc/ufw"
197 instance.no_sync = True # prevent instance from being synced until we're done with it
Scott Baker645c0c52017-09-15 10:38:32 -0700198 super(VSGServiceInstancePolicy, self).save_instance(instance)
Scott Baker96746882017-06-09 14:12:15 -0700199 try:
200 if instance.isolation in ["container", "container_vm"]:
201 raise Exception("Not supported")
202
203 if instance.isolation in ["vm"]:
Scott Baker645c0c52017-09-15 10:38:32 -0700204 lan_network = self.get_lan_network(service_instance, instance)
Scott Baker96746882017-06-09 14:12:15 -0700205 port = self.find_or_make_port(instance, lan_network)
Scott Baker645c0c52017-09-15 10:38:32 -0700206 self.port_set_parameter(port, "c_tag", service_instance.volt.c_tag)
207 self.port_set_parameter(port, "s_tag", service_instance.volt.s_tag)
208 self.port_set_parameter(port, "neutron_port_name", "stag-%s" % service_instance.volt.s_tag)
Scott Baker96746882017-06-09 14:12:15 -0700209 port.save()
210
211 # tag the instance with the s-tag, so we can easily find the
212 # instance later
Scott Baker645c0c52017-09-15 10:38:32 -0700213 if service_instance.volt and service_instance.volt.s_tag:
214 tags = Tag.objects.filter(name="s_tag", value=service_instance.volt.s_tag)
Scott Baker96746882017-06-09 14:12:15 -0700215 if not tags:
Scott Baker645c0c52017-09-15 10:38:32 -0700216 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 -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",
Scott Baker645c0c52017-09-15 10:38:32 -0700224 subscriber_service=service_instance.owner)
Scott Bakeradd58dd2017-08-23 15:56:58 -0700225 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 Baker645c0c52017-09-15 10:38:32 -0700228 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 -0700229 tag.save()
230
231 instance.no_sync = False # allow the synchronizer to run now
Scott Baker645c0c52017-09-15 10:38:32 -0700232 super(VSGServiceInstancePolicy, self).save_instance(instance)
Scott Baker96746882017-06-09 14:12:15 -0700233 except:
234 # need to clean up any failures here
235 raise
236
237