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