use xmlrpcprotocol instead of geniclient
[sfa.git] / sfa / server / registry.py
1 #
2 # Registry is a GeniServer 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.geniserver import GeniServer
14 from sfa.util.genitable import GeniTable
15 from sfa.util.faults import *
16 from sfa.util.storage import *
17 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
18 import sfa.util.soapprotocol as soapprotocol
19  
20 # GeniLight client support is optional
21 try:
22     from egeni.geniLight_client import *
23 except ImportError:
24     GeniClientLight = None            
25
26 ##
27 # Registry is a GeniServer that serves registry and slice operations at PLC.
28
29 class Registry(GeniServer):
30     ##
31     # Create a new registry object.
32     #
33     # @param ip the ip address to listen on
34     # @param port the port to listen on
35     # @param key_file private key filename of registry
36     # @param cert_file certificate filename containing public key (could be a GID file)
37
38     def __init__(self, ip, port, key_file, cert_file):
39         GeniServer.__init__(self, ip, port, key_file, cert_file)
40         self.server.interface = 'registry' 
41
42
43 ##
44 # Registries is a dictionary of geniclient registry connections keyed on the registry
45 # hrn
46
47 class Registries(dict):
48
49     required_fields = ['hrn', 'addr', 'port']
50
51     def __init__(self, api, file = "/etc/sfa/registries.xml"):
52         dict.__init__(self, {})
53         self.api = api
54         self.interfaces = []
55        
56         # create default connection dict
57         connection_dict = {}
58         for field in self.required_fields:
59             connection_dict[field] = ''
60         registries_dict = {'registries': {'registry': [connection_dict]}}
61
62         # get possible config file locations
63         loaded = False
64         path = os.path.dirname(os.path.abspath(__file__))
65         filename = file.split(os.sep)[-1]
66         alt_file = path + os.sep + filename
67         files = [file, alt_file]
68     
69         for f in files:
70             try:
71                 if os.path.isfile(f):
72                     self.registry_info = XmlStorage(f, registries_dict)
73                     loaded = True
74             except: pass
75
76         # if file is missing, just recreate it in the right place
77         if not loaded:
78             self.registry_info = XmlStorage(file, registries_dict)
79         self.registry_info.load()
80         self.connectRegistries()
81         
82     def connectRegistries(self):
83         """
84         Get connection details for the trusted peer registries from file and 
85         create an GeniClient connection to each. 
86         """
87         registries = self.registry_info['registries']['registry']
88         if isinstance(registries, dict):
89             registries = [registries]
90         if isinstance(registries, list):
91             for registry in registries:
92                 # make sure the required fields are present
93                 if not set(self.required_fields).issubset(registry.keys()):
94                     continue
95                 hrn, address, port = registry['hrn'], registry['addr'], registry['port']
96                 if not hrn or not address or not port:
97                     continue
98                 self.interfaces.append(registry)
99                 # check which client we should use
100                 # geniclient is default
101                 client_type = 'geniclient'
102                 if registry.has_key('client') and registry['client'] in ['geniclientlight']:
103                     client_type = 'geniclientlight'
104                 
105                 # create url
106                 url = 'http://%(address)s:%(port)s' % locals()
107
108                 # create the client connection
109                 # make sure module exists before trying to instantiate it
110                 if client_type in ['geniclientlight'] and GeniClientLight:
111                     self[hrn] = GeniClientLight(url, self.api.key_file, self.api.cert_file) 
112                 else:    
113                     self[hrn] = xmlrpcprotocol.get_server(url, self.api.key_file, self.api.cert_file)
114
115         # set up a connection to the local registry
116         # connect to registry using GeniClient
117         address = self.api.config.SFA_REGISTRY_HOST
118         port = self.api.config.SFA_REGISTRY_PORT
119         url = 'http://%(address)s:%(port)s' % locals()
120         local_registry = {'hrn': self.api.hrn, 'addr': address, 'port': port}
121         self.interfaces.append(local_registry)
122         self[self.api.hrn] = xmlrpcprotocol.get_server(url, self.api.key_file, self.api.cert_file)            
123