Merge branch 'geni-v3' of ssh://git.onelab.eu/git/sfa into geni-v3
[sfa.git] / sfa / iotlab / iotlabshell.py
index 8dd4d65..e546943 100644 (file)
@@ -8,7 +8,6 @@ from datetime import datetime
 
 from sfa.util.sfalogging import logger
 
-from sfa.storage.alchemy import dbsession
 from sqlalchemy.orm import joinedload
 from sfa.storage.model import RegRecord, RegUser, RegSlice, RegKey
 from sfa.iotlab.iotlabpostgres import TestbedAdditionalSfaDB, LeaseTableXP
@@ -21,14 +20,14 @@ from sfa.trust.certificate import Keypair, convert_public_key
 from sfa.trust.gid import create_uuid
 from sfa.trust.hierarchy import Hierarchy
 
-from sfa.iotlab.iotlabaggregate import iotlab_xrn_object
+from sfa.iotlab.iotlabxrn import xrn_object
 
 class IotlabShell():
     """ Class enabled to use LDAP and OAR api calls. """
 
     _MINIMUM_DURATION = 10  # 10 units of granularity 60 s, 10 mins
 
-    def __init__(self, config):
+    def __init__(self, api):
         """Creates an instance of OARrestapi and LDAPapi which will be used to
         issue calls to OAR or LDAP methods.
         Set the time format  and the testbed granularity used for OAR
@@ -37,7 +36,9 @@ class IotlabShell():
         :param config: configuration object from sfa.util.config
         :type config: Config object
         """
-        self.iotlab_db = TestbedAdditionalSfaDB(config)
+        self.api = api
+        config = api.config
+        self.leases_db = TestbedAdditionalSfaDB(config)
         self.oar = OARrestapi()
         self.ldap = LDAPapi()
         self.time_format = "%Y-%m-%d %H:%M:%S"
@@ -57,8 +58,8 @@ class IotlabShell():
         """
         return IotlabShell._MINIMUM_DURATION
 
-    @staticmethod
-    def GetPeers(peer_filter=None ):
+
+    def GetPeers(self, peer_filter=None ):
         """ Gathers registered authorities in SFA DB and looks for specific peer
         if peer_filter is specified.
         :param peer_filter: name of the site authority looked for.
@@ -70,7 +71,7 @@ class IotlabShell():
         existing_records = {}
         existing_hrns_by_types = {}
         logger.debug("IOTLAB_API \tGetPeers peer_filter %s " % (peer_filter))
-        all_records = dbsession.query(RegRecord).filter(RegRecord.type.like('%authority%')).all()
+        all_records = self.api.dbsession().query(RegRecord).filter(RegRecord.type.like('%authority%')).all()
 
         for record in all_records:
             existing_records[(record.hrn, record.type)] = record
@@ -405,9 +406,7 @@ class IotlabShell():
         return return_node_list
 
 
-
-    @staticmethod
-    def AddSlice(slice_record, user_record):
+    def AddSlice(self, slice_record, user_record):
         """
 
         Add slice to the local iotlab sfa tables if the slice comes
@@ -430,11 +429,11 @@ class IotlabShell():
         logger.debug("IOTLAB_API.PY AddSlice  sfa_record %s user_record %s"
                      % (sfa_record, user_record))
         sfa_record.just_created()
-        dbsession.add(sfa_record)
-        dbsession.commit()
+        self.api.dbsession().add(sfa_record)
+        self.api.dbsession().commit()
         #Update the reg-researcher dependance table
         sfa_record.reg_researchers = [user_record]
-        dbsession.commit()
+        self.api.dbsession().commit()
 
         return
 
@@ -526,8 +525,8 @@ class IotlabShell():
                     delete_failed))
         return delete_failed or True
 
-    @staticmethod
-    def __add_person_to_db(user_dict):
+
+    def __add_person_to_db(self, user_dict):
         """
         Add a federated user straight to db when the user issues a lease
         request with iotlab nodes and that he has not registered with iotlab
@@ -540,7 +539,7 @@ class IotlabShell():
 
         """
         check_if_exists = \
-        dbsession.query(RegUser).filter_by(email = user_dict['email']).first()
+        self.api.dbsession().query(RegUser).filter_by(email = user_dict['email']).first()
         #user doesn't exists
         if not check_if_exists:
             logger.debug("__add_person_to_db \t Adding %s \r\n \r\n \
@@ -572,8 +571,8 @@ class IotlabShell():
                                     email=user_dict['email'], gid = person_gid)
             user_record.reg_keys = [RegKey(user_dict['pkey'])]
             user_record.just_created()
-            dbsession.add (user_record)
-            dbsession.commit()
+            self.api.dbsession().add (user_record)
+            self.api.dbsession().commit()
         return
 
 
@@ -815,6 +814,7 @@ class IotlabShell():
         :type slice_record: dict
         :type lease_start_time: integer
         :type lease_duration: integer
+        :returns: job_id, can be None if the job request failed.
 
         """
         logger.debug("IOTLAB_API \r\n \r\n \t AddLeases hostname_list %s  \
@@ -829,31 +829,32 @@ class IotlabShell():
                                     slice_record['hrn'], \
                                     lease_start_time, lease_duration, \
                                     username)
