blob: 68b9adc73eb2f2390b5ede0690276bdd79a79f32 [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):
76 tags = Tag.select_by_content_object(self.instance).filter(name="vm_vrouter_tenant")
77 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):
85 tags = Tag.select_by_content_object(self.instance).filter(name="vm_vrouter_tenant")
86 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):
101 vrouterServices = VRouterService.get_service_objects().all()
102 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):
144 return VSGService.get_service_objects().get(id=self.provider_service.id)
145
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
232def save_instance(self, instance):
233 with transaction.atomic():
234 instance.volumes = "/etc/dnsmasq.d,/etc/ufw"
235 super(VSGTenant, self).save_instance(instance)
236
237 if instance.isolation in ["container", "container_vm"]:
238 lan_network = self.get_lan_network(instance)
239 port = self.find_or_make_port(instance, lan_network, ip="192.168.0.1", port_id="unmanaged")
240 port.set_parameter("c_tag", self.volt.c_tag)
241 port.set_parameter("s_tag", self.volt.s_tag)
242 port.set_parameter("device", "eth1")
243 port.set_parameter("bridge", "br-lan")
244
245 wan_networks = [x for x in instance.slice.networks.all() if "wan" in x.name]
246 if not wan_networks:
247 raise XOSProgrammingError("No wan_network")
248 port = self.find_or_make_port(instance, wan_networks[0])
249 port.set_parameter("next_hop", value="10.0.1.253") # FIX ME
250 port.set_parameter("device", "eth0")
251
252 if instance.isolation in ["vm"]:
253 lan_network = self.get_lan_network(instance)
254 port = self.find_or_make_port(instance, lan_network)
255 port.set_parameter("c_tag", self.volt.c_tag)
256 port.set_parameter("s_tag", self.volt.s_tag)
257 port.set_parameter("neutron_port_name", "stag-%s" % self.volt.s_tag)
258 port.save()
259
260 # tag the instance with the s-tag, so we can easily find the
261 # instance later
262 if self.volt and self.volt.s_tag:
263 tags = Tag.objects.filter(name="s_tag", value=self.volt.s_tag)
264 if not tags:
Scott Baker48f9ac12017-05-17 12:03:03 -0700265 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 +0200266 tag.save()
267
268 # VTN-CORD needs a WAN address for the VM, so that the VM can
269 # be configured.
270 tags = Tag.select_by_content_object(instance).filter(name="vm_vrouter_tenant")
271 if not tags:
272 vrouter = self.get_vrouter_service().get_tenant(address_pool_name="addresses_vsg", subscriber_service = self.provider_service)
273 vrouter.set_attribute("tenant_for_instance_id", instance.id)
274 vrouter.save()
Scott Baker48f9ac12017-05-17 12:03:03 -0700275 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 +0200276 tag.save()
277
278def save(self, *args, **kwargs):
279 if not self.creator:
280 if not getattr(self, "caller", None):
281 # caller must be set when creating a vCPE since it creates a slice
282 raise XOSProgrammingError("VSGTenant's self.caller was not set")
283 self.creator = self.caller
284 if not self.creator:
285 raise XOSProgrammingError("VSGTenant's self.creator was not set")
286
287 super(VSGTenant, self).save(*args, **kwargs)
288 model_policy_vcpe(self.pk)
289
290def delete(self, *args, **kwargs):
291 self.cleanup_vrouter()
292 self.cleanup_container()
293 super(VSGTenant, self).delete(*args, **kwargs)
294