removed another bunch of references to geni
[sfa.git] / sfa / server / registry.py
1 #
2 # Registry is a SfaServer that implements the Registry interface
3 #
4 ### $Id$
5 ### $URL$
6 #
7
8 import tempfile
9 import os
10 import time
11 import sys
12
13 from sfa.util.server import SfaServer
14 from sfa.util.faults import *
15 from sfa.util.storage import *
16 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
17 import sfa.util.soapprotocol as soapprotocol
18  
19 # GeniLight client support is optional
20 try:
21     from egeni.geniLight_client import *
22 except ImportError:
23     GeniClientLight = None            
24
25 ##
26 # Registry is a SfaServer that serves registry and slice operations at PLC.
27
28 class Registry(SfaServer):
29     ##
30     # Create a new registry object.
31     #
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)
36
37     def __init__(self, ip, port, key_file, cert_file):
38         SfaServer.__init__(self, ip, port, key_file, cert_file)
39         self.server.interface = 'registry' 
40
41
42 ##
43 # Registries is a dictionary of registry connections keyed on the registry
44 # hrn
45
46 class Registries(dict):
47
48     required_fields = ['hrn', 'addr', 'port']
49
50     def __init__(self, api, file = "/etc/sfa/registries.xml"):
51         dict.__init__(self, {})
52         self.api = api
53         self.interfaces = []
54        
55         # create default connection dict
56         connection_dict = {}
57         for field in self.required_fields:
58             connection_dict[field] = ''
59         registries_dict = {'registries': {'registry': [connection_dict]}}
60
61         # get possible config file locations
62         loaded = False
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]
67     
68         for f in files:
69             try:
70                 if os.path.isfile(f):
71                     self.registry_info = XmlStorage(f, registries_dict)
72                     loaded = True
73             except: pass
74
75         # if file is missing, just recreate it in the right place
76         if not loaded:
77             self.registry_info = XmlStorage(file, registries_dict)
78         self.registry_info.load()
79         self.connectRegistries()
80         
81     def connectRegistries(self):
82         """
83         Get connection details for the trusted peer registries from file and 
84         create a connection to each. 
85         """
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()):
93                     continue
94                 hrn, address, port = registry['hrn'], registry['addr'], registry['port']
95                 if not hrn or not address or not port:
96                     continue
97                 self.interfaces.append(registry)
98                 # check which client we should use
99                 # sfa.util.xmlrpcprotocol is default
100                 client_type = 'xmlrpcprotocol'
101                 if registry.has_key('client') and registry['client'] in ['geniclientlight']:
102                     client_type = 'geniclientlight'
103                 
104                 # create url
105                 url = 'http://%(address)s:%(port)s' % locals()
106
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) 
111                 else:    
112                     self[hrn] = xmlrpcprotocol.get_server(url, self.api.key_file, self.api.cert_file)
113
114         # set up a connection to the local registry
115         address = self.api.config.SFA_REGISTRY_HOST
116         port = self.api.config.SFA_REGISTRY_PORT
117         url = 'http://%(address)s:%(port)s' % locals()
118         local_registry = {'hrn': self.api.hrn, 'addr': address, 'port': port}
119         self.interfaces.append(local_registry)
120         self[self.api.hrn] = xmlrpcprotocol.get_server(url, self.api.key_file, self.api.cert_file)            
121