Trying to fix iotlab driver, need to be tested
Loic Baron [Wed, 30 Jul 2014 05:17:49 +0000 (07:17 +0200)]
sfa/iotlab/LDAPapi.py
sfa/iotlab/OARrestapi.py
sfa/iotlab/iotlabaggregate.py
sfa/iotlab/iotlabdriver.py
sfa/iotlab/iotlabshell.py
sfa/iotlab/iotlabslices.py

index 15067ac..32dbe95 100644 (file)
@@ -466,6 +466,8 @@ class LDAPapi:
                 if record['first_name'] != record['last_name']:
                     req_ldapdict['cn'] = str(record['first_name'])+" "\
                         + str(record['last_name'])
+            if 'uid' in record:
+                req_ldapdict['uid'] = record['uid']
             if 'email' in record:
                 req_ldapdict['mail'] = record['email']
             if 'mail' in record:
@@ -1021,4 +1023,4 @@ class LDAPapi:
         else:
         #Asked for all users in ldap
             results = self._process_ldap_info_for_all_users(result_data)
-        return results
\ No newline at end of file
+        return results
index 21c78d3..575473d 100644 (file)
@@ -228,8 +228,8 @@ class OARrestapi:
         #                    % (error))
             #raise ServerError("GET_OAR_SRVR : Could not reach OARserver")
         if resp.status >= 400:
-            raise ValueError ("Response Error %s, %s" %(resp.status,
-                resp.reason))
+            raise ValueError ("Response Error %s, %s, %s" %(resp.status,
+                resp.reason, resp.read()))
         try:
             js_dict = json.loads(body)
             #print "\r\n \t\t\t js_dict keys" , js_dict.keys(), " \r\n", js_dict
@@ -261,6 +261,7 @@ class OARrestapi:
                                                 str(datadict['strval']))
             del datadict['strval']
 
