1ecee511fb743aadfe469f1fc05b162e679f2cca
[sfa.git] / sfa / server / interface.py
1 from sfa.util.faults import *
2 from sfa.util.storage import XmlStorage
3 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
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 get_server(self, key_file, cert_file, timeout=30):
29         server = None 
30         if  self.client_type ==  'geniclientlight' and GeniClientLight:
31             server = GeniClientLight(url, self.api.key_file, self.api.cert_file)
32         else:
33             server = xmlrpcprotocol.get_server(self.get_url(), key_file, cert_file, timeout) 
34  
35         return server       
36 ##
37 # In is a dictionary of registry connections keyed on the registry
38 # hrn
39
40 class Interfaces(dict):
41     """
42     Interfaces is a base class for managing information on the
43     peers we are federated with. Provides connections (xmlrpc or soap) to federated peers
44     """
45
46     # fields that must be specified in the config file
47     default_fields = {
48         'hrn': '',
49         'addr': '', 
50         'port': '', 
51     }
52
53     # defined by the class 
54     default_dict = {}
55
56     def __init__(self, conf_file):
57         dict.__init__(self, {})
58         # load config file
59         self.interface_info = XmlStorage(conf_file, self.default_dict)
60         self.interface_info.load()
61         records = self.interface_info.values()[0]
62         if not isinstance(records, list):
63             records = [records]
64         
65         required_fields = self.default_fields.keys()
66         for record in records:
67             if not record or not set(required_fields).issubset(record.keys()):
68                 continue
69             # port is appended onto the domain, before the path. Should look like:
70             # http://domain:port/path
71             hrn, address, port = record['hrn'], record['addr'], record['port']
72             interface = Interface(hrn, address, port) 
73             self[hrn] = interface
74
75     def get_server(self, hrn, key_file, cert_file, timeout=30):
76         return self[hrn].get_server(key_file, cert_file, timeout)