blob: 6bbbfa2235bf2e4c377e61e9b8cc52789dc94cf0 [file] [log] [blame]
Scott Bakerc7f363b2015-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 Baker2fbf7dc2015-01-13 16:22:57 -08003from operator import itemgetter, attrgetter
Scott Baker5e19c662015-01-24 00:01:29 -08004from rest_framework.exceptions import APIException
Scott Baker5f4770d2014-07-13 11:17:58 -07005
Scott Bakere791dc62014-08-28 14:02:54 -07006class SlicePlus(Slice, PlusObjectMixin):
Scott Baker88e34372014-07-13 11:46:36 -07007 class Meta:
8 proxy = True
9
Scott Baker2fbf7dc2015-01-13 16:22:57 -080010 def __init__(self, *args, **kwargs):
11 super(SlicePlus, self).__init__(*args, **kwargs)
Scott Baker2fbf7dc2015-01-13 16:22:57 -080012 self._update_users = None
Scott Bakera31bb242015-01-16 19:07:36 -080013 self._sliceInfo = None
Scott Bakercd369ea2015-01-18 16:07:58 -080014 self.getSliceInfo()
15 self._site_allocation = self._sliceInfo["sitesUsed"]
16 self._initial_site_allocation = self._site_allocation
Scott Baker977a4652015-01-20 13:30:40 -080017 self._network_ports = self._sliceInfo["networkPorts"]
18 self._initial_network_ports = self._network_ports
Scott Baker2fbf7dc2015-01-13 16:22:57 -080019
Scott Baker5f4770d2014-07-13 11:17:58 -070020 def getSliceInfo(self, user=None):
Scott Bakera31bb242015-01-16 19:07:36 -080021 if not self._sliceInfo:
22 used_sites = {}
Scott Bakera4a379c2015-01-22 15:21:24 -080023 ready_sites = {}
Scott Bakera31bb242015-01-16 19:07:36 -080024 used_deployments = {}
25 sliverCount = 0
Scott Baker334919e2015-01-20 01:02:08 -080026 sshCommands = []
Scott Bakera31bb242015-01-16 19:07:36 -080027 for sliver in self.slivers.all():
28 site = sliver.node.site_deployment.site
29 deployment = sliver.node.site_deployment.deployment
30 used_sites[site.name] = used_sites.get(site.name, 0) + 1
31 used_deployments[deployment.name] = used_deployments.get(deployment.name, 0) + 1
32 sliverCount = sliverCount + 1
Scott Baker5f4770d2014-07-13 11:17:58 -070033
Scott Baker7ccc6ad2015-01-25 22:16:13 -080034 sshCommand = sliver.get_ssh_command()
35 if sshCommand:
36 sshCommands.append(sshCommand)
Scott Baker334919e2015-01-20 01:02:08 -080037
Scott Bakera4a379c2015-01-22 15:21:24 -080038 ready_sites[site.name] = ready_sites.get(site.name, 0) + 1
39
Scott Bakera31bb242015-01-16 19:07:36 -080040 users = {}
41 for priv in SlicePrivilege.objects.filter(slice=self):
42 if not (priv.user.id in users.keys()):
43 users[priv.user.id] = {"name": priv.user.email, "id": priv.user.id, "roles": []}
44 users[priv.user.id]["roles"].append(priv.role.role)
Scott Baker5f4770d2014-07-13 11:17:58 -070045
Scott Baker977a4652015-01-20 13:30:40 -080046 # XXX this assumes there is only one network that can have ports bound
47 # to it for a given slice. This is intended for the tenant view, which
48 # will obey this field.
49 networkPorts = ""
50 for networkSlice in self.networkslices.all():
51 network = networkSlice.network
52 if (network.owner.id != self.id):
53 continue
54 if network.ports:
55 networkPorts = network.ports
56
Scott Bakera31bb242015-01-16 19:07:36 -080057 self._sliceInfo= {"sitesUsed": used_sites,
Scott Bakera4a379c2015-01-22 15:21:24 -080058 "sitesReady": ready_sites,
Scott Bakera31bb242015-01-16 19:07:36 -080059 "deploymentsUsed": used_deployments,
60 "sliverCount": sliverCount,
61 "siteCount": len(used_sites.keys()),
62 "users": users,
Scott Baker334919e2015-01-20 01:02:08 -080063 "roles": [],
Scott Baker977a4652015-01-20 13:30:40 -080064 "sshCommands": sshCommands,
65 "networkPorts": networkPorts}
Scott Bakera31bb242015-01-16 19:07:36 -080066
67 if user:
68 auser = self._sliceInfo["users"].get(user.id, None)
69 if (auser):
70 self._sliceInfo["roles"] = auser["roles"]
71
72 return self._sliceInfo
Scott Baker88e34372014-07-13 11:46:36 -070073
Scott Baker9062b0c2015-01-08 22:34:51 -080074 @property
Scott Bakera4a379c2015-01-22 15:21:24 -080075 def site_ready(self):
76 return self.getSliceInfo()["sitesReady"]
77
78 @site_ready.setter
79 def site_ready(self, value):
80 pass
81
82 @property
Scott Baker03696ee2015-01-11 13:45:19 -080083 def site_allocation(self):
Scott Bakercd369ea2015-01-18 16:07:58 -080084 return self._site_allocation
Scott Baker03696ee2015-01-11 13:45:19 -080085
86 @site_allocation.setter
87 def site_allocation(self, value):
Scott Bakercd369ea2015-01-18 16:07:58 -080088 self._site_allocation = value
Scott Baker03696ee2015-01-11 13:45:19 -080089
90 @property
Scott Bakera31bb242015-01-16 19:07:36 -080091 def user_names(self):
92 return [user["name"] for user in self.getSliceInfo()["users"].values()]
93
Scott Bakera4101002015-01-16 19:26:54 -080094 @user_names.setter
95 def user_names(self, value):
96 pass # it's read-only
97
Scott Bakera31bb242015-01-16 19:07:36 -080098 @property
Scott Bakerfcf655e2015-01-12 19:45:40 -080099 def users(self):
Scott Bakera31bb242015-01-16 19:07:36 -0800100 return [user["id"] for user in self.getSliceInfo()["users"].values()]
Scott Bakerfcf655e2015-01-12 19:45:40 -0800101
102 @users.setter
103 def users(self, value):
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800104 self._update_users = value
105 #print "XXX set users to", value
Scott Bakerfcf655e2015-01-12 19:45:40 -0800106
107 @property
Scott Baker697512d2015-01-08 22:37:34 -0800108 def network_ports(self):
Scott Baker977a4652015-01-20 13:30:40 -0800109 return self._network_ports
Scott Baker9062b0c2015-01-08 22:34:51 -0800110
Scott Baker697512d2015-01-08 22:37:34 -0800111 @network_ports.setter
112 def network_ports(self, value):
Scott Baker977a4652015-01-20 13:30:40 -0800113 self._network_ports = value
114 #print "XXX set networkPorts to", value
Scott Baker9062b0c2015-01-08 22:34:51 -0800115
Scott Baker88e34372014-07-13 11:46:36 -0700116 @staticmethod
117 def select_by_user(user):
Scott Baker88e34372014-07-13 11:46:36 -0700118 if user.is_admin:
119 qs = SlicePlus.objects.all()
120 else:
121 slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)]
122 qs = SlicePlus.objects.filter(id__in=slice_ids)
Scott Baker88e34372014-07-13 11:46:36 -0700123 return qs
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800124
Scott Bakerc7f363b2015-01-14 00:34:45 -0800125 def get_node_allocation(self, siteList):
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800126 siteIDList = [site.id for site in siteList]
127 nodeList = []
128 for node in Node.objects.all():
129 if (node.site_deployment.site.id in siteIDList):
130 node.sliverCount = 0
131 for sliver in node.slivers.all():
132 if sliver.slice.id == self.id:
133 node.sliverCount = node.sliverCount + 1
134 nodeList.append(node)
135 return nodeList
136
137 def save(self, *args, **kwargs):
Scott Bakercd369ea2015-01-18 16:07:58 -0800138 updated_image = self.has_field_changed("default_image")
139 updated_flavor = self.has_field_changed("default_flavor")
140
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800141 super(SlicePlus, self).save(*args, **kwargs)
142
Scott Baker977a4652015-01-20 13:30:40 -0800143 # try things out first
144
Scott Bakercd369ea2015-01-18 16:07:58 -0800145 updated_sites = (self._site_allocation != self._initial_site_allocation) or updated_image or updated_flavor
146 if updated_sites:
147 self.save_site_allocation(noAct=True, reset=(updated_image or updated_flavor))
Scott Bakerc7f363b2015-01-14 00:34:45 -0800148
149 if self._update_users:
150 self.save_users(noAct=True)
151
Scott Baker977a4652015-01-20 13:30:40 -0800152 if (self._network_ports != self._initial_network_ports):
153 self.save_network_ports(noAct=True)
154
155 # now actually save them
156
Scott Bakercd369ea2015-01-18 16:07:58 -0800157 if updated_sites:
158 self.save_site_allocation(reset=(updated_image or updated_flavor))
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800159
Scott Bakerc7f363b2015-01-14 00:34:45 -0800160 if self._update_users:
161 self.save_users()
162
Scott Baker977a4652015-01-20 13:30:40 -0800163 if (self._network_ports != self._initial_network_ports):
164 self.save_network_ports()
165
Scott Bakercd369ea2015-01-18 16:07:58 -0800166 def save_site_allocation(self, noAct = False, reset=False):
167 print "save_site_allocation, reset=",reset
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800168
Scott Baker34b32242015-01-19 08:43:50 -0800169 if (not self._site_allocation):
170 # Must be a sliver that was just created, and has not site_allocation
171 # field.
172 return
173
Scott Bakerc7f363b2015-01-14 00:34:45 -0800174 all_slice_slivers = self.slivers.all()
Scott Bakercd369ea2015-01-18 16:07:58 -0800175 for site_name in self._site_allocation.keys():
176 desired_allocation = self._site_allocation[site_name]
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800177
178 # make a list of the slivers for this site
179 slivers = []
180 for sliver in all_slice_slivers:
181 if sliver.node.site_deployment.site.name == site_name:
182 slivers.append(sliver)
183
184 # delete extra slivers
Scott Bakercd369ea2015-01-18 16:07:58 -0800185 while (reset and len(slivers)>0) or (len(slivers) > desired_allocation):
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800186 sliver = slivers.pop()
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800187 if (not noAct):
Scott Bakercd369ea2015-01-18 16:07:58 -0800188 print "deleting sliver", sliver
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800189 sliver.delete()
Scott Bakercd369ea2015-01-18 16:07:58 -0800190 else:
191 print "would delete sliver", sliver
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800192
193 # add more slivers
194 if (len(slivers) < desired_allocation):
195 site = Site.objects.get(name = site_name)
Scott Bakerc7f363b2015-01-14 00:34:45 -0800196 nodes = self.get_node_allocation([site])
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800197
198 if (not nodes):
Scott Baker5e19c662015-01-24 00:01:29 -0800199 raise APIException(detail="no nodes in site %s" % site_name)
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800200
201 while (len(slivers) < desired_allocation):
202 # pick the least allocated node
203 nodes = sorted(nodes, key=attrgetter("sliverCount"))
204 node = nodes[0]
205
206 sliver = Sliver(name=node.name,
207 slice=self,
208 node=node,
209 image = self.default_image,
210 flavor = self.default_flavor,
211 creator = self.creator,
212 deployment = node.site_deployment.deployment)
213 slivers.append(sliver)
214 if (not noAct):
Scott Bakercd369ea2015-01-18 16:07:58 -0800215 print "added sliver", sliver
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800216 sliver.save()
Scott Bakercd369ea2015-01-18 16:07:58 -0800217 else:
218 print "would add sliver", sliver
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800219
220 node.sliverCount = node.sliverCount + 1
221
Scott Bakerc7f363b2015-01-14 00:34:45 -0800222 def save_users(self, noAct = False):
223 new_users = self._update_users
224
225 default_role = SliceRole.objects.get(role="default")
226
227 slice_privs = self.sliceprivileges.all()
228 slice_user_ids = [priv.user.id for priv in slice_privs]
229
230 for user_id in new_users:
231 if (user_id not in slice_user_ids):
Scott Bakerc7f363b2015-01-14 00:34:45 -0800232 priv = SlicePrivilege(slice=self, user=User.objects.get(id=user_id), role=default_role)
233 if (not noAct):
234 priv.save()
235
236 print "added user id", user_id
237
238 for priv in slice_privs:
239 if (priv.role.id != default_role.id):
240 # only mess with 'default' users; don't kill an admin
241 continue
242
243 if (priv.user.id not in new_users):
244 if (not noAct):
245 priv.delete()
246
247 print "deleted user id", user_id
248
Scott Baker977a4652015-01-20 13:30:40 -0800249 def save_network_ports(self, noAct=False):
250 # First search for any network that already has a filled in 'ports'
251 # field. We'll assume there can only be one, so it must be the right
252 # one.
253 for networkSlice in self.networkslices.all():
254 network = networkSlice.network
255 if (network.owner.id != self.id):
256 continue
257 if network.ports:
258 network.ports = self._network_ports
259 if (not noAct):
260 network.save()
261 return
262
263 # Now try a network that is a "NAT", since setting ports on a non-NAT
264 # network doesn't make much sense.
265 for networkSlice in self.networkslices.all():
266 network = networkSlice.network
267 if (network.owner.id != self.id):
268 continue
269 if network.template.translation=="NAT":
270 network.ports = self._network_ports
271 if (not noAct):
272 network.save()
273 return
274
275 # uh oh, we didn't find a network
276
Scott Baker5e19c662015-01-24 00:01:29 -0800277 raise APIException(detail="No network was found that ports could be set on")
Scott Baker977a4652015-01-20 13:30:40 -0800278
Scott Bakerc7f363b2015-01-14 00:34:45 -0800279
280
281
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800282