blob: cd86fcef75e7f6345ff18f11911071695772dd48 [file] [log] [blame]
Scott Baker435c2c92015-01-14 00:34:45 -08001from core.models import Slice, SlicePrivilege, SliceRole, Sliver, Site, Node, User
Scott Bakere791dc62014-08-28 14:02:54 -07002from plus import PlusObjectMixin
Scott Bakera76f65d2015-01-13 16:22:57 -08003from operator import itemgetter, attrgetter
Scott Baker5f4770d2014-07-13 11:17:58 -07004
Scott Bakere791dc62014-08-28 14:02:54 -07005class SlicePlus(Slice, PlusObjectMixin):
Scott Baker88e34372014-07-13 11:46:36 -07006 class Meta:
7 proxy = True
8
Scott Bakera76f65d2015-01-13 16:22:57 -08009 def __init__(self, *args, **kwargs):
10 super(SlicePlus, self).__init__(*args, **kwargs)
Scott Bakera76f65d2015-01-13 16:22:57 -080011 self._update_users = None
Scott Baker7a76f322015-01-16 19:07:36 -080012 self._sliceInfo = None
Scott Baker55f6de62015-01-18 16:07:58 -080013 self.getSliceInfo()
14 self._site_allocation = self._sliceInfo["sitesUsed"]
15 self._initial_site_allocation = self._site_allocation
Scott Bakera76f65d2015-01-13 16:22:57 -080016
Scott Baker5f4770d2014-07-13 11:17:58 -070017 def getSliceInfo(self, user=None):
Scott Baker7a76f322015-01-16 19:07:36 -080018 if not self._sliceInfo:
19 used_sites = {}
20 used_deployments = {}
21 sliverCount = 0
22 for sliver in self.slivers.all():
23 site = sliver.node.site_deployment.site
24 deployment = sliver.node.site_deployment.deployment
25 used_sites[site.name] = used_sites.get(site.name, 0) + 1
26 used_deployments[deployment.name] = used_deployments.get(deployment.name, 0) + 1
27 sliverCount = sliverCount + 1
Scott Baker5f4770d2014-07-13 11:17:58 -070028
Scott Baker7a76f322015-01-16 19:07:36 -080029 users = {}
30 for priv in SlicePrivilege.objects.filter(slice=self):
31 if not (priv.user.id in users.keys()):
32 users[priv.user.id] = {"name": priv.user.email, "id": priv.user.id, "roles": []}
33 users[priv.user.id]["roles"].append(priv.role.role)
Scott Baker5f4770d2014-07-13 11:17:58 -070034
Scott Baker7a76f322015-01-16 19:07:36 -080035 self._sliceInfo= {"sitesUsed": used_sites,
36 "deploymentsUsed": used_deployments,
37 "sliverCount": sliverCount,
38 "siteCount": len(used_sites.keys()),
39 "users": users,
40 "roles": []}
41
42 if user:
43 auser = self._sliceInfo["users"].get(user.id, None)
44 if (auser):
45 self._sliceInfo["roles"] = auser["roles"]
46
47 return self._sliceInfo
Scott Baker88e34372014-07-13 11:46:36 -070048
Scott Baker8b89d302015-01-08 22:34:51 -080049 @property
Scott Bakerdcf6fbf2015-01-11 13:45:19 -080050 def site_allocation(self):
Scott Baker55f6de62015-01-18 16:07:58 -080051 return self._site_allocation
Scott Bakerdcf6fbf2015-01-11 13:45:19 -080052
53 @site_allocation.setter
54 def site_allocation(self, value):
Scott Baker55f6de62015-01-18 16:07:58 -080055 self._site_allocation = value
Scott Bakerdcf6fbf2015-01-11 13:45:19 -080056
57 @property
Scott Baker7a76f322015-01-16 19:07:36 -080058 def user_names(self):
59 return [user["name"] for user in self.getSliceInfo()["users"].values()]
60
Scott Baker12154242015-01-16 19:26:54 -080061 @user_names.setter
62 def user_names(self, value):
63 pass # it's read-only
64
Scott Baker7a76f322015-01-16 19:07:36 -080065 @property
Scott Baker97acad92015-01-12 19:45:40 -080066 def users(self):
Scott Baker7a76f322015-01-16 19:07:36 -080067 return [user["id"] for user in self.getSliceInfo()["users"].values()]
Scott Baker97acad92015-01-12 19:45:40 -080068
69 @users.setter
70 def users(self, value):
Scott Bakera76f65d2015-01-13 16:22:57 -080071 self._update_users = value
72 #print "XXX set users to", value
Scott Baker97acad92015-01-12 19:45:40 -080073
74 @property
Scott Bakerd3a6b2c2015-01-08 22:37:34 -080075 def network_ports(self):
Scott Baker8b89d302015-01-08 22:34:51 -080076 # XXX this assumes there is only one network that can have ports bound
77 # to it for a given slice. This is intended for the tenant view, which
78 # will obey this field.
79 networkPorts = ""
80 for networkSlice in self.networkslices.all():
81 network = networkSlice.network
82 if network.ports:
83 networkPorts = network.ports
84
85 return networkPorts
86
Scott Bakerd3a6b2c2015-01-08 22:37:34 -080087 @network_ports.setter
88 def network_ports(self, value):
Scott Baker8b89d302015-01-08 22:34:51 -080089 print "XXX set networkPorts to", value
90
Scott Baker88e34372014-07-13 11:46:36 -070091 @staticmethod
92 def select_by_user(user):
Scott Baker88e34372014-07-13 11:46:36 -070093 if user.is_admin:
94 qs = SlicePlus.objects.all()
95 else:
96 slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)]
97 qs = SlicePlus.objects.filter(id__in=slice_ids)
Scott Baker88e34372014-07-13 11:46:36 -070098 return qs
Scott Bakera76f65d2015-01-13 16:22:57 -080099
Scott Baker435c2c92015-01-14 00:34:45 -0800100 def get_node_allocation(self, siteList):
Scott Bakera76f65d2015-01-13 16:22:57 -0800101 siteIDList = [site.id for site in siteList]
102 nodeList = []
103 for node in Node.objects.all():
104 if (node.site_deployment.site.id in siteIDList):
105 node.sliverCount = 0
106 for sliver in node.slivers.all():
107 if sliver.slice.id == self.id:
108 node.sliverCount = node.sliverCount + 1
109 nodeList.append(node)
110 return nodeList
111
112 def save(self, *args, **kwargs):
Scott Baker55f6de62015-01-18 16:07:58 -0800113 updated_image = self.has_field_changed("default_image")
114 updated_flavor = self.has_field_changed("default_flavor")
115
Scott Bakera76f65d2015-01-13 16:22:57 -0800116 super(SlicePlus, self).save(*args, **kwargs)
117
Scott Baker55f6de62015-01-18 16:07:58 -0800118 updated_sites = (self._site_allocation != self._initial_site_allocation) or updated_image or updated_flavor
119 if updated_sites:
120 self.save_site_allocation(noAct=True, reset=(updated_image or updated_flavor))
Scott Baker435c2c92015-01-14 00:34:45 -0800121
122 if self._update_users:
123 self.save_users(noAct=True)
124
Scott Baker55f6de62015-01-18 16:07:58 -0800125 if updated_sites:
126 self.save_site_allocation(reset=(updated_image or updated_flavor))
Scott Bakera76f65d2015-01-13 16:22:57 -0800127
Scott Baker435c2c92015-01-14 00:34:45 -0800128 if self._update_users:
129 self.save_users()
130
Scott Baker55f6de62015-01-18 16:07:58 -0800131 def save_site_allocation(self, noAct = False, reset=False):
132 print "save_site_allocation, reset=",reset
Scott Bakera76f65d2015-01-13 16:22:57 -0800133
Scott Baker435c2c92015-01-14 00:34:45 -0800134 all_slice_slivers = self.slivers.all()
Scott Baker55f6de62015-01-18 16:07:58 -0800135 for site_name in self._site_allocation.keys():
136 desired_allocation = self._site_allocation[site_name]
Scott Bakera76f65d2015-01-13 16:22:57 -0800137
138 # make a list of the slivers for this site
139 slivers = []
140 for sliver in all_slice_slivers:
141 if sliver.node.site_deployment.site.name == site_name:
142 slivers.append(sliver)
143
144 # delete extra slivers
Scott Baker55f6de62015-01-18 16:07:58 -0800145 while (reset and len(slivers)>0) or (len(slivers) > desired_allocation):
Scott Bakera76f65d2015-01-13 16:22:57 -0800146 sliver = slivers.pop()
Scott Bakera76f65d2015-01-13 16:22:57 -0800147 if (not noAct):
Scott Baker55f6de62015-01-18 16:07:58 -0800148 print "deleting sliver", sliver
Scott Bakera76f65d2015-01-13 16:22:57 -0800149 sliver.delete()
Scott Baker55f6de62015-01-18 16:07:58 -0800150 else:
151 print "would delete sliver", sliver
Scott Bakera76f65d2015-01-13 16:22:57 -0800152
153 # add more slivers
154 if (len(slivers) < desired_allocation):
155 site = Site.objects.get(name = site_name)
Scott Baker435c2c92015-01-14 00:34:45 -0800156 nodes = self.get_node_allocation([site])
Scott Bakera76f65d2015-01-13 16:22:57 -0800157
158 if (not nodes):
Scott Baker1f7901b2015-01-13 16:28:05 -0800159 raise ValueError("no nodes in site %s" % site_name)
Scott Bakera76f65d2015-01-13 16:22:57 -0800160
161 while (len(slivers) < desired_allocation):
162 # pick the least allocated node
163 nodes = sorted(nodes, key=attrgetter("sliverCount"))
164 node = nodes[0]
165
166 sliver = Sliver(name=node.name,
167 slice=self,
168 node=node,
169 image = self.default_image,
170 flavor = self.default_flavor,
171 creator = self.creator,
172 deployment = node.site_deployment.deployment)
173 slivers.append(sliver)
174 if (not noAct):
Scott Baker55f6de62015-01-18 16:07:58 -0800175 print "added sliver", sliver
Scott Bakera76f65d2015-01-13 16:22:57 -0800176 sliver.save()
Scott Baker55f6de62015-01-18 16:07:58 -0800177 else:
178 print "would add sliver", sliver
Scott Bakera76f65d2015-01-13 16:22:57 -0800179
180 node.sliverCount = node.sliverCount + 1
181
Scott Baker435c2c92015-01-14 00:34:45 -0800182 def save_users(self, noAct = False):
183 new_users = self._update_users
184
185 default_role = SliceRole.objects.get(role="default")
186
187 slice_privs = self.sliceprivileges.all()
188 slice_user_ids = [priv.user.id for priv in slice_privs]
189
190 for user_id in new_users:
191 if (user_id not in slice_user_ids):
Scott Baker435c2c92015-01-14 00:34:45 -0800192 priv = SlicePrivilege(slice=self, user=User.objects.get(id=user_id), role=default_role)
193 if (not noAct):
194 priv.save()
195
196 print "added user id", user_id
197
198 for priv in slice_privs:
199 if (priv.role.id != default_role.id):
200 # only mess with 'default' users; don't kill an admin
201 continue
202
203 if (priv.user.id not in new_users):
204 if (not noAct):
205 priv.delete()
206
207 print "deleted user id", user_id
208
209
210
211
Scott Bakera76f65d2015-01-13 16:22:57 -0800212