From de6e581a83658003a2a741348e4fe05e0f9fca95 Mon Sep 17 00:00:00 2001 From: Tony Mack Date: Sun, 16 Oct 2011 22:15:15 -0400 Subject: [PATCH] refactored. Added Interface class --- sfa/server/aggregate.py | 27 +++++------- sfa/server/interface.py | 98 ++++++++++++++++------------------------- sfa/server/registry.py | 27 +++++------- 3 files changed, 60 insertions(+), 92 deletions(-) diff --git a/sfa/server/aggregate.py b/sfa/server/aggregate.py index 1a96e157..59a3e6b8 100644 --- a/sfa/server/aggregate.py +++ b/sfa/server/aggregate.py @@ -1,7 +1,8 @@ from sfa.util.faults import * from sfa.util.server import SfaServer from sfa.util.xrn import hrn_to_urn -from sfa.server.interface import Interfaces +from sfa.server.interface import Interfaces, Interface +from sfa.util.config import Config class Aggregate(SfaServer): @@ -22,19 +23,13 @@ class Aggregates(Interfaces): default_dict = {'aggregates': {'aggregate': [Interfaces.default_fields]}} - def __init__(self, api, conf_file = "/etc/sfa/aggregates.xml"): - Interfaces.__init__(self, api, conf_file) + def __init__(self, conf_file = "/etc/sfa/aggregates.xml"): + Interfaces.__init__(self, conf_file) + sfa_config = Config() # set up a connection to the local aggregate - if self.api.config.SFA_AGGREGATE_ENABLED: - address = self.api.config.SFA_AGGREGATE_HOST - port = self.api.config.SFA_AGGREGATE_PORT - url = 'http://%(address)s:%(port)s' % locals() - local_aggregate = {'hrn': self.api.hrn, - 'urn': hrn_to_urn(self.api.hrn, 'authority'), - 'addr': address, - 'port': port, - 'url': url} - self.interfaces[self.api.hrn] = local_aggregate - - # get connections - self.update(self.get_connections()) + if sfa_config.SFA_AGGREGATE_ENABLED: + addr = sfa_config.SFA_AGGREGATE_HOST + port = sfa_config.SFA_AGGREGATE_PORT + hrn = sfa_config.SFA_INTERFACE_HRN + interface = Interface(hrn, addr, port) + self[hrn] = interface diff --git a/sfa/server/interface.py b/sfa/server/interface.py index 52df22be..baeb2e79 100644 --- a/sfa/server/interface.py +++ b/sfa/server/interface.py @@ -16,6 +16,29 @@ except ImportError: GeniClientLight = None + +class Interface: + + def __init__(self, hrn, addr, port, client_type='sfa'): + self.hrn = hrn + self.addr = addr + self.port = port + self.client_type = client_type + + def get_url(self): + address_parts = self.addr.split('/') + address_parts[0] = address_parts[0] + ":" + str(self.port) + url = "http://%s" % "/".join(address_parts) + return url + + def get_server(self, key_file, cert_file, timeout=30): + server = None + if self.client_type == 'geniclientlight' and GeniClientLight: + server = GeniClientLight(url, self.api.key_file, self.api.cert_file) + else: + server = xmlrpcprotocol.get_server(self.get_url(), key_file, cert_file, timeout) + + return server ## # In is a dictionary of registry connections keyed on the registry # hrn @@ -23,12 +46,7 @@ except ImportError: class Interfaces(dict): """ Interfaces is a base class for managing information on the - peers we are federated with. It is responsible for the following: - - 1) Makes sure a record exist in the local registry for the each - fedeated peer - 2) Attempts to fetch and install trusted gids - 3) Provides connections (xmlrpc or soap) to federated peers + peers we are federated with. Provides connections (xmlrpc or soap) to federated peers """ # fields that must be specified in the config file @@ -41,64 +59,24 @@ class Interfaces(dict): # defined by the class default_dict = {} - types = ['authority'] - - def __init__(self, api, conf_file, type='authority'): - if type not in self.types: - raise SfaInfaildArgument('Invalid type %s: must be in %s' % (type, self.types)) + def __init__(self, conf_file): dict.__init__(self, {}) - self.api = api - self.type = type # load config file self.interface_info = XmlStorage(conf_file, self.default_dict) self.interface_info.load() - interfaces = self.interface_info.values()[0].values()[0] - if not isinstance(interfaces, list): - interfaces = [self.interfaces] - # set the url and urn - for interface in interfaces: + records = self.interface_info.values()[0].values()[0] + if not isinstance(records, list): + records = [records] + + required_fields = self.default_fields.keys() + for record in records: + if not set(required_fields).issubset(record.keys()): + continue # port is appended onto the domain, before the path. Should look like: # http://domain:port/path - hrn, address, port = interface['hrn'], interface['addr'], interface['port'] - address_parts = address.split('/') - address_parts[0] = address_parts[0] + ":" + str(port) - url = "http://%s" % "/".join(address_parts) - interface['url'] = url - interface['urn'] = hrn_to_urn(hrn, 'authority') - - self.interfaces = {} - required_fields = self.default_fields.keys() - for interface in interfaces: - valid = True - # skp any interface definition that has a null hrn, - # address or port - for field in required_fields: - if field not in interface or not interface[field]: - valid = False - break - if valid: - self.interfaces[interface['hrn']] = interface - - - def get_connections(self): - """ - read connection details for the trusted peer registries from file return - a dictionary of connections keyed on interface hrn. - """ - connections = {} - required_fields = self.default_fields.keys() - for interface in self.interfaces.values(): - url = interface['url'] -# sfa_logger().debug("Interfaces.get_connections - looping on neighbour %s"%url) - # check which client we should use - # sfa.util.xmlrpcprotocol is default - client_type = 'xmlrpcprotocol' - if interface.has_key('client') and \ - interface['client'] in ['geniclientlight'] and \ - GeniClientLight: - client_type = 'geniclientlight' - connections[hrn] = GeniClientLight(url, self.api.key_file, self.api.cert_file) - else: - connections[interface['hrn']] = xmlrpcprotocol.get_server(url, self.api.key_file, self.api.cert_file, timeout=30) + hrn, address, port = record['hrn'], record['addr'], record['port'] + interface = Interface(hrn, address, port) + self[hrn] = interface - return connections + def get_server(self, hrn, key_file, cert_file, timeout=30): + return self[hrn].get_server(key_file, cert_file, timeout) diff --git a/sfa/server/registry.py b/sfa/server/registry.py index da25b2a4..0536c5c1 100644 --- a/sfa/server/registry.py +++ b/sfa/server/registry.py @@ -8,8 +8,8 @@ from sfa.util.server import SfaServer from sfa.util.faults import * from sfa.util.xrn import hrn_to_urn -from sfa.server.interface import Interfaces - +from sfa.server.interface import Interfaces, Interface +from sfa.util.config import Config ## # Registry is a SfaServer that serves registry and slice operations at PLC. @@ -33,17 +33,12 @@ class Registries(Interfaces): default_dict = {'registries': {'registry': [Interfaces.default_fields]}} - def __init__(self, api, conf_file = "/etc/sfa/registries.xml"): - Interfaces.__init__(self, api, conf_file) - address = self.api.config.SFA_REGISTRY_HOST - port = self.api.config.SFA_REGISTRY_PORT - url = 'http://%(address)s:%(port)s' % locals() - local_registry = {'hrn': self.api.hrn, - 'urn': hrn_to_urn(self.api.hrn, 'authority'), - 'addr': address, - 'port': port, - 'url': url} - self.interfaces[self.api.hrn] = local_registry - - # get connections - self.update(self.get_connections()) + def __init__(self, conf_file = "/etc/sfa/registries.xml"): + Interfaces.__init__(self, conf_file) + sfa_config = Config() + if sfa_config.SFA_REGISTRY_ENABLED: + addr = sfa_config.SFA_REGISTRY_HOST + port = sfa_config.SFA_REGISTRY_PORT + hrn = sfa_config.SFA_INTERFACE_HRN + interface = Interface(hrn, addr, port) + self[hrn] = interface -- 2.43.0