removed config arg from class constuctor
[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 # GeniLight client support is optional
11 try:
12     from egeni.geniLight_client import *
13 except ImportError:
14     GeniClientLight = None
15 from geni.util.storage import *
16 from geni.util.excep import *
17
18 class Aggregate(GeniServer):
19
20     ##
21     # Create a new aggregate object.
22     #
23     # @param ip the ip address to listen on
24     # @param port the port to listen on
25     # @param key_file private key filename of registry
26     # @param cert_file certificate filename containing public key (could be a GID file)     
27
28     def __init__(self, ip, port, key_file, cert_file):
29         GeniServer.__init__(self, ip, port, key_file, cert_file)
30         self.server.interface = 'aggregate'
31
32 ##
33 # Aggregates is a dictionary of geniclient aggregate connections keyed on the aggregate hrn
34
35 class Aggregates(dict):
36
37     required_fields = ['hrn', 'addr', 'port']
38      
39     def __init__(self, api):
40         dict.__init__(self, {})
41         self.api = api
42         aggregates_file = self.api.server_basedir + os.sep + 'aggregates.xml'
43         connection_dict = {}
44         for field in self.required_fields:
45             connection_dict[field] = ''
46         self.aggregate_info = XmlStorage(aggregates_file, {'aggregates': {'aggregate': [connection_dict]}})
47         self.aggregate_info.load()
48         self.connectAggregates()
49
50
51     def connectAggregates(self):
52         """
53         Get connection details for the trusted peer aggregates from file and 
54         create an GeniClient connection to each. 
55         """
56         aggregates = self.aggregate_info['aggregates']['aggregate']
57         if isinstance(aggregates, dict):
58             aggregates = [aggregates]
59         if isinstance(aggregates, list):
60             for aggregate in aggregates:
61                 # make sure the required fields are present
62                 if not set(self.required_fields).issubset(aggregate.keys()):
63                     continue
64                 hrn, address, port = aggregate['hrn'], aggregate['addr'], aggregate['port']
65                 if not hrn or not address or not port:
66                     continue
67                 # check which client we should use
68                 # geniclient is default
69                 client_type = 'geniclient'
70                 if aggregate.has_key('client') and aggregate['client'] in ['geniclientlight']:
71                     client_type = 'geniclientlight'
72                 
73                 # create url
74                 url = 'http://%(address)s:%(port)s' % locals()
75
76                 # create the client connection
77                 # make sure module exists before trying to instantiate it
78                 if client_type in ['geniclientlight'] and GeniClientLight:
79                     self[hrn] = GeniClientLight(url, self.api.key_file, self.api.cert_file)
80                 else:
81                     self[hrn] = GeniClient(url, self.api.key_file, self.api.cert_file)
82
83         # set up a connection to the local registry
84         # connect to registry using GeniClient
85         address = self.api.config.GENI_AGGREGATE_HOSTNAME
86         port = self.api.config.GENI_AGGREGATE_PORT
87         url = 'http://%(address)s:%(port)s' % locals()
88         self[self.api.hrn] = GeniClient(url, self.api.key_file, self.api.cert_file)
89