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