blob: 476cf8ed1bf526a58940b671f1abc24662b54de6 [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
2from django.db import models
Siobhan Tully30fd4292013-05-10 08:59:56 -04003from core.models import PlCoreBase
4from core.models import Site
Tony Mackc195d722014-08-20 11:15:17 -04005from core.models.site import SitePrivilege
Siobhan Tully30fd4292013-05-10 08:59:56 -04006from core.models import User
7from core.models import Role
Tony Mack51c4a7d2014-11-30 15:33:35 -05008from core.models import Controller,ControllerLinkManager,ControllerLinkDeletionManager
Scott Bakere8d596f2013-05-13 23:17:13 -07009from core.models import ServiceClass
Scott Bakerd6dc5052014-09-12 12:24:24 -070010from core.models.serviceclass import get_default_serviceclass
Siobhan Tullyde5450d2013-06-21 11:35:33 -040011from core.models import Tag
12from django.contrib.contenttypes import generic
Siobhan Tullyce652d02013-10-08 21:52:35 -040013from core.models import Service
Tony Mack51c4a7d2014-11-30 15:33:35 -050014from core.models import Controller
Scott Baker3c2b5cd2015-01-12 13:13:05 -080015from core.models import Flavor, Image
Tony Mackd0cf6122015-02-01 19:51:39 -050016from django.core.exceptions import PermissionDenied, ValidationError
Siobhan Tully4bc09f22013-04-10 21:15:21 -040017
18# Create your models here.
19
20class Slice(PlCoreBase):
Tony Mack48952032013-04-12 11:49:34 -040021 name = models.CharField(unique=True, help_text="The Name of the Slice", max_length=80)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040022 enabled = models.BooleanField(default=True, help_text="Status for this Slice")
Scott Bakerd6dc5052014-09-12 12:24:24 -070023 omf_friendly = models.BooleanField(default=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040024 description=models.TextField(blank=True,help_text="High level description of the slice and expected activities", max_length=1024)
25 slice_url = models.URLField(blank=True, max_length=512)
Tony Mack360afb82013-12-17 13:32:45 -050026 site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Slice belongs to")
Scott Bakerd6dc5052014-09-12 12:24:24 -070027 max_slivers = models.IntegerField(default=10)
Siobhan Tullyce652d02013-10-08 21:52:35 -040028 service = models.ForeignKey(Service, related_name='service', null=True, blank=True)
Scott Baker4b0cedd2014-04-22 21:01:21 -070029 network = models.CharField(default="Private Only",null=True, blank=True, max_length=256)
Siobhan Tullyde5450d2013-06-21 11:35:33 -040030 tags = generic.GenericRelation(Tag)
Scott Bakerd6dc5052014-09-12 12:24:24 -070031 serviceClass = models.ForeignKey(ServiceClass, related_name = "slices", null=True, default=get_default_serviceclass)
Tony Mack2bd5b412013-06-11 21:05:06 -040032 creator = models.ForeignKey(User, related_name='slices', blank=True, null=True)
Siobhan Tully231f4c82013-05-02 05:47:24 -040033
Scott Bakerffce7852015-01-03 16:26:38 -080034 # for tenant view
Scott Baker3c2b5cd2015-01-12 13:13:05 -080035 default_flavor = models.ForeignKey(Flavor, related_name = "slices", null=True, blank=True)
36 default_image = models.ForeignKey(Image, related_name = "slices", null=True, blank=True);
Scott Bakerffce7852015-01-03 16:26:38 -080037 mount_data_sets = models.CharField(default="GenBank",null=True, blank=True, max_length=256)
38
Siobhan Tully4bc09f22013-04-10 21:15:21 -040039 def __unicode__(self): return u'%s' % (self.name)
40
Tony Mack2721d6f2014-08-11 11:14:58 -040041 @property
42 def slicename(self):
43 return "%s_%s" % (self.site.login_base, self.name)
44
Tony Mack62bc59a2013-04-14 23:27:12 -040045 def save(self, *args, **kwds):
Tony Mackdac85762014-09-03 13:19:42 -040046 site = Site.objects.get(id=self.site.id)
Tony Mack78070352014-09-24 12:44:22 -040047 # allow preexisting slices to keep their original name for now
48 if not self.id and not self.name.startswith(site.login_base):
Tony Mackdac85762014-09-03 13:19:42 -040049 raise ValidationError('slice name must begin with %s' % site.login_base)
Scott Baker8c520512015-01-29 17:16:10 -080050
51 if self.name == site.login_base+"_":
52 raise ValidationError('slice name is too short')
53
54 if " " in self.name:
55 raise ValidationError('slice name must not contain spaces')
56
Scott Bakere8d596f2013-05-13 23:17:13 -070057 if self.serviceClass is None:
58 # We allowed None=True for serviceClass because Django evolution
59 # will fail unless it is allowed. But, we we really don't want it to
60 # ever save None, so fix it up here.
61 self.serviceClass = ServiceClass.get_default()
Tony Mackd0cf6122015-02-01 19:51:39 -050062
63 # set creator on first save
Tony Mack2bd5b412013-06-11 21:05:06 -040064 if not self.creator and hasattr(self, 'caller'):
65 self.creator = self.caller
Tony Mackd0cf6122015-02-01 19:51:39 -050066
67 # only admins change a slice's creator
68 if 'creator' in self.changed_fields and \
69 (not hasattr(self, 'caller') or not self.caller.is_admin):
Scott Baker8cfe3d22015-02-02 11:00:53 -080070
Scott Bakerbd46a922015-02-03 15:02:17 -080071 if (self._initial["creator"]==None) and (self.creator==getattr(self,"caller",None)):
Scott Baker8cfe3d22015-02-02 11:00:53 -080072 # it's okay if the creator is being set by the caller to
73 # himeself on a new slice object.
Scott Baker8cfe3d22015-02-02 11:00:53 -080074 pass
75 else:
76 raise PermissionDenied("Insufficient privileges to change slice creator")
Tony Mackd0cf6122015-02-01 19:51:39 -050077
Scott Baker9a9a9ab2015-01-29 17:53:26 -080078 if not self.creator:
79 raise ValidationError('slice has no creator')
Tony Mackd0cf6122015-02-01 19:51:39 -050080
Tony Mack62bc59a2013-04-14 23:27:12 -040081 super(Slice, self).save(*args, **kwds)
82
Tony Mack5b061472014-02-04 07:57:10 -050083 def can_update(self, user):
84 if user.is_readonly:
85 return False
86 if user.is_admin:
87 return True
Tony Mackcf29cfa2015-02-05 06:13:04 -050088 if user == self.creator:
89 return True
Tony Mackc195d722014-08-20 11:15:17 -040090 # slice admins can update
Tony Mackcf29cfa2015-02-05 06:13:04 -050091 if SlicePrivilege.objects.filter(
92 user=user, slice=self, role__role='admin'):
93 return True
Tony Mackc195d722014-08-20 11:15:17 -040094 # site pis can update
Tony Mackcf29cfa2015-02-05 06:13:04 -050095 if SitePrivilege.objects.filter(
96 user=user, site=self.site, role__role__in=['admin', 'pi']):
97 return True
Tony Mackc195d722014-08-20 11:15:17 -040098
Tony Mack5b061472014-02-04 07:57:10 -050099 return False
100
Tony Mack5b061472014-02-04 07:57:10 -0500101 @staticmethod
102 def select_by_user(user):
103 if user.is_admin:
104 qs = Slice.objects.all()
105 else:
Tony Mack33df82b2014-08-20 11:29:40 -0400106 # users can see slices they belong to
Tony Mack5b061472014-02-04 07:57:10 -0500107 slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)]
Tony Mack33df82b2014-08-20 11:29:40 -0400108 # pis can see slices at their sites
109 sites = [sp.site for sp in SitePrivilege.objects.filter(user=user)\
110 if sp.role.role == 'pi']
111 slice_ids.extend([s.id for s in Slice.objects.filter(site__in=sites)])
Tony Mack5b061472014-02-04 07:57:10 -0500112 qs = Slice.objects.filter(id__in=slice_ids)
113 return qs
114
Tony Mack82d901b2014-10-22 13:30:41 -0400115 def delete(self, *args, **kwds):
116 # delete networks associated with this slice
117 from core.models.network import Network
118 nets = Network.objects.filter(slices=self)
119 nets.delete()
Tony Mack51c4a7d2014-11-30 15:33:35 -0500120 # delete slice controllers
Tony Mack3066a952015-01-05 22:48:11 -0500121 slice_controllers = ControllerSlice.objects.filter(slice=self)
Tony Mack51c4a7d2014-11-30 15:33:35 -0500122 slice_controllers.delete()
Tony Macka8812242014-11-12 10:01:26 -0500123 # delete slice privilege
124 slice_privileges = SlicePrivilege.objects.filter(slice=self)
125 slice_privileges.delete()
126 # continue with normal delete
Sapana283f502014-11-24 16:44:48 -0500127 super(Slice, self).delete(*args, **kwds)
Tony Macka8812242014-11-12 10:01:26 -0500128
Tony Mack82d901b2014-10-22 13:30:41 -0400129
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400130class SliceRole(PlCoreBase):
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500131 ROLE_CHOICES = (('admin','Admin'),('default','Default'))
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400132
133 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
134
135 def __unicode__(self): return u'%s' % (self.role)
136
137class SlicePrivilege(PlCoreBase):
Sapan Bhatia6bfa2ca2014-11-11 21:47:45 -0500138 user = models.ForeignKey('User', related_name='sliceprivileges')
139 slice = models.ForeignKey('Slice', related_name='sliceprivileges')
140 role = models.ForeignKey('SliceRole',related_name='sliceprivileges')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400141
142 def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role)
Tony Mack5b061472014-02-04 07:57:10 -0500143
144 def can_update(self, user):
Tony Mackb7b4f842014-02-04 19:50:31 -0500145 return self.slice.can_update(user)
Tony Mack5b061472014-02-04 07:57:10 -0500146
Tony Mack5b061472014-02-04 07:57:10 -0500147 @staticmethod
148 def select_by_user(user):
149 if user.is_admin:
150 qs = SlicePrivilege.objects.all()
151 else:
152 sp_ids = [sp.id for sp in SlicePrivilege.objects.filter(user=user)]
153 qs = SlicePrivilege.objects.filter(id__in=sp_ids)
154 return qs
Tony Macke4be32f2014-03-11 20:45:25 -0400155
Tony Mack3066a952015-01-05 22:48:11 -0500156class ControllerSlice(PlCoreBase):
Tony Mack51c4a7d2014-11-30 15:33:35 -0500157 objects = ControllerLinkManager()
158 deleted_objects = ControllerLinkDeletionManager()
Sapan Bhatiaa6609032014-09-22 14:55:08 -0400159
Tony Mack51c4a7d2014-11-30 15:33:35 -0500160 controller = models.ForeignKey(Controller, related_name='controllerslices')
161 slice = models.ForeignKey(Slice, related_name='controllerslices')
Tony Mackc218b592014-03-19 15:17:53 -0400162 tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id")
Tony Macke4be32f2014-03-11 20:45:25 -0400163
Tony Mack51c4a7d2014-11-30 15:33:35 -0500164 def __unicode__(self): return u'%s %s' % (self.slice, self.controller)
Tony Macke4be32f2014-03-11 20:45:25 -0400165
166 @staticmethod
167 def select_by_user(user):
168 if user.is_admin:
Tony Mack3066a952015-01-05 22:48:11 -0500169 qs = ControllerSlice.objects.all()
Tony Macke4be32f2014-03-11 20:45:25 -0400170 else:
171 slices = Slice.select_by_user(user)
Tony Mack3066a952015-01-05 22:48:11 -0500172 qs = ControllerSlice.objects.filter(slice__in=slices)
Tony Macke4be32f2014-03-11 20:45:25 -0400173 return qs
Sapan Bhatia772c7c22014-12-16 01:09:04 -0500174
175 def get_cpu_stats(self):
176 filter = 'project_id=%s'%self.tenant_id
177 return monitor.get_meter('cpu',filter,None)
178
179 def get_bw_stats(self):
180 filter = 'project_id=%s'%self.tenant_id
181 return monitor.get_meter('network.outgoing.bytes',filter,None)
182
183 def get_node_stats(self):
184 return len(self.slice.slivers)