blob: 4bac02c72045f06dece13091c24454fa3408bab9 [file] [log] [blame]
Scott Baker7211f5b2015-04-14 17:18:51 -07001import json
Jeremy Mowerya74c31d2016-04-04 22:30:44 -07002from operator import attrgetter
3
4from core.models import PlCoreBase, PlCoreBaseManager, SingletonModel
5from core.models.plcorebase import StrippedCharField
Jeremy Mowery61c012c2016-04-19 10:26:15 -07006from django.db import models
Jeremy Mowerya74c31d2016-04-04 22:30:44 -07007from xos.exceptions import *
Siobhan Tully00353f72013-10-08 21:53:27 -04008
Jeremy Moweryda57d402016-04-15 17:39:49 -07009COARSE_KIND = "coarse"
10
Scott Bakerc24f86d2015-08-14 09:10:11 -070011
Scott Baker9d1c6d92015-07-13 13:07:27 -070012class AttributeMixin(object):
Jeremy Moweryda57d402016-04-15 17:39:49 -070013 # helper for extracting things from a json-encoded
14 # service_specific_attribute
15
Scott Baker9d1c6d92015-07-13 13:07:27 -070016 def get_attribute(self, name, default=None):
17 if self.service_specific_attribute:
18 attributes = json.loads(self.service_specific_attribute)
19 else:
20 attributes = {}
21 return attributes.get(name, default)
22
23 def set_attribute(self, name, value):
24 if self.service_specific_attribute:
25 attributes = json.loads(self.service_specific_attribute)
26 else:
27 attributes = {}
Jeremy Moweryda57d402016-04-15 17:39:49 -070028 attributes[name] = value
Scott Baker9d1c6d92015-07-13 13:07:27 -070029 self.service_specific_attribute = json.dumps(attributes)
30
31 def get_initial_attribute(self, name, default=None):
32 if self._initial["service_specific_attribute"]:
Jeremy Moweryda57d402016-04-15 17:39:49 -070033 attributes = json.loads(
34 self._initial["service_specific_attribute"])
Scott Baker9d1c6d92015-07-13 13:07:27 -070035 else:
36 attributes = {}
37 return attributes.get(name, default)
38
Scott Bakereb098e62015-07-13 13:54:06 -070039 @classmethod
Scott Baker440d1152016-03-01 20:10:24 -080040 def get_default_attribute(cls, name):
41 for (attrname, default) in cls.simple_attributes:
Jeremy Moweryda57d402016-04-15 17:39:49 -070042 if attrname == name:
Scott Baker440d1152016-03-01 20:10:24 -080043 return default
Jeremy Moweryda57d402016-04-15 17:39:49 -070044 if hasattr(cls, "default_attributes"):
Scott Baker5d45b8a2016-03-31 11:46:02 -070045 if name in cls.default_attributes:
46 return cls.default_attributes[name]
47
48 return None
Scott Baker440d1152016-03-01 20:10:24 -080049
50 @classmethod
Scott Bakereb098e62015-07-13 13:54:06 -070051 def setup_simple_attributes(cls):
52 for (attrname, default) in cls.simple_attributes:
Scott Bakere2879d32015-07-13 14:27:51 -070053 setattr(cls, attrname, property(lambda self, attrname=attrname, default=default: self.get_attribute(attrname, default),
Jeremy Moweryda57d402016-04-15 17:39:49 -070054 lambda self, value, attrname=attrname: self.set_attribute(
55 attrname, value),
Scott Bakere2879d32015-07-13 14:27:51 -070056 None,
57 attrname))
Scott Bakereb098e62015-07-13 13:54:06 -070058
Jeremy Moweryda57d402016-04-15 17:39:49 -070059
Scott Baker9d1c6d92015-07-13 13:07:27 -070060class Service(PlCoreBase, AttributeMixin):
Scott Baker0d306722015-04-15 20:58:20 -070061 # when subclassing a service, redefine KIND to describe the new service
62 KIND = "generic"
63
Jeremy Moweryda57d402016-04-15 17:39:49 -070064 description = models.TextField(
65 max_length=254, null=True, blank=True, help_text="Description of Service")
Siobhan Tully00353f72013-10-08 21:53:27 -040066 enabled = models.BooleanField(default=True)
Jeremy Moweryda57d402016-04-15 17:39:49 -070067 kind = StrippedCharField(
68 max_length=30, help_text="Kind of service", default=KIND)
Tony Mack50e12212015-03-09 13:03:56 -040069 name = StrippedCharField(max_length=30, help_text="Service Name")
Scott Bakere332de52016-05-02 09:38:24 -070070 versionNumber = StrippedCharField(blank=True, null=True,
Jeremy Moweryda57d402016-04-15 17:39:49 -070071 max_length=30, help_text="Version of Service Definition")
Siobhan Tullycf04fb62014-01-11 11:25:57 -050072 published = models.BooleanField(default=True)
Tony Mack50e12212015-03-09 13:03:56 -040073 view_url = StrippedCharField(blank=True, null=True, max_length=1024)
74 icon_url = StrippedCharField(blank=True, null=True, max_length=1024)
Jeremy Moweryda57d402016-04-15 17:39:49 -070075 public_key = models.TextField(
76 null=True, blank=True, max_length=1024, help_text="Public key string")
Scott Bakerdc63fb32015-11-12 16:22:52 -080077 private_key_fn = StrippedCharField(blank=True, null=True, max_length=1024)
Siobhan Tully00353f72013-10-08 21:53:27 -040078
Scott Bakerb9040e92015-07-13 12:33:28 -070079 # Service_specific_attribute and service_specific_id are opaque to XOS
Jeremy Moweryda57d402016-04-15 17:39:49 -070080 service_specific_id = StrippedCharField(
81 max_length=30, blank=True, null=True)
Scott Bakerb9040e92015-07-13 12:33:28 -070082 service_specific_attribute = models.TextField(blank=True, null=True)
83
Scott Baker0d306722015-04-15 20:58:20 -070084 def __init__(self, *args, **kwargs):
85 # for subclasses, set the default kind appropriately
86 self._meta.get_field("kind").default = self.KIND
87 super(Service, self).__init__(*args, **kwargs)
88
89 @classmethod
90 def get_service_objects(cls):
Jeremy Moweryda57d402016-04-15 17:39:49 -070091 return cls.objects.filter(kind=cls.KIND)
Scott Baker0d306722015-04-15 20:58:20 -070092
Scott Baker27de6012015-07-24 15:36:02 -070093 @classmethod
Scott Bakercd32ad02015-10-19 21:18:53 -070094 def get_deleted_service_objects(cls):
Jeremy Moweryda57d402016-04-15 17:39:49 -070095 return cls.deleted_objects.filter(kind=cls.KIND)
Scott Bakercd32ad02015-10-19 21:18:53 -070096
97 @classmethod
Scott Baker27de6012015-07-24 15:36:02 -070098 def get_service_objects_by_user(cls, user):
Jeremy Moweryda57d402016-04-15 17:39:49 -070099 return cls.select_by_user(user).filter(kind=cls.KIND)
Scott Baker27de6012015-07-24 15:36:02 -0700100
101 @classmethod
102 def select_by_user(cls, user):
103 if user.is_admin:
104 return cls.objects.all()
105 else:
Jeremy Moweryda57d402016-04-15 17:39:49 -0700106 service_ids = [
107 sp.slice.id for sp in ServicePrivilege.objects.filter(user=user)]
Scott Baker27de6012015-07-24 15:36:02 -0700108 return cls.objects.filter(id__in=service_ids)
109
Scott Bakerbcea8cf2015-12-07 22:20:40 -0800110 @property
111 def serviceattribute_dict(self):
112 attrs = {}
113 for attr in self.serviceattributes.all():
114 attrs[attr.name] = attr.value
115 return attrs
116
Siobhan Tully00353f72013-10-08 21:53:27 -0400117 def __unicode__(self): return u'%s' % (self.name)
118
Tony Mack950b4492015-04-29 12:23:10 -0400119 def can_update(self, user):
120 return user.can_update_service(self, allow=['admin'])
Scott Bakerb2385622015-07-06 14:27:31 -0700121
Scott Baker25757222015-05-11 16:36:41 -0700122 def get_scalable_nodes(self, slice, max_per_node=None, exclusive_slices=[]):
123 """
124 Get a list of nodes that can be used to scale up a slice.
125
126 slice - slice to scale up
Tony Mackd8515472015-08-19 11:58:18 -0400127 max_per_node - maximum numbers of instances that 'slice' can have on a single node
Scott Baker25757222015-05-11 16:36:41 -0700128 exclusive_slices - list of slices that must have no nodes in common with 'slice'.
129 """
130
Jeremy Moweryda57d402016-04-15 17:39:49 -0700131 # late import to get around order-of-imports constraint in __init__.py
132 from core.models import Node, Instance
Scott Baker25757222015-05-11 16:36:41 -0700133
134 nodes = list(Node.objects.all())
135
Jeremy Moweryda57d402016-04-15 17:39:49 -0700136 conflicting_instances = Instance.objects.filter(
137 slice__in=exclusive_slices)
138 conflicting_nodes = Node.objects.filter(
139 instances__in=conflicting_instances)
Scott Baker25757222015-05-11 16:36:41 -0700140
141 nodes = [x for x in nodes if x not in conflicting_nodes]
142
Tony Mackd8515472015-08-19 11:58:18 -0400143 # If max_per_node is set, then limit the number of instances this slice
Scott Baker25757222015-05-11 16:36:41 -0700144 # can have on a single node.
145 if max_per_node:
146 acceptable_nodes = []
147 for node in nodes:
Tony Mackd8515472015-08-19 11:58:18 -0400148 existing_count = node.instances.filter(slice=slice).count()
Scott Baker25757222015-05-11 16:36:41 -0700149 if existing_count < max_per_node:
150 acceptable_nodes.append(node)
151 nodes = acceptable_nodes
152
153 return nodes
154
155 def pick_node(self, slice, max_per_node=None, exclusive_slices=[]):
156 # Pick the best node to scale up a slice.
157
158 nodes = self.get_scalable_nodes(slice, max_per_node, exclusive_slices)
Tony Mackd8515472015-08-19 11:58:18 -0400159 nodes = sorted(nodes, key=lambda node: node.instances.all().count())
Scott Baker25757222015-05-11 16:36:41 -0700160 if not nodes:
161 return None
162 return nodes[0]
163
164 def adjust_scale(self, slice_hint, scale, max_per_node=None, exclusive_slices=[]):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700165 # late import to get around order-of-imports constraint in __init__.py
166 from core.models import Instance
Scott Baker25757222015-05-11 16:36:41 -0700167
168 slices = [x for x in self.slices.all() if slice_hint in x.name]
169 for slice in slices:
Tony Mackd8515472015-08-19 11:58:18 -0400170 while slice.instances.all().count() > scale:
171 s = slice.instances.all()[0]
172 # print "drop instance", s
Scott Baker25757222015-05-11 16:36:41 -0700173 s.delete()
174
Tony Mackd8515472015-08-19 11:58:18 -0400175 while slice.instances.all().count() < scale:
Scott Baker25757222015-05-11 16:36:41 -0700176 node = self.pick_node(slice, max_per_node, exclusive_slices)
177 if not node:
178 # no more available nodes
179 break
180
181 image = slice.default_image
182 if not image:
Jeremy Moweryda57d402016-04-15 17:39:49 -0700183 raise XOSConfigurationError(
184 "No default_image for slice %s" % slice.name)
Scott Baker25757222015-05-11 16:36:41 -0700185
186 flavor = slice.default_flavor
187 if not flavor:
Jeremy Moweryda57d402016-04-15 17:39:49 -0700188 raise XOSConfigurationError(
189 "No default_flavor for slice %s" % slice.name)
Scott Baker25757222015-05-11 16:36:41 -0700190
Tony Mackd8515472015-08-19 11:58:18 -0400191 s = Instance(slice=slice,
Jeremy Moweryda57d402016-04-15 17:39:49 -0700192 node=node,
193 creator=slice.creator,
194 image=image,
195 flavor=flavor,
196 deployment=node.site_deployment.deployment)
Scott Baker25757222015-05-11 16:36:41 -0700197 s.save()
198
Tony Mackd8515472015-08-19 11:58:18 -0400199 # print "add instance", s
Tony Mack950b4492015-04-29 12:23:10 -0400200
Scott Bakercbf4c782015-12-09 22:54:52 -0800201 def get_vtn_src_nets(self):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700202 nets = []
Scott Bakercbf4c782015-12-09 22:54:52 -0800203 for slice in self.slices.all():
204 for ns in slice.networkslices.all():
205 if not ns.network:
206 continue
Scott Bakereb3bad92016-01-12 19:59:12 -0800207# if ns.network.template.access in ["direct", "indirect"]:
208# # skip access networks; we want to use the private network
209# continue
Scott Bakeracae4bd2016-04-11 21:26:39 -0700210 if "management" in ns.network.name:
211 # don't try to connect the management network to anything
212 continue
Scott Bakercbf4c782015-12-09 22:54:52 -0800213 if ns.network.name in ["wan_network", "lan_network"]:
214 # we don't want to attach to the vCPE's lan or wan network
215 # we only want to attach to its private network
216 # TODO: fix hard-coding of network name
217 continue
218 for cn in ns.network.controllernetworks.all():
219 if cn.net_id:
220 net = {"name": ns.network.name, "net_id": cn.net_id}
221 nets.append(net)
222 return nets
223
Scott Baker1b7d98b2015-12-08 21:31:18 -0800224 def get_vtn_nets(self):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700225 nets = []
Scott Baker1b7d98b2015-12-08 21:31:18 -0800226 for slice in self.slices.all():
227 for ns in slice.networkslices.all():
228 if not ns.network:
229 continue
Scott Bakercbf4c782015-12-09 22:54:52 -0800230 if ns.network.template.access not in ["direct", "indirect"]:
231 # skip anything that's not an access network
232 continue
Scott Baker1b7d98b2015-12-08 21:31:18 -0800233 for cn in ns.network.controllernetworks.all():
234 if cn.net_id:
235 net = {"name": ns.network.name, "net_id": cn.net_id}
236 nets.append(net)
237 return nets
238
239 def get_vtn_dependencies_nets(self):
240 provider_nets = []
Scott Baker012c54b2015-12-08 19:27:50 -0800241 for tenant in self.subscribed_tenants.all():
242 if tenant.provider_service:
Scott Baker1b7d98b2015-12-08 21:31:18 -0800243 for net in tenant.provider_service.get_vtn_nets():
244 if not net in provider_nets:
245 provider_nets.append(net)
246 return provider_nets
247
248 def get_vtn_dependencies_ids(self):
249 return [x["net_id"] for x in self.get_vtn_dependencies_nets()]
250
251 def get_vtn_dependencies_names(self):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700252 return [x["name"] + "_" + x["net_id"] for x in self.get_vtn_dependencies_nets()]
Scott Baker1b7d98b2015-12-08 21:31:18 -0800253
Scott Bakercbf4c782015-12-09 22:54:52 -0800254 def get_vtn_src_ids(self):
255 return [x["net_id"] for x in self.get_vtn_src_nets()]
Scott Baker1b7d98b2015-12-08 21:31:18 -0800256
Scott Bakercbf4c782015-12-09 22:54:52 -0800257 def get_vtn_src_names(self):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700258 return [x["name"] + "_" + x["net_id"] for x in self.get_vtn_src_nets()]
Scott Baker012c54b2015-12-08 19:27:50 -0800259
260
Siobhan Tully00353f72013-10-08 21:53:27 -0400261class ServiceAttribute(PlCoreBase):
Scott Bakerbcea8cf2015-12-07 22:20:40 -0800262 name = models.CharField(help_text="Attribute Name", max_length=128)
Tony Mack50e12212015-03-09 13:03:56 -0400263 value = StrippedCharField(help_text="Attribute Value", max_length=1024)
Jeremy Moweryda57d402016-04-15 17:39:49 -0700264 service = models.ForeignKey(Service, related_name='serviceattributes',
265 help_text="The Service this attribute is associated with")
266
Siobhan Tully00353f72013-10-08 21:53:27 -0400267
Tony Mack950b4492015-04-29 12:23:10 -0400268class ServiceRole(PlCoreBase):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700269 ROLE_CHOICES = (('admin', 'Admin'),)
Tony Mack950b4492015-04-29 12:23:10 -0400270 role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30)
271
Jeremy Moweryda57d402016-04-15 17:39:49 -0700272 def __unicode__(self): return u'%s' % (self.role)
273
Tony Mack950b4492015-04-29 12:23:10 -0400274
275class ServicePrivilege(PlCoreBase):
276 user = models.ForeignKey('User', related_name='serviceprivileges')
277 service = models.ForeignKey('Service', related_name='serviceprivileges')
Jeremy Moweryda57d402016-04-15 17:39:49 -0700278 role = models.ForeignKey('ServiceRole', related_name='serviceprivileges')
Tony Mack950b4492015-04-29 12:23:10 -0400279
280 class Meta:
Jeremy Moweryda57d402016-04-15 17:39:49 -0700281 unique_together = ('user', 'service', 'role')
Tony Mack950b4492015-04-29 12:23:10 -0400282
Jeremy Moweryda57d402016-04-15 17:39:49 -0700283 def __unicode__(self): return u'%s %s %s' % (
284 self.service, self.user, self.role)
Tony Mack950b4492015-04-29 12:23:10 -0400285
286 def can_update(self, user):
287 if not self.service.enabled:
288 raise PermissionDenied, "Cannot modify permission(s) of a disabled service"
289 return self.service.can_update(user)
290
291 def save(self, *args, **kwds):
292 if not self.service.enabled:
293 raise PermissionDenied, "Cannot modify permission(s) of a disabled service"
294 super(ServicePrivilege, self).save(*args, **kwds)
295
296 def delete(self, *args, **kwds):
297 if not self.service.enabled:
298 raise PermissionDenied, "Cannot modify permission(s) of a disabled service"
Scott Bakera86489f2015-07-01 18:29:08 -0700299 super(ServicePrivilege, self).delete(*args, **kwds)
300
Scott Baker27de6012015-07-24 15:36:02 -0700301 @classmethod
302 def select_by_user(cls, user):
Tony Mack950b4492015-04-29 12:23:10 -0400303 if user.is_admin:
Scott Baker27de6012015-07-24 15:36:02 -0700304 qs = cls.objects.all()
Tony Mack950b4492015-04-29 12:23:10 -0400305 else:
Scott Baker27de6012015-07-24 15:36:02 -0700306 qs = cls.objects.filter(user=user)
Scott Bakera86489f2015-07-01 18:29:08 -0700307 return qs
308
Jeremy Moweryda57d402016-04-15 17:39:49 -0700309
Scott Baker9d1c6d92015-07-13 13:07:27 -0700310class TenantRoot(PlCoreBase, AttributeMixin):
Scott Bakera86489f2015-07-01 18:29:08 -0700311 """ A tenantRoot is one of the things that can sit at the root of a chain
312 of tenancy. This object represents a node.
313 """
314
Jeremy Moweryda57d402016-04-15 17:39:49 -0700315 KIND = "generic"
Scott Bakera86489f2015-07-01 18:29:08 -0700316 kind = StrippedCharField(max_length=30, default=KIND)
Jeremy Moweryda57d402016-04-15 17:39:49 -0700317 name = StrippedCharField(
318 max_length=255, help_text="name", blank=True, null=True)
Scott Bakera86489f2015-07-01 18:29:08 -0700319
Scott Baker29415a82015-07-07 12:12:42 -0700320 service_specific_attribute = models.TextField(blank=True, null=True)
Jeremy Moweryda57d402016-04-15 17:39:49 -0700321 service_specific_id = StrippedCharField(
322 max_length=30, blank=True, null=True)
Scott Bakera86489f2015-07-01 18:29:08 -0700323
Scott Baker126ad472015-07-07 17:59:44 -0700324 def __init__(self, *args, **kwargs):
325 # for subclasses, set the default kind appropriately
326 self._meta.get_field("kind").default = self.KIND
327 super(TenantRoot, self).__init__(*args, **kwargs)
328
Scott Bakerb2385622015-07-06 14:27:31 -0700329 def __unicode__(self):
330 if not self.name:
331 return u"%s-tenant_root-#%s" % (str(self.kind), str(self.id))
332 else:
333 return self.name
334
335 def can_update(self, user):
336 return user.can_update_tenant_root(self, allow=['admin'])
337
Scott Baker29415a82015-07-07 12:12:42 -0700338 def get_subscribed_tenants(self, tenant_class):
339 ids = self.subscribed_tenants.filter(kind=tenant_class.KIND)
Jeremy Moweryda57d402016-04-15 17:39:49 -0700340 return tenant_class.objects.filter(id__in=ids)
Scott Baker29415a82015-07-07 12:12:42 -0700341
342 def get_newest_subscribed_tenant(self, kind):
343 st = list(self.get_subscribed_tenants(kind))
344 if not st:
345 return None
346 return sorted(st, key=attrgetter('id'))[0]
347
348 @classmethod
349 def get_tenant_objects(cls):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700350 return cls.objects.filter(kind=cls.KIND)
Scott Baker29415a82015-07-07 12:12:42 -0700351
Scott Baker27de6012015-07-24 15:36:02 -0700352 @classmethod
353 def get_tenant_objects_by_user(cls, user):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700354 return cls.select_by_user(user).filter(kind=cls.KIND)
Scott Baker27de6012015-07-24 15:36:02 -0700355
356 @classmethod
357 def select_by_user(cls, user):
358 if user.is_admin:
359 return cls.objects.all()
360 else:
Jeremy Moweryda57d402016-04-15 17:39:49 -0700361 tr_ids = [
362 trp.tenant_root.id for trp in TenantRootPrivilege.objects.filter(user=user)]
Scott Baker27de6012015-07-24 15:36:02 -0700363 return cls.objects.filter(id__in=tr_ids)
364
Jeremy Moweryda57d402016-04-15 17:39:49 -0700365 # helper function to be used in subclasses that want to ensure
366 # service_specific_id is unique
Scott Bakerf3735762016-03-31 14:45:31 -0700367 def validate_unique_service_specific_id(self, none_okay=False):
368 if not none_okay and (self.service_specific_id is None):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700369 raise XOSMissingField("subscriber_specific_id is None, and it's a required field", fields={
370 "service_specific_id": "cannot be none"})
Scott Bakerf3735762016-03-31 14:45:31 -0700371
372 if self.service_specific_id:
Jeremy Moweryda57d402016-04-15 17:39:49 -0700373 conflicts = self.get_tenant_objects().filter(
374 service_specific_id=self.service_specific_id)
Scott Bakerf3735762016-03-31 14:45:31 -0700375 if self.pk:
Scott Baker64a16582016-04-01 16:28:41 -0700376 conflicts = conflicts.exclude(pk=self.pk)
Scott Bakerf3735762016-03-31 14:45:31 -0700377 if conflicts:
Jeremy Moweryda57d402016-04-15 17:39:49 -0700378 raise XOSDuplicateKey("service_specific_id %s already exists" % self.service_specific_id, fields={
379 "service_specific_id": "duplicate key"})
380
Scott Bakerf3735762016-03-31 14:45:31 -0700381
Scott Baker9d1c6d92015-07-13 13:07:27 -0700382class Tenant(PlCoreBase, AttributeMixin):
Scott Baker91e85882015-04-10 16:42:26 -0700383 """ A tenant is a relationship between two entities, a subscriber and a
Scott Bakera86489f2015-07-01 18:29:08 -0700384 provider. This object represents an edge.
Scott Baker91e85882015-04-10 16:42:26 -0700385
386 The subscriber can be a User, a Service, or a Tenant.
387
388 The provider is always a Service.
Scott Bakera86489f2015-07-01 18:29:08 -0700389
390 TODO: rename "Tenant" to "Tenancy"
Scott Baker91e85882015-04-10 16:42:26 -0700391 """
Scott Baker0d306722015-04-15 20:58:20 -0700392
Jeremy Moweryda57d402016-04-15 17:39:49 -0700393 CONNECTIVITY_CHOICES = (('public', 'Public'),
394 ('private', 'Private'), ('na', 'Not Applicable'))
Scott Bakeref58a842015-04-26 20:30:40 -0700395
Scott Baker0d306722015-04-15 20:58:20 -0700396 # when subclassing a service, redefine KIND to describe the new service
397 KIND = "generic"
398
399 kind = StrippedCharField(max_length=30, default=KIND)
Jeremy Moweryda57d402016-04-15 17:39:49 -0700400 provider_service = models.ForeignKey(
401 Service, related_name='provided_tenants')
Scott Bakera86489f2015-07-01 18:29:08 -0700402
403 # The next four things are the various type of objects that can be subscribers of this Tenancy
404 # relationship. One and only one can be used at a time.
Scott Baker5d268e52016-04-13 16:56:39 -0700405 # XXX these should really be changed to GenericForeignKey
Jeremy Moweryda57d402016-04-15 17:39:49 -0700406 subscriber_service = models.ForeignKey(
407 Service, related_name='subscribed_tenants', blank=True, null=True)
408 subscriber_tenant = models.ForeignKey(
409 "Tenant", related_name='subscribed_tenants', blank=True, null=True)
410 subscriber_user = models.ForeignKey(
411 "User", related_name='subscribed_tenants', blank=True, null=True)
412 subscriber_root = models.ForeignKey(
413 "TenantRoot", related_name="subscribed_tenants", blank=True, null=True)
414 subscriber_network = models.ForeignKey(
415 "Network", related_name="subscribed_tenants", blank=True, null=True)
Scott Bakera86489f2015-07-01 18:29:08 -0700416
417 # Service_specific_attribute and service_specific_id are opaque to XOS
Jeremy Moweryda57d402016-04-15 17:39:49 -0700418 service_specific_id = StrippedCharField(
419 max_length=30, blank=True, null=True)
Scott Baker1b7c6f12015-05-06 19:49:31 -0700420 service_specific_attribute = models.TextField(blank=True, null=True)
Scott Bakera86489f2015-07-01 18:29:08 -0700421
422 # Connect_method is only used by Coarse tenants
Jeremy Moweryda57d402016-04-15 17:39:49 -0700423 connect_method = models.CharField(
424 null=False, blank=False, max_length=30, choices=CONNECTIVITY_CHOICES, default="na")
Scott Baker91e85882015-04-10 16:42:26 -0700425
Scott Baker0d306722015-04-15 20:58:20 -0700426 def __init__(self, *args, **kwargs):
427 # for subclasses, set the default kind appropriately
428 self._meta.get_field("kind").default = self.KIND
429 super(Tenant, self).__init__(*args, **kwargs)
430
Scott Baker91e85882015-04-10 16:42:26 -0700431 def __unicode__(self):
Scott Bakerfe91f622015-05-20 20:42:04 -0700432 return u"%s-tenant-%s" % (str(self.kind), str(self.id))
Scott Baker91e85882015-04-10 16:42:26 -0700433
Scott Baker0d306722015-04-15 20:58:20 -0700434 @classmethod
435 def get_tenant_objects(cls):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700436 return cls.objects.filter(kind=cls.KIND)
Scott Baker0d306722015-04-15 20:58:20 -0700437
Scott Bakereb50ee32015-05-05 17:52:03 -0700438 @classmethod
Scott Baker27de6012015-07-24 15:36:02 -0700439 def get_tenant_objects_by_user(cls, user):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700440 return cls.select_by_user(user).filter(kind=cls.KIND)
Scott Baker27de6012015-07-24 15:36:02 -0700441
442 @classmethod
Scott Bakereb50ee32015-05-05 17:52:03 -0700443 def get_deleted_tenant_objects(cls):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700444 return cls.deleted_objects.filter(kind=cls.KIND)
Scott Bakereb50ee32015-05-05 17:52:03 -0700445
Scott Bakerbcea8cf2015-12-07 22:20:40 -0800446 @property
447 def tenantattribute_dict(self):
448 attrs = {}
449 for attr in self.tenantattributes.all():
450 attrs[attr.name] = attr.value
451 return attrs
452
Jeremy Moweryda57d402016-04-15 17:39:49 -0700453 # helper function to be used in subclasses that want to ensure
454 # service_specific_id is unique
Scott Baker7f8ef8f2015-04-20 14:24:29 -0700455 def validate_unique_service_specific_id(self):
456 if self.pk is None:
457 if self.service_specific_id is None:
Jeremy Moweryda57d402016-04-15 17:39:49 -0700458 raise XOSMissingField("subscriber_specific_id is None, and it's a required field", fields={
459 "service_specific_id": "cannot be none"})
Scott Baker7f8ef8f2015-04-20 14:24:29 -0700460
Jeremy Moweryda57d402016-04-15 17:39:49 -0700461 conflicts = self.get_tenant_objects().filter(
462 service_specific_id=self.service_specific_id)
Scott Baker7f8ef8f2015-04-20 14:24:29 -0700463 if conflicts:
Jeremy Moweryda57d402016-04-15 17:39:49 -0700464 raise XOSDuplicateKey("service_specific_id %s already exists" % self.service_specific_id, fields={
465 "service_specific_id": "duplicate key"})
Scott Baker7f8ef8f2015-04-20 14:24:29 -0700466
Scott Bakerb2385622015-07-06 14:27:31 -0700467 def save(self, *args, **kwargs):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700468 subCount = sum([1 for e in [self.subscriber_service, self.subscriber_tenant,
469 self.subscriber_user, self.subscriber_root] if e is not None])
Scott Bakerb2385622015-07-06 14:27:31 -0700470 if (subCount > 1):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700471 raise XOSConflictingField(
472 "Only one of subscriber_service, subscriber_tenant, subscriber_user, subscriber_root should be set")
Scott Bakerb2385622015-07-06 14:27:31 -0700473
474 super(Tenant, self).save(*args, **kwargs)
475
476 def get_subscribed_tenants(self, tenant_class):
477 ids = self.subscribed_tenants.filter(kind=tenant_class.KIND)
Jeremy Moweryda57d402016-04-15 17:39:49 -0700478 return tenant_class.objects.filter(id__in=ids)
Scott Bakerb2385622015-07-06 14:27:31 -0700479
480 def get_newest_subscribed_tenant(self, kind):
481 st = list(self.get_subscribed_tenants(kind))
482 if not st:
483 return None
484 return sorted(st, key=attrgetter('id'))[0]
485
Jeremy Moweryda57d402016-04-15 17:39:49 -0700486
Scott Bakerc8914bf2015-11-18 20:58:08 -0800487class Scheduler(object):
488 # XOS Scheduler Abstract Base Class
489 # Used to implement schedulers that pick which node to put instances on
490
491 def __init__(self, slice):
492 self.slice = slice
493
494 def pick(self):
495 # this method should return a tuple (node, parent)
496 # node is the node to instantiate on
497 # parent is for container_vm instances only, and is the VM that will
498 # hold the container
499
500 raise Exception("Abstract Base")
501
Jeremy Moweryda57d402016-04-15 17:39:49 -0700502
Scott Bakerc8914bf2015-11-18 20:58:08 -0800503class LeastLoadedNodeScheduler(Scheduler):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700504 # This scheduler always return the node with the fewest number of
505 # instances.
Scott Bakerc8914bf2015-11-18 20:58:08 -0800506
Scott Baker67074ab2016-03-04 11:29:02 -0800507 def __init__(self, slice, label=None):
Scott Bakerc8914bf2015-11-18 20:58:08 -0800508 super(LeastLoadedNodeScheduler, self).__init__(slice)
Scott Baker67074ab2016-03-04 11:29:02 -0800509 self.label = label
Scott Bakerc8914bf2015-11-18 20:58:08 -0800510
511 def pick(self):
512 from core.models import Node
Scott Baker67074ab2016-03-04 11:29:02 -0800513 nodes = Node.objects.all()
514
515 if self.label:
Jeremy Moweryda57d402016-04-15 17:39:49 -0700516 nodes = nodes.filter(nodelabels__name=self.label)
Scott Baker67074ab2016-03-04 11:29:02 -0800517
518 nodes = list(nodes)
519
520 if not nodes:
Jeremy Moweryda57d402016-04-15 17:39:49 -0700521 raise Exception(
522 "LeastLoadedNodeScheduler: No suitable nodes to pick from")
Scott Bakerbcea8cf2015-12-07 22:20:40 -0800523
Scott Bakerc8914bf2015-11-18 20:58:08 -0800524 # TODO: logic to filter nodes by which nodes are up, and which
525 # nodes the slice can instantiate on.
526 nodes = sorted(nodes, key=lambda node: node.instances.all().count())
527 return [nodes[0], None]
528
Jeremy Moweryda57d402016-04-15 17:39:49 -0700529
Scott Bakerc8914bf2015-11-18 20:58:08 -0800530class ContainerVmScheduler(Scheduler):
531 # This scheduler picks a VM in the slice with the fewest containers inside
532 # of it. If no VMs are suitable, then it creates a VM.
533
534 # this is a hack and should be replaced by something smarter...
Jeremy Moweryda57d402016-04-15 17:39:49 -0700535 LOOK_FOR_IMAGES = ["ubuntu-vcpe4", # ONOS demo machine -- preferred vcpe image
536 "Ubuntu 14.04 LTS", # portal
537 "Ubuntu-14.04-LTS", # ONOS demo machine
538 "trusty-server-multi-nic", # CloudLab
539 ]
Scott Bakerc8914bf2015-11-18 20:58:08 -0800540
541 MAX_VM_PER_CONTAINER = 10
542
543 def __init__(self, slice):
544 super(ContainerVmScheduler, self).__init__(slice)
545
546 @property
547 def image(self):
548 from core.models import Image
549
550 look_for_images = self.LOOK_FOR_IMAGES
551 for image_name in look_for_images:
Jeremy Moweryda57d402016-04-15 17:39:49 -0700552 images = Image.objects.filter(name=image_name)
Scott Bakerc8914bf2015-11-18 20:58:08 -0800553 if images:
554 return images[0]
555
Jeremy Moweryda57d402016-04-15 17:39:49 -0700556 raise XOSProgrammingError(
557 "No ContainerVM image (looked for %s)" % str(look_for_images))
Scott Bakerc8914bf2015-11-18 20:58:08 -0800558
559 def make_new_instance(self):
560 from core.models import Instance, Flavor
561
562 flavors = Flavor.objects.filter(name="m1.small")
563 if not flavors:
564 raise XOSConfigurationError("No m1.small flavor")
565
Jeremy Moweryda57d402016-04-15 17:39:49 -0700566 (node, parent) = LeastLoadedNodeScheduler(self.slice).pick()
Scott Bakerc8914bf2015-11-18 20:58:08 -0800567
Jeremy Moweryda57d402016-04-15 17:39:49 -0700568 instance = Instance(slice=self.slice,
569 node=node,
570 image=self.image,
571 creator=self.slice.creator,
572 deployment=node.site_deployment.deployment,
573 flavor=flavors[0],
574 isolation="vm",
575 parent=parent)
Scott Bakerc8914bf2015-11-18 20:58:08 -0800576 instance.save()
577 # We rely on a special naming convention to identify the VMs that will
578 # hole containers.
579 instance.name = "%s-outer-%s" % (instance.slice.name, instance.id)
580 instance.save()
581 return instance
582
583 def pick(self):
584 from core.models import Instance, Flavor
585
586 for vm in self.slice.instances.filter(isolation="vm"):
587 avail_vms = []
588 if (vm.name.startswith("%s-outer-" % self.slice.name)):
589 container_count = Instance.objects.filter(parent=vm).count()
590 if (container_count < self.MAX_VM_PER_CONTAINER):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700591 avail_vms.append((vm, container_count))
Scott Bakerc8914bf2015-11-18 20:58:08 -0800592 # sort by least containers-per-vm
Jeremy Moweryda57d402016-04-15 17:39:49 -0700593 avail_vms = sorted(avail_vms, key=lambda x: x[1])
Scott Bakerc8914bf2015-11-18 20:58:08 -0800594 print "XXX", avail_vms
595 if avail_vms:
596 instance = avail_vms[0][0]
597 return (instance.node, instance)
598
599 instance = self.make_new_instance()
600 return (instance.node, instance)
601
Jeremy Moweryda57d402016-04-15 17:39:49 -0700602
Scott Bakerc1584b82015-09-09 16:36:06 -0700603class TenantWithContainer(Tenant):
604 """ A tenant that manages a container """
605
606 # this is a hack and should be replaced by something smarter...
Jeremy Moweryda57d402016-04-15 17:39:49 -0700607 LOOK_FOR_IMAGES = ["ubuntu-vcpe4", # ONOS demo machine -- preferred vcpe image
608 "Ubuntu 14.04 LTS", # portal
609 "Ubuntu-14.04-LTS", # ONOS demo machine
610 "trusty-server-multi-nic", # CloudLab
611 ]
Scott Bakerc1584b82015-09-09 16:36:06 -0700612
Jeremy Moweryda57d402016-04-15 17:39:49 -0700613 LOOK_FOR_CONTAINER_IMAGES = ["docker-vcpe"]
Scott Bakera759fe32015-11-16 22:51:02 -0800614
Scott Bakerc1584b82015-09-09 16:36:06 -0700615 class Meta:
616 proxy = True
617
618 def __init__(self, *args, **kwargs):
619 super(TenantWithContainer, self).__init__(*args, **kwargs)
Jeremy Moweryda57d402016-04-15 17:39:49 -0700620 self.cached_instance = None
Tony Mack32010062015-09-13 22:50:39 +0000621 self.orig_instance_id = self.get_initial_attribute("instance_id")
Scott Baker5c125e42015-11-02 20:54:28 -0800622
Scott Bakerc1584b82015-09-09 16:36:06 -0700623 @property
Tony Mack32010062015-09-13 22:50:39 +0000624 def instance(self):
625 from core.models import Instance
626 if getattr(self, "cached_instance", None):
627 return self.cached_instance
Jeremy Moweryda57d402016-04-15 17:39:49 -0700628 instance_id = self.get_attribute("instance_id")
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600629 if not instance_id:
630 return None
Jeremy Moweryda57d402016-04-15 17:39:49 -0700631 instances = Instance.objects.filter(id=instance_id)
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600632 if not instances:
633 return None
Jeremy Moweryda57d402016-04-15 17:39:49 -0700634 instance = instances[0]
Tony Mack32010062015-09-13 22:50:39 +0000635 instance.caller = self.creator
636 self.cached_instance = instance
637 return instance
Scott Bakerc1584b82015-09-09 16:36:06 -0700638
Tony Mack32010062015-09-13 22:50:39 +0000639 @instance.setter
640 def instance(self, value):
Scott Bakerc1584b82015-09-09 16:36:06 -0700641 if value:
642 value = value.id
Tony Mack32010062015-09-13 22:50:39 +0000643 if (value != self.get_attribute("instance_id", None)):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700644 self.cached_instance = None
Tony Mack32010062015-09-13 22:50:39 +0000645 self.set_attribute("instance_id", value)
Scott Bakerc1584b82015-09-09 16:36:06 -0700646
Scott Baker5c125e42015-11-02 20:54:28 -0800647 @property
Scott Baker268e2aa2016-02-10 12:23:53 -0800648 def external_hostname(self):
649 return self.get_attribute("external_hostname", "")
650
651 @external_hostname.setter
652 def external_hostname(self, value):
653 self.set_attribute("external_hostname", value)
654
655 @property
656 def external_container(self):
657 return self.get_attribute("external_container", "")
658
659 @external_container.setter
660 def external_container(self, value):
661 self.set_attribute("external_container", value)
662
663 @property
Scott Bakerc1584b82015-09-09 16:36:06 -0700664 def creator(self):
665 from core.models import User
666 if getattr(self, "cached_creator", None):
667 return self.cached_creator
Jeremy Moweryda57d402016-04-15 17:39:49 -0700668 creator_id = self.get_attribute("creator_id")
Scott Bakerc1584b82015-09-09 16:36:06 -0700669 if not creator_id:
670 return None
Jeremy Moweryda57d402016-04-15 17:39:49 -0700671 users = User.objects.filter(id=creator_id)
Scott Bakerc1584b82015-09-09 16:36:06 -0700672 if not users:
673 return None
Jeremy Moweryda57d402016-04-15 17:39:49 -0700674 user = users[0]
Scott Bakerc1584b82015-09-09 16:36:06 -0700675 self.cached_creator = users[0]
676 return user
677
678 @creator.setter
679 def creator(self, value):
680 if value:
681 value = value.id
682 if (value != self.get_attribute("creator_id", None)):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700683 self.cached_creator = None
Scott Bakerc1584b82015-09-09 16:36:06 -0700684 self.set_attribute("creator_id", value)
685
686 @property
687 def image(self):
688 from core.models import Image
689 # Implement the logic here to pick the image that should be used when
690 # instantiating the VM that will hold the container.
Scott Bakera759fe32015-11-16 22:51:02 -0800691
692 slice = self.provider_service.slices.all()
693 if not slice:
694 raise XOSProgrammingError("provider service has no slice")
695 slice = slice[0]
696
697 if slice.default_isolation in ["container", "container_vm"]:
698 look_for_images = self.LOOK_FOR_CONTAINER_IMAGES
699 else:
700 look_for_images = self.LOOK_FOR_IMAGES
701
702 for image_name in look_for_images:
Jeremy Moweryda57d402016-04-15 17:39:49 -0700703 images = Image.objects.filter(name=image_name)
Scott Bakerc1584b82015-09-09 16:36:06 -0700704 if images:
705 return images[0]
706
Jeremy Moweryda57d402016-04-15 17:39:49 -0700707 raise XOSProgrammingError(
708 "No VPCE image (looked for %s)" % str(look_for_images))
Scott Baker5c125e42015-11-02 20:54:28 -0800709
Scott Bakera759fe32015-11-16 22:51:02 -0800710 def save_instance(self, instance):
711 # Override this function to do custom pre-save or post-save processing,
712 # such as creating ports for containers.
713 instance.save()
Scott Baker5c125e42015-11-02 20:54:28 -0800714
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600715 def pick_least_loaded_instance_in_slice(self, slices):
716 for slice in slices:
717 if slice.instances.all().count() > 0:
718 for instance in slice.instances.all():
Jeremy Moweryda57d402016-04-15 17:39:49 -0700719 # Pick the first instance that has lesser than 5 tenants
720 if self.count_of_tenants_of_an_instance(instance) < 5:
721 return instance
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600722 return None
723
Jeremy Moweryda57d402016-04-15 17:39:49 -0700724 # TODO: Ideally the tenant count for an instance should be maintained using a
725 # many-to-one relationship attribute, however this model being proxy, it does
726 # not permit any new attributes to be defined. Find if any better solutions
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600727 def count_of_tenants_of_an_instance(self, instance):
728 tenant_count = 0
729 for tenant in self.get_tenant_objects().all():
730 if tenant.get_attribute("instance_id", None) == instance.id:
731 tenant_count += 1
732 return tenant_count
733
Scott Bakera759fe32015-11-16 22:51:02 -0800734 def manage_container(self):
Tony Mack32010062015-09-13 22:50:39 +0000735 from core.models import Instance, Flavor
Scott Bakerc1584b82015-09-09 16:36:06 -0700736
737 if self.deleted:
738 return
739
Tony Mack32010062015-09-13 22:50:39 +0000740 if (self.instance is not None) and (self.instance.image != self.image):
741 self.instance.delete()
742 self.instance = None
Scott Bakerc1584b82015-09-09 16:36:06 -0700743
Tony Mack32010062015-09-13 22:50:39 +0000744 if self.instance is None:
Scott Bakerc1584b82015-09-09 16:36:06 -0700745 if not self.provider_service.slices.count():
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600746 raise XOSConfigurationError("The service has no slices")
Scott Bakerc1584b82015-09-09 16:36:06 -0700747
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600748 new_instance_created = False
749 instance = None
750 if self.get_attribute("use_same_instance_for_multiple_tenants", default=False):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700751 # Find if any existing instances can be used for this tenant
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600752 slices = self.provider_service.slices.all()
753 instance = self.pick_least_loaded_instance_in_slice(slices)
Scott Bakerc1584b82015-09-09 16:36:06 -0700754
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600755 if not instance:
Srikanth Vavilapalli3406fbb2015-11-17 13:41:38 -0600756 slice = self.provider_service.slices.all()[0]
Scott Bakera759fe32015-11-16 22:51:02 -0800757
Srikanth Vavilapalli204a5c42016-02-15 01:18:09 -0500758 flavor = slice.default_flavor
759 if not flavor:
760 flavors = Flavor.objects.filter(name="m1.small")
761 if not flavors:
762 raise XOSConfigurationError("No m1.small flavor")
763 flavor = flavors[0]
764
Srikanth Vavilapalli3406fbb2015-11-17 13:41:38 -0600765 if slice.default_isolation == "container_vm":
Scott Bakerc8914bf2015-11-18 20:58:08 -0800766 (node, parent) = ContainerVmScheduler(slice).pick()
Srikanth Vavilapalli3406fbb2015-11-17 13:41:38 -0600767 else:
Scott Bakerc8914bf2015-11-18 20:58:08 -0800768 (node, parent) = LeastLoadedNodeScheduler(slice).pick()
Scott Bakera759fe32015-11-16 22:51:02 -0800769
Jeremy Moweryda57d402016-04-15 17:39:49 -0700770 instance = Instance(slice=slice,
771 node=node,
772 image=self.image,
773 creator=self.creator,
774 deployment=node.site_deployment.deployment,
775 flavor=flavor,
776 isolation=slice.default_isolation,
777 parent=parent)
Srikanth Vavilapalli3406fbb2015-11-17 13:41:38 -0600778 self.save_instance(instance)
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600779 new_instance_created = True
Scott Bakerc1584b82015-09-09 16:36:06 -0700780
781 try:
Tony Mack32010062015-09-13 22:50:39 +0000782 self.instance = instance
Scott Bakerc1584b82015-09-09 16:36:06 -0700783 super(TenantWithContainer, self).save()
784 except:
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600785 if new_instance_created:
786 instance.delete()
Scott Bakerc1584b82015-09-09 16:36:06 -0700787 raise
788
789 def cleanup_container(self):
Tony Mack32010062015-09-13 22:50:39 +0000790 if self.instance:
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600791 if self.get_attribute("use_same_instance_for_multiple_tenants", default=False):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700792 # Delete the instance only if this is last tenant in that
793 # instance
794 tenant_count = self.count_of_tenants_of_an_instance(
795 self.instance)
Srikanth Vavilapalli17b5a3c2015-11-17 12:21:02 -0600796 if tenant_count == 0:
797 self.instance.delete()
798 else:
799 self.instance.delete()
Tony Mack32010062015-09-13 22:50:39 +0000800 self.instance = None
Scott Bakerb2385622015-07-06 14:27:31 -0700801
Scott Baker88fa6732015-12-10 23:23:07 -0800802 def save(self, *args, **kwargs):
803 if (not self.creator) and (hasattr(self, "caller")) and (self.caller):
804 self.creator = self.caller
805 super(TenantWithContainer, self).save(*args, **kwargs)
806
Jeremy Moweryda57d402016-04-15 17:39:49 -0700807
Scott Bakeref58a842015-04-26 20:30:40 -0700808class CoarseTenant(Tenant):
Scott Bakera86489f2015-07-01 18:29:08 -0700809 """ TODO: rename "CoarseTenant" --> "StaticTenant" """
Scott Bakeref58a842015-04-26 20:30:40 -0700810 class Meta:
811 proxy = True
Siobhan Tully00353f72013-10-08 21:53:27 -0400812
Scott Bakerc24f86d2015-08-14 09:10:11 -0700813 KIND = COARSE_KIND
Scott Bakeref58a842015-04-26 20:30:40 -0700814
815 def save(self, *args, **kwargs):
816 if (not self.subscriber_service):
817 raise XOSValidationError("subscriber_service cannot be null")
818 if (self.subscriber_tenant or self.subscriber_user):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700819 raise XOSValidationError(
820 "subscriber_tenant and subscriber_user must be null")
Scott Bakeref58a842015-04-26 20:30:40 -0700821
Jeremy Moweryda57d402016-04-15 17:39:49 -0700822 super(CoarseTenant, self).save()
823
Scott Bakera86489f2015-07-01 18:29:08 -0700824
825class Subscriber(TenantRoot):
826 """ Intermediate class for TenantRoots that are to be Subscribers """
827
828 class Meta:
829 proxy = True
830
831 KIND = "Subscriber"
832
Jeremy Moweryda57d402016-04-15 17:39:49 -0700833
Scott Bakera86489f2015-07-01 18:29:08 -0700834class Provider(TenantRoot):
835 """ Intermediate class for TenantRoots that are to be Providers """
836
837 class Meta:
838 proxy = True
839
840 KIND = "Provider"
841
Jeremy Moweryda57d402016-04-15 17:39:49 -0700842
Scott Baker1e7e3482015-10-15 15:59:19 -0700843class TenantAttribute(PlCoreBase):
Scott Baker3ab4db82015-10-20 17:12:36 -0700844 name = models.CharField(help_text="Attribute Name", max_length=128)
Scott Baker1e7e3482015-10-15 15:59:19 -0700845 value = models.TextField(help_text="Attribute Value")
Jeremy Moweryda57d402016-04-15 17:39:49 -0700846 tenant = models.ForeignKey(Tenant, related_name='tenantattributes',
847 help_text="The Tenant this attribute is associated with")
Scott Baker1e7e3482015-10-15 15:59:19 -0700848
Scott Bakerb0955d92016-04-06 14:34:49 -0700849 def __unicode__(self): return u'%s-%s' % (self.name, self.id)
850
Jeremy Moweryda57d402016-04-15 17:39:49 -0700851
Scott Bakera86489f2015-07-01 18:29:08 -0700852class TenantRootRole(PlCoreBase):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700853 ROLE_CHOICES = (('admin', 'Admin'), ('access', 'Access'))
Scott Bakera86489f2015-07-01 18:29:08 -0700854
855 role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30)
856
Jeremy Moweryda57d402016-04-15 17:39:49 -0700857 def __unicode__(self): return u'%s' % (self.role)
858
Scott Bakera86489f2015-07-01 18:29:08 -0700859
860class TenantRootPrivilege(PlCoreBase):
861 user = models.ForeignKey('User', related_name="tenant_root_privileges")
Jeremy Moweryda57d402016-04-15 17:39:49 -0700862 tenant_root = models.ForeignKey(
863 'TenantRoot', related_name="tenant_root_privileges")
864 role = models.ForeignKey(
865 'TenantRootRole', related_name="tenant_root_privileges")
Scott Bakera86489f2015-07-01 18:29:08 -0700866
867 class Meta:
868 unique_together = ('user', 'tenant_root', 'role')
869
Jeremy Moweryda57d402016-04-15 17:39:49 -0700870 def __unicode__(self): return u'%s %s %s' % (
871 self.tenant_root, self.user, self.role)
Scott Bakera86489f2015-07-01 18:29:08 -0700872
873 def save(self, *args, **kwds):
874 if not self.user.is_active:
875 raise PermissionDenied, "Cannot modify role(s) of a disabled user"
Scott Bakerc8e947a2015-07-24 10:15:31 -0700876 super(TenantRootPrivilege, self).save(*args, **kwds)
Scott Bakera86489f2015-07-01 18:29:08 -0700877
878 def can_update(self, user):
Scott Bakerc8e947a2015-07-24 10:15:31 -0700879 return user.can_update_tenant_root_privilege(self)
Scott Bakera86489f2015-07-01 18:29:08 -0700880
Scott Baker27de6012015-07-24 15:36:02 -0700881 @classmethod
882 def select_by_user(cls, user):
Scott Bakera86489f2015-07-01 18:29:08 -0700883 if user.is_admin:
Scott Baker1729e342015-07-24 15:48:03 -0700884 return cls.objects.all()
Scott Bakera86489f2015-07-01 18:29:08 -0700885 else:
Scott Baker1729e342015-07-24 15:48:03 -0700886 # User can see his own privilege
887 trp_ids = [trp.id for trp in cls.objects.filter(user=user)]
888
889 # A slice admin can see the SlicePrivileges for his Slice
890 for priv in cls.objects.filter(user=user, role__role="admin"):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700891 trp_ids.extend(
892 [trp.id for trp in cls.objects.filter(tenant_root=priv.tenant_root)])
Scott Baker1729e342015-07-24 15:48:03 -0700893
894 return cls.objects.filter(id__in=trp_ids)
895
Jeremy Moweryda57d402016-04-15 17:39:49 -0700896
Jeremy Mowery752dc502016-03-14 23:59:11 -0700897class TenantRole(PlCoreBase):
Jeremy Mowery308e8f02016-04-15 00:05:27 -0700898 """A TenantRole option."""
Jeremy Moweryda57d402016-04-15 17:39:49 -0700899 ROLE_CHOICES = (('admin', 'Admin'), ('access', 'Access'))
Jeremy Mowery752dc502016-03-14 23:59:11 -0700900 role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30)
Jeremy Moweryda57d402016-04-15 17:39:49 -0700901
902 def __unicode__(self): return u'%s' % (self.role)
903
Jeremy Mowery752dc502016-03-14 23:59:11 -0700904
905class TenantPrivilege(PlCoreBase):
Jeremy Mowery308e8f02016-04-15 00:05:27 -0700906 """"A TenantPrivilege which defines how users can access a particular Tenant.
907
908 Attributes:
909 id (models.AutoField): The ID of the privilege.
910 user (models.ForeignKey): A Foreign Key to the a User.
911 tenant (models.ForeignKey): A ForeignKey to the Tenant.
912 role (models.ForeignKey): A ForeignKey to the TenantRole.
913 """
Jeremy Mowery05d16d42016-04-10 23:00:54 -0700914 id = models.AutoField(primary_key=True)
Jeremy Mowerya74c31d2016-04-04 22:30:44 -0700915 user = models.ForeignKey('User', related_name="tenantprivileges")
916 tenant = models.ForeignKey('Tenant', related_name="tenantprivileges")
917 role = models.ForeignKey('TenantRole', related_name="tenantprivileges")
Jeremy Mowery752dc502016-03-14 23:59:11 -0700918
Jeremy Moweryda57d402016-04-15 17:39:49 -0700919 def __unicode__(self): return u'%s %s %s' % (
920 self.tenant, self.user, self.role)
Jeremy Mowery752dc502016-03-14 23:59:11 -0700921
922 def save(self, *args, **kwds):
923 if not self.user.is_active:
924 raise PermissionDenied, "Cannot modify role(s) of a disabled user"
925 super(TenantPrivilege, self).save(*args, **kwds)
926
927 def can_update(self, user):
928 return user.can_update_tenant_privilege(self)
929
930 @classmethod
931 def select_by_user(cls, user):
932 if user.is_admin:
933 return cls.objects.all()
934 else:
935 # User can see his own privilege
936 trp_ids = [trp.id for trp in cls.objects.filter(user=user)]
937
938 # A tenant admin can see the TenantPrivileges for their Tenants
939 for priv in cls.objects.filter(user=user, role__role="admin"):
Jeremy Moweryda57d402016-04-15 17:39:49 -0700940 trp_ids.extend(
941 [trp.id for trp in cls.objects.filter(tenant=priv.tenant)])
Jeremy Mowery752dc502016-03-14 23:59:11 -0700942
943 return cls.objects.filter(id__in=trp_ids)