2 from types import StringTypes
3 from PLC.Sites import Sites
5 from sfa.plc.api import GeniAPI
6 from sfa.util.geniclient import *
7 from sfa.util.config import *
8 from sfa.trust.credential import *
9 from sfa.plc.sfaImport import cleanup_string
10 from sfa.server.registry import Registries
11 from sfa.util.record import *
12 from sfa.trust.hierarchy import *
13 from sfa.util.misc import *
14 packages_imported = True
16 packages_imported = False
18 def wrap_exception(method):
19 def wrap(*args, **kwds):
21 return method(*args, **kwds)
26 def required_packages_imported(method):
27 def wrap(*args, **kwds):
29 return method(*args, **kwds)
37 @required_packages_imported
38 def __init__(self, api):
42 # Get the path to the sfa server key/cert files from
43 # the sfa hierarchy object
44 sfa_hierarchy = Hierarchy()
45 sfa_key_path = sfa_hierarchy.basedir
46 key_file = os.path.join(sfa_key_path, "server.key")
47 cert_file = os.path.join(sfa_key_path, "server.cert")
49 # get a connection to our local sfa registry
50 # and a valid credential
52 self.authority = config.SFA_INTERFACE_HRN
53 url = 'http://%s:%s/' %(config.SFA_REGISTRY_HOST, config.SFA_REGISTRY_PORT)
54 self.registry = GeniCleint(url, key_file, cert_file)
55 self.sfa_api = GeniAPI(key_file = key_file, cert_file = cert_file)
56 self.credential = self.sfa_api.getCredential()
57 #cred_file = '/etc/sfa/slicemgr.plc.authority.cred'
58 #self.credential = Credential(filename = cred_file)
60 def get_login_base(self, site_id):
61 sites = Sites(self.api, [site_id], ['login_base'])
62 login_base = sites[0]['login_base']
66 def get_login_bases(self, object):
71 if object.has_key('site_id') and object['site_id']:
72 site_ids.append(object['site_id'])
73 elif object.has_key('site_ids') and object['site_ids']:
74 site_ids.extend(object['site_ids'])
79 for site_id in site_ids:
80 login_bases.append(self.get_login_base(site_id))
84 def get_object_hrn(self, type, object, authority, login_base):
85 parent_hrn = authority + "." + login_base
86 if type in ['person', 'user']:
87 name_parts = object['email'].split("@")
88 hrn = parent_hrn + "." + name_parts[:1][0]
90 elif type in ['slice']:
91 name_parts = object['name'].split("_")
92 hrn = parent_hrn + "." + name_parts[-1:][0]
94 elif type in ['node']:
95 hrn = hostname_to_hrn(self.authority, login_base, object['hostname'])
97 elif type in ['site', 'authority']:
101 raise Exception, "Invalid record type %(type)s" % locals()
105 def sfa_record_exists(self, hrn, type):
107 check if the record (hrn and type) already exist in our sfa db
110 # list is quicker than resolve
111 parent_hrn = get_authority(hrn)
112 if not parent_hrn: parent_hrn = hrn
113 #records = self.registry.list(self.credential, parent_hrn)
114 records = self.registry.resolve(self.credential, hrn)
115 for record in records:
116 if record['type'] == type and record['hrn'] == hrn:
121 @required_packages_imported
122 def update_record(self, object, type, login_bases = None):
123 # determine this objects site and login_base
125 login_bases = self.get_login_bases(object)
127 if isinstance(login_bases, StringTypes):
128 login_bases = [login_bases]
130 for login_base in login_bases:
131 login_base = cleanup_string(login_base)
132 parent_hrn = self.authority + "." + login_base
134 if type in ['person']:
136 elif type in ['site']:
139 # set the object hrn, tpye and create the sfa record
141 object['hrn'] = self.get_object_hrn(type, object, self.authority, login_base)
142 object['type'] = type
144 record = UserRecord(dict=object)
146 elif type in ['slice']:
147 record = SliceRecord(dict=object)
149 elif type in ['node']:
150 record = NodeRecord(dict=object)
152 elif type in ['authority']:
153 record = AuthorityRecord(dict=object)
156 raise Exception, "Invalid record type %(type)s" % locals()
158 # add the record to sfa
159 if not self.sfa_record_exists(object['hrn'], type):
160 self.registry.register(self.credential, record)
162 self.registry.update(self.credential, record)
165 @required_packages_imported
166 def delete_record(self, object, type, login_base = None):
167 if type in ['person']:
169 elif type in ['site']:
172 if type not in ['user', 'slice', 'node', 'authority']:
173 raise Exception, "Invalid type %(type)s" % locals()
176 login_bases = self.get_login_bases(object)
178 login_bases = [login_base]
180 for login_base in login_bases:
181 login_base = cleanup_string(login_base)
182 hrn = self.get_object_hrn(type, object, self.authority, login_base)
183 if self.sfa_record_exists(hrn, type):
184 self.registry.remove(self.credential, type, hrn)