2013-12-05 07:21:06 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2013 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2013-12-06 04:39:27 +00:00
|
|
|
"""
|
|
|
|
Base class (interface) for modules
|
|
|
|
"""
|
|
|
|
|
2014-03-16 03:41:04 +00:00
|
|
|
import sys
|
2014-04-02 22:10:59 +00:00
|
|
|
import traceback
|
2013-12-22 00:34:51 +00:00
|
|
|
import gns3server.jsonrpc as jsonrpc
|
2013-12-05 07:21:06 +00:00
|
|
|
import multiprocessing
|
|
|
|
import zmq
|
2014-02-21 00:39:03 +00:00
|
|
|
import signal
|
2013-12-05 07:21:06 +00:00
|
|
|
|
2014-04-23 18:31:33 +00:00
|
|
|
from jsonschema import validate, ValidationError
|
|
|
|
|
2013-12-05 07:21:06 +00:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class IModule(multiprocessing.Process):
|
|
|
|
"""
|
2013-12-22 00:34:51 +00:00
|
|
|
Module interface.
|
|
|
|
|
|
|
|
:param name: module name
|
|
|
|
:param args: arguments for the module
|
|
|
|
:param kwargs: named arguments for the module
|
2013-12-05 07:21:06 +00:00
|
|
|
"""
|
|
|
|
|
2014-03-11 21:45:04 +00:00
|
|
|
modules = {}
|
2013-12-05 07:21:06 +00:00
|
|
|
|
2014-03-11 21:45:04 +00:00
|
|
|
def __init__(self, name, *args, **kwargs):
|
2013-12-05 07:21:06 +00:00
|
|
|
|
2014-03-11 21:45:04 +00:00
|
|
|
multiprocessing.Process.__init__(self, name=name)
|
2013-12-05 07:21:06 +00:00
|
|
|
self._context = None
|
|
|
|
self._ioloop = None
|
|
|
|
self._stream = None
|
2014-04-26 23:51:47 +00:00
|
|
|
self._dealer = None
|
2014-04-12 00:33:42 +00:00
|
|
|
self._zmq_host = args[0] # ZeroMQ server address
|
|
|
|
self._zmq_port = args[1] # ZeroMQ server port
|
2013-12-05 07:21:06 +00:00
|
|
|
self._current_session = None
|
|
|
|
self._current_destination = None
|
2013-12-22 00:34:51 +00:00
|
|
|
self._current_call_id = None
|
2014-04-24 21:59:34 +00:00
|
|
|
self._stopping = False
|
2013-12-05 07:21:06 +00:00
|
|
|
|
2013-12-06 04:39:27 +00:00
|
|
|
def _setup(self):
|
2013-12-05 07:21:06 +00:00
|
|
|
"""
|
|
|
|
Sets up PyZMQ and creates the stream to handle requests
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._context = zmq.Context()
|
|
|
|
self._ioloop = zmq.eventloop.ioloop.IOLoop.instance()
|
2014-04-12 00:33:42 +00:00
|
|
|
self._stream = self._create_stream(self._zmq_host, self._zmq_port, self._decode_request)
|
2013-12-05 07:21:06 +00:00
|
|
|
|
2013-12-06 04:39:27 +00:00
|
|
|
def _create_stream(self, host=None, port=0, callback=None):
|
2013-12-05 07:21:06 +00:00
|
|
|
"""
|
2013-12-22 00:34:51 +00:00
|
|
|
Creates a new ZMQ stream.
|
|
|
|
|
2014-02-28 04:50:46 +00:00
|
|
|
:returns: ZMQ stream instance
|
2013-12-05 07:21:06 +00:00
|
|
|
"""
|
|
|
|
|
2014-04-26 23:51:47 +00:00
|
|
|
self._dealer = self._context.socket(zmq.DEALER)
|
|
|
|
self._dealer.setsockopt(zmq.IDENTITY, self.name.encode("utf-8"))
|
2013-12-05 07:21:06 +00:00
|
|
|
if host and port:
|
|
|
|
log.info("ZeroMQ client ({}) connecting to {}:{}".format(self.name, host, port))
|
|
|
|
try:
|
2014-04-26 23:51:47 +00:00
|
|
|
self._dealer.connect("tcp://{}:{}".format(host, port))
|
2013-12-05 07:21:06 +00:00
|
|
|
except zmq.error.ZMQError as e:
|
|
|
|
log.critical("Could not connect to ZeroMQ server on {}:{}, reason: {}".format(host, port, e))
|
|
|
|
raise SystemExit
|
|
|
|
else:
|
|
|
|
log.info("ZeroMQ client ({}) connecting to ipc:///tmp/gns3.ipc".format(self.name))
|
|
|
|
try:
|
2014-04-26 23:51:47 +00:00
|
|
|
self._dealer.connect("ipc:///tmp/gns3.ipc")
|
2013-12-05 07:21:06 +00:00
|
|
|
except zmq.error.ZMQError as e:
|
|
|
|
log.critical("Could not connect to ZeroMQ server on ipc:///tmp/gns3.ipc, reason: {}".format(e))
|
|
|
|
raise SystemExit
|
|
|
|
|
2014-04-26 23:51:47 +00:00
|
|
|
stream = zmq.eventloop.zmqstream.ZMQStream(self._dealer, self._ioloop)
|
2013-12-05 07:21:06 +00:00
|
|
|
if callback:
|
|
|
|
stream.on_recv(callback)
|
|
|
|
return stream
|
|
|
|
|
2014-03-02 22:20:03 +00:00
|
|
|
def add_periodic_callback(self, callback, time):
|
|
|
|
"""
|
|
|
|
Adds a periodic callback to the ioloop.
|
|
|
|
|
|
|
|
:param callback: callback to be called
|
|
|
|
:param time: frequency when the callback is executed
|
|
|
|
"""
|
|
|
|
|
|
|
|
periodic_callback = zmq.eventloop.ioloop.PeriodicCallback(callback, time, self._ioloop)
|
|
|
|
return periodic_callback
|
2014-01-31 23:31:34 +00:00
|
|
|
|
2013-12-05 07:21:06 +00:00
|
|
|
def run(self):
|
|
|
|
"""
|
2013-12-06 04:39:27 +00:00
|
|
|
Starts the event loop
|
2013-12-05 07:21:06 +00:00
|
|
|
"""
|
|
|
|
|
2014-02-21 00:39:03 +00:00
|
|
|
def signal_handler(signum=None, frame=None):
|
|
|
|
log.warning("Module {} got signal {}, exiting...".format(self.name, signum))
|
2014-04-26 23:51:47 +00:00
|
|
|
self.stop(signum)
|
2014-02-21 00:39:03 +00:00
|
|
|
|
2014-03-16 03:41:04 +00:00
|
|
|
signals = [signal.SIGTERM, signal.SIGINT]
|
|
|
|
if not sys.platform.startswith("win"):
|
|
|
|
signals.extend([signal.SIGHUP, signal.SIGQUIT])
|
2014-04-01 00:36:45 +00:00
|
|
|
else:
|
|
|
|
signals.extend([signal.SIGBREAK])
|
2014-03-16 03:41:04 +00:00
|
|
|
for sig in signals:
|
2014-02-21 00:39:03 +00:00
|
|
|
signal.signal(sig, signal_handler)
|
|
|
|
|
2013-12-07 00:52:16 +00:00
|
|
|
log.info("{} module running with PID {}".format(self.name, self.pid))
|
2013-12-06 04:39:27 +00:00
|
|
|
self._setup()
|
2013-12-05 07:21:06 +00:00
|
|
|
try:
|
|
|
|
self._ioloop.start()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
return
|
|
|
|
|
2014-04-24 21:59:34 +00:00
|
|
|
log.info("{} module has stopped".format(self.name))
|
|
|
|
|
2014-04-26 23:51:47 +00:00
|
|
|
def _shutdown(self):
|
2013-12-05 07:21:06 +00:00
|
|
|
"""
|
2014-04-26 23:51:47 +00:00
|
|
|
Shutdowns the I/O loop and the ZeroMQ stream & socket
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._ioloop.stop()
|
|
|
|
|
|
|
|
if self._stream and not self._stream.closed:
|
|
|
|
# close the zeroMQ stream
|
|
|
|
self._stream.close()
|
|
|
|
|
|
|
|
if self._dealer and not self._dealer.closed:
|
|
|
|
# close the ZeroMQ dealer socket
|
|
|
|
self._dealer.close()
|
|
|
|
|
|
|
|
def stop(self, signum=None):
|
|
|
|
"""
|
|
|
|
Adds a callback to stop the event loop & ZeroMQ.
|
2014-04-30 16:40:07 +00:00
|
|
|
|
2014-04-26 23:51:47 +00:00
|
|
|
:param signum: signal number (if called by the signal handler)
|
2013-12-05 07:21:06 +00:00
|
|
|
"""
|
|
|
|
|
2014-04-24 21:59:34 +00:00
|
|
|
if not self._stopping:
|
|
|
|
self._stopping = True
|
2014-04-26 23:51:47 +00:00
|
|
|
if signum:
|
|
|
|
self._ioloop.add_callback_from_signal(self._shutdown)
|
|
|
|
else:
|
|
|
|
self._shutdown()
|
2013-12-05 07:21:06 +00:00
|
|
|
|
2013-12-22 00:34:51 +00:00
|
|
|
def send_response(self, results):
|
2013-12-05 07:21:06 +00:00
|
|
|
"""
|
2013-12-22 00:34:51 +00:00
|
|
|
Sends a response back to the requester.
|
2013-12-06 04:39:27 +00:00
|
|
|
|
2013-12-22 00:34:51 +00:00
|
|
|
:param results: JSON results to the ZeroMQ server
|
2013-12-05 07:21:06 +00:00
|
|
|
"""
|
|
|
|
|
2013-12-22 00:34:51 +00:00
|
|
|
jsonrpc_response = jsonrpc.JSONRPCResponse(results, self._current_call_id)()
|
|
|
|
|
|
|
|
# add session to the response
|
|
|
|
response = [self._current_session, jsonrpc_response]
|
2013-12-05 07:21:06 +00:00
|
|
|
log.debug("ZeroMQ client ({}) sending: {}".format(self.name, response))
|
|
|
|
self._stream.send_json(response)
|
|
|
|
|
2013-12-22 00:34:51 +00:00
|
|
|
def send_param_error(self):
|
|
|
|
"""
|
|
|
|
Sends a param error back to the requester.
|
|
|
|
"""
|
|
|
|
|
|
|
|
jsonrpc_response = jsonrpc.JSONRPCInvalidParams(self._current_call_id)()
|
|
|
|
|
|
|
|
# add session to the response
|
|
|
|
response = [self._current_session, jsonrpc_response]
|
|
|
|
log.info("ZeroMQ client ({}) sending JSON-RPC param error for call id {}".format(self.name, self._current_call_id))
|
|
|
|
self._stream.send_json(response)
|
|
|
|
|
|
|
|
def send_internal_error(self):
|
|
|
|
"""
|
|
|
|
Sends a param error back to the requester.
|
|
|
|
"""
|
|
|
|
|
|
|
|
jsonrpc_response = jsonrpc.JSONRPCInternalError()()
|
|
|
|
|
|
|
|
# add session to the response
|
|
|
|
response = [self._current_session, jsonrpc_response]
|
|
|
|
log.critical("ZeroMQ client ({}) sending JSON-RPC internal error".format(self.name))
|
|
|
|
self._stream.send_json(response)
|
|
|
|
|
|
|
|
def send_custom_error(self, message, code=-3200):
|
|
|
|
"""
|
|
|
|
Sends a custom error back to the requester.
|
|
|
|
"""
|
|
|
|
|
|
|
|
jsonrpc_response = jsonrpc.JSONRPCCustomError(code, message, self._current_call_id)()
|
|
|
|
|
|
|
|
# add session to the response
|
|
|
|
response = [self._current_session, jsonrpc_response]
|
2014-01-31 23:31:34 +00:00
|
|
|
log.info("ZeroMQ client ({}) sending JSON-RPC custom error: {} for call id {}".format(self.name,
|
|
|
|
message,
|
|
|
|
self._current_call_id))
|
2013-12-22 00:34:51 +00:00
|
|
|
self._stream.send_json(response)
|
|
|
|
|
2014-03-20 00:48:42 +00:00
|
|
|
def send_notification(self, destination, results):
|
2014-03-16 03:41:04 +00:00
|
|
|
"""
|
|
|
|
Sends a notification
|
|
|
|
|
2014-03-20 00:48:42 +00:00
|
|
|
:param destination: destination (or method)
|
|
|
|
:param results: JSON results to the ZeroMQ router
|
2014-03-16 03:41:04 +00:00
|
|
|
"""
|
|
|
|
|
2014-03-20 00:48:42 +00:00
|
|
|
jsonrpc_response = jsonrpc.JSONRPCNotification(destination, results)()
|
2014-03-16 03:41:04 +00:00
|
|
|
|
|
|
|
# add session to the response
|
|
|
|
response = [self._current_session, jsonrpc_response]
|
|
|
|
log.debug("ZeroMQ client ({}) sending: {}".format(self.name, response))
|
|
|
|
self._stream.send_json(response)
|
|
|
|
|
2013-12-06 04:39:27 +00:00
|
|
|
def _decode_request(self, request):
|
2013-12-05 07:21:06 +00:00
|
|
|
"""
|
2013-12-22 00:34:51 +00:00
|
|
|
Decodes the request to JSON.
|
2013-12-06 04:39:27 +00:00
|
|
|
|
|
|
|
:param request: request from ZeroMQ server
|
2013-12-05 07:21:06 +00:00
|
|
|
"""
|
|
|
|
|
2014-04-30 16:40:07 +00:00
|
|
|
# server is shutting down, do not process
|
|
|
|
# more request
|
|
|
|
if self._stopping:
|
|
|
|
return
|
|
|
|
|
2013-12-05 07:21:06 +00:00
|
|
|
try:
|
|
|
|
request = zmq.utils.jsonapi.loads(request[0])
|
|
|
|
except ValueError:
|
2013-12-22 00:34:51 +00:00
|
|
|
self._current_session = None
|
|
|
|
self.send_internal_error()
|
2013-12-05 07:21:06 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
log.debug("ZeroMQ client ({}) received: {}".format(self.name, request))
|
|
|
|
self._current_session = request[0]
|
2013-12-22 00:34:51 +00:00
|
|
|
self._current_call_id = request[1].get("id")
|
|
|
|
destination = request[1].get("method")
|
|
|
|
params = request[1].get("params")
|
2013-12-05 07:21:06 +00:00
|
|
|
|
2014-03-11 21:45:04 +00:00
|
|
|
if destination not in self.modules[self.name]:
|
2013-12-22 00:34:51 +00:00
|
|
|
self.send_internal_error()
|
2013-12-05 07:21:06 +00:00
|
|
|
return
|
2013-12-22 00:34:51 +00:00
|
|
|
|
|
|
|
log.debug("Routing request to {}: {}".format(destination, request[1]))
|
2014-01-31 23:31:34 +00:00
|
|
|
|
|
|
|
try:
|
2014-03-11 21:45:04 +00:00
|
|
|
self.modules[self.name][destination](self, params)
|
2014-01-31 23:31:34 +00:00
|
|
|
except Exception as e:
|
|
|
|
log.error("uncaught exception {type}".format(type=type(e)), exc_info=1)
|
2014-04-02 22:10:59 +00:00
|
|
|
exc_type, exc_value, exc_tb = sys.exc_info()
|
|
|
|
lines = traceback.format_exception(exc_type, exc_value, exc_tb)
|
|
|
|
tb = "\n" . join(lines)
|
|
|
|
self.send_custom_error("uncaught exception {type}: {string}\n{tb}".format(type=type(e),
|
|
|
|
string=str(e),
|
|
|
|
tb=tb))
|
2013-12-05 07:21:06 +00:00
|
|
|
|
2014-04-23 18:31:33 +00:00
|
|
|
def validate_request(self, request, schema):
|
|
|
|
"""
|
|
|
|
Validates a request.
|
|
|
|
|
|
|
|
:param request: request (JSON-RPC params)
|
|
|
|
:param schema: JSON-SCHEMA to validate the request
|
|
|
|
|
|
|
|
:returns: True or False
|
|
|
|
"""
|
|
|
|
|
|
|
|
# check if we have a request
|
|
|
|
if request == None:
|
|
|
|
self.send_param_error()
|
|
|
|
return False
|
|
|
|
log.debug("received request {}".format(request))
|
|
|
|
|
|
|
|
# validate the request
|
|
|
|
try:
|
|
|
|
validate(request, schema)
|
|
|
|
except ValidationError as e:
|
|
|
|
self.send_custom_error("request validation error: {}".format(e))
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2013-12-05 07:21:06 +00:00
|
|
|
def destinations(self):
|
|
|
|
"""
|
2013-12-06 04:39:27 +00:00
|
|
|
Destinations handled by this module.
|
|
|
|
|
|
|
|
:returns: list of destinations
|
2013-12-05 07:21:06 +00:00
|
|
|
"""
|
|
|
|
|
2014-03-11 21:45:04 +00:00
|
|
|
if not self.name in self.modules:
|
|
|
|
log.warn("no destinations found for module {}".format(self.name))
|
|
|
|
return []
|
|
|
|
return self.modules[self.name].keys()
|
2013-12-05 07:21:06 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def route(cls, destination):
|
|
|
|
"""
|
|
|
|
Decorator to register a destination routed to a method
|
2013-12-06 04:39:27 +00:00
|
|
|
|
|
|
|
:param destination: destination to be routed
|
2013-12-05 07:21:06 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def wrapper(method):
|
2014-03-11 21:45:04 +00:00
|
|
|
module = destination.split(".")[0]
|
|
|
|
if not module in cls.modules:
|
|
|
|
cls.modules[module] = {}
|
|
|
|
cls.modules[module][destination] = method
|
2013-12-05 07:21:06 +00:00
|
|
|
return method
|
|
|
|
return wrapper
|