+        logger.debug("JORDAN USERNAME FOR OAR QUERY: %r to server %r" % (username, self.oarserver))
         data = json.dumps(datadict)
         headers = {'X-REMOTE_IDENT':username, \
                 'content-type': self.POST_FORMAT['json']['content'], \
@@ -286,8 +287,8 @@ class OARrestapi:
             conn.close()
 
         if resp.status >= 400:
-            raise ValueError ("Response Error %s, %s" %(resp.status,
-                resp.reason))
+            raise ValueError ("Response Error %s, %s: %r" %(resp.status,
+                resp.reason, body))
 
 
         try:
index 34eb38f..52acfad 100644 (file)
@@ -403,7 +403,9 @@ class IotlabAggregate:
     def get_leases(self, slice=None, options={}):
         filter={}
         if slice:
-           filter.update({'name':slice['slice_name']})
+           #filter.update({'name':slice['slice_name']}) # JORDAN: this is = "upmc" !!!
+           filter.update({'slice_hrn':slice['slice_hrn']}) # JORDAN: this is = "upmc" !!!
+            # slice_hrn = "ple.upmc.myslicedemo
         #return_fields = ['lease_id', 'hostname', 'site_id', 'name', 't_from', 't_until']
         leases = self.driver.GetLeases(lease_filter_dict=filter)
         grain = self.driver.testbed_shell.GetLeaseGranularity()
@@ -613,7 +615,7 @@ class IotlabAggregate:
         .. seealso:: http://groups.geni.net/geni/wiki/GAPI_AM_API_V3/CommonConcepts#urns
         """
 
-
+        SLICE_KEY = 'slice_hrn' # slice_hrn
         slice_ids = set()
         node_ids = []
         for urn in urns:
@@ -639,26 +641,34 @@ class IotlabAggregate:
                        \r\n" % (xrn, slice_names))
         filter_sliver = {}
         if slice_names:
-            filter_sliver['slice_hrn'] = list(slice_names)
-            slice_hrn = filter_sliver['slice_hrn'][0]
+            filter_sliver[SLICE_KEY] = list(slice_names)
+            slice_hrn = filter_sliver[SLICE_KEY][0]
 
-            slice_filter_type = 'slice_hrn'
+            slice_filter_type = SLICE_KEY
 
         # if slice_ids:
         #     filter['slice_id'] = list(slice_ids)
         # # get slices
         if slice_hrn:
+            logger.debug("JORDAN SLICE_HRN=%r" % slice_hrn)
             slices = self.driver.GetSlices(slice_hrn,
                 slice_filter_type)
-            leases = self.driver.GetLeases({'slice_hrn':slice_hrn})
+            leases = self.driver.GetLeases({SLICE_KEY:slice_hrn})
         logger.debug("IotlabAggregate \t get_slivers \
                        slices %s leases %s\r\n" % (slices, leases ))
         if not slices:
             return []
 
+        logger.debug("LOIC SLICES = %r" % slices)
         single_slice = slices[0]
         # get sliver users
-        user = single_slice['reg_researchers'][0].__dict__
+
+        # XXX LOIC !!! XXX QUICK AND DIRTY - Let's try...
+        logger.debug("LOIC Number of reg_researchers = %s" % len(single_slice['reg_researchers']))
+        if 'reg_researchers' in single_slice and len(single_slice['reg_researchers'])==0:
+            user = {'uid':single_slice['user']}
+        else:
+            user = single_slice['reg_researchers'][0].__dict__
         logger.debug("IotlabAggregate \t get_slivers user %s \
                        \r\n" % (user))
 
@@ -668,14 +678,19 @@ class IotlabAggregate:
                        \r\n" % (person))
         # name = person['last_name']
         user['login'] = person['uid']
-        user['user_urn'] = hrn_to_urn(user['hrn'], 'user')
-        user['keys'] = person['pkey']
+
+        # XXX LOIC !!! if we have more info, let's fill user
+        if 'hrn' in user:
+            user['user_urn'] = hrn_to_urn(user['hrn'], 'user')
+        if 'keys' in user:
+            user['keys'] = person['pkey']
 
 
         try:
             node_ids = single_slice['node_ids']
-            node_list = self.driver.testbed_shell.GetNodes(
-                    {'hostname':single_slice['node_ids']})
+            node_list = self.driver.testbed_shell.GetNodes()
+# JORDAN REMOVED FILTER so that next check always succeed
+#                    {'hostname':single_slice['node_ids']})
             node_by_hostname = dict([(node['hostname'], node)
                                         for node in node_list])
         except KeyError:
@@ -695,8 +710,13 @@ class IotlabAggregate:
                 node_id = current_lease['resource_ids'][index]
                 # node['slice_name'] = user['login']
                 # node.update(single_slice)
+                # JORDAN XXX This fails sometimes when hostname not in the list
+                #if hostname in node_by_hostname:
                 more_info = node_by_hostname[hostname]
                 node.update(more_info)
+                #else:
+                #    # This can happen when specifying a lease without the resource, then all subsequent calls will fail
+                #    logger.debug("Ignored missing hostname for now one")
                 # oar_job_id is the slice_id (lease_id)
                 sliver_hrn = '%s.%s-%s' % (self.driver.hrn,
                             current_lease['lease_id'], node_id)
@@ -803,6 +823,7 @@ class IotlabAggregate:
         # get slivers
         geni_slivers = []
         slivers = self.get_slivers(urns, options)
+        logger.debug("SLIVERS=%r" % slivers)
         if slivers:
             rspec_expires = datetime_to_string(utcparse(slivers[0]['expires']))
         else:
@@ -820,7 +841,9 @@ class IotlabAggregate:
             geni_urn = sliver_allocation.slice_urn
             sliver_allocation_dict[sliver_allocation.sliver_id] = \
                                                             sliver_allocation
-        if not options.get('list_leases') or options['list_leases'] != 'leases':                                                    
+        show_leases = options.get('list_leases')
+        if show_leases in ['resources', 'all']:
+        #if not options.get('list_leases') or options['list_leases'] != 'leases':                                                    
             # add slivers
             nodes_dict = {}
             for sliver in slivers:
@@ -835,9 +858,12 @@ class IotlabAggregate:
                 geni_slivers.append(geni_sliver)
             rspec.version.add_nodes(rspec_nodes)
 
-        if not options.get('list_leases') or options['list_leases'] == 'resources':
+        logger.debug("SHOW LEASES = %r" % show_leases)
+        if show_leases in ['leases', 'all']:
+        #if not options.get('list_leases') or options['list_leases'] == 'resources':
             if slivers:
-                leases = self.get_leases(slivers[0])
+                leases = self.get_leases(slice=slivers[0])
+                logger.debug("JORDAN: getting leases from slice: %r" % slivers[0])
                 rspec.version.add_leases(leases)
 
         return {'geni_urn': geni_urn,
index eb055d9..bbe362f 100644 (file)
@@ -135,7 +135,7 @@ class IotlabDriver(Driver):
 
 
 
-    def AddPerson(self, record):
+    def AddPerson(self, record, add_to_ldap = True):
         """
 
         Adds a new account. Any fields specified in records are used,
@@ -150,6 +150,10 @@ class IotlabDriver(Driver):
         :rtype: dict
 
         """
+        if not add_to_ldap:
+            ret = self.__add_person_to_db(record)
+            return ret
+
         ret = self.testbed_shell.ldap.LdapAddUser(record)
 
         if ret['bool'] is True:
@@ -173,6 +177,7 @@ class IotlabDriver(Driver):
         """
         query = self.api.dbsession().query(RegUser)
         check_if_exists = query.filter_by(email = user_dict['email']).first()
+        logger.debug("LOIC __add_person_to_db %s" % check_if_exists)
         #user doesn't exists
         if not check_if_exists:
             logger.debug("__add_person_to_db \t Adding %s \r\n \r\n \
@@ -305,6 +310,7 @@ class IotlabDriver(Driver):
         .. seealso::_sql_get_slice_info_from_user
         .. seealso:: _sql_get_slice_info
         """
+        logger.debug("JORDAN get_slice_records slice_filter=%r slice_filter_type=%r" % (slice_filter, slice_filter_type))
 
         #Get list of slices based on the slice hrn
         if slice_filter_type == 'slice_hrn':
@@ -363,8 +369,10 @@ class IotlabDriver(Driver):
 
         #First try to get information on the slice based on the filter provided
         if slice_filter_type in authorized_filter_types_list:
+            logger.debug("JORDAN GET SLICES 1")
             fixed_slicerec_dict = self._get_slice_records(slice_filter,
                                                     slice_filter_type)
+            logger.debug("JORDAN GET SLICE RECORDS %r" % fixed_slicerec_dict)
             # if the slice was not found in the sfa db
             if fixed_slicerec_dict is None:
                 return return_slicerec_dictlist
@@ -386,12 +394,14 @@ class IotlabDriver(Driver):
             #If no job is running or no job scheduled
             #return only the slice record
             if leases_list == [] and fixed_slicerec_dict:
+                logger.debug("JORDAN CASE 1")
                 return_slicerec_dictlist.append(fixed_slicerec_dict)
 
             # if the jobs running don't belong to the user/slice we are looking
             # for
             leases_hrn = [lease['slice_hrn'] for lease in leases_list]
             if slice_hrn not in leases_hrn:
+                logger.debug("JORDAN CASE 2")
                 return_slicerec_dictlist.append(fixed_slicerec_dict)
             #If several jobs for one slice , put the slice record into
             # each lease information dict
@@ -436,6 +446,7 @@ class IotlabDriver(Driver):
 
 
         else:
+            logger.debug("JORDAN GET SLICES 2")
             #Get all slices from the iotlab sfa database ,
             #put them in dict format
             #query_slice_list = dbsession.query(RegRecord).all()
@@ -704,7 +715,7 @@ class IotlabDriver(Driver):
 
         sfa_record = RegSlice(hrn=slice_record['hrn'],
                               gid=slice_record['gid'],
-                              pointer=slice_record['slice_id'],
+                              #pointer=slice_record['slice_id'],
                               authority=slice_record['authority'])
         logger.debug("IOTLAB_API.PY AddSlice  sfa_record %s user_record %s"
                      % (sfa_record, user_record))
@@ -712,7 +723,10 @@ class IotlabDriver(Driver):
         self.api.dbsession().add(sfa_record)
         self.api.dbsession().commit()
         #Update the reg-researchers dependency table
-        sfa_record.reg_researchers = [user_record]
+        if user_record is not None:
+            sfa_record.reg_researchers = [user_record]
+        else:
+            sfa_record.reg_researchers = slice_record['reg-researchers']
         self.api.dbsession().commit()
 
         return
@@ -1465,30 +1479,59 @@ class IotlabDriver(Driver):
 
         # ensure site record exists
 
-        # ensure slice record exists
-
-        current_slice = slices.verify_slice(xrn.hrn, slice_record, sfa_peer)
-        logger.debug("IOTLABDRIVER.PY \t ===============allocate \t\
-                            \r\n \r\n  current_slice %s" % (current_slice))
         # ensure person records exists
 
         # oui c'est degueulasse, le slice_record se retrouve modifie
         # dans la methode avec les infos du user, els infos sont propagees
         # dans verify_slice_leases
         logger.debug("IOTLABDRIVER.PY  BEFORE slices.verify_persons")
-        persons = slices.verify_persons(xrn.hrn, slice_record, users,
-                                        options=options)
+        # XXX JORDAN XXX slice_record devrait recevoir le caller_xrn...
+        logger.debug("LOIC users = %r" % users)
+        # XXX LOIC XXX Trying here to bypass the verify_persons function
+        # But it doesn't work, It seems we have to add users in LDAP anyway...
+        for user in users:
+            # XXX LOIC using hrn is a workaround because the function 
+            # Xrn.get_urn returns 'urn:publicid:IDN+onelab:upmc+timur_friedman'
+            # Instead of this     'urn:publicid:IDN+onelab:upmc+user+timur_friedman'
+            user['hrn'] = urn_to_hrn(user['urn'])[0]
+            # XXX LOIC adding the users of the slice to reg-researchers
+            # reg-researchers is used in iotlabslices.py verify_slice in order to add the slice
+            if 'reg-researchers' not in slice_record:
+                slice_record['reg-researchers'] = list()
+            slice_record['reg-researchers'].append(user['hrn'])
+            if caller_hrn == user['hrn']:
+                #hierarchical_user = user['hrn'].split(".")
+                #user['login'] = hierarchical_user[-1]            
+                #slice_record['login'] = user['login']
+                slice_record['user']=user
+
+        # XXX LOIC XXX Need to re-activate this function and understand exactly what is required
+
+        persons = slices.verify_persons(xrn.hrn, slice_record, [slice_record['user']], options=options)
         logger.debug("IOTLABDRIVER.PY  AFTER slices.verify_persons")
+        logger.debug("LOIC - IOTLABDRIVER.PY - AFTER slices.verify_persons slice_record = %r" % slice_record)
         # ensure slice attributes exists
-        # slices.verify_slice_attributes(slice, requested_attributes,
-                                    # options=options)
+        # XXX LOIC XXX This method doesn't exist !!!
+        #slices.verify_slice_attributes(slice, requested_attributes, options=options)
+
+        # XXX LOIC !!! Essayons d'inverser : d'abord verify_persons ensuite verify_slice
+        
+        # ensure slice record exists
+        # XXX LOIC !!! verify_slice uniquement pour iotlab - les slices externes ne doivent pas être vérifiés = Ils ne sont pas dans le registry IOTLAB
+        current_slice = None
+        #current_slice = slices.verify_slice(xrn.hrn, slice_record, sfa_peer)
+        #logger.debug("IOTLABDRIVER.PY \t ===============allocate \t\
+        #                    \r\n \r\n  current_slice %s" % (current_slice))
 
         # add/remove slice from nodes
+        # XXX JORDAN ensure requested_xp_dict returns a dict with all new leases
         requested_xp_dict = self._process_requested_xp_dict(rspec)
 
         logger.debug("IOTLABDRIVER.PY \tallocate  requested_xp_dict %s "
                      % (requested_xp_dict))
         request_nodes = rspec.version.get_nodes_with_slivers()
+
+        # JORDAN: nodes_list will contain a list of newly allocated nodes
         nodes_list = []
         for start_time in requested_xp_dict:
             lease = requested_xp_dict[start_time]
@@ -1503,31 +1546,57 @@ class IotlabDriver(Driver):
         rspec_requested_leases = rspec.version.get_leases()
         leases = slices.verify_slice_leases(slice_record,
                                                 requested_xp_dict, peer)
+        # JORDAN: 
+        #   leases = already in slice
+        #   rspec_requested_leases = newly requested
         logger.debug("IOTLABDRIVER.PY \tallocate leases  %s \
                         rspec_requested_leases %s" % (leases,
                         rspec_requested_leases))
-         # update sliver allocations
+
+        # XXX LOIC !!! What is in the slice_record?
+        # Where is affected reg_researchers value???
+        logger.debug("LOIC - IOTLABDRIVER.PY - After verify_slice_leases slice_record = %r" %slice_record)
+        # update sliver allocations
+        # JORDAN Here we loop over newly allocated nodes
         for hostname in nodes_list:
+            logger.debug("FORLOOP JORDAN hostname=%r" % hostname)
             client_id = hostname
             node_urn = xrn_object(self.testbed_shell.root_auth, hostname).urn
             component_id = node_urn
-            if 'reg-urn' in current_slice:
-                slice_urn = current_slice['reg-urn']
+            if current_slice is not None:
+                if 'reg-urn' in current_slice:
+                    slice_urn = current_slice['reg-urn']
+                else:
+                    slice_urn = current_slice['urn']
             else:
-                slice_urn = current_slice['urn']
-            for lease in leases:
+                slice_urn = slice_record['urn']
+            # JORDAN: We loop over leases previously in the slice
+            for lease in leases: # rspec_requested_leases ?????? XXX
+                logger.debug("FOR LEASE LOOP JORDAN lease=%r" % lease)
+                logger.debug("JORDAN hostname=%r lease['reserved_nodes']=%r, bool=%r" % (hostname, lease['reserved_nodes'], hostname in lease['reserved_nodes']))
                 if hostname in lease['reserved_nodes']:
+                    logger.debug("JORDAN IF OK")
                     index = lease['reserved_nodes'].index(hostname)
+                    logger.debug("JORDAN index=%r" % index)
                     sliver_hrn = '%s.%s-%s' % (self.hrn, lease['lease_id'],
                                    lease['resource_ids'][index] )
-            sliver_id = Xrn(sliver_hrn, type='sliver').urn
-            record = SliverAllocation(sliver_id=sliver_id, client_id=client_id,
-                                      component_id=component_id,
-                                      slice_urn = slice_urn,
-                                      allocation_state='geni_allocated')
-            record.sync(self.api.dbsession())
 
-        return aggregate.describe([xrn.get_urn()], version=rspec.version)
+                    logger.debug("LOIC sliver_hrn=%r" % sliver_hrn)
+                    sliver_id = Xrn(sliver_hrn, type='sliver').urn
+
+                    logger.debug("LOIC sliver_id=%r" % sliver_id)
+                    record = SliverAllocation(sliver_id=sliver_id, client_id=client_id,
+                                              component_id=component_id,
+                                              slice_urn = slice_urn,
+                                              allocation_state='geni_allocated')
+                    record.sync(self.api.dbsession())
+
+        # JORDAN : added describe_options which was not specified at all
+        describe_options = {
+            'geni_slice_urn': urn,
+            'list_leases': 'all',
+        }
+        return aggregate.describe([xrn.get_urn()], version=rspec.version, options=describe_options)
 
     def provision(self, urns, options={}):
         # update users
@@ -1535,6 +1604,7 @@ class IotlabDriver(Driver):
         aggregate = IotlabAggregate(self)
         slivers = aggregate.get_slivers(urns)
         current_slice = slivers[0]
+        logger.debug("Provision current slice: %r" % (current_slice,))
         peer = slices.get_peer(current_slice['hrn'])
         sfa_peer = slices.get_sfa_peer(current_slice['hrn'])
         users = options.get('geni_users', [])
@@ -1549,4 +1619,10 @@ class IotlabDriver(Driver):
         version_manager = VersionManager()
         rspec_version = version_manager.get_version(options[
                                                         'geni_rspec_version'])
-        return self.describe(urns, rspec_version, options=options)
+        # JORDAN : added describe_options instead of options
+        # urns at the begining ???
+        describe_options = {
+            'geni_slice_urn': current_slice['urn'],
+            'list_leases': 'all',
+        }
+        return self.describe(urns, rspec_version, options=describe_options)
index 25aba10..990561c 100644 (file)
@@ -498,7 +498,8 @@ class IotlabShell():
             # Put the duration in seconds first
             #desired_walltime = duration * 60
             desired_walltime = duration
-            total_walltime = desired_walltime + 240 #+4 min Update SA 23/10/12
+            # XXX total_walltime = desired_walltime + 240 #+4 min Update SA 23/10/12
+            total_walltime = desired_walltime # Needed to have slots aligned in MySlice (temp fix) # JA 11/07/2014
             sleep_walltime = desired_walltime  # 0 sec added Update SA 23/10/12
             walltime = []
             #Put the walltime back in str form
index e486624..9449627 100644 (file)
@@ -2,7 +2,7 @@
 This file defines the IotlabSlices class by which all the slice checkings
 upon lease creation are done.
 """
-from sfa.util.xrn import get_authority, urn_to_hrn
+from sfa.util.xrn import get_authority, urn_to_hrn, hrn_to_urn
 from sfa.util.sfalogging import logger
 
 MAXINT = 2L**31-1
@@ -135,6 +135,7 @@ class IotlabSlices:
                 * self.driver.testbed_shell.GetLeaseGranularity())
             if job['duration'] < \
                     self.driver.testbed_shell.GetLeaseGranularity():
+                logger.debug("JORDAN removed too short lease !!!! %r" % (requested_jobs_dict[job['start_time']],))
                 del requested_jobs_dict[job['start_time']]
 
         #Requested jobs
@@ -165,6 +166,7 @@ class IotlabSlices:
                          "%( start_time))
             if start_time in current_nodes_reserved_by_start_time:
 
+                # JORDAN : if we request the same nodes: do nothing
                 if requested_nodes_by_start_time[start_time] == \
                     current_nodes_reserved_by_start_time[start_time]:
                     continue
@@ -212,11 +214,12 @@ class IotlabSlices:
                     job['hostname'],
                     sfa_slice, int(job['start_time']),
                     int(job['duration']))
