blob: d7680985539954879de8c391d1e9c62e1949920c [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):
147 #s_tags = STagBlock.objects.find(s_s_tag)
148 #if s_tags:
149 # return s_tags[0].instance
150
151 tags = Tag.objects.filter(name="s_tag", value=s_tag)
152 if tags:
153 return tags[0].content_object
154
155 return None
156
157def find_or_make_instance_for_s_tag(self, s_tag):
158 instance = self.find_instance_for_s_tag(self.volt.s_tag)
159 if instance:
160 return instance
161
162 flavors = Flavor.objects.filter(name="m1.small")
163 if not flavors:
164 raise XOSConfigurationError("No m1.small flavor")
165
166 slice = self.provider_service.slices.all()[0]
167
168 if slice.default_isolation == "container_vm":
169 (node, parent) = ContainerVmScheduler(slice).pick()
170 else:
171 (node, parent) = LeastLoadedNodeScheduler(slice, label=self.get_vsg_service().node_label).pick()
172
173 instance = Instance(slice = slice,
174 node = node,
175 image = self.image,
176 creator = self.creator,
177 deployment = node.site_deployment.deployment,
178 flavor = flavors[0],
179 isolation = slice.default_isolation,
180 parent = parent)
181
182 self.save_instance(instance)
183
184 return instance
185
186def manage_container(self):
187 from core.models import Instance, Flavor
188
189 if self.deleted:
190 return
191
192 # For container or container_vm isolation, use what TenantWithCotnainer
193 # provides us
194 slice = self.get_slice()
195 if slice.default_isolation in ["container_vm", "container"]:
196 super(VSGTenant,self).manage_container()
197 return
198
199 if not self.volt:
200 raise XOSConfigurationError("This vCPE container has no volt")
201
202 if self.instance:
203 # We're good.
204 return
205
206 instance = self.find_or_make_instance_for_s_tag(self.volt.s_tag)
207 self.instance = instance
208 super(TenantWithContainer, self).save()
209
210def cleanup_container(self):
211 if self.get_slice().default_isolation in ["container_vm", "container"]:
212 super(VSGTenant,self).cleanup_container()
213
214 # To-do: cleanup unused instances
215 pass
216
217def find_or_make_port(self, instance, network, **kwargs):
218 port = Port.objects.filter(instance=instance, network=network)
219 if port:
220 port = port[0]
221 else:
222 port = Port(instance=instance, network=network, **kwargs)
223 port.save()
224 return port
225
226def get_lan_network(self, instance):
227 slice = self.provider_service.slices.all()[0]
228 # there should only be one network private network, and its template should not be the management template
229 lan_networks = [x for x in slice.networks.all() if x.template.visibility=="private" and (not "management" in x.template.name)]
230 if len(lan_networks)>1:
231 raise XOSProgrammingError("The vSG slice should only have one non-management private network")
232 if not lan_networks:
233 raise XOSProgrammingError("No lan_network")
234 return lan_networks[0]
235
236def save_instance(self, instance):
237 with transaction.atomic():
238 instance.volumes = "/etc/dnsmasq.d,/etc/ufw"
239 super(VSGTenant, self).save_instance(instance)
240
241 if instance.isolation in ["container", "container_vm"]:
242 lan_network = self.get_lan_network(instance)
243 port = self.find_or_make_port(instance, lan_network, ip="192.168.0.1", port_id="unmanaged")
244 port.set_parameter("c_tag", self.volt.c_tag)
245 port.set_parameter("s_tag", self.volt.s_tag)
246 port.set_parameter("device", "eth1")
247 port.set_parameter("bridge", "br-lan")
248
249 wan_networks = [x for x in instance.slice.networks.all() if "wan" in x.name]
250 if not wan_networks:
251 raise XOSProgrammingError("No wan_network")
252 port = self.find_or_make_port(instance, wan_networks[0])
253 port.set_parameter("next_hop", value="10.0.1.253") # FIX ME
254 port.set_parameter("device", "eth0")
255
256 if instance.isolation in ["vm"]:
257 lan_network = self.get_lan_network(instance)
258 port = self.find_or_make_port(instance, lan_network)
259 port.set_parameter("c_tag", self.volt.c_tag)
260 port.set_parameter("s_tag", self.volt.s_tag)
261 port.set_parameter("neutron_port_name", "stag-%s" % self.volt.s_tag)
262 port.save()
263
264 # tag the instance with the s-tag, so we can easily find the
265 # instance later
266 if self.volt and self.volt.s_tag:
267 tags = Tag.objects.filter(name="s_tag", value=self.volt.s_tag)
268 if not tags:
269 tag = Tag(service=self.provider_service, content_object=instance, name="s_tag", value=self.volt.s_tag)
270 tag.save()
271
272 # VTN-CORD needs a WAN address for the VM, so that the VM can
273 # be configured.
274 tags = Tag.select_by_content_object(instance).filter(name="vm_vrouter_tenant")
275 if not tags:
276 vrouter = self.get_vrouter_service().get_tenant(address_pool_name="addresses_vsg", subscriber_service = self.provider_service)
277 vrouter.set_attribute("tenant_for_instance_id", instance.id)
278 vrouter.save()
279 tag = Tag(service=self.provider_service, content_object=instance, name="vm_vrouter_tenant", value="%d" % vrouter.id)
280 tag.save()
281
282def save(self, *args, **kwargs):
283 if not self.creator:
284 if not getattr(self, "caller", None):
285 # caller must be set when creating a vCPE since it creates a slice
286 raise XOSProgrammingError("VSGTenant's self.caller was not set")
287 self.creator = self.caller
288 if not self.creator:
289 raise XOSProgrammingError("VSGTenant's self.creator was not set")
290
291 super(VSGTenant, self).save(*args, **kwargs)
292 model_policy_vcpe(self.pk)
293
294def delete(self, *args, **kwargs):
295 self.cleanup_vrouter()
296 self.cleanup_container()
297 super(VSGTenant, self).delete(*args, **kwargs)
298