9 from sfa.util.server import SfaServer
10 from sfa.util.storage import *
11 from sfa.util.faults import *
12 from sfa.trust.gid import GID
13 from sfa.util.table import SfaTable
14 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
15 import sfa.util.soapprotocol as soapprotocol
17 # GeniLight client support is optional
19 from egeni.geniLight_client import *
21 GeniClientLight = None
24 class Aggregate(SfaServer):
27 # Create a new aggregate object.
29 # @param ip the ip address to listen on
30 # @param port the port to listen on
31 # @param key_file private key filename of registry
32 # @param cert_file certificate filename containing public key (could be a GID file)
33 def __init__(self, ip, port, key_file, cert_file):
34 SfaServer.__init__(self, ip, port, key_file, cert_file)
35 self.server.interface = 'aggregate'
38 # Aggregates is a dictionary of aggregate connections keyed on the aggregate hrn
40 class Aggregates(dict):
48 def __init__(self, api, file = "/etc/sfa/aggregates.xml"):
49 dict.__init__(self, {})
52 # create default connection dict
53 aggregates_dict = {'aggregates': {'aggregate': [default_fields]}}
56 self.aggregate_info = XmlStorage(file, aggregates_dict)
57 self.aggregate_info.load()
58 self.interfaces = self.registry_info['aggregates']['aggregate']
59 if not isinstance(self.interfaces, list):
60 self.interfaces = [self.interfaces]
62 # Attempt to get any missing peer gids
63 # There should be a gid file in /etc/sfa/trusted_roots for every
64 # peer registry found in in the aggregates.xml config file. If there
65 # are any missing gids, request a new one from the peer registry.
66 gids_current = self.api.auth.trusted_cert_list.get_list()
67 hrns_current = [gid.get_hrn() for gid in gids_found]
68 hrns_expected = [interface['hrn'] for interfaces in self.interfaces]
69 new_hrns = set(hrns_current).difference(hrns_expected)
71 self.get_peer_gids(new_hrns)
72 self.connectAggregates()
74 def connectAggregates(self):
76 Get connection details for the trusted peer aggregates from file and
77 create an connection to each.
79 aggregates = self.aggregate_info['aggregates']['aggregate']
80 if isinstance(aggregates, dict):
81 aggregates = [aggregates]
82 if isinstance(aggregates, list):
83 for aggregate in aggregates:
84 # make sure the required fields are present
85 if not set(self.required_fields).issubset(aggregate.keys()):
87 hrn, address, port = aggregate['hrn'], aggregate['addr'], aggregate['port']
88 if not hrn or not address or not port:
90 self.interfaces.append(aggregate)
91 # check which client we should use
92 # sfa.util.xmlrpcprotocol is default
93 client_type = 'xmlrpcprotocol'
94 if aggregate.has_key('client') and aggregate['client'] in ['geniclientlight']:
95 client_type = 'geniclientlight'
98 url = 'http://%(address)s:%(port)s' % locals()
100 # create the client connection
101 # make sure module exists before trying to instantiate it
102 if client_type in ['geniclientlight'] and GeniClientLight:
103 self[hrn] = GeniClientLight(url, self.api.key_file, self.api.cert_file)
105 self[hrn] = xmlrpcprotocol.get_server(url, self.api.key_file, self.api.cert_file)
107 # set up a connection to the local registry
108 address = self.api.config.SFA_AGGREGATE_HOST
109 port = self.api.config.SFA_AGGREGATE_PORT
110 url = 'http://%(address)s:%(port)s' % locals()
111 local_aggregate = {'hrn': self.api.hrn, 'addr': address, 'port': port}
112 self.interfaces.append(local_aggregate)
113 self[self.api.hrn] = xmlrpcprotocol.get_server(url, self.api.key_file, self.api.cert_file)