remove unnecessary imports
[plcapi.git] / PLC / SFA.py
index 5d23e5a..4fdaa1c 100644 (file)
@@ -2,9 +2,10 @@ import traceback
 from types import StringTypes
 from PLC.Sites import Sites
 try:
-    from sfa.plc.api import GeniAPI
+    from sfa.util.geniclient import *
+    from sfa.util.config import *
+    from sfa.trust.credential import *         
     from sfa.plc.sfaImport import cleanup_string
-    from sfa.server.registry import Registries
     from sfa.util.record import *
     from sfa.trust.hierarchy import *
     from sfa.util.misc import *
@@ -45,13 +46,14 @@ class SFA:
     
         # get a connection to our local sfa registry
         # and a valid credential
-        self.sfa_api = GeniAPI(key_file = key_file, cert_file = cert_file)
-        self.sfa_api.interface = "other"
-        registries = Registries(self.sfa_api)
-        self.registry = registries[self.sfa_api.hrn]
-        self.credential = self.sfa_api.getCredential()
-        self.authority = self.sfa_api.hrn
-        
+        config = Config()
+        self.authority = config.SFA_INTERFACE_HRN
+        url = 'http://%s:%s/' %(config.SFA_REGISTRY_HOST, config.SFA_REGISTRY_PORT) 
+        self.registry = GeniCleint(url, key_file, cert_file)
+        #self.sfa_api = GeniAPI(key_file = key_file, cert_file = cert_file)
+        #self.credential = self.sfa_api.getCredential()
+        cred_file = '/etc/sfa/slicemgr.plc.authority.cred'
+        self.credential = Credential(filename = cred_file)   
 
     def get_login_base(self, site_id):
         sites = Sites(self.api, [site_id], ['login_base'])
@@ -69,7 +71,7 @@ class SFA:
         elif object.has_key('site_ids') and object['site_ids']:
             site_ids.extend(object['site_ids'])
         else:
-            raise Exception
+            return login_bases
 
         # get the login bases
         for site_id in site_ids:
@@ -81,11 +83,11 @@ class SFA:
         parent_hrn = authority + "." + login_base 
         if type in ['person', 'user']:
             name_parts = object['email'].split("@")
-            hrn = parent_hrn + "." + name_parts[:1]
+            hrn = parent_hrn + "." + name_parts[:1][0]
         
         elif type in ['slice']:
             name_parts = object['name'].split("_")
-            hrn = parent_hrn + "." + name_parts[-1:]
+            hrn = parent_hrn + "." + name_parts[-1:][0]
         
         elif type in ['node']:
             hrn = hostname_to_hrn(self.authority, login_base, object['hostname'])
@@ -98,6 +100,21 @@ class SFA:
 
         return hrn
 
+    def sfa_record_exists(self, hrn, type):
+        """
+        check if the record (hrn and type) already exist in our sfa db
+        """
+        exists = False
+        # list is quicker than resolve
+        parent_hrn = get_authority(hrn)
+        if not parent_hrn: parent_hrn = hrn
+        #records = self.registry.list(self.credential, parent_hrn)
+        records = self.registry.resolve(self.credential, hrn)
+        for record in records: 
+            if record['type'] == type and record['hrn'] == hrn:
+                exists = True
+        return exists 
+
     @wrap_exception
     @required_packages_imported
     def update_record(self, object, type, login_bases = None):
@@ -111,31 +128,36 @@ class SFA:
         for login_base in login_bases:
             login_base = cleanup_string(login_base)
             parent_hrn = self.authority + "." + login_base
-                
+                        
+            if type in ['person']:
+                type = 'user'
+            elif type in ['site']:
+                type = 'authority'
+        
             # set the object hrn, tpye and create the sfa record 
             # object 
             object['hrn'] = self.get_object_hrn(type, object, self.authority, login_base)   
-            if type in ['person', 'user']:
-                object['type'] = 'user'
+            object['type'] = type
+            if type in ['user']:
                 record = UserRecord(dict=object)
 
             elif type in ['slice']:
-                object['type'] = 'slice'
                 record = SliceRecord(dict=object)
 
             elif type in ['node']:
-                object['type'] = 'node'
                 record = NodeRecord(dict=object)
     
-            elif type in ['site']:
-                object['type'] = 'authority'
+            elif type in ['authority']:
                 record = AuthorityRecord(dict=object)
 
             else:
                 raise Exception, "Invalid record type %(type)s" % locals()
 
             # add the record to sfa
-            self.registry.register(self.credential, record)
+            if not self.sfa_record_exists(object['hrn'], type):
+                self.registry.register(self.credential, record)
+            else:
+                self.registry.update(self.credential, record)
 
     @wrap_exception
     @required_packages_imported
@@ -156,5 +178,6 @@ class SFA:
         for login_base in login_bases:
             login_base = cleanup_string(login_base)
             hrn = self.get_object_hrn(type, object, self.authority, login_base)
-            self.registry.remove(self.credential, type, hrn) 
+            if self.sfa_record_exists(hrn, type):
+                self.registry.remove(self.credential, type, hrn)