c1d74b5915cd5f6574e8e6923b5c68b7823b3c85
[sfa.git] / geni / aggregate.py
1 import os
2 import sys
3 import datetime
4 import time
5 import xmlrpclib
6
7 from types import StringTypes, ListType
8 from geni.util.geniserver import GeniServer
9 from geni.util.geniclient import GeniClient
10 from geni.util.storage import *
11 from geni.util.excep import *
12
13 class Aggregate(GeniServer):
14
15     ##
16     # Create a new aggregate object.
17     #
18     # @param ip the ip address to listen on
19     # @param port the port to listen on
20     # @param key_file private key filename of registry
21     # @param cert_file certificate filename containing public key (could be a GID file)     
22
23     def __init__(self, ip, port, key_file, cert_file, config = "/usr/share/geniwrapper/geni/util/geni_config"):
24         GeniServer.__init__(self, ip, port, key_file, cert_file)
25         self.server.interface = 'aggregate'
26
27 ##
28 # Aggregates is a dictionary of geniclient aggregate connections keyed on the aggregate hrn
29
30 class Aggregates(dict):
31     
32     def __init__(self, api):
33         dict.__init__(self, {})
34         self.api = api
35         aggregates_file = self.api.server_basedir + os.sep + 'aggregates.xml'
36         connection_dict = {'hrn': '', 'addr': '', 'port': ''}
37         self.aggregate_info = XmlStorage(aggregates_file, {'aggregates': {'aggregate': [connection_dict]}})
38         self.aggregate_info.load()
39         self.connectAggregates()
40
41
42     def connectAggregates(self):
43         """
44         Get connection details for the trusted peer aggregates from file and 
45         create an GeniClient connection to each. 
46         """
47         required_fields = ['hrn', 'addr', 'port']
48         aggregates = self.aggregate_info['aggregates']['aggregate']
49         if isinstance(aggregates, dict):
50             aggregates = [aggregates]
51         if isinstance(aggregates, list):
52             for aggregate in aggregates:
53                 # create xmlrpc connection using GeniClient
54                 if not set(required_fields).issubset(aggregate.keys()):
55                     continue
56                 hrn, address, port = aggregate['hrn'], aggregate['addr'], aggregate['port']
57                 if not hrn or not address or not port:
58                     continue
59                 url = 'http://%(address)s:%(port)s' % locals()
60                 self[hrn] = GeniClient(url, self.api.key_file, self.api.cert_file)
61
62         # set up a connection to the local registry
63         # connect to registry using GeniClient
64         address = self.api.config.GENI_AGGREGATE_HOSTNAME
65         port = self.api.config.GENI_AGGREGATE_PORT
66         url = 'http://%(address)s:%(port)s' % locals()
67         self[self.api.hrn] = GeniClient(url, self.api.key_file, self.api.cert_file)
68