Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 1 | from core.models import Slice, SlicePrivilege, Sliver, Site, Node |
Scott Baker | e791dc6 | 2014-08-28 14:02:54 -0700 | [diff] [blame] | 2 | from plus import PlusObjectMixin |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 3 | from operator import itemgetter, attrgetter |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 4 | |
Scott Baker | e791dc6 | 2014-08-28 14:02:54 -0700 | [diff] [blame] | 5 | class SlicePlus(Slice, PlusObjectMixin): |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 6 | class Meta: |
| 7 | proxy = True |
| 8 | |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 9 | def __init__(self, *args, **kwargs): |
| 10 | super(SlicePlus, self).__init__(*args, **kwargs) |
| 11 | self._update_site_allocation = None |
| 12 | self._update_users = None |
| 13 | |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 14 | def getSliceInfo(self, user=None): |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 15 | used_sites = {} |
| 16 | used_deployments = {} |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 17 | sliverCount = 0 |
| 18 | for sliver in self.slivers.all(): |
Scott Baker | dc84412 | 2015-01-07 11:15:08 -0800 | [diff] [blame] | 19 | site = sliver.node.site_deployment.site |
| 20 | deployment = sliver.node.site_deployment.deployment |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 21 | used_sites[site.name] = used_sites.get(site.name, 0) + 1 |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 22 | used_deployments[deployment.name] = used_deployments.get(deployment.name, 0) + 1 |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 23 | sliverCount = sliverCount + 1 |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 24 | |
| 25 | roles = [] |
| 26 | if (user!=None): |
Scott Baker | c865508 | 2014-12-23 11:03:58 -0800 | [diff] [blame] | 27 | roles = [x.role.role for x in self.sliceprivileges.filter(user=user)] |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 28 | |
| 29 | return {"sitesUsed": used_sites, |
| 30 | "deploymentsUsed": used_deployments, |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 31 | "sliverCount": sliverCount, |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 32 | "siteCount": len(used_sites.keys()), |
| 33 | "roles": roles} |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 34 | |
Scott Baker | 9062b0c | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 35 | @property |
Scott Baker | 03696ee | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 36 | def site_allocation(self): |
| 37 | return self.getSliceInfo()["sitesUsed"] |
| 38 | |
| 39 | @site_allocation.setter |
| 40 | def site_allocation(self, value): |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 41 | self._update_site_allocation = value |
| 42 | #print "XXX set sitesUsed to", value |
Scott Baker | 03696ee | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 43 | |
| 44 | @property |
Scott Baker | fcf655e | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 45 | def users(self): |
| 46 | user_ids = [] |
| 47 | for priv in SlicePrivilege.objects.filter(slice=self): |
| 48 | if not (priv.user.id in user_ids): |
| 49 | user_ids.append(priv.user.id) |
| 50 | return user_ids |
| 51 | |
| 52 | @users.setter |
| 53 | def users(self, value): |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 54 | self._update_users = value |
| 55 | #print "XXX set users to", value |
Scott Baker | fcf655e | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 56 | |
| 57 | @property |
Scott Baker | 697512d | 2015-01-08 22:37:34 -0800 | [diff] [blame] | 58 | def network_ports(self): |
Scott Baker | 9062b0c | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 59 | # XXX this assumes there is only one network that can have ports bound |
| 60 | # to it for a given slice. This is intended for the tenant view, which |
| 61 | # will obey this field. |
| 62 | networkPorts = "" |
| 63 | for networkSlice in self.networkslices.all(): |
| 64 | network = networkSlice.network |
| 65 | if network.ports: |
| 66 | networkPorts = network.ports |
| 67 | |
| 68 | return networkPorts |
| 69 | |
Scott Baker | 697512d | 2015-01-08 22:37:34 -0800 | [diff] [blame] | 70 | @network_ports.setter |
| 71 | def network_ports(self, value): |
Scott Baker | 9062b0c | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 72 | print "XXX set networkPorts to", value |
| 73 | |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 74 | @staticmethod |
| 75 | def select_by_user(user): |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 76 | if user.is_admin: |
| 77 | qs = SlicePlus.objects.all() |
| 78 | else: |
| 79 | slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)] |
| 80 | qs = SlicePlus.objects.filter(id__in=slice_ids) |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 81 | return qs |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 82 | |
| 83 | def get_site_node_allocation(self, siteList): |
| 84 | siteIDList = [site.id for site in siteList] |
| 85 | nodeList = [] |
| 86 | for node in Node.objects.all(): |
| 87 | if (node.site_deployment.site.id in siteIDList): |
| 88 | node.sliverCount = 0 |
| 89 | for sliver in node.slivers.all(): |
| 90 | if sliver.slice.id == self.id: |
| 91 | node.sliverCount = node.sliverCount + 1 |
| 92 | nodeList.append(node) |
| 93 | return nodeList |
| 94 | |
| 95 | def save(self, *args, **kwargs): |
| 96 | super(SlicePlus, self).save(*args, **kwargs) |
| 97 | |
| 98 | if self._update_site_allocation: |
| 99 | self.save_site_allocation() |
| 100 | |
| 101 | def save_site_allocation(self, noAct = False): |
| 102 | new_site_allocation = self._update_site_allocation |
| 103 | |
| 104 | all_slice_slivers = self.slivers.all() # Sliver.objects.filter(slice=self) |
| 105 | for site_name in new_site_allocation.keys(): |
| 106 | desired_allocation = new_site_allocation[site_name] |
| 107 | |
| 108 | # make a list of the slivers for this site |
| 109 | slivers = [] |
| 110 | for sliver in all_slice_slivers: |
| 111 | if sliver.node.site_deployment.site.name == site_name: |
| 112 | slivers.append(sliver) |
| 113 | |
| 114 | # delete extra slivers |
| 115 | while (len(slivers) > desired_allocation): |
| 116 | sliver = slivers.pop() |
| 117 | print "deleting sliver", sliver |
| 118 | if (not noAct): |
| 119 | sliver.delete() |
| 120 | |
| 121 | # add more slivers |
| 122 | if (len(slivers) < desired_allocation): |
| 123 | site = Site.objects.get(name = site_name) |
| 124 | nodes = self.get_site_node_allocation([site]) |
| 125 | |
| 126 | if (not nodes): |
| 127 | print "XXX no nodes in site", site_name |
| 128 | continue |
| 129 | |
| 130 | while (len(slivers) < desired_allocation): |
| 131 | # pick the least allocated node |
| 132 | nodes = sorted(nodes, key=attrgetter("sliverCount")) |
| 133 | node = nodes[0] |
| 134 | |
| 135 | sliver = Sliver(name=node.name, |
| 136 | slice=self, |
| 137 | node=node, |
| 138 | image = self.default_image, |
| 139 | flavor = self.default_flavor, |
| 140 | creator = self.creator, |
| 141 | deployment = node.site_deployment.deployment) |
| 142 | slivers.append(sliver) |
| 143 | if (not noAct): |
| 144 | sliver.save() |
| 145 | |
| 146 | print "added sliver", sliver |
| 147 | |
| 148 | node.sliverCount = node.sliverCount + 1 |
| 149 | |
| 150 | |