-                if job_id is not None:
-                    new_leases = self.driver.GetLeases(login=
-                        sfa_slice['login'])
-                    for new_lease in new_leases:
-                        leases.append(new_lease)
+                # Removed by jordan
+                #if job_id is not None:
+                #    new_leases = self.driver.GetLeases(login=
+                #        sfa_slice['login'])
+                #    for new_lease in new_leases:
+                #        leases.append(new_lease)
 
         #Deleted leases are the ones with lease id not declared in the Rspec
         if deleted_leases:
@@ -229,13 +232,17 @@ class IotlabSlices:
                          % (sfa_slice, deleted_leases))
 
         if reschedule_jobs_dict:
+            logger.debug("JORDAN re-schedule jobs: %r" % (reschedule_jobs_dict,))
             for start_time in reschedule_jobs_dict:
                 job = reschedule_jobs_dict[start_time]
                 self.driver.AddLeases(
                     job['hostname'],
                     sfa_slice, int(job['start_time']),
                     int(job['duration']))
-        return leases
+
+        # Added by Jordan: until we find a better solution, always update the list of leases
+        return self.driver.GetLeases(login= sfa_slice['login'])
+        #return leases
 
     def verify_slice_nodes(self, sfa_slice, requested_slivers, peer):
         """Check for wanted and unwanted nodes in the slice.
@@ -307,52 +314,92 @@ class IotlabSlices:
         """
 
         slicename = slice_hrn
