blob: b65defc3b86b0e599363be202b79fd37637ddc26 [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.");
31 tenant.delete()
32 return
33
Scott Baker96746882017-06-09 14:12:15 -070034 self.manage_container(tenant)
Scott Bakeradd58dd2017-08-23 15:56:58 -070035 self.manage_address_service_instance(tenant)
Scott Baker96746882017-06-09 14:12:15 -070036 self.cleanup_orphans(tenant)
37
38 def handle_delete(self, tenant):
Scott Bakeradd58dd2017-08-23 15:56:58 -070039 if tenant.address_service_instance:
40 tenant.address_service_instance.delete()
Scott Baker96746882017-06-09 14:12:15 -070041
Scott Bakeradd58dd2017-08-23 15:56:58 -070042 def manage_address_service_instance(self, tenant):
Scott Baker96746882017-06-09 14:12:15 -070043 if tenant.deleted:
44 return
45
Scott Bakeradd58dd2017-08-23 15:56:58 -070046 if tenant.address_service_instance is None:
47 address_service_instance = self.allocate_public_service_instance(address_pool_name="addresses_vsg", subscriber_tenant=tenant)
48 address_service_instance.save()
Scott Baker96746882017-06-09 14:12:15 -070049
50 def cleanup_orphans(self, tenant):
Scott Bakeradd58dd2017-08-23 15:56:58 -070051 # ensure vSG only has one AddressManagerServiceInstance
52 cur_asi = tenant.address_service_instance
Scott Baker80238f82017-07-18 16:01:10 -070053 for link in tenant.subscribed_links.all():
54 # TODO: hardcoded dependency
Scott Bakeradd58dd2017-08-23 15:56:58 -070055 # cast from ServiceInstance to AddressManagerServiceInstance
56 asis = AddressManagerServiceInstance.objects.filter(id = link.provider_service_instance.id)
57 for asi in asis:
58 if (not cur_asi) or (asi.id != cur_asi.id):
59 asi.delete()
Scott Baker96746882017-06-09 14:12:15 -070060
61 def get_vsg_service(self, tenant):
Scott Baker80238f82017-07-18 16:01:10 -070062 return VSGService.objects.get(id=tenant.owner.id)
Scott Baker96746882017-06-09 14:12:15 -070063
64 def find_instance_for_s_tag(self, s_tag):
65 tags = Tag.objects.filter(name="s_tag", value=s_tag)
66 if tags:
67 return tags[0].content_object
68
69 return None
70
71 def find_or_make_instance_for_s_tag(self, tenant, s_tag):
72 instance = self.find_instance_for_s_tag(tenant.volt.s_tag)
73 if instance:
74 if instance.no_sync:
75 # if no_sync is still set, then perhaps we failed while saving it and need to retry.
76 self.save_instance(tenant, instance)
77 return instance
78
79 desired_image = self.get_image(tenant)
80
81 flavors = Flavor.objects.filter(name="m1.small")
82 if not flavors:
83 raise SynchronizerConfigurationError("No m1.small flavor")
84
Scott Baker80238f82017-07-18 16:01:10 -070085 slice = tenant.owner.slices.first()
Scott Baker96746882017-06-09 14:12:15 -070086
87 (node, parent) = LeastLoadedNodeScheduler(slice, label=self.get_vsg_service(tenant).node_label).pick()
88
89 assert (slice is not None)
90 assert (node is not None)
91 assert (desired_image is not None)
92 assert (tenant.creator is not None)
93 assert (node.site_deployment.deployment is not None)
94 assert (desired_image is not None)
95
96 instance = Instance(slice=slice,
97 node=node,
98 image=desired_image,
99 creator=tenant.creator,
100 deployment=node.site_deployment.deployment,
101 flavor=flavors[0],
102 isolation=slice.default_isolation,
103 parent=parent)
104
105 self.save_instance(tenant, instance)
106
107 return instance
108
109 def manage_container(self, tenant):
110 if tenant.deleted:
111 return
112
113 if not tenant.volt:
114 raise SynchronizerConfigurationError("This VSG container has no volt")
115
116 if tenant.instance:
117 # We're good.
118 return
119
120 instance = self.find_or_make_instance_for_s_tag(tenant, tenant.volt.s_tag)
121 tenant.instance = instance
122 # TODO: possible for partial failure here?
123 tenant.save()
124
125 def find_or_make_port(self, instance, network, **kwargs):
126 port = Port.objects.filter(instance_id=instance.id, network_id=network.id)
127 if port:
128 port = port[0]
129 else:
130 port = Port(instance=instance, network=network, **kwargs)
131 port.save()
132 return port
133
134 def get_lan_network(self, tenant, instance):
Scott Baker80238f82017-07-18 16:01:10 -0700135 slice = tenant.owner.slices.all()[0]
Scott Baker96746882017-06-09 14:12:15 -0700136 # there should only be one network private network, and its template should not be the management template
137 lan_networks = [x for x in slice.networks.all() if
138 x.template.visibility == "private" and (not "management" in x.template.name)]
139 if len(lan_networks) > 1:
140 raise SynchronizerProgrammingError("The vSG slice should only have one non-management private network")
141 if not lan_networks:
142 raise SynchronizerProgrammingError("No lan_network")
143 return lan_networks[0]
144
145 def port_set_parameter(self, port, name, value):
146 pt = NetworkParameterType.objects.get(name=name)
147 existing_params = NetworkParameter.objects.filter(parameter_id=pt.id, content_type=port.self_content_type_id, object_id=port.id)
148
149 if existing_params:
150 p = existing_params[0]
151 p.value = str(value)
152 p.save()
153 else:
154 p = NetworkParameter(parameter=pt, content_type=port.self_content_type_id, object_id=port.id, value=str(value))
155 p.save()
156
157 def save_instance(self, tenant, instance):
158 instance.volumes = "/etc/dnsmasq.d,/etc/ufw"
159 instance.no_sync = True # prevent instance from being synced until we're done with it
160 super(VSGTenantPolicy, self).save_instance(instance)
161 try:
162 if instance.isolation in ["container", "container_vm"]:
163 raise Exception("Not supported")
164
165 if instance.isolation in ["vm"]:
166 lan_network = self.get_lan_network(tenant, instance)
167 port = self.find_or_make_port(instance, lan_network)
168 self.port_set_parameter(port, "c_tag", tenant.volt.c_tag)
169 self.port_set_parameter(port, "s_tag", tenant.volt.s_tag)
170 self.port_set_parameter(port, "neutron_port_name", "stag-%s" % tenant.volt.s_tag)
171 port.save()
172
173 # tag the instance with the s-tag, so we can easily find the
174 # instance later
175 if tenant.volt and tenant.volt.s_tag:
176 tags = Tag.objects.filter(name="s_tag", value=tenant.volt.s_tag)
177 if not tags:
Scott Baker80238f82017-07-18 16:01:10 -0700178 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 -0700179 tag.save()
180
181 # VTN-CORD needs a WAN address for the VM, so that the VM can
182 # be configured.
183 tags = Tag.objects.filter(content_type=instance.self_content_type_id, object_id=instance.id, name="vm_vrouter_tenant")
184 if not tags:
Scott Bakeradd58dd2017-08-23 15:56:58 -0700185 address_service_instance = self.allocate_public_service_instance(address_pool_name="addresses_vsg",
186 subscriber_service=tenant.owner)
187 address_service_instance.set_attribute("tenant_for_instance_id", instance.id)
188 address_service_instance.save()
Scott Baker96746882017-06-09 14:12:15 -0700189 # TODO: potential partial failure
Scott Bakeradd58dd2017-08-23 15:56:58 -0700190 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 -0700191 tag.save()
192
193 instance.no_sync = False # allow the synchronizer to run now
194 super(VSGTenantPolicy, self).save_instance(instance)
195 except:
196 # need to clean up any failures here
197 raise
198
199