+
+ job_info = self.raw_json
+ logger.debug("OARESTAPI ParseJobsIds %s" %(self.raw_json))
+ values = []
+ try:
+ for k in job_resources:
+ values.append(job_info[k])
+ return dict(zip(job_resources, values))
+
+ except KeyError:
+ logger.log_exc("ParseJobsIds KeyError ")
+
+
+ def ParseJobsIdResources(self):
+ """ BROKEN since oar 2.5
+ Parses the json produced by the request
+ /oarapi/jobs/id/resources.json.
+ Returns a list of oar node ids that are scheduled for the
+ given job id.
+
+ """
+ job_resources = []
+ for resource in self.raw_json['items']:
+ job_resources.append(resource['id'])
+
+ logger.debug("OARESTAPI \tParseJobsIdResources %s" %(self.raw_json))
+ return job_resources
+
+ def ParseResources(self) :
+ """ Parses the json produced by a get_resources request on oar."""
+
+ logger.debug("OARESTAPI \tParseResources " )
+ #resources are listed inside the 'items' list from the json
+ self.raw_json = self.raw_json['items']
+ self.ParseNodes()
+
+ def ParseReservedNodes(self):
+ """ Returns an array containing the list of the reserved nodes """
+
+ #resources are listed inside the 'items' list from the json
+ reservation_list = []
+ print "ParseReservedNodes_%s" %(self.raw_json['items'])
+ job = {}
+ #Parse resources info
+ for json_element in self.raw_json['items']:
+ job['t_from'] = json_element['scheduled_start']
+ #Get resources id list for the job
+ job['resource_ids'] = \
+ [ node_dict['id'] for node_dict in json_element['resources'] ]
+
+ job['state'] = json_element['state']
+ job['lease_id'] = json_element['id']
+ job['t_until'] = json_element['scheduled_start'] + \
+ json_element['walltime']
+ job['user'] = json_element['owner']
+ logger.debug("ParseReservedNodes________job %s" %(job))
+ reservation_list.append(job)
+ #reset dict
+ job = {}
+ return reservation_list
+
+ def ParseRunningJobs(self):
+ """ Gets the list of nodes currently in use from the attributes of the
+ running jobs.
+
+ """
+ logger.debug("OARESTAPI \tParseRunningJobs__________________________ ")
+ #resources are listed inside the 'items' list from the json
+ nodes = []
+ for job in self.raw_json['items']:
+ for node in job['nodes']:
+ nodes.append(node['network_address'])
+ return nodes
+
+
+
+ def ParseDeleteJobs(self):
+ """ No need to parse anything in this function.A POST
+ is done to delete the job.
+
+ """
+ return
+
+ def ParseResourcesFull(self) :
+ """ This method is responsible for parsing all the attributes
+ of all the nodes returned by OAR when issuing a get resources full.
+ The information from the nodes and the sites are separated.
+ Updates the node_dictlist so that the dictionnary of the platform's
+ nodes is available afterwards.
+
+ """
+ logger.debug("OARRESTAPI ParseResourcesFull________________________ ")
+ #print self.raw_json[1]
+ #resources are listed inside the 'items' list from the json
+ if self.version_json_dict['apilib_version'] != "0.2.10" :
+ self.raw_json = self.raw_json['items']
+ self.ParseNodes()
+ self.ParseSites()
+ return self.node_dictlist
+
+ def ParseResourcesFullSites(self) :
+ """ UNUSED. Originally used to get information from the sites.
+ ParseResourcesFull is used instead.
+
+ """
+ if self.version_json_dict['apilib_version'] != "0.2.10" :
+ self.raw_json = self.raw_json['items']
+ self.ParseNodes()
+ self.ParseSites()
+ return self.site_dict
+
+
+
+ def ParseNodes(self):
+ """ Parse nodes properties from OAR
+ Put them into a dictionary with key = node id and value is a dictionary
+ of the node properties and properties'values.
+
+ """
+ node_id = None
+ keys = self.resources_fulljson_dict.keys()
+ keys.sort()
+
+ for dictline in self.raw_json:
+ node_id = None
+ # dictionary is empty and/or a new node has to be inserted
+ node_id = self.resources_fulljson_dict['network_address'](\
+ self.node_dictlist, dictline['network_address'])
+ for k in keys:
+ if k in dictline:
+ if k == 'network_address':
+ continue
+
+ self.resources_fulljson_dict[k](\
+ self.node_dictlist[node_id], dictline[k])
+
+ #The last property has been inserted in the property tuple list,
+ #reset node_id
+ #Turn the property tuple list (=dict value) into a dictionary
+ self.node_dictlist[node_id] = dict(self.node_dictlist[node_id])
+ node_id = None
+
+
+ def hostname_to_hrn(self, root_auth, login_base, hostname):
+ return PlXrn(auth = root_auth, \
+ hostname = login_base + '_' + hostname).get_hrn()
+
+
+ def ParseSites(self):
+ """ Returns a list of dictionnaries containing the sites' attributes."""
+
+ nodes_per_site = {}
+ config = Config()
+ logger.debug(" OARrestapi.py \tParseSites self.node_dictlist %s"\
+ %(self.node_dictlist))
+ # Create a list of nodes per site_id
+ for node_id in self.node_dictlist.keys():
+ node = self.node_dictlist[node_id]
+
+ if node['site'] not in nodes_per_site:
+ nodes_per_site[node['site']] = []
+ nodes_per_site[node['site']].append(node['node_id'])
+ else:
+ if node['node_id'] not in nodes_per_site[node['site']]:
+ nodes_per_site[node['site']].append(node['node_id'])
+
+ #Create a site dictionary with key is site_login_base (name of the site)
+ # and value is a dictionary of properties, including the list
+ #of the node_ids
+ for node_id in self.node_dictlist.keys():
+ node = self.node_dictlist[node_id]
+ node.update({'hrn':self.hostname_to_hrn(self.interface_hrn, \
+ node['site'],node['hostname'])})
+
+ self.node_dictlist.update({node_id:node})
+
+ if node['site'] not in self.site_dict:
+ self.site_dict[node['site']] = {
+ 'site':node['site'],
+ 'node_ids':nodes_per_site[node['site']],
+ 'latitude':"48.83726",
+ 'longitude':"- 2.10336",'name':config.SFA_REGISTRY_ROOT_AUTH,
+ 'pcu_ids':[], 'max_slices':None, 'ext_consortium_id':None,
+ 'max_slivers':None, 'is_public':True, 'peer_site_id': None,
+ 'abbreviated_name':"senslab", 'address_ids': [],
+ 'url':"http,//www.senslab.info", 'person_ids':[],
+ 'site_tag_ids':[], 'enabled': True, 'slice_ids':[],
+ 'date_created': None, 'peer_id': None }
+ #if node['site_login_base'] not in self.site_dict.keys():
+ #self.site_dict[node['site_login_base']] = {'login_base':node['site_login_base'],
+ #'node_ids':nodes_per_site[node['site_login_base']],
+ #'latitude':"48.83726",
+ #'longitude':"- 2.10336",'name':"senslab",
+ #'pcu_ids':[], 'max_slices':None, 'ext_consortium_id':None,
+ #'max_slivers':None, 'is_public':True, 'peer_site_id': None,
+ #'abbreviated_name':"senslab", 'address_ids': [],
+ #'url':"http,//www.senslab.info", 'person_ids':[],
+ #'site_tag_ids':[], 'enabled': True, 'slice_ids':[],
+ #'date_created': None, 'peer_id': None }
+
+
+
+
+ OARrequests_uri_dict = {
+ 'GET_version':
+ {'uri':'/oarapi/version.json', 'parse_func': ParseVersion},
+ 'GET_timezone':
+ {'uri':'/oarapi/timezone.json' ,'parse_func': ParseTimezone },
+ 'GET_jobs':
+ {'uri':'/oarapi/jobs.json','parse_func': ParseJobs},
+ 'GET_jobs_id':
+ {'uri':'/oarapi/jobs/id.json','parse_func': ParseJobsIds},
+ 'GET_jobs_id_resources':
+ {'uri':'/oarapi/jobs/id/resources.json',\
+ 'parse_func': ParseJobsIdResources},
+ 'GET_jobs_table':
+ {'uri':'/oarapi/jobs/table.json','parse_func': ParseJobsTable},
+ 'GET_jobs_details':
+ {'uri':'/oarapi/jobs/details.json',\
+ 'parse_func': ParseJobsDetails},
+ 'GET_reserved_nodes':
+ {'uri':
+ '/oarapi/jobs/details.json?state=Running,Waiting,Launching',\
+ 'parse_func':ParseReservedNodes},
+ 'GET_running_jobs':
+ {'uri':'/oarapi/jobs/details.json?state=Running',\
+ 'parse_func':ParseRunningJobs},
+ 'GET_resources_full':
+ {'uri':'/oarapi/resources/full.json',\
+ 'parse_func': ParseResourcesFull},
+ 'GET_sites':
+ {'uri':'/oarapi/resources/full.json',\
+ 'parse_func': ParseResourcesFullSites},
+ 'GET_resources':
+ {'uri':'/oarapi/resources.json' ,'parse_func': ParseResources},
+ 'DELETE_jobs_id':
+ {'uri':'/oarapi/jobs/id.json' ,'parse_func': ParseDeleteJobs}
+ }
+
+
+
+ def SendRequest(self, request, strval = None , username = None):
+ """ Connects to OAR , sends the valid GET requests and uses
+ the appropriate json parsing functions.
+
+ """
+ if request in self.OARrequests_uri_dict :
+ self.raw_json = self.server.GETRequestToOARRestAPI(request, \
+ strval, \
+ username)
+ return self.OARrequests_uri_dict[request]['parse_func'](self)
+ else:
+ logger.error("OARRESTAPI OARGetParse __init__ : ERROR_REQUEST " \
+ %(request))
+