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