-        start_time = \
-                datetime.fromtimestamp(int(lease_start_time)).\
-                strftime(self.time_format)
-        end_time = lease_start_time + lease_duration
+        if job_id is not None:
+            start_time = \
+                    datetime.fromtimestamp(int(lease_start_time)).\
+                    strftime(self.time_format)
+            end_time = lease_start_time + lease_duration
 
 
-        logger.debug("IOTLAB_API \r\n \r\n \t AddLeases TURN ON LOGGING SQL \
-                        %s %s %s "%(slice_record['hrn'], job_id, end_time))
+            logger.debug("IOTLAB_API \r\n \r\n \t AddLeases TURN ON LOGGING SQL \
+                            %s %s %s "%(slice_record['hrn'], job_id, end_time))
 
 
-        logger.debug("IOTLAB_API \r\n \r\n \t AddLeases %s %s %s " \
-                %(type(slice_record['hrn']), type(job_id), type(end_time)))
+            logger.debug("IOTLAB_API \r\n \r\n \t AddLeases %s %s %s " \
+                    %(type(slice_record['hrn']), type(job_id), type(end_time)))
 
-        iotlab_ex_row = LeaseTableXP(slice_hrn = slice_record['hrn'], experiment_id=job_id,
-                                 end_time= end_time)
+            iotlab_ex_row = LeaseTableXP(slice_hrn = slice_record['hrn'], experiment_id=job_id,
+                                     end_time= end_time)
 
-        logger.debug("IOTLAB_API \r\n \r\n \t AddLeases iotlab_ex_row %s" \
-                %(iotlab_ex_row))
-        self.iotlab_db.testbed_session.add(iotlab_ex_row)
-        self.iotlab_db.testbed_session.commit()
+            logger.debug("IOTLAB_API \r\n \r\n \t AddLeases iotlab_ex_row %s" \
+                    %(iotlab_ex_row))
+            self.leases_db.testbed_session.add(iotlab_ex_row)
+            self.leases_db.testbed_session.commit()
 
-        logger.debug("IOTLAB_API \t AddLeases hostname_list start_time %s " \
-                %(start_time))
+            logger.debug("IOTLAB_API \t AddLeases hostname_list start_time %s " \
+                    %(start_time))
 
-        return
+        return job_id
 
 
     #Delete the jobs from job_iotlab table
@@ -897,30 +898,6 @@ class IotlabShell():
         return self.grain
 
 
-    # @staticmethod
-    # def update_experiments_in_additional_sfa_db( job_oar_list, jobs_psql):
-    #     """ Cleans the iotlab db by deleting expired and cancelled jobs.
-    #     Compares the list of job ids given by OAR with the job ids that
-    #     are already in the database, deletes the jobs that are no longer in
-    #     the OAR job id list.
-    #     :param  job_oar_list: list of job ids coming from OAR
-    #     :type job_oar_list: list
-    #     :param job_psql: list of job ids cfrom the database.
-    #     type job_psql: list
-    #     """
-    #     #Turn the list into a set
-    #     set_jobs_psql = set(jobs_psql)
-
-    #     kept_jobs = set(job_oar_list).intersection(set_jobs_psql)
-    #     logger.debug ( "\r\n \t\ update_experiments_in_additional_sfa_db jobs_psql %s \r\n \t \
-    #         job_oar_list %s kept_jobs %s "%(set_jobs_psql, job_oar_list, kept_jobs))
-    #     deleted_jobs = set_jobs_psql.difference(kept_jobs)
-    #     deleted_jobs = list(deleted_jobs)
-    #     if len(deleted_jobs) > 0:
-    #         self.iotlab_db.testbed_session.query(LeaseTableXP).filter(LeaseTableXP.job_id.in_(deleted_jobs)).delete(synchronize_session='fetch')
-    #         self.iotlab_db.testbed_session.commit()
-
-    #     return
 
     @staticmethod
     def filter_lease_name(reservation_list, filter_value):
@@ -978,7 +955,7 @@ class IotlabShell():
         #the same user in LDAP SA 27/07/12
         job_oar_list = []
 
-        jobs_psql_query = self.iotlab_db.testbed_session.query(LeaseTableXP).all()
+        jobs_psql_query = self.leases_db.testbed_session.query(LeaseTableXP).all()
         jobs_psql_dict = dict([(row.experiment_id, row.__dict__)
                                for row in jobs_psql_query])
         #jobs_psql_dict = jobs_psql_dict)
@@ -1010,7 +987,7 @@ class IotlabShell():
             #Transform the hostnames into urns (component ids)
             for node in resa['reserved_nodes']:
 
