- first shot at GetSlivers
[plcapi.git] / PLC / Methods / GetSlivers.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Auth import PasswordAuth
5 from PLC.Nodes import Node, Nodes
6 from PLC.NodeNetworks import NodeNetwork, NodeNetworks
7 from PLC.NodeGroups import NodeGroup, NodeGroups
8 from PLC.ConfFiles import ConfFile, ConfFiles
9 from PLC.Slices import Slice, Slices
10 from PLC.Persons import Person, Persons
11 from PLC.Keys import Key, Keys
12 from PLC.SliceAttributes import SliceAttribute, SliceAttributes
13
14 class GetSlivers(Method):
15     """
16     Returns an array of structs representing slivers (slices bound to
17     nodes). If node_id_or_hostname_list is specified, only slivers
18     bound to the specified nodes are queried.
19
20     All of the information returned by this call can be gathered from
21     other calls, e.g. GetNodes, GetNodeNetworks, GetSlices, etc. This
22     function exists primarily for the benefit of Node Manager and
23     Federation Manager.
24     """
25
26     roles = ['admin']
27
28     accepts = [
29         PasswordAuth(),
30         [Mixed(Node.fields['node_id'],
31                Node.fields['hostname'])]
32         ]
33
34     returns = [{
35         'timestamp': Parameter(int, "Timestamp of this call, in seconds since UNIX epoch"),
36         'id': Node.fields['node_id'],
37         'hostname': Node.fields['hostname'],
38         'boot_state': Node.fields['boot_state'],
39         'networks': [NodeNetwork.fields],
40         'groups': [NodeGroup.fields['name']],
41         'conf_files': [ConfFile.fields],
42         'slivers': [{
43             'name': Slice.fields['name'],
44             'id': Slice.fields['slice_id'],
45             'instantiation': Slice.fields['instantiation'],
46             'expires': Slice.fields['expires'],
47             'keys': [{
48                 'key_type': Key.fields['key_type'],
49                 'key': Key.fields['key']
50             }],
51             'attributes': [{
52                 'name': SliceAttribute.fields['name'],
53                 'value': SliceAttribute.fields['value']
54             }]
55         }]
56     }]
57
58     def call(self, auth, node_id_or_hostname_list = None):
59         timestamp = int(time.time())
60
61         all_nodes = Nodes(self.api, node_id_or_hostname_list)
62
63         nodenetwork_ids = set()
64         nodegroup_ids = set()
65         conf_file_ids = set()
66         slice_ids = set()
67         for node_id, node in all_nodes.iteritems():
68             nodenetwork_ids.update(node['nodenetwork_ids'])
69             nodegroup_ids.update(node['nodegroup_ids'])
70             conf_file_ids.update(node['conf_file_ids'])
71             slice_ids.update(node['slice_ids'])
72
73         # Get nodenetwork information
74         if nodenetwork_ids:
75             all_nodenetworks = NodeNetworks(self.api, nodenetwork_ids)
76         else:
77             all_nodenetworks = {}
78
79         # Get node group information
80         if nodegroup_ids:
81             all_nodegroups = NodeGroups(self.api, nodegroup_ids)
82
83             for nodegroup_id, nodegroup in all_nodegroups.iteritems():
84                 conf_file_ids.update(nodegroup['conf_file_ids'])
85         else:
86             all_nodegroups = {}
87
88         # Get configuration files
89         if conf_file_ids:
90             all_conf_files = ConfFiles(self.api, conf_file_ids)
91         else:
92             all_conf_files = {}
93
94         if slice_ids:
95             # Get slices
96             all_slices = Slices(self.api, slice_ids)
97
98             person_ids = set()
99             slice_attribute_ids = set()
100             for slice_id, slice in all_slices.iteritems():
101                 person_ids.update(slice['person_ids'])
102                 slice_attribute_ids.update(slice['slice_attribute_ids'])
103
104             # Get user accounts
105             all_persons = Persons(self.api, person_ids)
106
107             key_ids = set()
108             for person_id, person in all_persons.iteritems():
109                 key_ids.update(person['key_ids'])
110
111             # Get user account keys
112             all_keys = Keys(self.api, key_ids)
113
114             # Get slice attributes
115             all_slice_attributes = SliceAttributes(self.api, slice_attribute_ids)
116
117         nodes = []
118         for node_id, node in all_nodes.iteritems():
119             networks = [all_nodenetworks[nodenetwork_id] for nodenetwork_id in node['nodenetwork_ids']]
120             nodegroups = [all_nodegroups[nodegroup_id] for nodegroup_id in node['nodegroup_ids']]
121             groups = [nodegroup['name'] for nodegroup in nodegroups]
122
123             # If a node belongs to multiple node
124             # groups for which the same configuration file is defined,
125             # it is undefined which one takes precedence.
126             conf_files = {}
127             for nodegroup in nodegroups:
128                 for conf_file in map(lambda id: all_conf_files[id], nodegroup['conf_file_ids']):
129                     conf_files[conf_file['dest']] = conf_file
130
131             # Node configuration files always take precedence over
132             # node group configuration files.
133             for conf_file in map(lambda id: all_conf_files[id], node['conf_file_ids']):
134                 conf_files[conf_file['dest']] = conf_file
135
136             slivers = []
137             for slice in map(lambda id: all_slices[id], node['slice_ids']):
138                 keys = []
139                 for person in map(lambda id: all_persons[id], slice['person_ids']):
140                     keys += [{'key_type': all_keys[key_id]['key_type'],
141                               'key': all_keys[key_id]['key']} \
142                              for key_id in person['key_ids']]
143
144                 attributes = {}
145                 for slice_attribute in map(lambda id: all_slice_attributes[id],
146                                            slice['slice_attribute_ids']):
147                     # Per-node sliver attributes (slice attributes
148                     # with non-null node_id fields) take precedence
149                     # over global slice attributes.
150                     if not attributes.has_key(slice_attribute['name']) or \
151                        slice_attribute['node_id'] is not None:
152                         attributes[slice_attribute['name']] = {
153                             'name': slice_attribute['name'],
154                             'value': slice_attribute['value']
155                             }
156
157                 slivers.append({
158                     'name': slice['name'],
159                     'slice_id': slice['slice_id'],
160                     'instantiation': slice['instantiation'],
161                     'expires': slice['expires'],
162                     'keys': keys,
163                     'attributes': attributes.values()
164                     })
165
166             nodes.append({
167                 'timestamp': timestamp,
168                 'id': node['node_id'],
169                 'hostname': node['hostname'],
170                 'networks': networks,
171                 'groups': groups,
172                 'conf_files': conf_files.values(),
173                 'slivers': slivers
174                 })
175
176         return nodes