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)
116 for record in records:
117 if record['type'] == type and record['hrn'] == hrn:
122 @required_packages_imported
123 def update_record(self, object, type, login_bases = None):
124 # determine this objects site and login_base
126 login_bases = self.get_login_bases(object)
128 if isinstance(login_bases, StringTypes):
129 login_bases = [login_bases]
131 for login_base in login_bases:
132 login_base = cleanup_string(login_base)
133 parent_hrn = self.authority + "." + login_base
135 if type in ['person']:
137 elif type in ['site']:
140 # set the object hrn, tpye and create the sfa record
142 object['hrn'] = self.get_object_hrn(type, object, self.authority, login_base)
143 object['type'] = type
145 record = UserRecord(dict=object)
147 elif type in ['slice']:
148 record = SliceRecord(dict=object)
150 elif type in ['node']:
151 record = NodeRecord(dict=object)
153 elif type in ['authority']:
154 record = AuthorityRecord(dict=object)
157 raise Exception, "Invalid record type %(type)s" % locals()
159 # add the record to sfa
160 if not self.sfa_record_exists(object['hrn'], type):
161 self.registry.register(self.credential, record)
163 self.registry.update(self.credential, record)
166 @required_packages_imported
167 def delete_record(self, object, type, login_base = None):
168 if type in ['person']:
170 elif type in ['site']:
173 if type not in ['user', 'slice', 'node', 'authority']:
174 raise Exception, "Invalid type %(type)s" % locals()
177 login_bases = self.get_login_bases(object)
179 login_bases = [login_base]
181 for login_base in login_bases:
182 login_base = cleanup_string(login_base)
183 hrn = self.get_object_hrn(type, object, self.authority, login_base)
184 if self.sfa_record_exists(hrn, type):
185 self.registry.remove(self.credential, type, hrn)