blob: 7ec8e2772304c714b8366f1bec775a7b0064e9ce [file] [log] [blame]
Scott Baker5f4770d2014-07-13 11:17:58 -07001from core.models.slice import Slice
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 Baker98b26b62015-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 Bakerdf54cf22014-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 Baker8b89d302015-01-08 22:34:51 -080029 @property
Scott Bakerdcf6fbf2015-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 Bakerd3a6b2c2015-01-08 22:37:34 -080038 def network_ports(self):
Scott Baker8b89d302015-01-08 22:34:51 -080039 # XXX this assumes there is only one network that can have ports bound
40 # to it for a given slice. This is intended for the tenant view, which
41 # will obey this field.
42 networkPorts = ""
43 for networkSlice in self.networkslices.all():
44 network = networkSlice.network
45 if network.ports:
46 networkPorts = network.ports
47
48 return networkPorts
49
Scott Bakerd3a6b2c2015-01-08 22:37:34 -080050 @network_ports.setter
51 def network_ports(self, value):
Scott Baker8b89d302015-01-08 22:34:51 -080052 print "XXX set networkPorts to", value
53
Scott Baker88e34372014-07-13 11:46:36 -070054 @staticmethod
55 def select_by_user(user):
Scott Baker88e34372014-07-13 11:46:36 -070056 if user.is_admin:
57 qs = SlicePlus.objects.all()
58 else:
59 slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)]
60 qs = SlicePlus.objects.filter(id__in=slice_ids)
Scott Baker88e34372014-07-13 11:46:36 -070061 return qs