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