first stab at a design where each incoming API call has its own dbsession
[sfa.git] / sfa / importer / iotlabimporter.py
index 3212de0..bfa094b 100644 (file)
@@ -7,11 +7,13 @@ from sfa.util.config import Config
 from sfa.util.xrn import Xrn, get_authority, hrn_to_urn
 
 from sfa.iotlab.iotlabdriver import IotlabDriver
-from sfa.iotlab.iotlabpostgres import IotlabDB
+from sfa.iotlab.iotlabpostgres import TestbedAdditionalSfaDB
 from sfa.trust.certificate import Keypair, convert_public_key
 from sfa.trust.gid import create_uuid
 
-from sfa.storage.alchemy import dbsession
+# using global alchemy.session() here is fine 
+# as importer is on standalone one-shot process
+from sfa.storage.alchemy import global_dbsession
 from sfa.storage.model import RegRecord, RegAuthority, RegSlice, RegNode, \
     RegUser, RegKey
 
@@ -43,7 +45,7 @@ class IotlabImporter:
         self.logger = loc_logger
         self.logger.setLevelDebug()
         #retrieve all existing SFA objects
-        self.all_records = dbsession.query(RegRecord).all()
+        self.all_records = global_dbsession.query(RegRecord).all()
 
         # initialize record.stale to True by default,
         # then mark stale=False on the ones that are in use
@@ -151,75 +153,25 @@ class IotlabImporter:
             return
         self.records_by_type_hrn[rec_tuple] = record
 
-    def import_sites_and_nodes(self, iotlabdriver):
-        """
-
-        Gets all the sites and nodes from OAR, process the information,
-        creates hrns and RegAuthority for sites, and feed them to the database.
-        For each site, import the site's nodes to the DB by calling
-        import_nodes.
-
-        :param iotlabdriver: IotlabDriver object, used to have access to
-            iotlabdriver methods and fetching info on sites and nodes.
-        :type iotlabdriver: IotlabDriver
-        """
-
-        sites_listdict = iotlabdriver.iotlab_api.GetSites()
-        nodes_listdict = iotlabdriver.iotlab_api.GetNodes()
-        nodes_by_id = dict([(node['node_id'], node) for node in nodes_listdict])
-        for site in sites_listdict:
-            site_hrn = site['name']
-            site_record = self.find_record_by_type_hrn ('authority', site_hrn)
-            if not site_record:
-                try:
-                    urn = hrn_to_urn(site_hrn, 'authority')
-                    if not self.auth_hierarchy.auth_exists(urn):
-                        self.auth_hierarchy.create_auth(urn)
-
-                    auth_info = self.auth_hierarchy.get_auth_info(urn)
-                    site_record = \
-                        RegAuthority(hrn=site_hrn,
-                                     gid=auth_info.get_gid_object(),
-                                     pointer='-1',
-                                     authority=get_authority(site_hrn))
-                    site_record.just_created()
-                    dbsession.add(site_record)
-                    dbsession.commit()
-                    self.logger.info("IotlabImporter: imported authority \
-                                    (site) %s" % site_record)
-                    self.update_just_added_records_dict(site_record)
-                except SQLAlchemyError:
-                    # if the site import fails then there is no point in
-                    # trying to import the
-                    # site's child records(node, slices, persons), so skip them.
-                    self.logger.log_exc("IotlabImporter: failed to import \
-                        site. Skipping child records")
-                    continue
-            else:
-                # xxx update the record ...
-                pass
-
-            site_record.stale = False
-            self.import_nodes(site['node_ids'], nodes_by_id, iotlabdriver)
-
-            return
 
     def import_nodes(self, site_node_ids, nodes_by_id, iotlabdriver):
         """
 
-        Creates appropriate hostnames and RegNode records for
-        each node in site_node_ids, based on the information given by the
-        dict nodes_by_id that was made from data from OAR.
-        Saves the records to the DB.
+        Creates appropriate hostnames and RegNode records for each node in
+        site_node_ids, based on the information given by the dict nodes_by_id
+        that was made from data from OAR. Saves the records to the DB.
 
         :param site_node_ids: site's node ids
         :type site_node_ids: list of integers
         :param nodes_by_id: dictionary , key is the node id, value is the a dict
-        with node information.
+            with node information.
         :type nodes_by_id: dictionary
-        :param iotlabdriver:IotlabDriver object, used to have access to
+        :param iotlabdriver: IotlabDriver object, used to have access to
             iotlabdriver attributes.
-        :type iotlabdriver:IotlabDriver
+        :type iotlabdriver: IotlabDriver
+
+        :returns: None
+        :rtype: None
 
         """
 
@@ -263,8 +215,8 @@ class IotlabImporter:
                 try:
 
                     node_record.just_created()
-                    dbsession.add(node_record)
-                    dbsession.commit()
+                    global_dbsession.add(node_record)
+                    global_dbsession.commit()
                     self.logger.info("IotlabImporter: imported node: %s"
                                      % node_record)
                     self.update_just_added_records_dict(node_record)
@@ -275,6 +227,63 @@ class IotlabImporter:
                 pass
             node_record.stale = False
 
