1#!/usr/bin/python3 2 3# Copyright 2014 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7import logging 8import logging.handlers 9 10import common 11 12from autotest_lib.client.common_lib import error 13from autotest_lib.client.cros import constants 14from autotest_lib.client.cros import xmlrpc_server 15from autotest_lib.client.cros.networking import apmanager_proxy 16 17 18class ApmanagerXmlRpcDelegate(xmlrpc_server.XmlRpcDelegate): 19 """Exposes methods called remotely during APManager autotests. 20 21 All instance methods of this object without a preceding '_' are exposed via 22 an XMLRPC server. This is not a stateless handler object, which means that 23 if you store state inside the delegate, that state will remain around for 24 future calls. 25 26 """ 27 28 29 def __init__(self): 30 self._apmanager_proxy = apmanager_proxy.ApmanagerProxy() 31 32 33 def __enter__(self): 34 super(ApmanagerXmlRpcDelegate, self).__enter__() 35 36 37 def __exit__(self, exception, value, traceback): 38 super(ApmanagerXmlRpcDelegate, self).__exit__(exception, value, traceback) 39 40 41 @xmlrpc_server.dbus_safe(None) 42 def start_service(self, config_params): 43 """Create/start an AP service. 44 45 @param config_params dictionary of configuration parameters. 46 @return string object path for the AP service. 47 48 """ 49 return self._apmanager_proxy.start_service(config_params) 50 51 52 def terminate_service(self, service): 53 """Remove/terminate an AP service. 54 55 @param service string object path of the AP service. 56 57 """ 58 self._apmanager_proxy.terminate_service(service) 59 60 61if __name__ == '__main__': 62 logging.basicConfig(level=logging.DEBUG) 63 handler = logging.handlers.SysLogHandler(address='/dev/log') 64 formatter = logging.Formatter( 65 'apmanager_xmlrpc_server: [%(levelname)s] %(message)s') 66 handler.setFormatter(formatter) 67 logging.getLogger().addHandler(handler) 68 logging.debug('apmanager_xmlrpc_server main...') 69 server = xmlrpc_server.XmlRpcServer('localhost', 70 constants.APMANAGER_XMLRPC_SERVER_PORT) 71 if server is None: 72 raise error.TestFail('Failed to setup xmlrpc server for apmanager') 73 else: 74 logging.debug('Server setup') 75 server.register_delegate(ApmanagerXmlRpcDelegate()) 76 server.run() 77