Fixed bug for sfi.py show slice record.
[sfa.git] / sfa / senslab / slabdriver.py
index f009aa4..95fa8d7 100644 (file)
@@ -1,7 +1,6 @@
 import subprocess
 
 from datetime import datetime
-from time import gmtime
 
 from sfa.util.faults import SliverDoesNotExist, UnknownSfaType
 from sfa.util.sfalogging import logger
@@ -16,7 +15,7 @@ from sfa.managers.driver import Driver
 from sfa.rspecs.version_manager import VersionManager
 from sfa.rspecs.rspec import RSpec
 
-from sfa.util.xrn import hrn_to_urn, urn_to_sliver_id, get_leaf
+from sfa.util.xrn import hrn_to_urn
 
 
 ## thierry: everything that is API-related (i.e. handling incoming requests) 
@@ -42,18 +41,20 @@ from sfa.senslab.slabslices import SlabSlices
 # GetNodes or GetSites sorts of calls directly
 # and thus minimize the differences in the managers with the pl version
 class SlabDriver(Driver):
-
+    """ Senslab Driver class inherited from Driver generic class.
+    
+    Contains methods compliant with the SFA standard and the testbed
+    infrastructure (calls to LDAP and OAR).
+    """
     def __init__(self, config):
         Driver.__init__ (self, config)
         self.config = config
         self.hrn = config.SFA_INTERFACE_HRN
-
         self.root_auth = config.SFA_REGISTRY_ROOT_AUTH
-
         self.oar = OARrestapi()
         self.ldap = LDAPapi()
         self.time_format = "%Y-%m-%d %H:%M:%S"
-        self.db = SlabDB(config,debug = True)
+        self.db = SlabDB(config, debug = True)
         self.cache = None
         
     
@@ -134,8 +135,10 @@ class SlabDriver(Driver):
                 res['pl_boot_state'] = nodeall_byhostname[node['hostname']]['boot_state']
                 #res['pl_last_contact'] = strftime(self.time_format, \
                                                     #gmtime(float(timestamp)))
-                sliver_id = urn_to_sliver_id(slice_urn, sl['record_id_slice'], \
-                                            nodeall_byhostname[node['hostname']]['node_id']) 
+                sliver_id =  Xrn(slice_urn, type='slice', \
+                        id=nodeall_byhostname[node['hostname']]['node_id'], \
+                        authority=self.hrn).urn
+    
                 res['geni_urn'] = sliver_id 
                 if nodeall_byhostname[node['hostname']]['boot_state'] == 'Alive':
 
@@ -364,57 +367,18 @@ class SlabDriver(Driver):
     
         return slice_urns
     
-    #No site or node register supported
+   
     def register (self, sfa_record, hrn, pub_key):
-        record_type = sfa_record['type']
-        slab_record = self.sfa_fields_to_slab_fields(record_type, hrn, \
-                                                            sfa_record)
-    
-
-        if record_type == 'slice':
-            acceptable_fields = ['url', 'instantiation', 'name', 'description']
-            for key in slab_record.keys():
-                if key not in acceptable_fields:
-                    slab_record.pop(key) 
-            logger.debug("SLABDRIVER.PY register")
-            slices = self.GetSlices(slice_filter=slab_record['hrn'], \
-                                            slice_filter_type = 'slice_hrn')
-            if not slices:
-                pointer = self.AddSlice(slab_record)
-            else:
-                pointer = slices[0]['slice_id']
-    
-        elif record_type == 'user':  
-            persons = self.GetPersons([sfa_record])
-            #persons = self.GetPersons([sfa_record['hrn']])
-            if not persons:
-                sfa_record['enabled'] = False
-                #For Senslab LDAP, if the user is a new one, disable the 
-                #account so that admins have to acknowledge the user first.
-                pointer = self.AddPerson(dict(sfa_record))
-                #add in LDAP 
-            else:
-                pointer = persons[0]['person_id']
-                
-            #Does this make sense to senslab ?
-            #if 'enabled' in sfa_record and sfa_record['enabled']:
-                #self.UpdatePerson(pointer, \
-                                    #{'enabled': sfa_record['enabled']})
-                
-            #TODO register Change this AddPersonToSite stuff 05/07/2012 SA   
-            #No site in senslab 28/08/12 SA
-           
-    
-            # What roles should this user have?
-            #No user roles in Slab/SFA 28/08/12 SA: roles are handled in LDAP
-            # Add the user's key
-            if pub_key:
-                self.AddPersonKey(pointer, {'key_type' : 'ssh', \
-                                                'key' : pub_key})
-                
-        #No node adding outside OAR
-
-        return pointer
+        """ 
+        Adding new user, slice, node or site should not be handled
+        by SFA.
+        
+        Adding nodes = OAR
+        Adding users = LDAP Senslab
+        Adding slice = Import from LDAP users
+        Adding site = OAR
+        """
+        return -1
             
     #No site or node record update allowed       
     def update (self, old_sfa_record, new_sfa_record, hrn, new_key):
@@ -1298,15 +1262,19 @@ class SlabDriver(Driver):
                     record.update({'PI':[recuser.hrn],
                                 'researcher': [recuser.hrn],
                                 'name':record['hrn'], 
-                                'oar_job_id':[rec['oar_job_id'] for rec in recslice_list],
+                                'oar_job_id':[],
                                 'node_ids': [],
                                 'person_ids':[recslice_list[0]['record_id_user']],
                                 'geni_urn':'',  #For client_helper.py compatibility
                                 'keys':'',  #For client_helper.py compatibility
                                 'key_ids':''})  #For client_helper.py compatibility
+                    
+                    try:
+                        for rec in recslice_list:
+                            record['oar_job_id'].append(rec['oar_job_id'])
+                    except KeyError:
+                        pass
 
-                    #for rec in recslice_list:
-                        #record['oar_job_id'].append(rec['oar_job_id'])
                     logger.debug( "SLABDRIVER.PY \t fill_record_info SLICE \
                                                     recslice_list  %s \r\n \t RECORD %s \r\n \r\n" %(recslice_list,record)) 
                 if str(record['type']) == 'user':
@@ -1332,12 +1300,17 @@ class SlabDriver(Driver):
                         'researcher': [recuser.hrn],
                         'name':record['hrn'], 
                         'node_ids': [],
-                        'oar_job_id': [rec['oar_job_id'] for rec in recslice_list],
-                        'person_ids':[recslice_list[0]['record_id_user']]})
+                        'oar_job_id': [],
+                        'person_ids':[recslice_list[0]['record_id_user']]}) 
+                    try:
+                        for rec in recslice_list:
+                            recslice['oar_job_id'].append(rec['oar_job_id'])
+                    except KeyError:
+                        pass
+                            
                     recslice.update({'type':'slice', \
                                                 'hrn':recslice_list[0]['slice_hrn']})
-                    #for rec in recslice_list:
-                        #recslice['oar_job_id'].append(rec['oar_job_id'])
+
 
                     #GetPersons takes [] as filters 
                     #user_slab = self.GetPersons([{'hrn':recuser.hrn}])