+        sfa_slice = None
+
         # check if slice belongs to Iotlab
-        slices_list = self.driver.GetSlices(slice_filter=slicename,
-                                            slice_filter_type='slice_hrn')
+        if slicename.startswith("iotlab"):
+            slices_list = self.driver.GetSlices(slice_filter=slicename,
+                                                slice_filter_type='slice_hrn')
 
-        sfa_slice = None
 
-        if slices_list:
-            for sl in slices_list:
+            if slices_list:
+                for sl in slices_list:
 
-                logger.debug("IOTLABSLICES \t verify_slice slicename %s \
-                                slices_list %s sl %s \r slice_record %s"
-                             % (slicename, slices_list, sl, slice_record))
-                sfa_slice = sl
-                sfa_slice.update(slice_record)
+                    logger.debug("IOTLABSLICES \t verify_slice slicename %s \
+                                    slices_list %s sl %s \r slice_record %s"
+                                 % (slicename, slices_list, sl, slice_record))
+                    sfa_slice = sl
+                    sfa_slice.update(slice_record)
 
+            else:
+                #Search for user in ldap based on email SA 14/11/12
+                ldap_user = self.driver.testbed_shell.ldap.LdapFindUser(\
+                                                        slice_record['user'])
+                logger.debug(" IOTLABSLICES \tverify_slice Oups \
+                            slice_record %s sfa_peer %s ldap_user %s"
+                            % (slice_record, sfa_peer, ldap_user))
+                #User already registered in ldap, meaning user should be in SFA db
+                #and hrn = sfa_auth+ uid
+                sfa_slice = {'hrn': slicename,
+                             'node_list': [],
+                             'authority': slice_record['authority'],
+                             'gid': slice_record['gid'],
+                             'slice_id': slice_record['record_id'],
+                             'urn': hrn_to_urn(slicename,'slice'),
+                             'reg-researchers': slice_record['reg-researchers'],
+                             #'peer_authority': str(sfa_peer)
+                             }
+
+                if ldap_user:
+                    hrn = self.driver.testbed_shell.root_auth + '.' \
+                                                    + ldap_user['uid']
+                    # Registry returns a user record from get_user_record 
+                    user = self.driver.get_user_record(hrn)
+
+                    logger.debug(" IOTLABSLICES \tverify_slice hrn %s USER %s"
+                                 % (hrn, user))
+
+                     # add the external slice to the local SFA iotlab DB
+                    if sfa_slice:
+                        self.driver.AddSlice(sfa_slice, user)
+
+                logger.debug("IOTLABSLICES \tverify_slice ADDSLICE OK")
         else:
