920857c74e9a7b72c3dd3ac816d9f7b9521f47b1
[sfa.git] / sfa / server / interface.py
1 #from sfa.util.faults import *
2 import sfa.client.sfaprotocol as sfaprotocol
3 from sfa.util.xml import XML
4
5 # GeniLight client support is optional
6 try:
7     from egeni.geniLight_client import *
8 except ImportError:
9     GeniClientLight = None            
10
11 class Interface:
12     """
13     Interface to another SFA service, typically a peer, or the local aggregate
14     can retrieve a xmlrpclib.ServerProxy object for issuing calls there
15     """
16     def __init__(self, hrn, addr, port, client_type='sfa'):
17         self.hrn = hrn
18         self.addr = addr
19         self.port = port
20         self.client_type = client_type
21   
22     def get_url(self):
23         address_parts = self.addr.split('/')
24         address_parts[0] = address_parts[0] + ":" + str(self.port)
25         url =  "http://%s" %  "/".join(address_parts)
26         return url
27
28     def server_proxy(self, key_file, cert_file, timeout=30):
29         server = None 
30         if  self.client_type ==  'geniclientlight' and GeniClientLight:
31             # xxx url and self.api are undefined
32             server = GeniClientLight(url, self.api.key_file, self.api.cert_file)
33         else:
34             server = sfaprotocol.server_proxy(self.get_url(), key_file, cert_file, timeout) 
35  
36         return server       
37 ##
38 # In is a dictionary of registry connections keyed on the registry
39 # hrn
40
41 class Interfaces(dict):
42     """
43     Interfaces is a base class for managing information on the
44     peers we are federated with. Provides connections (xmlrpc or soap) to federated peers
45     """
46
47     # fields that must be specified in the config file
48     default_fields = {
49         'hrn': '',
50         'addr': '', 
51         'port': '', 
52     }
53
54     # defined by the class 
55     default_dict = {}
56
57     def __init__(self, conf_file):
58         dict.__init__(self, {})
59         # load config file
60         required_fields = set(self.default_fields.keys())
61         self.interface_info = XML(conf_file).todict()
62         for value in self.interface_info.values():
63             if isinstance(value, list):
64                 for record in value:
65                     if isinstance(record, dict) and \
66                       required_fields.issubset(record.keys()):
67                         hrn, address, port = record['hrn'], record['addr'], record['port']
68                         # sometime this is called at a very early stage with no config loaded
69                         # avoid to remember this instance in such a case
70                         if not address or not port:
71                             continue     
72                         interface = Interface(hrn, address, port)
73                         self[hrn] = interface   
74
75     def server_proxy(self, hrn, key_file, cert_file, timeout=30):
76         return self[hrn].server_proxy(key_file, cert_file, timeout)