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