-            #Search for user in ldap based on email SA 14/11/12
-            ldap_user = self.driver.testbed_shell.ldap.LdapFindUser(\
-                                                    slice_record['user'])
-            logger.debug(" IOTLABSLICES \tverify_slice Oups \
-                        slice_record %s sfa_peer %s ldap_user %s"
-                        % (slice_record, sfa_peer, ldap_user))
-            #User already registered in ldap, meaning user should be in SFA db
-            #and hrn = sfa_auth+ uid
             sfa_slice = {'hrn': slicename,
-                         'node_list': [],
-                         'authority': slice_record['authority'],
-                         'gid': slice_record['gid'],
-                         'slice_id': slice_record['record_id'],
-                         #'reg-researchers': slice_record['reg-researchers'],
-                         #'peer_authority': str(sfa_peer)
-                         }
-
-            if ldap_user:
-                hrn = self.driver.testbed_shell.root_auth + '.' \
-                                                + ldap_user['uid']
-                user = self.driver.get_user_record(hrn)
-
-                logger.debug(" IOTLABSLICES \tverify_slice hrn %s USER %s"
-                             % (hrn, user))
-
-                 # add the external slice to the local SFA iotlab DB
-                if sfa_slice:
-                    self.driver.AddSlice(sfa_slice, user)
-
-            logger.debug("IOTLABSLICES \tverify_slice ADDSLICE OK")
+             'node_list': [],
+             'authority': slice_record['authority'],
+             'gid': slice_record['gid'],
+             'urn': hrn_to_urn(slicename,'slice'),
+             #'slice_id': slice_record['record_id'],
+             'reg-researchers': slice_record['reg-researchers'],
+             #'peer_authority': str(sfa_peer)
+            }
+
+
+            # JORDAN
+            logger.debug("JORDAN ADDSLICE")
+            logger.debug("ADDSLICE user hrn = %s" % slice_record['user']['hrn'])
+            # XXX LOIC !!! searching in IOTLAB DB because has been added in verify_persons
+            user = self.driver.get_user_record(slice_record['user']['hrn'])
+
+            logger.debug("LOIC ADDSLICE Search in IOTLAB DB for user = %s" % user)
+            # XXX LOIC !!! not searching in LDAP because this has been done in verify_persons
+            #ldap_user = self.driver.testbed_shell.ldap.LdapFindUser(\
+            #                                        slice_record['user'])
+            #if ldap_user:
+            #    hrn = self.driver.testbed_shell.root_auth + '.' \
+            #                                    + ldap_user['uid']
+            #    user = self.driver.get_user_record(hrn)
+            #    logger.debug(" IOTLAB SLICES JORDAN user: %r %r " % (user, hrn))
+
+            #    logger.debug(" IOTLABSLICES \tverify_slice hrn %s USER %s"
+            #                 % (hrn, user))
+
+            # add the external slice to the local SFA iotlab DB
+            if sfa_slice:
+                self.driver.AddSlice(sfa_slice, user)
+                logger.debug("IOTLABSLICES \tverify_slice ADDSLICE OK")
+
         return sfa_slice
 
 
