blob: 3b58df4c6f03a829ed73330608b1e4efb46a2c04 [file] [log] [blame]
Sapan Bhatia46482ee2017-04-21 17:59:48 +02001sync_attributes = ("wan_container_ip", "wan_container_mac", "wan_container_netbits",
2 "wan_container_gateway_ip", "wan_container_gateway_mac",
3 "wan_vm_ip", "wan_vm_mac")
4
5def __init__(self, *args, **kwargs):
6 super(VSGTenant, self).__init__(*args, **kwargs)
7 self.cached_vrouter=None
8
9@property
10def vrouter(self):
11 vrouter = self.get_newest_subscribed_tenant(VRouterTenant)
12 if not vrouter:
13 return None
14
15 # always return the same object when possible
16 if (self.cached_vrouter) and (self.cached_vrouter.id == vrouter.id):
17 return self.cached_vrouter
18
19 vrouter.caller = self.creator
20 self.cached_vrouter = vrouter
21 return vrouter
22
23@vrouter.setter
24def vrouter(self, value):
25 raise XOSConfigurationError("VSGTenant.vrouter setter is not implemented")
26
27@property
28def volt(self):
29 from services.volt.models import VOLTTenant
30 if not self.subscriber_tenant:
31 return None
32 volts = VOLTTenant.objects.filter(id=self.subscriber_tenant.id)
33 if not volts:
34 return None
35 return volts[0]
36
37@volt.setter
38def volt(self, value):
39 raise XOSConfigurationError("VSGTenant.volt setter is not implemented")
40
41@property
42def ssh_command(self):
43 if self.instance:
44 return self.instance.get_ssh_command()
45 else:
46 return "no-instance"
47
48def get_vrouter_field(self, name, default=None):
49 if self.vrouter:
50 return getattr(self.vrouter, name, default)
51 else:
52 return default
53
54@property
55def wan_container_ip(self):
56 return self.get_vrouter_field("public_ip", None)
57
58@property
59def wan_container_mac(self):
60 return self.get_vrouter_field("public_mac", None)
61
62@property
63def wan_container_netbits(self):
64 return self.get_vrouter_field("netbits", None)
65
66@property
67def wan_container_gateway_ip(self):
68 return self.get_vrouter_field("gateway_ip", None)
69
70@property
71def wan_container_gateway_mac(self):
72 return self.get_vrouter_field("gateway_mac", None)
73
74@property
75def wan_vm_ip(self):
Scott Baker248ed752017-05-24 16:40:28 -070076 tags = Tag.objects.filter(content_type=self.instance.get_content_type_key(), object_id=self.instance.id, name="vm_vrouter_tenant")
Sapan Bhatia46482ee2017-04-21 17:59:48 +020077 if tags:
78 tenant = VRouterTenant.objects.get(id=tags[0].value)
79 return tenant.public_ip
80 else:
81 raise Exception("no vm_vrouter_tenant tag for instance %s" % o.instance)
82
83@property
84def wan_vm_mac(self):
Scott Baker248ed752017-05-24 16:40:28 -070085 tags = Tag.objects.filter(content_type=self.instance.get_content_type_key(), object_id=self.instance.id, name="vm_vrouter_tenant")
Sapan Bhatia46482ee2017-04-21 17:59:48 +020086 if tags:
87 tenant = VRouterTenant.objects.get(id=tags[0].value)
88 return tenant.public_mac
89 else:
90 raise Exception("no vm_vrouter_tenant tag for instance %s" % o.instance)
91
92@property
93def is_synced(self):
94 return (self.enacted is not None) and (self.enacted >= self.updated)
95
96@is_synced.setter
97def is_synced(self, value):
98 pass
99
100def get_vrouter_service(self):
Scott Baker248ed752017-05-24 16:40:28 -0700101 vrouterServices = VRouterService.objects.all()
Sapan Bhatia46482ee2017-04-21 17:59:48 +0200102 if not vrouterServices:
103 raise XOSConfigurationError("No VROUTER Services available")
104 return vrouterServices[0]
105
106def manage_vrouter(self):
107 # Each vCPE object owns exactly one vRouterTenant object
108
109 if self.deleted:
110 return
111
112 if self.vrouter is None:
113 vrouter = self.get_vrouter_service().get_tenant(address_pool_name="addresses_vsg", subscriber_tenant = self)
114 vrouter.caller = self.creator
115 vrouter.save()
116
117def cleanup_vrouter(self):
118 if self.vrouter:
119 # print "XXX cleanup vrouter", self.vrouter
120 self.vrouter.delete()
121
122def cleanup_orphans(self):
123 # ensure vCPE only has one vRouter
124 cur_vrouter = self.vrouter
125 for vrouter in list(self.get_subscribed_tenants(VRouterTenant)):
126 if (not cur_vrouter) or (vrouter.id != cur_vrouter.id):
127 # print "XXX clean up orphaned vrouter", vrouter
128 vrouter.delete()
129
130 if self.orig_instance_id and (self.orig_instance_id != self.get_attribute("instance_id")):
131 instances=Instance.objects.filter(id=self.orig_instance_id)
132 if instances:
133 # print "XXX clean up orphaned instance", instances[0]
134 instances[0].delete()
135
136def get_slice(self):
137 if not self.provider_service.slices.count():
138 print self, "dio porco"
139 raise XOSConfigurationError("The service has no slices")
140 slice = self.provider_service.slices.all()[0]
141 return slice
142
143def get_vsg_service(self):
Scott Baker248ed752017-05-24 16:40:28 -0700144 return VSGService.objects.get(id=self.provider_service.id)
Sapan Bhatia46482ee2017-04-21 17:59:48 +0200145
146def find_instance_for_s_tag(self, s_tag):
Sapan Bhatia46482ee2017-04-21 17:59:48 +0200147 tags = Tag.objects.filter(name="s_tag", value=s_tag)
148 if tags:
Scott Baker48f9ac12017-05-17 12:03:03 -0700149 return Tag.get_content_object(tags[0].content_type, tags[0].object_id)
Sapan Bhatia46482ee2017-04-21 17:59:48 +0200150
151 return None
152
153def find_or_make_instance_for_s_tag(self, s_tag):
154 instance = self.find_instance_for_s_tag(self.volt.s_tag)
155 if instance:
156 return instance
157
158 flavors = Flavor.objects.filter(name="m1.small")
159 if not flavors:
160 raise XOSConfigurationError("No m1.small flavor")
161
162 slice = self.provider_service.slices.all()[0]
163
164 if slice.default_isolation == "container_vm":
165 (node, parent) = ContainerVmScheduler(slice).pick()
166 else:
167 (node, parent) = LeastLoadedNodeScheduler(slice, label=self.get_vsg_service().node_label).pick()
168
169 instance = Instance(slice = slice,
170 node = node,
171 image = self.image,
172 creator = self.creator,
173 deployment = node.site_deployment.deployment,
174 flavor = flavors[0],
175 isolation = slice.default_isolation,
176 parent = parent)
177
178 self.save_instance(instance)
179
180 return instance
181
182def manage_container(self):
183 from core.models import Instance, Flavor
184
185 if self.deleted:
186 return
187
188 # For container or container_vm isolation, use what TenantWithCotnainer
189 # provides us
190 slice = self.get_slice()
191 if slice.default_isolation in ["container_vm", "container"]:
192 super(VSGTenant,self).manage_container()
193 return
194
195 if not self.volt:
196 raise XOSConfigurationError("This vCPE container has no volt")
197
198 if self.instance:
199 # We're good.
200 return
201
202 instance = self.find_or_make_instance_for_s_tag(self.volt.s_tag)
203 self.instance = instance
204 super(TenantWithContainer, self).save()
205
206def cleanup_container(self):
207 if self.get_slice().default_isolation in ["container_vm", "container"]:
208 super(VSGTenant,self).cleanup_container()
209
210 # To-do: cleanup unused instances
211 pass
212
213def find_or_make_port(self, instance, network, **kwargs):
214 port = Port.objects.filter(instance=instance, network=network)
215 if port:
216 port = port[0]
217 else:
218 port = Port(instance=instance, network=network, **kwargs)
219 port.save()
220 return port
221
222def get_lan_network(self, instance):
223 slice = self.provider_service.slices.all()[0]
224 # there should only be one network private network, and its template should not be the management template
225 lan_networks = [x for x in slice.networks.all() if x.template.visibility=="private" and (not "management" in x.template.name)]
226 if len(lan_networks)>1:
227 raise XOSProgrammingError("The vSG slice should only have one non-management private network")
228 if not lan_networks:
229 raise XOSProgrammingError("No lan_network")
230 return lan_networks[0]
231
Scott Baker248ed752017-05-24 16:40:28 -0700232def port_set_parameter(self, port, name, value):
233 instance_type = port.get_content_type_key()
234 existing_params = NetworkParameter.objects.filter(parameter__name=name, content_type=instance_type, object_id=port.id)
235 if existing_params:
236 p=existing_params[0]
237 p.value = value
238 p.save()
239 else:
240 pt = NetworkParameterType.objects.get(name=name)
241 p = NetworkParameter(parameter=pt, content_type=instance_type, object_id=port.id, value=value)
242 p.save()
243
Sapan Bhatia46482ee2017-04-21 17:59:48 +0200244def save_instance(self, instance):
245 with transaction.atomic():
246 instance.volumes = "/etc/dnsmasq.d,/etc/ufw"
247 super(VSGTenant, self).save_instance(instance)
248
249 if instance.isolation in ["container", "container_vm"]:
250 lan_network = self.get_lan_network(instance)
251 port = self.find_or_make_port(instance, lan_network, ip="192.168.0.1", port_id="unmanaged")
Scott Baker248ed752017-05-24 16:40:28 -0700252 self.port_set_parameter(port, "c_tag", self.volt.c_tag)
253 self.port_set_parameter(port, "s_tag", self.volt.s_tag)
254 self.port_set_parameter(port, "device", "eth1")
255 self.port_set_parameter(port, "bridge", "br-lan")
Sapan Bhatia46482ee2017-04-21 17:59:48 +0200256
257 wan_networks = [x for x in instance.slice.networks.all() if "wan" in x.name]
258 if not wan_networks:
259 raise XOSProgrammingError("No wan_network")
260 port = self.find_or_make_port(instance, wan_networks[0])
Scott Baker248ed752017-05-24 16:40:28 -0700261 self.port_set_parameter(port, "next_hop", value="10.0.1.253") # FIX ME
262 self.port_set_parameter(port, "device", "eth0")
Sapan Bhatia46482ee2017-04-21 17:59:48 +0200263
264 if instance.isolation in ["vm"]:
265 lan_network = self.get_lan_network(instance)
266 port = self.find_or_make_port(instance, lan_network)
Scott Baker248ed752017-05-24 16:40:28 -0700267 self.port_set_parameter(port, "c_tag", self.volt.c_tag)
268 self.port_set_parameter(port, "s_tag", self.volt.s_tag)
269 self.port_set_parameter(port, "neutron_port_name", "stag-%s" % self.volt.s_tag)
Sapan Bhatia46482ee2017-04-21 17:59:48 +0200270 port.save()
271
272 # tag the instance with the s-tag, so we can easily find the
273 # instance later
274 if self.volt and self.volt.s_tag:
275 tags = Tag.objects.filter(name="s_tag", value=self.volt.s_tag)
276 if not tags:
Scott Baker48f9ac12017-05-17 12:03:03 -0700277 tag = Tag(service=self.provider_service, content_type=instance.get_content_type_key(), object_id=instance.id, name="s_tag", value=self.volt.s_tag)
Sapan Bhatia46482ee2017-04-21 17:59:48 +0200278 tag.save()
279
280 # VTN-CORD needs a WAN address for the VM, so that the VM can
281 # be configured.
Scott Baker248ed752017-05-24 16:40:28 -0700282 tags = Tag.objects.filter(content_type=instance.get_content_type_key(), object_id=instance.id, name="vm_vrouter_tenant")
Sapan Bhatia46482ee2017-04-21 17:59:48 +0200283 if not tags:
284 vrouter = self.get_vrouter_service().get_tenant(address_pool_name="addresses_vsg", subscriber_service = self.provider_service)
285 vrouter.set_attribute("tenant_for_instance_id", instance.id)
286 vrouter.save()
Scott Baker48f9ac12017-05-17 12:03:03 -0700287 tag = Tag(service=self.provider_service, content_type=instance.get_content_type_key(), object_id=instance.id, name="vm_vrouter_tenant", value="%d" % vrouter.id)
Sapan Bhatia46482ee2017-04-21 17:59:48 +0200288 tag.save()
289
290def save(self, *args, **kwargs):
291 if not self.creator:
292 if not getattr(self, "caller", None):
293 # caller must be set when creating a vCPE since it creates a slice
294 raise XOSProgrammingError("VSGTenant's self.caller was not set")
295 self.creator = self.caller
296 if not self.creator:
297 raise XOSProgrammingError("VSGTenant's self.creator was not set")
298
299 super(VSGTenant, self).save(*args, **kwargs)
300 model_policy_vcpe(self.pk)
301
302def delete(self, *args, **kwargs):
303 self.cleanup_vrouter()
304 self.cleanup_container()
305 super(VSGTenant, self).delete(*args, **kwargs)
306