blob: 1a1b6efa172fae8c80ab4d2da3e8e76dafc0b749 [file] [log] [blame]
Siobhan Tully00353f72013-10-08 21:53:27 -04001from django.db import models
Scott Baker0d306722015-04-15 20:58:20 -07002from core.models import PlCoreBase,SingletonModel,PlCoreBaseManager
Tony Mack50e12212015-03-09 13:03:56 -04003from core.models.plcorebase import StrippedCharField
Scott Baker7f8ef8f2015-04-20 14:24:29 -07004from xos.exceptions import *
Scott Bakerb2385622015-07-06 14:27:31 -07005from operator import attrgetter
Scott Baker7211f5b2015-04-14 17:18:51 -07006import json
Siobhan Tully00353f72013-10-08 21:53:27 -04007
Scott Bakerc24f86d2015-08-14 09:10:11 -07008COARSE_KIND="coarse"
9
Scott Baker9d1c6d92015-07-13 13:07:27 -070010class AttributeMixin(object):
11 # helper for extracting things from a json-encoded service_specific_attribute
12 def get_attribute(self, name, default=None):
13 if self.service_specific_attribute:
14 attributes = json.loads(self.service_specific_attribute)
15 else:
16 attributes = {}
17 return attributes.get(name, default)
18
19 def set_attribute(self, name, value):
20 if self.service_specific_attribute:
21 attributes = json.loads(self.service_specific_attribute)
22 else:
23 attributes = {}
24 attributes[name]=value
25 self.service_specific_attribute = json.dumps(attributes)
26
27 def get_initial_attribute(self, name, default=None):
28 if self._initial["service_specific_attribute"]:
29 attributes = json.loads(self._initial["service_specific_attribute"])
30 else:
31 attributes = {}
32 return attributes.get(name, default)
33
Scott Bakereb098e62015-07-13 13:54:06 -070034 @classmethod
35 def setup_simple_attributes(cls):
36 for (attrname, default) in cls.simple_attributes:
Scott Bakere2879d32015-07-13 14:27:51 -070037 setattr(cls, attrname, property(lambda self, attrname=attrname, default=default: self.get_attribute(attrname, default),
38 lambda self, value, attrname=attrname: self.set_attribute(attrname, value),
39 None,
40 attrname))
Scott Bakereb098e62015-07-13 13:54:06 -070041
Scott Baker9d1c6d92015-07-13 13:07:27 -070042class Service(PlCoreBase, AttributeMixin):
Scott Baker0d306722015-04-15 20:58:20 -070043 # when subclassing a service, redefine KIND to describe the new service
44 KIND = "generic"
45
Siobhan Tully00353f72013-10-08 21:53:27 -040046 description = models.TextField(max_length=254,null=True, blank=True,help_text="Description of Service")
47 enabled = models.BooleanField(default=True)
Scott Baker0d306722015-04-15 20:58:20 -070048 kind = StrippedCharField(max_length=30, help_text="Kind of service", default=KIND)
Tony Mack50e12212015-03-09 13:03:56 -040049 name = StrippedCharField(max_length=30, help_text="Service Name")
50 versionNumber = StrippedCharField(max_length=30, help_text="Version of Service Definition")
Siobhan Tullycf04fb62014-01-11 11:25:57 -050051 published = models.BooleanField(default=True)
Tony Mack50e12212015-03-09 13:03:56 -040052 view_url = StrippedCharField(blank=True, null=True, max_length=1024)
53 icon_url = StrippedCharField(blank=True, null=True, max_length=1024)
Scott Baker5b044612015-04-30 14:30:56 -070054 public_key = models.TextField(null=True, blank=True, max_length=1024, help_text="Public key string")
Scott Bakerdc63fb32015-11-12 16:22:52 -080055 private_key_fn = StrippedCharField(blank=True, null=True, max_length=1024)
Siobhan Tully00353f72013-10-08 21:53:27 -040056
Scott Bakerb9040e92015-07-13 12:33:28 -070057 # Service_specific_attribute and service_specific_id are opaque to XOS
58 service_specific_id = StrippedCharField(max_length=30, blank=True, null=True)
59 service_specific_attribute = models.TextField(blank=True, null=True)
60
Scott Baker0d306722015-04-15 20:58:20 -070061 def __init__(self, *args, **kwargs):
62 # for subclasses, set the default kind appropriately
63 self._meta.get_field("kind").default = self.KIND
64 super(Service, self).__init__(*args, **kwargs)
65
66 @classmethod
67 def get_service_objects(cls):
68 return cls.objects.filter(kind = cls.KIND)
69
Scott Baker27de6012015-07-24 15:36:02 -070070 @classmethod
Scott Bakercd32ad02015-10-19 21:18:53 -070071 def get_deleted_service_objects(cls):
72 return cls.deleted_objects.filter(kind = cls.KIND)
73
74 @classmethod
Scott Baker27de6012015-07-24 15:36:02 -070075 def get_service_objects_by_user(cls, user):
76 return cls.select_by_user(user).filter(kind = cls.KIND)
77
78 @classmethod
79 def select_by_user(cls, user):
80 if user.is_admin:
81 return cls.objects.all()
82 else:
83 service_ids = [sp.slice.id for sp in ServicePrivilege.objects.filter(user=user)]
84 return cls.objects.filter(id__in=service_ids)
85
Scott Bakerbcea8cf2015-12-07 22:20:40 -080086 @property
87 def serviceattribute_dict(self):
88 attrs = {}
89 for attr in self.serviceattributes.all():
90 attrs[attr.name] = attr.value
91 return attrs
92
Siobhan Tully00353f72013-10-08 21:53:27 -040093 def __unicode__(self): return u'%s' % (self.name)
94
Tony Mack950b4492015-04-29 12:23:10 -040095 def can_update(self, user):
96 return user.can_update_service(self, allow=['admin'])
Scott Bakerb2385622015-07-06 14:27:31 -070097
Scott Baker25757222015-05-11 16:36:41 -070098 def get_scalable_nodes(self, slice, max_per_node=None, exclusive_slices=[]):
99 """
100 Get a list of nodes that can be used to scale up a slice.
101
102 slice - slice to scale up
Tony Mackd8515472015-08-19 11:58:18 -0400103 max_per_node - maximum numbers of instances that 'slice' can have on a single node
Scott Baker25757222015-05-11 16:36:41 -0700104 exclusive_slices - list of slices that must have no nodes in common with 'slice'.
105 """
106
Tony Mackd8515472015-08-19 11:58:18 -0400107 from core.models import Node, Instance # late import to get around order-of-imports constraint in __init__.py
Scott Baker25757222015-05-11 16:36:41 -0700108
109 nodes = list(Node.objects.all())
110
Tony Mackd8515472015-08-19 11:58:18 -0400111 conflicting_instances = Instance.objects.filter(slice__in = exclusive_slices)
112 conflicting_nodes = Node.objects.filter(instances__in = conflicting_instances)
Scott Baker25757222015-05-11 16:36:41 -0700113
114 nodes = [x for x in nodes if x not in conflicting_nodes]
115
Tony Mackd8515472015-08-19 11:58:18 -0400116 # If max_per_node is set, then limit the number of instances this slice
Scott Baker25757222015-05-11 16:36:41 -0700117 # can have on a single node.
118 if max_per_node:
119 acceptable_nodes = []
120 for node in nodes:
Tony Mackd8515472015-08-19 11:58:18 -0400121 existing_count = node.instances.filter(slice=slice).count()
Scott Baker25757222015-05-11 16:36:41 -0700122 if existing_count < max_per_node:
123 acceptable_nodes.append(node)
124 nodes = acceptable_nodes
125
126 return nodes
127
128 def pick_node(self, slice, max_per_node=None, exclusive_slices=[]):
129 # Pick the best node to scale up a slice.
130
131 nodes = self.get_scalable_nodes(slice, max_per_node, exclusive_slices)
Tony Mackd8515472015-08-19 11:58:18 -0400132 nodes = sorted(nodes, key=lambda node: node.instances.all().count())
Scott Baker25757222015-05-11 16:36:41 -0700133 if not nodes:
134 return None
135 return nodes[0]
136
137 def adjust_scale(self, slice_hint, scale, max_per_node=None, exclusive_slices=[]):
Tony Mackd8515472015-08-19 11:58:18 -0400138 from core.models import Instance # late import to get around order-of-imports constraint in __init__.py
Scott Baker25757222015-05-11 16:36:41 -0700139
140 slices = [x for x in self.slices.all() if slice_hint in x.name]
141 for slice in slices:
Tony Mackd8515472015-08-19 11:58:18 -0400142 while slice.instances.all().count() > scale:
143 s = slice.instances.all()[0]
144 # print "drop instance", s
Scott Baker25757222015-05-11 16:36:41 -0700145 s.delete()
146
Tony Mackd8515472015-08-19 11:58:18 -0400147 while slice.instances.all().count() < scale:
Scott Baker25757222015-05-11 16:36:41 -0700148 node = self.pick_node(slice, max_per_node, exclusive_slices)
149 if not node:
150 # no more available nodes
151 break
152
153 image = slice.default_image
154 if not image:
155 raise XOSConfigurationError("No default_image for slice %s" % slice.name)
156
157 flavor = slice.default_flavor
158 if not flavor:
159 raise XOSConfigurationError("No default_flavor for slice %s" % slice.name)
160
Tony Mackd8515472015-08-19 11:58:18 -0400161 s = Instance(slice=slice,
Scott Baker25757222015-05-11 16:36:41 -0700162 node=node,
163 creator=slice.creator,
164 image=image,
165 flavor=flavor,
166 deployment=node.site_deployment.deployment)
167 s.save()
168
Tony Mackd8515472015-08-19 11:58:18 -0400169 # print "add instance", s
Tony Mack950b4492015-04-29 12:23:10 -0400170
Scott Bakercbf4c782015-12-09 22:54:52 -0800171 def get_vtn_src_nets(self):
172 nets=[]
173 for slice in self.slices.all():
174 for ns in slice.networkslices.all():
175 if not ns.network:
176 continue
Scott Bakereb3bad92016-01-12 19:59:12 -0800177# if ns.network.template.access in ["direct", "indirect"]:
178# # skip access networks; we want to use the private network
179# continue
Scott Bakercbf4c782015-12-09 22:54:52 -0800180 if ns.network.name in ["wan_network", "lan_network"]:
181 # we don't want to attach to the vCPE's lan or wan network
182 # we only want to attach to its private network
183 # TODO: fix hard-coding of network name
184 continue
185 for cn in ns.network.controllernetworks.all():
186 if cn.net_id:
187 net = {"name": ns.network.name, "net_id": cn.net_id}
188 nets.append(net)
189 return nets
190
Scott Baker1b7d98b2015-12-08 21:31:18 -0800191 def get_vtn_nets(self):
192 nets=[]
193 for slice in self.slices.all():
194 for ns in slice.networkslices.all():
195 if not ns.network:
196 continue
Scott Bakercbf4c782015-12-09 22:54:52 -0800197 if ns.network.template.access not in ["direct", "indirect"]:
198 # skip anything that's not an access network
199 continue
Scott Baker1b7d98b2015-12-08 21:31:18 -0800200 for cn in ns.network.controllernetworks.all():
201 if cn.net_id:
202 net = {"name": ns.network.name, "net_id": cn.net_id}
203 nets.append(net)
204 return nets
205
206 def get_vtn_dependencies_nets(self):
207 provider_nets = []
Scott Baker012c54b2015-12-08 19:27:50 -0800208 for tenant in self.subscribed_tenants.all():
209 if tenant.provider_service:
Scott Baker1b7d98b2015-12-08 21:31:18 -0800210 for net in tenant.provider_service.get_vtn_nets():
211 if not net in provider_nets:
212 provider_nets.append(net)
213 return provider_nets
214
215 def get_vtn_dependencies_ids(self):
216 return [x["net_id"] for x in self.get_vtn_dependencies_nets()]
217
218 def get_vtn_dependencies_names(self):
219 return [x["name"]+"_"+x["net_id"] for x in self.get_vtn_dependencies_nets()]
220
Scott Bakercbf4c782015-12-09 22:54:52 -0800221 def get_vtn_src_ids(self):
222 return [x["net_id"] for x in self.get_vtn_src_nets()]
Scott Baker1b7d98b2015-12-08 21:31:18 -0800223
Scott Bakercbf4c782015-12-09 22:54:52 -0800224 def get_vtn_src_names(self):
225 return [x["name"]+"_"+x["net_id"] for x in self.get_vtn_src_nets()]
Scott Baker012c54b2015-12-08 19:27:50 -0800226
227
Siobhan Tully00353f72013-10-08 21:53:27 -0400228class ServiceAttribute(PlCoreBase):
Scott Bakerbcea8cf2015-12-07 22:20:40 -0800229 name = models.CharField(help_text="Attribute Name", max_length=128)
Tony Mack50e12212015-03-09 13:03:56 -0400230 value = StrippedCharField(help_text="Attribute Value", max_length=1024)
Siobhan Tully00353f72013-10-08 21:53:27 -0400231 service = models.ForeignKey(Service, related_name='serviceattributes', help_text="The Service this attribute is associated with")
232
Tony Mack950b4492015-04-29 12:23:10 -0400233class ServiceRole(PlCoreBase):
234 ROLE_CHOICES = (('admin','Admin'),)
235 role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30)
236
237 def __unicode__(self): return u'%s' % (self.role)
238
239class ServicePrivilege(PlCoreBase):
240 user = models.ForeignKey('User', related_name='serviceprivileges')
241 service = models.ForeignKey('Service', related_name='serviceprivileges')
242 role = models.ForeignKey('ServiceRole',related_name='serviceprivileges')
243
244 class Meta:
Tony Mack02683de2015-05-13 12:21:28 -0400245 unique_together = ('user', 'service', 'role')
Tony Mack950b4492015-04-29 12:23:10 -0400246
247 def __unicode__(self): return u'%s %s %s' % (self.service, self.user, self.role)
248
249 def can_update(self, user):
250 if not self.service.enabled:
251 raise PermissionDenied, "Cannot modify permission(s) of a disabled service"
252 return self.service.can_update(user)
253
254 def save(self, *args, **kwds):
255 if not self.service.enabled:
256 raise PermissionDenied, "Cannot modify permission(s) of a disabled service"
257 super(ServicePrivilege, self).save(*args, **kwds)
258
259 def delete(self, *args, **kwds):
260 if not self.service.enabled:
261 raise PermissionDenied, "Cannot modify permission(s) of a disabled service"
Scott Bakera86489f2015-07-01 18:29:08 -0700262 super(ServicePrivilege, self).delete(*args, **kwds)
263
Scott Baker27de6012015-07-24 15:36:02 -0700264 @classmethod
265 def select_by_user(cls, user):
Tony Mack950b4492015-04-29 12:23:10 -0400266 if user.is_admin:
Scott Baker27de6012015-07-24 15:36:02 -0700267 qs = cls.objects.all()
Tony Mack950b4492015-04-29 12:23:10 -0400268 else:
Scott Baker27de6012015-07-24 15:36:02 -0700269 qs = cls.objects.filter(user=user)
Scott Bakera86489f2015-07-01 18:29:08 -0700270 return qs
271
Scott Baker9d1c6d92015-07-13 13:07:27 -0700272class TenantRoot(PlCoreBase, AttributeMixin):
Scott Bakera86489f2015-07-01 18:29:08 -0700273 """ A tenantRoot is one of the things that can sit at the root of a chain
274 of tenancy. This object represents a node.
275 """
276
277 KIND= "generic"
278 kind = StrippedCharField(max_length=30, default=KIND)
Scott Bakerb2385622015-07-06 14:27:31 -0700279 name = StrippedCharField(max_length=255, help_text="name", blank=True, null=True)
Scott Bakera86489f2015-07-01 18:29:08 -0700280
Scott Baker29415a82015-07-07 12:12:42 -0700281 service_specific_attribute = models.TextField(blank=True, null=True)
282 service_specific_id = StrippedCharField(max_length=30, blank=True, null=True)
Scott Bakera86489f2015-07-01 18:29:08 -0700283
Scott Baker126ad472015-07-07 17:59:44 -0700284 def __init__(self, *args, **kwargs):
285 # for subclasses, set the default kind appropriately
286 self._meta.get_field("kind").default = self.KIND
287 super(TenantRoot, self).__init__(*args, **kwargs)
288
Scott Bakerb2385622015-07-06 14:27:31 -0700289 def __unicode__(self):
290 if not self.name:
291 return u"%s-tenant_root-#%s" % (str(self.kind), str(self.id))
292 else:
293 return self.name
294
295 def can_update(self, user):
296 return user.can_update_tenant_root(self, allow=['admin'])
297
Scott Baker29415a82015-07-07 12:12:42 -0700298 def get_subscribed_tenants(self, tenant_class):
299 ids = self.subscribed_tenants.filter(kind=tenant_class.KIND)
300 return tenant_class.objects.filter(id__in = ids)
301
302 def get_newest_subscribed_tenant(self, kind):
303 st = list(self.get_subscribed_tenants(kind))
304 if not st:
305 return None
306 return sorted(st, key=attrgetter('id'))[0]
307
308 @classmethod
309 def get_tenant_objects(cls):
310 return cls.objects.filter(kind = cls.KIND)
311
Scott Baker27de6012015-07-24 15:36:02 -0700312 @classmethod
313 def get_tenant_objects_by_user(cls, user):
314 return cls.select_by_user(user).filter(kind = cls.KIND)
315
316 @classmethod
317 def select_by_user(cls, user):
318 if user.is_admin:
319 return cls.objects.all()
320 else:
321 tr_ids = [trp.tenant_root.id for trp in TenantRootPrivilege.objects.filter(user=user)]
322 return cls.objects.filter(id__in=tr_ids)
323
Scott Baker9d1c6d92015-07-13 13:07:27 -0700324class Tenant(PlCoreBase, AttributeMixin):
Scott Baker91e85882015-04-10 16:42:26 -0700325 """ A tenant is a relationship between two entities, a subscriber and a
Scott Bakera86489f2015-07-01 18:29:08 -0700326 provider. This object represents an edge.
Scott Baker91e85882015-04-10 16:42:26 -0700327
328 The subscriber can be a User, a Service, or a Tenant.
329
330 The provider is always a Service.
Scott Bakera86489f2015-07-01 18:29:08 -0700331
332 TODO: rename "Tenant" to "Tenancy"
Scott Baker91e85882015-04-10 16:42:26 -0700333 """
Scott Baker0d306722015-04-15 20:58:20 -0700334
Scott Bakeref58a842015-04-26 20:30:40 -0700335 CONNECTIVITY_CHOICES = (('public', 'Public'), ('private', 'Private'), ('na', 'Not Applicable'))
336
Scott Baker0d306722015-04-15 20:58:20 -0700337 # when subclassing a service, redefine KIND to describe the new service
338 KIND = "generic"
339
340 kind = StrippedCharField(max_length=30, default=KIND)
Scott Bakera86489f2015-07-01 18:29:08 -0700341 provider_service = models.ForeignKey(Service, related_name='provided_tenants')
342
343 # The next four things are the various type of objects that can be subscribers of this Tenancy
344 # relationship. One and only one can be used at a time.
345 subscriber_service = models.ForeignKey(Service, related_name='subscribed_tenants', blank=True, null=True)
346 subscriber_tenant = models.ForeignKey("Tenant", related_name='subscribed_tenants', blank=True, null=True)
347 subscriber_user = models.ForeignKey("User", related_name='subscribed_tenants', blank=True, null=True)
348 subscriber_root = models.ForeignKey("TenantRoot", related_name="subscribed_tenants", blank=True, null=True)
349
350 # Service_specific_attribute and service_specific_id are opaque to XOS
Scott Baker1b7c6f12015-05-06 19:49:31 -0700351 service_specific_id = StrippedCharField(max_length=30, blank=True, null=True)
352 service_specific_attribute = models.TextField(blank=True, null=True)
Scott Bakera86489f2015-07-01 18:29:08 -0700353
354 # Connect_method is only used by Coarse tenants
Scott Bakeref58a842015-04-26 20:30:40 -0700355 connect_method = models.CharField(null=False, blank=False, max_length=30, choices=CONNECTIVITY_CHOICES, default="na")
Scott Baker91e85882015-04-10 16:42:26 -0700356
Scott Baker0d306722015-04-15 20:58:20 -0700357 def __init__(self, *args, **kwargs):
358 # for subclasses, set the default kind appropriately
359 self._meta.get_field("kind").default = self.KIND
360 super(Tenant, self).__init__(*args, **kwargs)
361
Scott Baker91e85882015-04-10 16:42:26 -0700362 def __unicode__(self):
Scott Bakerfe91f622015-05-20 20:42:04 -0700363 return u"%s-tenant-%s" % (str(self.kind), str(self.id))
Scott Baker91e85882015-04-10 16:42:26 -0700364
Scott Baker0d306722015-04-15 20:58:20 -0700365 @classmethod
366 def get_tenant_objects(cls):
367 return cls.objects.filter(kind = cls.KIND)
368
Scott Bakereb50ee32015-05-05 17:52:03 -0700369 @classmethod
Scott Baker27de6012015-07-24 15:36:02 -0700370 def get_tenant_objects_by_user(cls, user):
371 return cls.select_by_user(user).filter(kind = cls.KIND)
372
373 @classmethod
Scott Bakereb50ee32015-05-05 17:52:03 -0700374 def get_deleted_tenant_objects(cls):
375 return cls.deleted_objects.filter(kind = cls.KIND)
376
Scott Bakerbcea8cf2015-12-07 22:20:40 -0800377 @property
378 def tenantattribute_dict(self):
379 attrs = {}
380 for attr in self.tenantattributes.all():
381 attrs[attr.name] = attr.value
382 return attrs
383
Scott Baker7f8ef8f2015-04-20 14:24:29 -0700384 # helper function to be used in subclasses that want to ensure service_specific_id is unique
385 def validate_unique_service_specific_id(self):
386 if self.pk is None:
387 if self.service_specific_id is None:
388 raise XOSMissingField("subscriber_specific_id is None, and it's a required field", fields={"service_specific_id": "cannot be none"})
389
390 conflicts = self.get_tenant_objects().filter(service_specific_id=self.service_specific_id)
391 if conflicts:
392 raise XOSDuplicateKey("service_specific_id %s already exists" % self.service_specific_id, fields={"service_specific_id": "duplicate key"})
393
Scott Bakerb2385622015-07-06 14:27:31 -0700394 def save(self, *args, **kwargs):
395 subCount = sum( [1 for e in [self.subscriber_service, self.subscriber_tenant, self.subscriber_user, self.subscriber_root] if e is not None])
396 if (subCount > 1):
397 raise XOSConflictingField("Only one of subscriber_service, subscriber_tenant, subscriber_user, subscriber_root should be set")
398
399 super(Tenant, self).save(*args, **kwargs)
400
401 def get_subscribed_tenants(self, tenant_class):
402 ids = self.subscribed_tenants.filter(kind=tenant_class.KIND)
403 return tenant_class.objects.filter(id__in = ids)
404
405 def get_newest_subscribed_tenant(self, kind):
406 st = list(self.get_subscribed_tenants(kind))
407 if not st:
408 return None
409 return sorted(st, key=attrgetter('id'))[0]
410
Scott Bakerc8914bf2015-11-18 20:58:08 -0800411class Scheduler(object):
412 # XOS Scheduler Abstract Base Class
413 # Used to implement schedulers that pick which node to put instances on
414
415 def __init__(self, slice):
416 self.slice = slice
417
418 def pick(self):
419 # this method should return a tuple (node, parent)
420 # node is the node to instantiate on
421 # parent is for container_vm instances only, and is the VM that will
422 # hold the container
423
424 raise Exception("Abstract Base")
425
426class LeastLoadedNodeScheduler(Scheduler):
427 # This scheduler always return the node with the fewest number of instances.
428
429 def __init__(self, slice):
430 super(LeastLoadedNodeScheduler, self).__init__(slice)
431
432 def pick(self):
433 from core.models import Node
434 nodes = list(Node.objects.all())
Scott Bakerbcea8cf2015-12-07 22:20:40 -0800435
Scott Bakerc8914bf2015-11-18 20:58:08 -0800436 # TODO: logic to filter nodes by which nodes are up, and which
437 # nodes the slice can instantiate on.
438 nodes = sorted(nodes, key=lambda node: node.instances.all().count())
439 return [nodes[0], None]
440
441class ContainerVmScheduler(Scheduler):
442 # This scheduler picks a VM in the slice with the fewest containers inside
443 # of it. If no VMs are suitable, then it creates a VM.
444
445 # this is a hack and should be replaced by something smarter...
446 LOOK_FOR_IMAGES=["ubuntu-vcpe4", # ONOS demo machine -- preferred vcpe image
447 "Ubuntu 14.04 LTS", # portal
448 "Ubuntu-14.04-LTS", # ONOS demo machine
449 "trusty-server-multi-nic", # CloudLab
450 ]
451
452 MAX_VM_PER_CONTAINER = 10
453
454 def __init__(self, slice):
455 super(ContainerVmScheduler, self).__init__(slice)
456
457 @property
458 def image(self):
459 from core.models import Image
460
461 look_for_images = self.LOOK_FOR_IMAGES
462 for image_name in look_for_images:
463 images = Image.objects.filter(name = image_name)
464 if images:
465 return images[0]
466
467 raise XOSProgrammingError("No ContainerVM image (looked for %s)" % str(look_for_images))
468
469 def make_new_instance(self):
470 from core.models import Instance, Flavor
471
472 flavors = Flavor.objects.filter(name="m1.small")
473 if not flavors:
474 raise XOSConfigurationError("No m1.small flavor")
475
476 (node,parent) = LeastLoadedNodeScheduler(self.slice).pick()
477
478 instance = Instance(slice = self.slice,
479 node = node,
480 image = self.image,
481 creator = self.slice.creator,
482 deployment = node.site_deployment.deployment,
483 flavor = flavors[0],
484 isolation = "vm",
485 parent = parent)
486 instance.save()
487 # We rely on a special naming convention to identify the VMs that will
488 # hole containers.
489 instance.name = "%s-outer-%s" % (instance.slice.name, instance.id)
490 instance.save()
491 return instance
492
493 def pick(self):
494 from core.models import Instance, Flavor
495
496 for vm in self.slice.instances.filter(isolation="vm"):
497 avail_vms = []
498 if (vm.name.startswith("%s-outer-" % self.slice.name)):
499 container_count = Instance.objects.filter(parent=vm).count()
500 if (container_count < self.MAX_VM_PER_CONTAINER):
501 avail_vms.append( (vm, container_count) )
502 # sort by least containers-per-vm
503 avail_vms = sorted(avail_vms, key = lambda x: x[1])
504 print "XXX", avail_vms
505 if avail_vms:
506 instance = avail_vms[0][0]
507 return (instance.node, instance)
508
509 instance = self.make_new_instance()
510 return (instance.node, instance)
511
Scott Bakerc1584b82015-09-09 16:36:06 -0700512class TenantWithContainer(Tenant):
513 """ A tenant that manages a container """
514
515 # this is a hack and should be replaced by something smarter...
Scott Bakerf05c4972015-09-09 16:43:39 -0700516 LOOK_FOR_IMAGES=["ubuntu-vcpe4", # ONOS demo machine -- preferred vcpe image
Scott Bakerc1584b82015-09-09 16:36:06 -0700517 "Ubuntu 14.04 LTS", # portal
518 "Ubuntu-14.04-LTS", # ONOS demo machine
Scott Bakerf05c4972015-09-09 16:43:39 -0700519 "trusty-server-multi-nic", # CloudLab
Scott Bakerc1584b82015-09-09 16:36:06 -0700520 ]
521
Scott Baker5e505a52015-12-14 10:21:53 -0800522 LOOK_FOR_CONTAINER_IMAGES=["docker-vcpe"]
Scott Bakera759fe32015-11-16 22:51:02 -0800523
Scott Bakerc1584b82015-09-09 16:36:06 -0700524 class Meta:
525 proxy = True
526
527 def __init__(self, *args, **kwargs):
528 super(TenantWithContainer, self).__init__(*args, **kwargs)
Tony Mack32010062015-09-13 22:50:39 +0000529 self.cached_instance=None
530 self.orig_instance_id = self.get_initial_attribute("instance_id")
Scott Baker5c125e42015-11-02 20:54:28 -0800531
Scott Bakerc1584b82015-09-09 16:36:06 -0700532 @property
Tony Mack32010062015-09-13 22:50:39 +0000533 def instance(self):
534 from core.models import Instance
535 if getattr(self, "cached_instance", None):
536 return self.cached_instance
537 instance_id=self.get_attribute("instance_id")
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600538 if not instance_id:
539 return None
540 instances=Instance.objects.filter(id=instance_id)
541 if not instances:
542 return None
543 instance=instances[0]
Tony Mack32010062015-09-13 22:50:39 +0000544 instance.caller = self.creator
545 self.cached_instance = instance
546 return instance
Scott Bakerc1584b82015-09-09 16:36:06 -0700547
Tony Mack32010062015-09-13 22:50:39 +0000548 @instance.setter
549 def instance(self, value):
Scott Bakerc1584b82015-09-09 16:36:06 -0700550 if value:
551 value = value.id
Tony Mack32010062015-09-13 22:50:39 +0000552 if (value != self.get_attribute("instance_id", None)):
553 self.cached_instance=None
554 self.set_attribute("instance_id", value)
Scott Bakerc1584b82015-09-09 16:36:06 -0700555
Scott Baker5c125e42015-11-02 20:54:28 -0800556 @property
Scott Baker268e2aa2016-02-10 12:23:53 -0800557 def external_hostname(self):
558 return self.get_attribute("external_hostname", "")
559
560 @external_hostname.setter
561 def external_hostname(self, value):
562 self.set_attribute("external_hostname", value)
563
564 @property
565 def external_container(self):
566 return self.get_attribute("external_container", "")
567
568 @external_container.setter
569 def external_container(self, value):
570 self.set_attribute("external_container", value)
571
572 @property
Scott Bakerc1584b82015-09-09 16:36:06 -0700573 def creator(self):
574 from core.models import User
575 if getattr(self, "cached_creator", None):
576 return self.cached_creator
577 creator_id=self.get_attribute("creator_id")
578 if not creator_id:
579 return None
580 users=User.objects.filter(id=creator_id)
581 if not users:
582 return None
583 user=users[0]
584 self.cached_creator = users[0]
585 return user
586
587 @creator.setter
588 def creator(self, value):
589 if value:
590 value = value.id
591 if (value != self.get_attribute("creator_id", None)):
592 self.cached_creator=None
593 self.set_attribute("creator_id", value)
594
595 @property
596 def image(self):
597 from core.models import Image
598 # Implement the logic here to pick the image that should be used when
599 # instantiating the VM that will hold the container.
Scott Bakera759fe32015-11-16 22:51:02 -0800600
601 slice = self.provider_service.slices.all()
602 if not slice:
603 raise XOSProgrammingError("provider service has no slice")
604 slice = slice[0]
605
606 if slice.default_isolation in ["container", "container_vm"]:
607 look_for_images = self.LOOK_FOR_CONTAINER_IMAGES
608 else:
609 look_for_images = self.LOOK_FOR_IMAGES
610
611 for image_name in look_for_images:
Scott Bakerc1584b82015-09-09 16:36:06 -0700612 images = Image.objects.filter(name = image_name)
613 if images:
614 return images[0]
615
Scott Bakerc8914bf2015-11-18 20:58:08 -0800616 raise XOSProgrammingError("No VPCE image (looked for %s)" % str(look_for_images))
Scott Baker5c125e42015-11-02 20:54:28 -0800617
Scott Bakera759fe32015-11-16 22:51:02 -0800618 def save_instance(self, instance):
619 # Override this function to do custom pre-save or post-save processing,
620 # such as creating ports for containers.
621 instance.save()
Scott Baker5c125e42015-11-02 20:54:28 -0800622
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600623 def pick_least_loaded_instance_in_slice(self, slices):
624 for slice in slices:
625 if slice.instances.all().count() > 0:
626 for instance in slice.instances.all():
627 #Pick the first instance that has lesser than 5 tenants
628 if self.count_of_tenants_of_an_instance(instance) < 5:
629 return instance
630 return None
631
632 #TODO: Ideally the tenant count for an instance should be maintained using a
633 #many-to-one relationship attribute, however this model being proxy, it does
634 #not permit any new attributes to be defined. Find if any better solutions
635 def count_of_tenants_of_an_instance(self, instance):
636 tenant_count = 0
637 for tenant in self.get_tenant_objects().all():
638 if tenant.get_attribute("instance_id", None) == instance.id:
639 tenant_count += 1
640 return tenant_count
641
Scott Bakera759fe32015-11-16 22:51:02 -0800642 def manage_container(self):
Tony Mack32010062015-09-13 22:50:39 +0000643 from core.models import Instance, Flavor
Scott Bakerc1584b82015-09-09 16:36:06 -0700644
645 if self.deleted:
646 return
647
Tony Mack32010062015-09-13 22:50:39 +0000648 if (self.instance is not None) and (self.instance.image != self.image):
649 self.instance.delete()
650 self.instance = None
Scott Bakerc1584b82015-09-09 16:36:06 -0700651
Tony Mack32010062015-09-13 22:50:39 +0000652 if self.instance is None:
Scott Bakerc1584b82015-09-09 16:36:06 -0700653 if not self.provider_service.slices.count():
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600654 raise XOSConfigurationError("The service has no slices")
Scott Bakerc1584b82015-09-09 16:36:06 -0700655
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600656 new_instance_created = False
657 instance = None
658 if self.get_attribute("use_same_instance_for_multiple_tenants", default=False):
659 #Find if any existing instances can be used for this tenant
660 slices = self.provider_service.slices.all()
661 instance = self.pick_least_loaded_instance_in_slice(slices)
Scott Bakerc1584b82015-09-09 16:36:06 -0700662
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600663 if not instance:
664 flavors = Flavor.objects.filter(name="m1.small")
665 if not flavors:
666 raise XOSConfigurationError("No m1.small flavor")
667
Srikanth Vavilapalli3406fbb2015-11-17 13:41:38 -0600668 slice = self.provider_service.slices.all()[0]
Scott Bakera759fe32015-11-16 22:51:02 -0800669
Srikanth Vavilapalli3406fbb2015-11-17 13:41:38 -0600670 if slice.default_isolation == "container_vm":
Scott Bakerc8914bf2015-11-18 20:58:08 -0800671 (node, parent) = ContainerVmScheduler(slice).pick()
Srikanth Vavilapalli3406fbb2015-11-17 13:41:38 -0600672 else:
Scott Bakerc8914bf2015-11-18 20:58:08 -0800673 (node, parent) = LeastLoadedNodeScheduler(slice).pick()
Scott Bakera759fe32015-11-16 22:51:02 -0800674
Srikanth Vavilapalli3406fbb2015-11-17 13:41:38 -0600675 instance = Instance(slice = slice,
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600676 node = node,
677 image = self.image,
678 creator = self.creator,
679 deployment = node.site_deployment.deployment,
Srikanth Vavilapalli3406fbb2015-11-17 13:41:38 -0600680 flavor = flavors[0],
681 isolation = slice.default_isolation,
682 parent = parent)
683 self.save_instance(instance)
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600684 new_instance_created = True
Scott Bakerc1584b82015-09-09 16:36:06 -0700685
686 try:
Tony Mack32010062015-09-13 22:50:39 +0000687 self.instance = instance
Scott Bakerc1584b82015-09-09 16:36:06 -0700688 super(TenantWithContainer, self).save()
689 except:
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600690 if new_instance_created:
691 instance.delete()
Scott Bakerc1584b82015-09-09 16:36:06 -0700692 raise
693
694 def cleanup_container(self):
Tony Mack32010062015-09-13 22:50:39 +0000695 if self.instance:
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600696 if self.get_attribute("use_same_instance_for_multiple_tenants", default=False):
697 #Delete the instance only if this is last tenant in that instance
698 tenant_count = self.count_of_tenants_of_an_instance(self.instance)
699 if tenant_count == 0:
700 self.instance.delete()
701 else:
702 self.instance.delete()
Tony Mack32010062015-09-13 22:50:39 +0000703 self.instance = None
Scott Bakerb2385622015-07-06 14:27:31 -0700704
Scott Baker88fa6732015-12-10 23:23:07 -0800705 def save(self, *args, **kwargs):
706 if (not self.creator) and (hasattr(self, "caller")) and (self.caller):
707 self.creator = self.caller
708 super(TenantWithContainer, self).save(*args, **kwargs)
709
Scott Bakeref58a842015-04-26 20:30:40 -0700710class CoarseTenant(Tenant):
Scott Bakera86489f2015-07-01 18:29:08 -0700711 """ TODO: rename "CoarseTenant" --> "StaticTenant" """
Scott Bakeref58a842015-04-26 20:30:40 -0700712 class Meta:
713 proxy = True
Siobhan Tully00353f72013-10-08 21:53:27 -0400714
Scott Bakerc24f86d2015-08-14 09:10:11 -0700715 KIND = COARSE_KIND
Scott Bakeref58a842015-04-26 20:30:40 -0700716
717 def save(self, *args, **kwargs):
718 if (not self.subscriber_service):
719 raise XOSValidationError("subscriber_service cannot be null")
720 if (self.subscriber_tenant or self.subscriber_user):
721 raise XOSValidationError("subscriber_tenant and subscriber_user must be null")
722
723 super(CoarseTenant,self).save()
Scott Bakera86489f2015-07-01 18:29:08 -0700724
725class Subscriber(TenantRoot):
726 """ Intermediate class for TenantRoots that are to be Subscribers """
727
728 class Meta:
729 proxy = True
730
731 KIND = "Subscriber"
732
733class Provider(TenantRoot):
734 """ Intermediate class for TenantRoots that are to be Providers """
735
736 class Meta:
737 proxy = True
738
739 KIND = "Provider"
740
Scott Baker1e7e3482015-10-15 15:59:19 -0700741class TenantAttribute(PlCoreBase):
Scott Baker3ab4db82015-10-20 17:12:36 -0700742 name = models.CharField(help_text="Attribute Name", max_length=128)
Scott Baker1e7e3482015-10-15 15:59:19 -0700743 value = models.TextField(help_text="Attribute Value")
744 tenant = models.ForeignKey(Tenant, related_name='tenantattributes', help_text="The Tenant this attribute is associated with")
745
Scott Bakera86489f2015-07-01 18:29:08 -0700746class TenantRootRole(PlCoreBase):
Scott Baker1729e342015-07-24 15:48:03 -0700747 ROLE_CHOICES = (('admin','Admin'), ('access','Access'))
Scott Bakera86489f2015-07-01 18:29:08 -0700748
749 role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30)
750
751 def __unicode__(self): return u'%s' % (self.role)
752
753class TenantRootPrivilege(PlCoreBase):
754 user = models.ForeignKey('User', related_name="tenant_root_privileges")
755 tenant_root = models.ForeignKey('TenantRoot', related_name="tenant_root_privileges")
756 role = models.ForeignKey('TenantRootRole', related_name="tenant_root_privileges")
757
758 class Meta:
759 unique_together = ('user', 'tenant_root', 'role')
760
Scott Bakerb2385622015-07-06 14:27:31 -0700761 def __unicode__(self): return u'%s %s %s' % (self.tenant_root, self.user, self.role)
Scott Bakera86489f2015-07-01 18:29:08 -0700762
763 def save(self, *args, **kwds):
764 if not self.user.is_active:
765 raise PermissionDenied, "Cannot modify role(s) of a disabled user"
Scott Bakerc8e947a2015-07-24 10:15:31 -0700766 super(TenantRootPrivilege, self).save(*args, **kwds)
Scott Bakera86489f2015-07-01 18:29:08 -0700767
768 def can_update(self, user):
Scott Bakerc8e947a2015-07-24 10:15:31 -0700769 return user.can_update_tenant_root_privilege(self)
Scott Bakera86489f2015-07-01 18:29:08 -0700770
Scott Baker27de6012015-07-24 15:36:02 -0700771 @classmethod
772 def select_by_user(cls, user):
Scott Bakera86489f2015-07-01 18:29:08 -0700773 if user.is_admin:
Scott Baker1729e342015-07-24 15:48:03 -0700774 return cls.objects.all()
Scott Bakera86489f2015-07-01 18:29:08 -0700775 else:
Scott Baker1729e342015-07-24 15:48:03 -0700776 # User can see his own privilege
777 trp_ids = [trp.id for trp in cls.objects.filter(user=user)]
778
779 # A slice admin can see the SlicePrivileges for his Slice
780 for priv in cls.objects.filter(user=user, role__role="admin"):
781 trp_ids.extend( [trp.id for trp in cls.objects.filter(tenant_root=priv.tenant_root)] )
782
783 return cls.objects.filter(id__in=trp_ids)
784
Scott Bakerb2385622015-07-06 14:27:31 -0700785