@@ -382,7 +429,6 @@ class IotlabSlices:
 
 
         """
-
         logger.debug("IOTLABSLICES \tverify_persons \tslice_hrn  %s  \
                     \t slice_record %s\r\n users %s \t  "
                      % (slice_hrn, slice_record, users))
@@ -426,37 +472,60 @@ class IotlabSlices:
             logger.debug(" \r\n IOTLABSLICES.PY \tverify_person  filter_user %s\
                        existing_users %s  "
                         % (filter_user, existing_users))
-            #User is in iotlab LDAP
-            if existing_users:
+            # User is in iotlab LDAP
+            # XXX LOIC !!! user is existing in iotlab LDAP but coming from OneLab portal
+            if existing_users and slice_hrn.startswith("iotlab"):
                 for user in existing_users:
                     user['login'] = user['uid']
                     users_dict[user['email']].update(user)
                     existing_user_emails.append(
                         users_dict[user['email']]['email'])
 
-
             # User from another known trusted federated site. Check
             # if a iotlab account matching the email has already been created.
             else:
                 req = 'mail='
                 if isinstance(users, list):
                     req += users[0]['email']
+                    user = users[0]
                 else:
                     req += users['email']
+                    user = users
                 ldap_reslt = self.driver.testbed_shell.ldap.LdapSearch(req)
 
                 if ldap_reslt:
                     logger.debug(" IOTLABSLICES.PY \tverify_person users \
                                 USER already in Iotlab \t ldap_reslt %s \
                                 " % (ldap_reslt))
-                    existing_users.append(ldap_reslt[1])
-
+                    
+                    #existing_users.append(ldap_reslt[1])
+                    # XXX LOIC !!! Not sure why we use to take the element 1
+                    if len(ldap_reslt)>0:
+                        ldap_reslt = ldap_reslt[0]
+                        logger.debug(ldap_reslt)
+                        if len(ldap_reslt)>1:
+                            ldap_reslt = ldap_reslt[1]
+                            logger.debug("LOIC - iotlabslices.py - ldap_reslt = %s" % (ldap_reslt))
+                            existing_users.append(ldap_reslt)
+                            existing_user_emails.append(ldap_reslt['mail'][0])
+                    
+                    # XXX LOIC !!! This login is required 
+                    # sfa/iotlab/iotlabdriver.py", line 523, in AddLeases 
+                    if 'uid' in ldap_reslt:
+                        # meaning that the Person was found in LDAP
+                        slice_record['login'] = ldap_reslt['uid'][0]
+
+                    # XXX LOIC !!! Add the user to IOTLAB DB Registry???
+                    #if 'keys' in user:
+                    #    user['pkey'] = user['keys'][0]
+
+                    #ret = self.driver.AddPerson(user,add_to_ldap=False)
+                    #logger.debug("LOIC verify_persons AddPerson ret = %s" % ret)
                 else:
                     #User not existing in LDAP
                     logger.debug("IOTLABSLICES.PY \tverify_person users \
                                 not in ldap ...NEW ACCOUNT NEEDED %s \r\n \t \
                                 ldap_reslt %s " % (users, ldap_reslt))
-
         requested_user_emails = users_by_email.keys()
         # requested_user_hrns = \
         #     [users_by_email[user]['hrn'] for user in users_by_email]
@@ -478,21 +547,63 @@ class IotlabSlices:
         #to remove/ add any user from/to a slice.
         #However a user from SFA which is not registered in Iotlab yet
         #should be added to the LDAP.
+        logger.debug("LOIC - iotlabslice.py - requested_user_emails = %r" % requested_user_emails)
+        logger.debug("LOIC - iotlabslice.py - existing_user_emails = %r" % existing_user_emails)
+
         added_user_emails = set(requested_user_emails).\
                                         difference(set(existing_user_emails))
-
-
+        logger.debug("LOIC - iotlabslice.py - added_user_emails = %r" % added_user_emails)
+        logger.debug("LOIC - iotlabslice.py - existing_user_emails = %r" % existing_user_emails)
         #self.verify_keys(existing_slice_users, updated_users_list, \
                                                             #peer, append)
 
+        # XXX JORDAN the uid of the user is put in slice_record['login']
+
         added_persons = []
         # add new users
         #requested_user_email is in existing_user_emails
         if len(added_user_emails) == 0:
-            slice_record['login'] = users_dict[requested_user_emails[0]]['uid']
+#            slice_record['login'] = users_dict[requested_user_emails[0]]['uid']
             logger.debug(" IOTLABSLICES  \tverify_person QUICK DIRTY %s"
                          % (slice_record))
 
+            # XXX JORDAN uid == 'register'
+#            new_hrn = slice_record['user']['hrn']
+#            new_user = self.driver.get_user_record(new_hrn)
+#            if not new_user:
+#                # XXX HERE WE SHOULD CREATE A SFA USER !!!!!!
+#                added_user = users_dict[requested_user_emails[0]]
+#                person = {}
+#                person['peer_person_id'] = None
+#                k_list = ['first_name', 'last_name', 'person_id']
+#                for k in k_list:
+#                    if k in added_user:
+#                        person[k] = added_user[k]
+#                # bug user without key
+#                if added_user['keys']:
+#                    person['pkey'] = added_user['keys'][0]
+#                person['mail'] = added_user['email']
+#                person['email'] = added_user['email']
+#                person['key_ids'] = added_user.get('key_ids', [])
+#                # LOIC !!! il faudrait transformer onelab.upmc.XXX en iotlab.XXX
+#                if new_hrn.startswith("iotlab"):
+#                    person['hrn'] = new_hrn                 
+#                else:
+#                    hrn_hierarchy = new_hrn.split(".")
+#                    person['hrn'] = "iotlab." + hrn_hierarchy[-1]
+#
+#                ret = self.driver.AddPerson(person, add_to_ldap=False)
+#                logger.debug("AddPerson return = %r type = %s" % (ret,type(ret)))
+#                # LOIC !!! XXX Dans un cas ça retourne un dict du LDAP dans l'autre cas pas de LDAP donc ça ne retourne rien
+#                if ret is None:
+#                    person['uid'] = slice_record['login']
+#                else:
+#                    if 'uid' in ret:
+#                        # meaning bool is True and the AddPerson was successful
+#                        person['uid'] = ret['uid']
+#                        slice_record['login'] = person['uid']
+
+        # XXX JORDAN i have no added_user_emails
         for added_user_email in added_user_emails:
             added_user = users_dict[added_user_email]
             logger.debug(" IOTLABSLICES \r\n \r\n  \t  verify_person \