Scott Baker | c7f363b | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 1 | from core.models import Slice, SlicePrivilege, SliceRole, Sliver, Site, Node, User |
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 | |
Scott Baker | c7f363b | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 83 | def get_node_allocation(self, siteList): |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 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: |
Scott Baker | 79be5c0 | 2015-01-13 16:28:05 -0800 | [diff] [blame] | 99 | self.save_site_allocation(noAct=True) |
Scott Baker | c7f363b | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 100 | |
| 101 | if self._update_users: |
| 102 | self.save_users(noAct=True) |
| 103 | |
| 104 | if self._update_site_allocation: |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 105 | self.save_site_allocation() |
| 106 | |
Scott Baker | c7f363b | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 107 | if self._update_users: |
| 108 | self.save_users() |
| 109 | |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 110 | def save_site_allocation(self, noAct = False): |
| 111 | new_site_allocation = self._update_site_allocation |
| 112 | |
Scott Baker | c7f363b | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 113 | all_slice_slivers = self.slivers.all() |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 114 | for site_name in new_site_allocation.keys(): |
| 115 | desired_allocation = new_site_allocation[site_name] |
| 116 | |
| 117 | # make a list of the slivers for this site |
| 118 | slivers = [] |
| 119 | for sliver in all_slice_slivers: |
| 120 | if sliver.node.site_deployment.site.name == site_name: |
| 121 | slivers.append(sliver) |
| 122 | |
| 123 | # delete extra slivers |
| 124 | while (len(slivers) > desired_allocation): |
| 125 | sliver = slivers.pop() |
| 126 | print "deleting sliver", sliver |
| 127 | if (not noAct): |
| 128 | sliver.delete() |
| 129 | |
| 130 | # add more slivers |
| 131 | if (len(slivers) < desired_allocation): |
| 132 | site = Site.objects.get(name = site_name) |
Scott Baker | c7f363b | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 133 | nodes = self.get_node_allocation([site]) |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 134 | |
| 135 | if (not nodes): |
Scott Baker | 79be5c0 | 2015-01-13 16:28:05 -0800 | [diff] [blame] | 136 | raise ValueError("no nodes in site %s" % site_name) |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 137 | |
| 138 | while (len(slivers) < desired_allocation): |
| 139 | # pick the least allocated node |
| 140 | nodes = sorted(nodes, key=attrgetter("sliverCount")) |
| 141 | node = nodes[0] |
| 142 | |
| 143 | sliver = Sliver(name=node.name, |
| 144 | slice=self, |
| 145 | node=node, |
| 146 | image = self.default_image, |
| 147 | flavor = self.default_flavor, |
| 148 | creator = self.creator, |
| 149 | deployment = node.site_deployment.deployment) |
| 150 | slivers.append(sliver) |
| 151 | if (not noAct): |
| 152 | sliver.save() |
| 153 | |
| 154 | print "added sliver", sliver |
| 155 | |
| 156 | node.sliverCount = node.sliverCount + 1 |
| 157 | |
Scott Baker | c7f363b | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 158 | def save_users(self, noAct = False): |
| 159 | new_users = self._update_users |
| 160 | |
| 161 | default_role = SliceRole.objects.get(role="default") |
| 162 | |
| 163 | slice_privs = self.sliceprivileges.all() |
| 164 | slice_user_ids = [priv.user.id for priv in slice_privs] |
| 165 | |
| 166 | for user_id in new_users: |
| 167 | if (user_id not in slice_user_ids): |
| 168 | print "XXX", user_id |
| 169 | priv = SlicePrivilege(slice=self, user=User.objects.get(id=user_id), role=default_role) |
| 170 | if (not noAct): |
| 171 | priv.save() |
| 172 | |
| 173 | print "added user id", user_id |
| 174 | |
| 175 | for priv in slice_privs: |
| 176 | if (priv.role.id != default_role.id): |
| 177 | # only mess with 'default' users; don't kill an admin |
| 178 | continue |
| 179 | |
| 180 | if (priv.user.id not in new_users): |
| 181 | if (not noAct): |
| 182 | priv.delete() |
| 183 | |
| 184 | print "deleted user id", user_id |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 189 | |