-                iotlab_xrn = iotlab_xrn_object(self.root_auth, node)
+                iotlab_xrn = xrn_object(self.root_auth, node)
                 resa['component_id_list'].append(iotlab_xrn.urn)
 
         if lease_filter_dict:
@@ -1044,7 +1021,7 @@ class IotlabShell():
         if lease_filter_dict is None:
             reservation_list = unfiltered_reservation_list
 
-        self.iotlab_db.update_experiments_in_additional_sfa_db(job_oar_list, jobs_psql_id_list)
+        self.leases_db.update_experiments_in_additional_sfa_db(job_oar_list, jobs_psql_id_list)
 
         logger.debug(" IOTLAB_API.PY \tGetLeases reservation_list %s"
                      % (reservation_list))
@@ -1125,14 +1102,14 @@ class IotlabShell():
 
         #"""
         ##new_row = FederatedToIotlab(iotlab_hrn, federated_hrn)
-        ##self.iotlab_db.testbed_session.add(new_row)
-        ##self.iotlab_db.testbed_session.commit()
+        ##self.leases_db.testbed_session.add(new_row)
+        ##self.leases_db.testbed_session.commit()
 
         #logger.debug("IOTLAB_API UpdatePerson EMPTY - DO NOTHING \r\n ")
         #return
 
-    @staticmethod
-    def GetKeys(key_filter=None):
+
+    def GetKeys(self, key_filter=None):
         """Returns a dict of dict based on the key string. Each dict entry
         contains the key id, the ssh key, the user's email and the
         user's hrn.
@@ -1146,9 +1123,9 @@ class IotlabShell():
         :rtype: dict
         """
         if key_filter is None:
-            keys = dbsession.query(RegKey).options(joinedload('reg_user')).all()
+            keys = self.api.dbsession().query(RegKey).options(joinedload('reg_user')).all()
         else:
-            keys = dbsession.query(RegKey).options(joinedload('reg_user')).filter(RegKey.key.in_(key_filter)).all()
+            keys = self.api.dbsession().query(RegKey).options(joinedload('reg_user')).filter(RegKey.key.in_(key_filter)).all()
 
         key_dict = {}
         for key in keys:
@@ -1186,10 +1163,7 @@ class IotlabShell():
         return ret['bool']
 
 
-
-
-    @staticmethod
-    def _sql_get_slice_info(slice_filter):
+    def _sql_get_slice_info(self, slice_filter):
         """
         Get the slice record based on the slice hrn. Fetch the record of the
         user associated with the slice by using joinedload based on the
@@ -1208,8 +1182,8 @@ class IotlabShell():
 
         #Only one entry for one user  = one slice in testbed_xp table
         #slicerec = dbsession.query(RegRecord).filter_by(hrn = slice_filter).first()
-        raw_slicerec = dbsession.query(RegSlice).options(joinedload('reg_researchers')).filter_by(hrn=slice_filter).first()
-        #raw_slicerec = dbsession.query(RegRecord).filter_by(hrn = slice_filter).first()
+        raw_slicerec = self.api.dbsession().query(RegSlice).options(joinedload('reg_researchers')).filter_by(hrn=slice_filter).first()
+        #raw_slicerec = self.api.dbsession().query(RegRecord).filter_by(hrn = slice_filter).first()
         if raw_slicerec:
             #load_reg_researcher
             #raw_slicerec.reg_researchers
@@ -1225,8 +1199,7 @@ class IotlabShell():
         else:
             return None
 
-    @staticmethod
-    def _sql_get_slice_info_from_user(slice_filter):
+    def _sql_get_slice_info_from_user(self, slice_filter):
         """
         Get the slice record based on the user recordid by using a joinedload
         on the relationship reg_slices_as_researcher. Format the sql record
@@ -1236,8 +1209,8 @@ class IotlabShell():
         :rtype:dict or None..
         """
         #slicerec = dbsession.query(RegRecord).filter_by(record_id = slice_filter).first()
-        raw_slicerec = dbsession.query(RegUser).options(joinedload('reg_slices_as_researcher')).filter_by(record_id=slice_filter).first()
-        #raw_slicerec = dbsession.query(RegRecord).filter_by(record_id = slice_filter).first()
+        raw_slicerec = self.api.dbsession().query(RegUser).options(joinedload('reg_slices_as_researcher')).filter_by(record_id=slice_filter).first()
+        #raw_slicerec = self.api.dbsession().query(RegRecord).filter_by(record_id = slice_filter).first()
         #Put it in correct order
         user_needed_fields = ['peer_authority', 'hrn', 'last_updated',
                               'classtype', 'authority', 'gid', 'record_id',
@@ -1418,7 +1391,7 @@ class IotlabShell():
             #put them in dict format
             #query_slice_list = dbsession.query(RegRecord).all()
             query_slice_list = \
-                dbsession.query(RegSlice).options(joinedload('reg_researchers')).all()
+                self.api.dbsession().query(RegSlice).options(joinedload('reg_researchers')).all()
 
             for record in query_slice_list:
                 tmp = record.__dict__