2 # Registry is a GeniServer that implements the Registry interface
13 from sfa.util.geniserver import GeniServer
14 from sfa.util.geniclient import GeniClient
15 from sfa.util.genitable import GeniTable
16 from sfa.util.faults import *
17 from sfa.util.storage import *
19 # GeniLight client support is optional
21 from egeni.geniLight_client import *
23 GeniClientLight = None
26 # Registry is a GeniServer that serves registry and slice operations at PLC.
28 class Registry(GeniServer):
30 # Create a new registry object.
32 # @param ip the ip address to listen on
33 # @param port the port to listen on
34 # @param key_file private key filename of registry
35 # @param cert_file certificate filename containing public key (could be a GID file)
37 def __init__(self, ip, port, key_file, cert_file):
38 GeniServer.__init__(self, ip, port, key_file, cert_file)
39 self.server.interface = 'registry'
43 # Registries is a dictionary of geniclient registry connections keyed on the registry
46 class Registries(dict):
48 required_fields = ['hrn', 'addr', 'port']
50 def __init__(self, api, file = "/etc/sfa/registries.xml"):
51 dict.__init__(self, {})
55 # create default connection dict
57 for field in self.required_fields:
58 connection_dict[field] = ''
59 registries_dict = {'registries': {'registry': [connection_dict]}}
61 # get possible config file locations
63 path = os.path.dirname(os.path.abspath(__file__))
64 filename = file.split(os.sep)[-1]
65 alt_file = path + os.sep + filename
66 files = [file, alt_file]
71 self.registry_info = XmlStorage(f, registries_dict)
75 # if file is missing, just recreate it in the right place
77 self.registry_info = XmlStorage(file, registries_dict)
78 self.registry_info.load()
79 self.connectRegistries()
81 def connectRegistries(self):
83 Get connection details for the trusted peer registries from file and
84 create an GeniClient connection to each.
86 registries = self.registry_info['registries']['registry']
87 if isinstance(registries, dict):
88 registries = [registries]
89 if isinstance(registries, list):
90 for registry in registries:
91 # make sure the required fields are present
92 if not set(self.required_fields).issubset(registry.keys()):
94 hrn, address, port = registry['hrn'], registry['addr'], registry['port']
95 if not hrn or not address or not port:
97 self.interfaces.append(registry)
98 # check which client we should use
99 # geniclient is default
100 client_type = 'geniclient'
101 if registry.has_key('client') and registry['client'] in ['geniclientlight']:
102 client_type = 'geniclientlight'
105 url = 'http://%(address)s:%(port)s' % locals()
107 # create the client connection
108 # make sure module exists before trying to instantiate it
109 if client_type in ['geniclientlight'] and GeniClientLight:
110 self[hrn] = GeniClientLight(url, self.api.key_file, self.api.cert_file)
112 self[hrn] = GeniClient(url, self.api.key_file, self.api.cert_file)
114 # set up a connection to the local registry
115 # connect to registry using GeniClient
116 address = self.api.config.SFA_REGISTRY_HOST
117 port = self.api.config.SFA_REGISTRY_PORT
118 url = 'http://%(address)s:%(port)s' % locals()
119 local_registry = {'hrn': self.api.hrn, 'addr': address, 'port': port}
120 self.interfaces.append(local_registry)
121 self[self.api.hrn] = GeniClient(url, self.api.key_file, self.api.cert_file)