+    def import_sites_and_nodes(self, iotlabdriver):
+        """
+
+        Gets all the sites and nodes from OAR, process the information,
+        creates hrns and RegAuthority for sites, and feed them to the database.
+        For each site, import the site's nodes to the DB by calling
+        import_nodes.
+
+        :param iotlabdriver: IotlabDriver object, used to have access to
+            iotlabdriver methods and fetching info on sites and nodes.
+        :type iotlabdriver: IotlabDriver
+        """
+
+        sites_listdict = iotlabdriver.iotlab_api.GetSites()
+        nodes_listdict = iotlabdriver.iotlab_api.GetNodes()
+        nodes_by_id = dict([(node['node_id'], node) for node in nodes_listdict])
+        for site in sites_listdict:
+            site_hrn = site['name']
+            site_record = self.find_record_by_type_hrn ('authority', site_hrn)
+            self.logger.info("IotlabImporter: import_sites_and_nodes \
+                                    (site) %s \r\n " % site_record)
+            if not site_record:
+                try:
+                    urn = hrn_to_urn(site_hrn, 'authority')
+                    if not self.auth_hierarchy.auth_exists(urn):
+                        self.auth_hierarchy.create_auth(urn)
+
+                    auth_info = self.auth_hierarchy.get_auth_info(urn)
+                    site_record = \
+                        RegAuthority(hrn=site_hrn,
+                                     gid=auth_info.get_gid_object(),
+                                     pointer='-1',
+                                     authority=get_authority(site_hrn))
+                    site_record.just_created()
+                    global_dbsession.add(site_record)
+                    global_dbsession.commit()
+                    self.logger.info("IotlabImporter: imported authority \
+                                    (site) %s" % site_record)
+                    self.update_just_added_records_dict(site_record)
+                except SQLAlchemyError:
+                    # if the site import fails then there is no point in
+                    # trying to import the
+                    # site's child records(node, slices, persons), so skip them.
+                    self.logger.log_exc("IotlabImporter: failed to import \
+                        site. Skipping child records")
+                    continue
+            else:
+                # xxx update the record ...
+                pass
+
+            site_record.stale = False
+            self.import_nodes(site['node_ids'], nodes_by_id, iotlabdriver)
+
+        return
+
+
+
     def init_person_key(self, person, iotlab_key):
         """
 
@@ -321,9 +330,9 @@ class IotlabImporter:
         import the user's slice onto the database as well by calling
         import_slice.
 
-        :param iotlabdriver:IotlabDriver object, used to have access to
+        :param iotlabdriver: IotlabDriver object, used to have access to
             iotlabdriver attributes.
-        :type iotlabdriver:IotlabDriver
+        :type iotlabdriver: IotlabDriver
         """
         ldap_person_listdict = iotlabdriver.iotlab_api.GetPersons()
         self.logger.info("IOTLABIMPORT \t ldap_person_listdict %s \r\n"
@@ -397,8 +406,8 @@ class IotlabImporter:
 
                         try:
                             user_record.just_created()
-                            dbsession.add (user_record)
-                            dbsession.commit()
+                            global_dbsession.add (user_record)
+                            global_dbsession.commit()
                             self.logger.info("IotlabImporter: imported person \
                                             %s" % (user_record))
                             self.update_just_added_records_dict(user_record)
@@ -433,7 +442,7 @@ class IotlabImporter:
                     user_record.email = person['email']
 
             try:
-                dbsession.commit()
+                global_dbsession.commit()
                 user_record.stale = False
             except SQLAlchemyError:
                 self.logger.log_exc("IotlabImporter: \
@@ -471,8 +480,8 @@ class IotlabImporter:
                                     authority=get_authority(slice_hrn))
             try:
                 slice_record.just_created()
-                dbsession.add(slice_record)
-                dbsession.commit()
+                global_dbsession.add(slice_record)
+                global_dbsession.commit()
 
 
                 self.update_just_added_records_dict(slice_record)
@@ -490,7 +499,7 @@ class IotlabImporter:
 
         slice_record.reg_researchers = [user_record]
         try:
-            dbsession.commit()
+            global_dbsession.commit()
             slice_record.stale = False
         except SQLAlchemyError:
             self.logger.log_exc("IotlabImporter: failed to update slice")
@@ -498,7 +507,7 @@ class IotlabImporter:
 
     def run(self, options):
         """
-        Create the special iotlab table, iotlab_xp, in the iotlab database.
+        Create the special iotlab table, testbed_xp, in the iotlab database.
         Import everything (users, slices, nodes and sites from OAR
         and LDAP) into the SFA database.
         Delete stale records that are no longer in OAR or LDAP.
@@ -508,12 +517,12 @@ class IotlabImporter:
         config = Config()
 
         iotlabdriver = IotlabDriver(config)
-        iotlab_db = IotlabDB(config)
+        iotlab_db = TestbedAdditionalSfaDB(config)
         #Create special slice table for iotlab
 
-        if not iotlab_db.exists('iotlab_xp'):
+        if not iotlab_db.exists('testbed_xp'):
             iotlab_db.createtable()
-            self.logger.info("IotlabImporter.run:  iotlab_xp table created ")
+            self.logger.info("IotlabImporter.run:  testbed_xp table created ")
 
         # import site and node records in site into the SFA db.
         self.import_sites_and_nodes(iotlabdriver)
@@ -544,8 +553,8 @@ class IotlabImporter:
                                  % (record))
 
                 try:
-                    dbsession.delete(record)
-                    dbsession.commit()
+                    global_dbsession.delete(record)
+                    global_dbsession.commit()
                 except SQLAlchemyError:
                     self.logger.log_exc("IotlabImporter: failed to delete \
                         stale record %s" % (record))