blob: adbc77e658a8d104c7c276d5c2e41efeb5ac78d3 [file] [log] [blame]
Scott Bakerfcf655e2015-01-12 19:45:40 -08001from core.models.slice import Slice, SlicePrivilege
Scott Bakere791dc62014-08-28 14:02:54 -07002from plus import PlusObjectMixin
Scott Baker5f4770d2014-07-13 11:17:58 -07003
Scott Bakere791dc62014-08-28 14:02:54 -07004class SlicePlus(Slice, PlusObjectMixin):
Scott Baker88e34372014-07-13 11:46:36 -07005 class Meta:
6 proxy = True
7
Scott Baker5f4770d2014-07-13 11:17:58 -07008 def getSliceInfo(self, user=None):
Scott Baker88e34372014-07-13 11:46:36 -07009 used_sites = {}
10 used_deployments = {}
Scott Baker5f4770d2014-07-13 11:17:58 -070011 sliverCount = 0
12 for sliver in self.slivers.all():
Scott Bakerdc844122015-01-07 11:15:08 -080013 site = sliver.node.site_deployment.site
14 deployment = sliver.node.site_deployment.deployment
Scott Baker88e34372014-07-13 11:46:36 -070015 used_sites[site.name] = used_sites.get(site.name, 0) + 1
Scott Baker5f4770d2014-07-13 11:17:58 -070016 used_deployments[deployment.name] = used_deployments.get(deployment.name, 0) + 1
Scott Baker88e34372014-07-13 11:46:36 -070017 sliverCount = sliverCount + 1
Scott Baker5f4770d2014-07-13 11:17:58 -070018
19 roles = []
20 if (user!=None):
Scott Bakerc8655082014-12-23 11:03:58 -080021 roles = [x.role.role for x in self.sliceprivileges.filter(user=user)]
Scott Baker5f4770d2014-07-13 11:17:58 -070022
23 return {"sitesUsed": used_sites,
24 "deploymentsUsed": used_deployments,
Scott Baker88e34372014-07-13 11:46:36 -070025 "sliverCount": sliverCount,
Scott Baker5f4770d2014-07-13 11:17:58 -070026 "siteCount": len(used_sites.keys()),
27 "roles": roles}
Scott Baker88e34372014-07-13 11:46:36 -070028
Scott Baker9062b0c2015-01-08 22:34:51 -080029 @property
Scott Baker03696ee2015-01-11 13:45:19 -080030 def site_allocation(self):
31 return self.getSliceInfo()["sitesUsed"]
32
33 @site_allocation.setter
34 def site_allocation(self, value):
35 print "XXX set sitesUsed to", value
36
37 @property
Scott Bakerfcf655e2015-01-12 19:45:40 -080038 def users(self):
39 user_ids = []
40 for priv in SlicePrivilege.objects.filter(slice=self):
41 if not (priv.user.id in user_ids):
42 user_ids.append(priv.user.id)
43 return user_ids
44
45 @users.setter
46 def users(self, value):
47 print "XXX set users to", value
48
49 @property
Scott Baker697512d2015-01-08 22:37:34 -080050 def network_ports(self):
Scott Baker9062b0c2015-01-08 22:34:51 -080051 # XXX this assumes there is only one network that can have ports bound
52 # to it for a given slice. This is intended for the tenant view, which
53 # will obey this field.
54 networkPorts = ""
55 for networkSlice in self.networkslices.all():
56 network = networkSlice.network
57 if network.ports:
58 networkPorts = network.ports
59
60 return networkPorts
61
Scott Baker697512d2015-01-08 22:37:34 -080062 @network_ports.setter
63 def network_ports(self, value):
Scott Baker9062b0c2015-01-08 22:34:51 -080064 print "XXX set networkPorts to", value
65
Scott Baker88e34372014-07-13 11:46:36 -070066 @staticmethod
67 def select_by_user(user):
Scott Baker88e34372014-07-13 11:46:36 -070068 if user.is_admin:
69 qs = SlicePlus.objects.all()
70 else:
71 slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)]
72 qs = SlicePlus.objects.filter(id__in=slice_ids)
Scott Baker88e34372014-07-13 11:46:36 -070073 return qs