import sys
 
 from sfa.util.config import Config
-from sfa.util.xrn import get_authority, hrn_to_urn
-from sfa.planetlab.plxrn import PlXrn
+from sfa.util.xrn import Xrn, get_authority, hrn_to_urn
 
 from sfa.senslab.slabdriver import SlabDriver
 from sfa.senslab.slabpostgres import SliceSenslab, slab_dbsession
 from sfa.trust.gid import create_uuid
 
 from sfa.storage.alchemy import dbsession
-from sfa.storage.model import RegRecord, RegAuthority, RegSlice, RegNode, RegUser, RegKey
+from sfa.storage.model import RegRecord, RegAuthority, RegSlice, RegNode, \
+                                                    RegUser, RegKey
 
 
+from sqlalchemy.exc import SQLAlchemyError
 
 
 def _get_site_hrn(site):
         self.auth_hierarchy = auth_hierarchy
         self.logger=logger
 
-       
-    def hostname_to_hrn(self,root_auth,login_base,hostname):
-        return PlXrn(auth=root_auth,hostname = hostname).get_hrn()   
+    def hostname_to_hrn_escaped(self, root_auth, hostname):
+        return '.'.join( [root_auth,Xrn.escape(hostname)] )
+
+
     
     def slicename_to_hrn(self, person_hrn):
         return  (person_hrn +'_slice')
         ldap_person_listdict = slabdriver.GetPersons()
         slices_listdict = slabdriver.GetSlices()
         try:
-            slices_by_userid = dict ( [ (slice.record_id_user, slice ) for slice in slices_listdict ] )
+            slices_by_userid = dict ( [ (one_slice['record_id_user'], one_slice ) for one_slice in slices_listdict ] )
         except TypeError:
              self.logger.log_exc("SlabImporter: failed to create list of slices by user id.") 
              pass
             site_record = self.find_record_by_type_hrn ('authority', site_hrn)
             if not site_record:
                 try:
-                    urn = hrn_to_urn(site_hrn, 'authority')
+                    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)
                     dbsession.commit()
                     self.logger.info("SlabImporter: imported authority (site) : %s" % site_record) 
                     self.update_just_added_records_dict(site_record)
-                except:
+                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("SlabImporter: failed to import site. Skipping child records") 
                     self.logger.warning ("SlabImporter: cannot find node_id %s - ignored"%node_id)
                     continue 
                 site_auth = get_authority(site_hrn)
-                site_name = site['name']
-                hrn =  self.hostname_to_hrn(slabdriver.root_auth, site_name, node['hostname'])
+                site_name = site['name']                
+                escaped_hrn =  self.hostname_to_hrn_escaped(slabdriver.root_auth, node['hostname'])
+                print>>sys.stderr, "\r\n \r\n SLABIMPORTER node %s " %(node)               
+                hrn =  node['hrn']
+
+
                 # xxx this sounds suspicious
                 if len(hrn) > 64: hrn = hrn[:64]
                 node_record = self.find_record_by_type_hrn( 'node', hrn )
                 if not node_record:
                     try:
                         pkey = Keypair(create=True)
-                        urn = hrn_to_urn(hrn, 'node') 
+                        urn = hrn_to_urn(escaped_hrn, 'node') 
                         node_gid = self.auth_hierarchy.create_gid(urn, create_uuid(), pkey)
                         node_record = RegNode (hrn=hrn, gid=node_gid, 
                                                 pointer = '-1',
-                                                authority=get_authority(hrn))
+                                                authority=get_authority(hrn)) 
                         node_record.just_created()
                         dbsession.add(node_record)
                         dbsession.commit()
-                        self.logger.info("SlabImporter: imported node: %s" % node_record)
-                        print>>sys.stderr, "\r\n \t\t\t SLAB IMPORTER NODE IMPORT NOTnode_record %s " %(node_record)  
+                        self.logger.info("SlabImporter: imported node: %s" % node_record)  
                         self.update_just_added_records_dict(node_record)
                     except:
                         self.logger.log_exc("SlabImporter: failed to import node") 
 
 #import urllib
 #import urllib2
 from sfa.util.config import Config
-from sfa.planetlab.plxrn import PlXrn
 #from sfa.util.xrn import hrn_to_urn, get_authority, Xrn, get_leaf
 
 from sfa.util.sfalogging import logger
             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 slab_hostname_to_hrn(self, root_auth,  hostname):             
+        return root_auth + '.'+ hostname 
+
                              
 
     def ParseSites(self):
         #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():
+        for node_id in self.node_dictlist:
             node  = self.node_dictlist[node_id]
             
             if node['site'] not in nodes_per_site:
                 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)
+        #Create a site dictionary whose 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():
+        for node_id in self.node_dictlist:
             node  = self.node_dictlist[node_id]
-            node.update({'hrn':self.hostname_to_hrn(self.interface_hrn, \
-                                            node['site'],node['hostname'])})
-
+            #node.update({'hrn':self.slab_hostname_to_hrn(self.interface_hrn, \
+                                            #node['site'],node['hostname'])})
+            node.update({'hrn':self.slab_hostname_to_hrn(self.interface_hrn, node['hostname'])})
             self.node_dictlist.update({node_id:node})
 
             if node['site'] not in self.site_dict: