Changing iotlabapi to iotlabshell - propagating the changes
[sfa.git] / sfa / iotlab / iotlabaggregate.py
1 """
2 File providing methods to generate valid RSpecs for the Iotlab testbed.
3 Contains methods to get information on slice, slivers, nodes and leases,
4 formatting them and turn it into a RSpec.
5 """
6 from sfa.util.xrn import hrn_to_urn, urn_to_hrn, get_authority
7
8 from sfa.rspecs.rspec import RSpec
9 #from sfa.rspecs.elements.location import Location
10 from sfa.rspecs.elements.hardware_type import HardwareType
11 from sfa.rspecs.elements.login import Login
12 from sfa.rspecs.elements.services import Services
13 from sfa.rspecs.elements.sliver import Sliver
14 from sfa.rspecs.elements.lease import Lease
15 from sfa.rspecs.elements.granularity import Granularity
16 from sfa.rspecs.version_manager import VersionManager
17
18 from sfa.rspecs.elements.versions.iotlabv1Node import IotlabPosition, \
19     IotlabNode, IotlabLocation
20
21 from sfa.util.sfalogging import logger
22 from sfa.util.xrn import Xrn
23
24
25 def iotlab_xrn_to_hostname(xrn):
26     """Returns a node's hostname from its xrn.
27     :param xrn: The nodes xrn identifier.
28     :type xrn: Xrn (from sfa.util.xrn)
29
30     :returns: node's hostname.
31     :rtype: string
32
33     """
34     return Xrn.unescape(Xrn(xrn=xrn, type='node').get_leaf())
35
36
37 def iotlab_xrn_object(root_auth, hostname):
38     """Creates a valid xrn object from the node's hostname and the authority
39     of the SFA server.
40
41     :param hostname: the node's hostname.
42     :param root_auth: the SFA root authority.
43     :type hostname: string
44     :type root_auth: string
45
46     :returns: the iotlab node's xrn
47     :rtype: Xrn
48
49     """
50     return Xrn('.'.join([root_auth, Xrn.escape(hostname)]), type='node')
51
52
53 class IotlabAggregate:
54     """Aggregate manager class for Iotlab. """
55
56     sites = {}
57     nodes = {}
58     api = None
59     interfaces = {}
60     links = {}
61     node_tags = {}
62
63     prepared = False
64
65     user_options = {}
66
67     def __init__(self, driver):
68         self.driver = driver
69
70     def get_slice_and_slivers(self, slice_xrn, login=None):
71         """
72         Get the slices and the associated leases if any from the iotlab
73             testbed. One slice can have mutliple leases.
74             For each slice, get the nodes in the  associated lease
75             and create a sliver with the necessary info and insert it into the
76             sliver dictionary, keyed on the node hostnames.
77             Returns a dict of slivers based on the sliver's node_id.
78             Called by get_rspec.
79
80
81         :param slice_xrn: xrn of the slice
82         :param login: user's login on iotlab ldap
83
84         :type slice_xrn: string
85         :type login: string
86         :returns: a list of slices dict and a list of Sliver object
87         :rtype: (list, list)
88
89         .. note: There is no real slivers in iotlab, only leases. The goal
90             is to be consistent with the SFA standard.
91
92         """
93         slivers = {}
94         sfa_slice = None
95         if slice_xrn is None:
96             return (sfa_slice, slivers)
97         slice_urn = hrn_to_urn(slice_xrn, 'slice')
98         slice_hrn, _ = urn_to_hrn(slice_xrn)
99         slice_name = slice_hrn
100
101         # GetSlices always returns a list, even if there is only one element
102         slices = self.driver.testbed_shell.GetSlices(slice_filter=str(slice_name),
103                                                   slice_filter_type='slice_hrn',
104                                                   login=login)
105
106         logger.debug("IotlabAggregate api \tget_slice_and_slivers \
107                       slice_hrn %s \r\n slices %s self.driver.hrn %s"
108                      % (slice_hrn, slices, self.driver.hrn))
109         if slices == []:
110             return (sfa_slice, slivers)
111
112         # sort slivers by node id , if there is a job
113         #and therefore, node allocated to this slice
114         # for sfa_slice in slices:
115         sfa_slice = slices[0]
116         try:
117             node_ids_list = sfa_slice['node_ids']
118         except KeyError:
119             logger.log_exc("IOTLABAGGREGATE \t \
120                         get_slice_and_slivers No nodes in the slice \
121                         - KeyError ")
122             node_ids_list = []
123             # continue
124
125         for node in node_ids_list:
126             sliver_xrn = Xrn(slice_urn, type='sliver', id=node)
127             sliver_xrn.set_authority(self.driver.hrn)
128             sliver = Sliver({'sliver_id': sliver_xrn.urn,
129                             'name': sfa_slice['hrn'],
130                             'type': 'iotlab-node',
131                             'tags': []})
132
133             slivers[node] = sliver
134
135         #Add default sliver attribute :
136         #connection information for iotlab
137         # if get_authority(sfa_slice['hrn']) == self.driver.testbed_shell.root_auth:
138         #     tmp = sfa_slice['hrn'].split('.')
139         #     ldap_username = tmp[1].split('_')[0]
140         #     ssh_access = None
141         #     slivers['default_sliver'] = {'ssh': ssh_access,
142         #                                  'login': ldap_username}
143         # look in ldap:
144         ldap_username = self.find_ldap_username_from_slice(sfa_slice)
145
146         if ldap_username is not None:
147             ssh_access = None
148             slivers['default_sliver'] = {'ssh': ssh_access,
149                                              'login': ldap_username}
150
151
152         logger.debug("IOTLABAGGREGATE api get_slice_and_slivers  slivers %s "
153                      % (slivers))
154         return (slices, slivers)
155
156     def find_ldap_username_from_slice(self, sfa_slice):
157         researchers = [sfa_slice['reg_researchers'][0].__dict__]
158         # look in ldap:
159         ldap_username = None
160         ret =  self.driver.testbed_shell.GetPersons(researchers)
161         if len(ret) != 0:
162             ldap_username = ret[0]['uid']
163
164         return ldap_username
165
166
167     def get_nodes(self, slices=None, slivers=[], options=None):
168         """Returns the nodes in the slice using the rspec format, with all the
169         nodes' properties.
170
171         Fetch the nodes ids in the slices dictionary and get all the nodes
172         properties from OAR. Makes a rspec dicitonary out of this and returns
173         it. If the slice does not have any job running or scheduled, that is
174         it has no reserved nodes, then returns an empty list.
175
176         :param slices: list of slices (record dictionaries)
177         :param slivers: the list of slivers in all the slices
178         :type slices: list of dicts
179         :type slivers: list of Sliver object (dictionaries)
180         :returns: An empty list if the slice has no reserved nodes, a rspec
181             list with all the nodes and their properties (a dict per node)
182             otherwise.
183         :rtype: list
184
185         .. seealso:: get_slice_and_slivers
186
187         """
188         # NT: the semantic of this function is not clear to me :
189         # if slice is not defined, then all the nodes should be returned
190         # if slice is defined, we should return only the nodes that
191         # are part of this slice
192         # but what is the role of the slivers parameter ?
193         # So i assume that slice['node_ids'] will be the same as slivers for us
194         slice_nodes_list = []
195         if slices is not None:
196             for one_slice in slices:
197                 try:
198                     slice_nodes_list = one_slice['node_ids']
199                      # if we are dealing with a slice that has no node just
200                      # return an empty list. In iotlab a slice can have multiple
201                      # jobs scheduled, so it either has at least one lease or
202                      # not at all.
203                 except KeyError:
204                     return []
205
206         # get the granularity in second for the reservation system
207         grain = self.driver.testbed_shell.GetLeaseGranularity()
208
209         nodes = self.driver.testbed_shell.GetNodes()
210
211         nodes_dict = {}
212
213         #if slices, this means we got to list all the nodes given to this slice
214         # Make a list of all the nodes in the slice before getting their
215         #attributes
216         rspec_nodes = []
217
218         logger.debug("IOTLABAGGREGATE api get_nodes slices  %s "
219                      % (slices))
220
221         reserved_nodes = self.driver.testbed_shell.GetNodesCurrentlyInUse()
222         logger.debug("IOTLABAGGREGATE api get_nodes slice_nodes_list  %s "
223                      % (slice_nodes_list))
224         for node in nodes:
225             nodes_dict[node['node_id']] = node
226             if slice_nodes_list == [] or node['hostname'] in slice_nodes_list:
227
228                 rspec_node = IotlabNode()
229                 # xxx how to retrieve site['login_base']
230                 #site_id=node['site_id']
231                 #site=sites_dict[site_id]
232
233                 rspec_node['mobile'] = node['mobile']
234                 rspec_node['archi'] = node['archi']
235                 rspec_node['radio'] = node['radio']
236
237                 iotlab_xrn = iotlab_xrn_object(self.driver.testbed_shell.root_auth,
238                                                node['hostname'])
239                 rspec_node['component_id'] = iotlab_xrn.urn
240                 rspec_node['component_name'] = node['hostname']
241                 rspec_node['component_manager_id'] = \
242                                 hrn_to_urn(self.driver.testbed_shell.root_auth,
243                                 'authority+sa')
244
245                 # Iotlab's nodes are federated : there is only one authority
246                 # for all Iotlab sites, registered in SFA.
247                 # Removing the part including the site
248                 # in authority_id SA 27/07/12
249                 rspec_node['authority_id'] = rspec_node['component_manager_id']
250
251                 # do not include boot state (<available> element)
252                 #in the manifest rspec
253
254
255                 rspec_node['boot_state'] = node['boot_state']
256                 if node['hostname'] in reserved_nodes:
257                     rspec_node['boot_state'] = "Reserved"
258                 rspec_node['exclusive'] = 'true'
259                 rspec_node['hardware_types'] = [HardwareType({'name': \
260                                                 'iotlab-node'})]
261
262
263                 location = IotlabLocation({'country':'France', 'site': \
264                                             node['site']})
265                 rspec_node['location'] = location
266
267
268                 position = IotlabPosition()
269                 for field in position :
270                     try:
271                         position[field] = node[field]
272                     except KeyError, error :
273                         logger.log_exc("IOTLABAGGREGATE\t get_nodes \
274                                                         position %s "% (error))
275
276                 rspec_node['position'] = position
277                 #rspec_node['interfaces'] = []
278
279                 # Granularity
280                 granularity = Granularity({'grain': grain})
281                 rspec_node['granularity'] = granularity
282                 rspec_node['tags'] = []
283                 if node['hostname'] in slivers:
284                     # add sliver info
285                     sliver = slivers[node['hostname']]
286                     rspec_node['sliver_id'] = sliver['sliver_id']
287                     rspec_node['client_id'] = node['hostname']
288                     rspec_node['slivers'] = [sliver]
289
290                     # slivers always provide the ssh service
291                     login = Login({'authentication': 'ssh-keys', \
292                             'hostname': node['hostname'], 'port':'22', \
293                             'username': sliver['name']})
294                     service = Services({'login': login})
295                     rspec_node['services'] = [service]
296                 rspec_nodes.append(rspec_node)
297
298         return (rspec_nodes)
299
300     def get_all_leases(self, ldap_username):
301         """
302
303         Get list of lease dictionaries which all have the mandatory keys
304         ('lease_id', 'hostname', 'site_id', 'name', 'start_time', 'duration').
305         All the leases running or scheduled are returned.
306
307         :param ldap_username: if ldap uid is not None, looks for the leases
308         belonging to this user.
309         :type ldap_username: string
310         :returns: rspec lease dictionary with keys lease_id, component_id,
311             slice_id, start_time, duration.
312         :rtype: dict
313
314         .. note::There is no filtering of leases within a given time frame.
315             All the running or scheduled leases are returned. options
316             removed SA 15/05/2013
317
318
319         """
320
321         #now = int(time.time())
322         #lease_filter = {'clip': now }
323
324         #if slice_record:
325             #lease_filter.update({'name': slice_record['name']})
326
327         #leases = self.driver.testbed_shell.GetLeases(lease_filter)
328
329         logger.debug("IOTLABAGGREGATE  get_all_leases ldap_username %s "
330                      % (ldap_username))
331         leases = self.driver.testbed_shell.GetLeases(login=ldap_username)
332         grain = self.driver.testbed_shell.GetLeaseGranularity()
333         # site_ids = []
334         rspec_leases = []
335         for lease in leases:
336             #as many leases as there are nodes in the job
337             for node in lease['reserved_nodes']:
338                 rspec_lease = Lease()
339                 rspec_lease['lease_id'] = lease['lease_id']
340                 #site = node['site_id']
341                 iotlab_xrn = iotlab_xrn_object(self.driver.testbed_shell.root_auth,
342                                                node)
343                 rspec_lease['component_id'] = iotlab_xrn.urn
344                 #rspec_lease['component_id'] = hostname_to_urn(self.driver.hrn,\
345                                         #site, node['hostname'])
346                 try:
347                     rspec_lease['slice_id'] = lease['slice_id']
348                 except KeyError:
349                     #No info on the slice used in testbed_xp table
350                     pass
351                 rspec_lease['start_time'] = lease['t_from']
352                 rspec_lease['duration'] = (lease['t_until'] - lease['t_from']) \
353                      / grain
354                 rspec_leases.append(rspec_lease)
355         return rspec_leases
356
357     def get_rspec(self, slice_xrn=None, login=None, version=None,
358                   options=None):
359         """
360         Returns xml rspec:
361         - a full advertisement rspec with the testbed resources if slice_xrn is
362         not specified.If a lease option is given, also returns the leases
363         scheduled on the testbed.
364         - a manifest Rspec with the leases and nodes in slice's leases if
365         slice_xrn is not None.
366
367         :param slice_xrn: srn of the slice
368         :type slice_xrn: string
369         :param login: user'uid (ldap login) on iotlab
370         :type login: string
371         :param version: can be set to sfa or iotlab
372         :type version: RSpecVersion
373         :param options: used to specify if the leases should also be included in
374             the returned rspec.
375         :type options: dict
376
377         :returns: Xml Rspec.
378         :rtype: XML
379
380
381         """
382
383         ldap_username= None
384         rspec = None
385         version_manager = VersionManager()
386         version = version_manager.get_version(version)
387         logger.debug("IotlabAggregate \t get_rspec ***version %s \
388                     version.type %s  version.version %s options %s \r\n"
389                      % (version, version.type, version.version, options))
390
391         if slice_xrn is None:
392             rspec_version = version_manager._get_version(version.type,
393                                                          version.version, 'ad')
394
395         else:
396             rspec_version = version_manager._get_version(
397                 version.type, version.version, 'manifest')
398
399         slices, slivers = self.get_slice_and_slivers(slice_xrn, login)
400         if slice_xrn and slices is not None:
401             #Get user associated with this slice
402             #for one_slice in slices :
403             ldap_username = self.find_ldap_username_from_slice(slices[0])
404             # ldap_username = slices[0]['reg_researchers'][0].__dict__['hrn']
405             #  # ldap_username = slices[0]['user']
406             # tmp = ldap_username.split('.')
407             # ldap_username = tmp[1]
408             logger.debug("IotlabAggregate \tget_rspec **** \
409                     LDAP USERNAME %s \r\n" \
410                     % (ldap_username))
411         #at this point sliver may be empty if no iotlab job
412         #is running for this user/slice.
413         rspec = RSpec(version=rspec_version, user_options=options)
414
415         logger.debug("\r\n \r\n IotlabAggregate \tget_rspec *** \
416                       slice_xrn %s slices  %s\r\n \r\n"
417                      % (slice_xrn, slices))
418
419         if options is not None:
420             lease_option = options['list_leases']
421         else:
422             #If no options are specified, at least print the resources
423             lease_option = 'all'
424            #if slice_xrn :
425                #lease_option = 'all'
426
427         if lease_option in ['all', 'resources']:
428         #if not options.get('list_leases') or options.get('list_leases')
429         #and options['list_leases'] != 'leases':
430             nodes = self.get_nodes(slices, slivers)
431             logger.debug("\r\n")
432             logger.debug("IotlabAggregate \t lease_option %s \
433                           get rspec  ******* nodes %s"
434                          % (lease_option, nodes))
435
436             sites_set = set([node['location']['site'] for node in nodes])
437
438             #In case creating a job,  slice_xrn is not set to None
439             rspec.version.add_nodes(nodes)
440             if slice_xrn and slices is not None:
441             #     #Get user associated with this slice
442             #     #for one_slice in slices :
443             #     ldap_username = slices[0]['reg_researchers']
444             #      # ldap_username = slices[0]['user']
445             #     tmp = ldap_username.split('.')
446             #     ldap_username = tmp[1]
447             #      # ldap_username = tmp[1].split('_')[0]
448
449                 logger.debug("IotlabAggregate \tget_rspec **** \
450                         version type %s ldap_ user %s \r\n" \
451                         % (version.type, ldap_username))
452                 if version.type == "Iotlab":
453                     rspec.version.add_connection_information(
454                         ldap_username, sites_set)
455
456             default_sliver = slivers.get('default_sliver', [])
457             if default_sliver and len(nodes) is not 0:
458                 #default_sliver_attribs = default_sliver.get('tags', [])
459                 logger.debug("IotlabAggregate \tget_rspec **** \
460                         default_sliver%s \r\n" % (default_sliver))
461                 for attrib in default_sliver:
462                     rspec.version.add_default_sliver_attribute(
463                         attrib, default_sliver[attrib])
464
465         if lease_option in ['all','leases']:
466             leases = self.get_all_leases(ldap_username)
467             rspec.version.add_leases(leases)
468             logger.debug("IotlabAggregate \tget_rspec **** \
469                        FINAL RSPEC %s \r\n" % (rspec.toxml()))
470         return rspec.toxml()