class AxonRootNamespaceClientAgent(object): """ Launch Servers in Root Namespace """ def __init__(self, record_queue): self.mngrs_map = {} self.connected_state = ConnectedStateProcessor(DBConnectedState()) self._primary_ep = None self._record_queue = record_queue self.log = logging.getLogger(__name__) # TODO(Pradeep Singh) MOve below code to a common location @property def primary_endpoint(self): """ Get the primary endpoint address from the DB It assumes in non namespace mode there will be only single interface. """ if not self._primary_ep: endpoints = self.connected_state.get_connected_state() if endpoints: self._primary_ep = endpoints[0]['endpoint'] return self._primary_ep def start_clients(self, namespace='localhost'): if not self.primary_endpoint: self.log.warning("Clients will not be started since " "no connected state exists yet") return clients = self.connected_state.get_clients(self.primary_endpoint) clients = clients if clients else [] if clients: src = self.primary_endpoint mngr = self.mngrs_map.get((namespace, src), RootNsClientManager(self._record_queue)) mngr.start_client(src, clients) self.mngrs_map[(namespace, src)] = mngr def stop_clients(self, namespace='localhost'): """ Stop all Clients :return: None """ for mngr in self.mngrs_map.values(): mngr.stop_clients() def stop_client(self, namespace='localhost', endpoint=None): ns_list = [namespace] endpoint = endpoint or self.primary_endpoint for (ns, src), mngr in self.mngrs_map.items(): if ns not in ns_list: continue if src == endpoint: mngr.stop_client() break
def __init__(self, config): self.log = logging.getLogger(__name__) self._conf = config self._cs_db = ConnectedStateProcessor(DBConnectedState()) namespaces = NamespaceManager().get_all_namespaces() if namespaces and self._conf.NAMESPACE_MODE: self.namespace_mode = True self._server_agent = AxonNameSpaceServerAgent() self._client_agent = AxonNameSpaceClientAgent() else: self.namespace_mode = False self._server_agent = AxonRootNamespaceServerAgent() self._client_agent = AxonRootNamespaceClientAgent()
class AxonRootNamespaceClientAgent(object): """ Launch Servers in Root Namespace """ def __init__(self): self.mngrs_map = {} self.connected_state = ConnectedStateProcessor(DBConnectedState()) self._primary_ep = None self.log = logging.getLogger(__name__) # TODO(Pradeep Singh) MOve below code to a common location @property def primary_endpoint(self): """ Get the primary endpoint address from the DB It assumes in non namespace mode there will be only single interface. """ if not self._primary_ep: endpoints = self.connected_state.get_connected_state() if endpoints: self._primary_ep = endpoints[0]['endpoint'] return self._primary_ep def start_clients(self): if not self.primary_endpoint: self.log.warning("Clients will not be started since " "no connected state exists yet") return clients = self.connected_state.get_clients(self.primary_endpoint) clients = clients if clients else [] if clients: mngr = RootNsClientManager() if not \ self.mngrs_map.get('localhost') else \ self.mngrs_map.get('localhost') mngr.start_client(self.primary_endpoint, clients) self.mngrs_map['localhost'] = mngr def stop_clients(self, namespace='localhost'): """ Stop all Clients :return: None """ for mngr in self.mngrs_map.values(): mngr.stop_clients() def stop_client(self, namespace='localhost'): for mngr in self.mngrs_map.values(): mngr.stop_client()
def __init__(self, config): self.log = logging.getLogger(__name__) self._conf = config self._cs_db = ConnectedStateProcessor(DBConnectedState()) namespaces = NamespaceManager().get_all_namespaces() record_queue = Queue(RECORD_QUEUE_SIZE) if namespaces and self._conf.NAMESPACE_MODE: self.namespace_mode = True self._server_agent = AxonNameSpaceServerAgent() self._client_agent = AxonNameSpaceClientAgent(record_queue) else: self.namespace_mode = False self._server_agent = AxonRootNamespaceServerAgent() self._client_agent = AxonRootNamespaceClientAgent(record_queue) self._start_db_pool_manager(record_queue)
def __init__(self, config, record_queue=None): self.log = logging.getLogger(__name__) self._conf = config self._cs_db = ConnectedStateProcessor(DBConnectedState()) namespaces = NamespaceManager().get_all_namespaces() record_queue = Queue( RECORD_QUEUE_SIZE) if record_queue is None else record_queue if self._conf.NAMESPACE_MODE: if not namespaces: self.log.warning( "No namespace is found but NAMESPACE_MODE " "is set to True on config file. Create namespace(s) " "and run rediscover_namespaces") self.namespace_mode = True self._server_agent = AxonNameSpaceServerAgent() self._client_agent = AxonNameSpaceClientAgent(record_queue) else: self.namespace_mode = False self._server_agent = AxonRootNamespaceServerAgent() self._client_agent = AxonRootNamespaceClientAgent(record_queue) self._start_db_pool_manager(record_queue)
class TrafficApp(object): def __init__(self, config, record_queue=None): self.log = logging.getLogger(__name__) self._conf = config self._cs_db = ConnectedStateProcessor(DBConnectedState()) namespaces = NamespaceManager().get_all_namespaces() record_queue = Queue( RECORD_QUEUE_SIZE) if record_queue is None else record_queue if self._conf.NAMESPACE_MODE: if not namespaces: self.log.warning( "No namespace is found but NAMESPACE_MODE " "is set to True on config file. Create namespace(s) " "and run rediscover_namespaces") self.namespace_mode = True self._server_agent = AxonNameSpaceServerAgent() self._client_agent = AxonNameSpaceClientAgent(record_queue) else: self.namespace_mode = False self._server_agent = AxonRootNamespaceServerAgent() self._client_agent = AxonRootNamespaceClientAgent(record_queue) self._start_db_pool_manager(record_queue) def _start_db_pool_manager(self, queue): manager = DBPoolManager(queue) thread = threading.Thread(target=manager.run) thread.daemon = True thread.start() def add_server(self, protocol, port, endpoint, namespace=None): if not self.namespace_mode and namespace: raise ValueError("namespace parameter must not be provided," "as the Axon is running in non namespace mode") self.log.info("Add %s Server on port %s started" % (protocol, port)) self._server_agent.add_server(port, protocol, endpoint, namespace) def delete_traffic_rules(self, endpoint=None): # If endpoint is None , traffic rules for all endpoints will be # deleted endpoint_name = endpoint if endpoint else 'all' self.log.info("Deleting traffic config for %s" % endpoint_name) self._cs_db.delete_connected_state(endpoint) def register_traffic(self, traffic_configs): self.log.info("Register traffic called with config %s" % traffic_configs) for config in traffic_configs: self._cs_db.create_or_update_connected_state( config['endpoint'], config['servers'], config['clients']) def unregister_traffic(self, traffic_configs): self.log.info("Un-Register traffic called with config %s" % traffic_configs) for config in traffic_configs: self._cs_db.delete_connected_state(config.get('endpoint'), config.get('servers', []), config.get('clients', [])) def get_traffic_rules(self, endpoint=None): return self._cs_db.get_connected_state(endpoint) def list_servers(self): return self._server_agent.list_servers() def get_server(self, protocol, port): return self._server_agent.get_server(protocol, port) def stop_servers(self, namespace=None): self.log.info("=====Stop servers called=====") if not self.namespace_mode and namespace: raise ValueError("namespace parameter must not be provided," "as the Axon is running in non namespace mode") self._server_agent.stop_servers(namespace) def start_servers(self, namespace=None): self.log.info("=====Start servers called=====") if not self.namespace_mode and namespace: raise ValueError("namespace parameter must not be provided," "as the Axon is running in non namespace mode") self._server_agent.start_servers(namespace) def stop_server(self, protocol, port, namespace=None, endpoint=None): self.log.info("stop %s server called on port %s" % (protocol, port)) if not self.namespace_mode and namespace: raise ValueError("namespace parameter must not be provided," "as the Axon is running in non namespace mode") self._server_agent.stop_server(protocol, port, namespace, endpoint) def stop_client(self, namespace=None, endpoint=None): self.log.info("====stop client initiated====") if not self.namespace_mode and namespace: raise ValueError("namespace parameter must not be provided," "as the Axon is running in non namespace mode") self._client_agent.stop_client(namespace, endpoint) def stop_clients(self, namespace=None): self.log.info("====stop clients initiated====") if not self.namespace_mode and namespace: raise ValueError("namespace parameter must not be provided," "as the Axon is running in non namespace mode") self._client_agent.stop_clients(namespace=namespace) def start_clients(self): self.log.info("====start clients initiated====") self._client_agent.start_clients() def rediscover_namespaces(self): self.log.info("====rediscover namespaces initiated====") self._server_agent.rediscover_namespaces() self._client_agent.rediscover_namespaces()
def __init__(self): self.mngrs_map = {} self.connected_state = ConnectedStateProcessor(DBConnectedState()) self._primary_ep = None self._if_manager = InterfaceManager() self.log = logging.getLogger(__name__)
class AxonRootNamespaceServerAgent(object): """ Launch Servers in Root Namespace """ def __init__(self): self.mngrs_map = {} self.connected_state = ConnectedStateProcessor(DBConnectedState()) self._primary_ep = None self._if_manager = InterfaceManager() self.log = logging.getLogger(__name__) @property def primary_endpoint(self): """ Get the primary endpoint address from the DB It assumes in non namespace mode there will be only single interface. """ if not self._primary_ep: endpoints = self.connected_state.get_connected_state() if endpoints: self._primary_ep = endpoints[0]['endpoint'] return self._primary_ep def start_servers(self, namespace='root'): """ Start Set of default servers :return: None """ if not self.primary_endpoint: self.log.warning("Server will not be started since " "no connected state exists yet") return mngr = RootNsServerManager() if not \ self.mngrs_map.get(namespace) else self.mngrs_map.get(namespace) servers = self.connected_state.get_servers(self.primary_endpoint) servers = servers if servers else [] for proto, port in servers: mngr.start_server(proto, port, self.primary_endpoint) self.mngrs_map[namespace] = mngr def stop_servers(self, namespace='root'): """ Stop all Servers :return: None """ for mngr in self.mngrs_map.values(): mngr.stop_all_servers() def add_server(self, port, protocol, endpoint, namespace='root'): """ Run a Server in a root namespace :param port: port on which server will listen :type port: int :param protocol: protocol on which server will listen :type protocol: str :return: None """ interface = self._if_manager.get_interface_by_ip(endpoint) if not interface: self.log.error("No interface fount with IP %s on host" % endpoint) return mngr = RootNsServerManager() if not \ self.mngrs_map.get(namespace) else self.mngrs_map.get(namespace) mngr.start_server(protocol, port, endpoint) self.connected_state.create_or_update_connected_state( endpoint, [(protocol, port)]) self.mngrs_map[namespace] = mngr def list_servers(self): server_list = [] for mngr in self.mngrs_map.values(): server_list.extend(mngr.list_servers()) return server_list def get_server(self, protocol, port): server_list = [] for mngr in self.mngrs_map.values(): server_list.extend(mngr.get_server(protocol, port)) return server_list def stop_server(self, protocol, port, namespace='root'): for mngr in self.mngrs_map.values(): mngr.stop_server(port, protocol)
def __init__(self, record_queue): self.mngrs_map = {} self.connected_state = ConnectedStateProcessor(DBConnectedState()) self._primary_ep = None self._record_queue = record_queue self.log = logging.getLogger(__name__)
class TrafficApp(object): def __init__(self, config): self.log = logging.getLogger(__name__) self._conf = config self._cs_db = ConnectedStateProcessor(DBConnectedState()) namespaces = NamespaceManager().get_all_namespaces() if namespaces and self._conf.NAMESPACE_MODE: self.namespace_mode = True self._server_agent = AxonNameSpaceServerAgent() self._client_agent = AxonNameSpaceClientAgent() else: self.namespace_mode = False self._server_agent = AxonRootNamespaceServerAgent() self._client_agent = AxonRootNamespaceClientAgent() def add_server(self, protocol, port, endpoint, namespace=None): if not self.namespace_mode and namespace: raise ValueError("namespace parameter must not be provided," "as the Axon is running in non namespace mode") self.log.info("Add %s Server on port %s started" % (protocol, port)) self._server_agent.add_server(port, protocol, endpoint, namespace) def register_traffic(self, traffic_configs): self.log.info("Register traffic called with config %s" % traffic_configs) for config in traffic_configs: self._cs_db.create_or_update_connected_state( config['endpoint'], config['servers'], config['clients']) def unregister_traffic(self, traffic_configs): self.log.info("Un-Register traffic called with config %s" % traffic_configs) for config in traffic_configs: self._cs_db.delete_connected_state(config.get('endpoint'), config.get('servers', []), config.get('clients', [])) def get_traffic_config(self, endpoint=None): return self._cs_db.get_connected_state(endpoint) def list_servers(self): return self._server_agent.list_servers() def get_server(self, protocol, port): return self._server_agent.get_server(protocol, port) def stop_servers(self, namespace=None): self.log.info("=====Stop servers called=====") if not self.namespace_mode and namespace: raise ValueError("namespace parameter must not be provided," "as the Axon is running in non namespace mode") self._server_agent.stop_servers(namespace) def start_servers(self, namespace=None): self.log.info("=====Start servers called=====") if not self.namespace_mode and namespace: raise ValueError("namespace parameter must not be provided," "as the Axon is running in non namespace mode") self._server_agent.start_servers(namespace) def stop_server(self, protocol, port, namespace=None): self.log.info("stop %s server called on port %s" % (protocol, port)) if not self.namespace_mode and namespace: raise ValueError("namespace parameter must not be provided," "as the Axon is running in non namespace mode") self._server_agent.stop_server(protocol, port, namespace) def stop_client(self, namespace=None): self.log.info("====stop client initiated====") if not self.namespace_mode and namespace: raise ValueError("namespace parameter must not be provided," "as the Axon is running in non namespace mode") self._client_agent.stop_client(namespace) def stop_clients(self, namespace=None): self.log.info("====stop clients initiated====") if not self.namespace_mode and namespace: raise ValueError("namespace parameter must not be provided," "as the Axon is running in non namespace mode") self._client_agent.stop_clients(namespace=namespace) def start_clients(self): self.log.info("====start clients initiated====") self._client_agent.start_clients()