blob: 47c93d764470f73542f38cc66c6e271a708fcb63 [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 Baker96e2b2c2015-02-10 19:26:00 -0800138 if (not hasattr(self,"caller")) or self.caller==None:
139 raise APIException("no self.caller in SlicePlus.save")
140
Scott Bakercd369ea2015-01-18 16:07:58 -0800141 updated_image = self.has_field_changed("default_image")
142 updated_flavor = self.has_field_changed("default_flavor")
143
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800144 super(SlicePlus, self).save(*args, **kwargs)
145
Scott Baker977a4652015-01-20 13:30:40 -0800146 # try things out first
147
Scott Bakercd369ea2015-01-18 16:07:58 -0800148 updated_sites = (self._site_allocation != self._initial_site_allocation) or updated_image or updated_flavor
149 if updated_sites:
150 self.save_site_allocation(noAct=True, reset=(updated_image or updated_flavor))
Scott Bakerc7f363b2015-01-14 00:34:45 -0800151
152 if self._update_users:
153 self.save_users(noAct=True)
154
Scott Baker977a4652015-01-20 13:30:40 -0800155 if (self._network_ports != self._initial_network_ports):
156 self.save_network_ports(noAct=True)
157
158 # now actually save them
159
Scott Bakercd369ea2015-01-18 16:07:58 -0800160 if updated_sites:
161 self.save_site_allocation(reset=(updated_image or updated_flavor))
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800162
Scott Bakerc7f363b2015-01-14 00:34:45 -0800163 if self._update_users:
164 self.save_users()
165
Scott Baker977a4652015-01-20 13:30:40 -0800166 if (self._network_ports != self._initial_network_ports):
167 self.save_network_ports()
168
Scott Bakercd369ea2015-01-18 16:07:58 -0800169 def save_site_allocation(self, noAct = False, reset=False):
170 print "save_site_allocation, reset=",reset
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800171
Scott Baker34b32242015-01-19 08:43:50 -0800172 if (not self._site_allocation):
173 # Must be a sliver that was just created, and has not site_allocation
174 # field.
175 return
176
Scott Bakerc7f363b2015-01-14 00:34:45 -0800177 all_slice_slivers = self.slivers.all()
Scott Bakercd369ea2015-01-18 16:07:58 -0800178 for site_name in self._site_allocation.keys():
179 desired_allocation = self._site_allocation[site_name]
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800180
181 # make a list of the slivers for this site
182 slivers = []
183 for sliver in all_slice_slivers:
184 if sliver.node.site_deployment.site.name == site_name:
185 slivers.append(sliver)
186
187 # delete extra slivers
Scott Bakercd369ea2015-01-18 16:07:58 -0800188 while (reset and len(slivers)>0) or (len(slivers) > desired_allocation):
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800189 sliver = slivers.pop()
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800190 if (not noAct):
Scott Bakercd369ea2015-01-18 16:07:58 -0800191 print "deleting sliver", sliver
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800192 sliver.delete()
Scott Bakercd369ea2015-01-18 16:07:58 -0800193 else:
194 print "would delete sliver", sliver
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800195
196 # add more slivers
197 if (len(slivers) < desired_allocation):
198 site = Site.objects.get(name = site_name)
Scott Bakerc7f363b2015-01-14 00:34:45 -0800199 nodes = self.get_node_allocation([site])
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800200
201 if (not nodes):
Scott Baker5e19c662015-01-24 00:01:29 -0800202 raise APIException(detail="no nodes in site %s" % site_name)
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800203
204 while (len(slivers) < desired_allocation):
205 # pick the least allocated node
206 nodes = sorted(nodes, key=attrgetter("sliverCount"))
207 node = nodes[0]
208
209 sliver = Sliver(name=node.name,
210 slice=self,
211 node=node,
212 image = self.default_image,
213 flavor = self.default_flavor,
214 creator = self.creator,
215 deployment = node.site_deployment.deployment)
Scott Baker96e2b2c2015-02-10 19:26:00 -0800216 sliver.caller = self.caller
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800217 slivers.append(sliver)
218 if (not noAct):
Scott Bakercd369ea2015-01-18 16:07:58 -0800219 print "added sliver", sliver
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800220 sliver.save()
Scott Bakercd369ea2015-01-18 16:07:58 -0800221 else:
222 print "would add sliver", sliver
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800223
224 node.sliverCount = node.sliverCount + 1
225
Scott Bakerc7f363b2015-01-14 00:34:45 -0800226 def save_users(self, noAct = False):
227 new_users = self._update_users
228
Scott Baker96e2b2c2015-02-10 19:26:00 -0800229 try:
230 default_role = SliceRole.objects.get(role="access")
231 except:
232 default_role = SliceRole.objects.get(role="default")
Scott Bakerc7f363b2015-01-14 00:34:45 -0800233
234 slice_privs = self.sliceprivileges.all()
235 slice_user_ids = [priv.user.id for priv in slice_privs]
236
237 for user_id in new_users:
238 if (user_id not in slice_user_ids):
Scott Bakerc7f363b2015-01-14 00:34:45 -0800239 priv = SlicePrivilege(slice=self, user=User.objects.get(id=user_id), role=default_role)
Scott Baker96e2b2c2015-02-10 19:26:00 -0800240 priv.caller = self.caller
Scott Bakerc7f363b2015-01-14 00:34:45 -0800241 if (not noAct):
242 priv.save()
243
244 print "added user id", user_id
245
246 for priv in slice_privs:
247 if (priv.role.id != default_role.id):
248 # only mess with 'default' users; don't kill an admin
249 continue
250
251 if (priv.user.id not in new_users):
252 if (not noAct):
253 priv.delete()
254
255 print "deleted user id", user_id
256
Scott Baker977a4652015-01-20 13:30:40 -0800257 def save_network_ports(self, noAct=False):
258 # First search for any network that already has a filled in 'ports'
259 # field. We'll assume there can only be one, so it must be the right
260 # one.
261 for networkSlice in self.networkslices.all():
262 network = networkSlice.network
263 if (network.owner.id != self.id):
264 continue
265 if network.ports:
266 network.ports = self._network_ports
Scott Baker96e2b2c2015-02-10 19:26:00 -0800267 network.caller = self.caller
Scott Baker977a4652015-01-20 13:30:40 -0800268 if (not noAct):
269 network.save()
270 return
271
272 # Now try a network that is a "NAT", since setting ports on a non-NAT
273 # network doesn't make much sense.
274 for networkSlice in self.networkslices.all():
275 network = networkSlice.network
276 if (network.owner.id != self.id):
277 continue
278 if network.template.translation=="NAT":
279 network.ports = self._network_ports
Scott Baker96e2b2c2015-02-10 19:26:00 -0800280 network.caller = self.caller
Scott Baker977a4652015-01-20 13:30:40 -0800281 if (not noAct):
282 network.save()
283 return
284
285 # uh oh, we didn't find a network
286
Scott Baker5e19c662015-01-24 00:01:29 -0800287 raise APIException(detail="No network was found that ports could be set on")
Scott Baker977a4652015-01-20 13:30:40 -0800288
Scott Bakerc7f363b2015-01-14 00:34:45 -0800289
290
291
Scott Baker2fbf7dc2015-01-13 16:22:57 -0800292