diff options
author | imarom <imarom@cisco.com> | 2015-08-20 13:51:31 +0300 |
---|---|---|
committer | imarom <imarom@cisco.com> | 2015-08-20 13:51:31 +0300 |
commit | a6be0ea727a21e24e1efb77eaf55893a545de233 (patch) | |
tree | a044166baa6aec4b1d5274dfb3cb72fec8126ca7 /src | |
parent | da56afe25e71f4dc65ae4669889eec5b8fc43afc (diff) |
added support for paramters in the python console
Diffstat (limited to 'src')
-rwxr-xr-x | src/console/trex_console.py | 27 | ||||
-rw-r--r-- | src/console/trex_rpc_client.py | 12 | ||||
-rw-r--r-- | src/rpc-server/include/trex_rpc_cmd_api.h | 14 | ||||
-rw-r--r-- | src/rpc-server/src/commands/trex_rpc_cmd_test.cpp | 12 | ||||
-rw-r--r-- | src/rpc-server/src/commands/trex_rpc_cmds.h | 10 | ||||
-rw-r--r-- | src/rpc-server/src/trex_rpc_jsonrpc_v2_parser.cpp | 10 |
6 files changed, 66 insertions, 19 deletions
diff --git a/src/console/trex_console.py b/src/console/trex_console.py index be84640c..b4048f5b 100755 --- a/src/console/trex_console.py +++ b/src/console/trex_console.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- import cmd import json +import ast from trex_rpc_client import RpcClient import trex_status @@ -75,10 +76,32 @@ class TrexConsole(cmd.Cmd): def do_rpc (self, line): '''\nLaunches a RPC on the server\n''' if line == "": - print "\nUsage: [method name] [param 1] ...\n" + print "\nUsage: [method name] [param dict as string]\n" return - rc, msg = self.rpc_client.invoke_rpc_method(line) + sp = line.split(' ', 1) + method = sp[0] + + params = None + bad_parse = False + if len(sp) > 1: + + try: + params = ast.literal_eval(sp[1]) + if not isinstance(params, dict): + bad_parse = True + + except ValueError as e1: + bad_parse = True + except SyntaxError as e2: + bad_parse = True + + if bad_parse: + print "\nValue should be a valid dict: '{0}'".format(sp[1]) + print "\nUsage: [method name] [param dict as string]\n" + return + + rc, msg = self.rpc_client.invoke_rpc_method(method, params) if rc: print "[SUCCESS]\n" else: diff --git a/src/console/trex_rpc_client.py b/src/console/trex_rpc_client.py index f3edd252..3ec0e1f6 100644 --- a/src/console/trex_rpc_client.py +++ b/src/console/trex_rpc_client.py @@ -24,7 +24,7 @@ class RpcClient(): return rc
def pretty_json (self, json_str):
- return json.dumps(json.loads(json_str), indent = 4, separators=(',', ': '))
+ return json.dumps(json.loads(json_str), indent = 4, separators=(',', ': '), sort_keys = True)
def verbose_msg (self, msg):
if not self.verbose:
@@ -38,9 +38,7 @@ class RpcClient(): msg["jsonrpc"] = "2.0"
msg["method"] = method_name
- msg["params"] = {}
- for key, value in params.iteritems():
- msg["params"][key] = value
+ msg["params"] = params
msg["id"] = id
@@ -101,13 +99,13 @@ class RpcClient(): def ping_rpc_server (self):
- return self.invoke_rpc_method("rpc_ping", block = False)
+ return self.invoke_rpc_method("ping", block = False)
def get_rpc_server_status (self):
- return self.invoke_rpc_method("rpc_get_status")
+ return self.invoke_rpc_method("get_status")
def query_rpc_server (self):
- return self.invoke_rpc_method("rpc_get_reg_cmds")
+ return self.invoke_rpc_method("get_reg_cmds")
def set_verbose (self, mode):
diff --git a/src/rpc-server/include/trex_rpc_cmd_api.h b/src/rpc-server/include/trex_rpc_cmd_api.h index 308e344c..c773b15f 100644 --- a/src/rpc-server/include/trex_rpc_cmd_api.h +++ b/src/rpc-server/include/trex_rpc_cmd_api.h @@ -69,6 +69,20 @@ protected: */ virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result) = 0; + /** + * error generating functions + * + */ + void genernate_err(Json::Value &result, const std::string &msg) { + result["specific_err"] = msg; + } + + void generate_err_param_count(Json::Value &result, int expected, int provided) { + std::stringstream ss; + ss << "method expects '" << expected << "' paramteres, '" << provided << "' provided"; + genernate_err(result, ss.str()); + } + std::string m_name; }; diff --git a/src/rpc-server/src/commands/trex_rpc_cmd_test.cpp b/src/rpc-server/src/commands/trex_rpc_cmd_test.cpp index e9cc4665..e2dc8959 100644 --- a/src/rpc-server/src/commands/trex_rpc_cmd_test.cpp +++ b/src/rpc-server/src/commands/trex_rpc_cmd_test.cpp @@ -37,11 +37,18 @@ TrexRpcCmdTestAdd::_run(const Json::Value ¶ms, Json::Value &result) { /* validate count */ if (params.size() != 2) { + generate_err_param_count(result, 2, params.size()); return (TrexRpcCommand::RPC_CMD_PARAM_COUNT_ERR); } /* check we have all the required paramters */ - if (!x.isInt() || !y.isInt()) { + if (!x.isInt()) { + genernate_err(result, "'x' is either missing or not an integer"); + return (TrexRpcCommand::RPC_CMD_PARAM_PARSE_ERR); + } + + if (!y.isInt()) { + genernate_err(result, "'y' is either missing or not an integer"); return (TrexRpcCommand::RPC_CMD_PARAM_PARSE_ERR); } @@ -62,6 +69,7 @@ TrexRpcCmdTestSub::_run(const Json::Value ¶ms, Json::Value &result) { /* validate count */ if (params.size() != 2) { + generate_err_param_count(result, 2, params.size()); return (TrexRpcCommand::RPC_CMD_PARAM_COUNT_ERR); } @@ -82,6 +90,7 @@ TrexRpcCmdPing::_run(const Json::Value ¶ms, Json::Value &result) { /* validate count */ if (params.size() != 0) { + generate_err_param_count(result, 0, params.size()); return (TrexRpcCommand::RPC_CMD_PARAM_COUNT_ERR); } @@ -98,6 +107,7 @@ TrexRpcCmdGetReg::_run(const Json::Value ¶ms, Json::Value &result) { /* validate count */ if (params.size() != 0) { + generate_err_param_count(result, 0, params.size()); return (TrexRpcCommand::RPC_CMD_PARAM_COUNT_ERR); } diff --git a/src/rpc-server/src/commands/trex_rpc_cmds.h b/src/rpc-server/src/commands/trex_rpc_cmds.h index 0778b75d..e37e1cda 100644 --- a/src/rpc-server/src/commands/trex_rpc_cmds.h +++ b/src/rpc-server/src/commands/trex_rpc_cmds.h @@ -35,7 +35,7 @@ limitations under the License. */ class TrexRpcCmdTestAdd : public TrexRpcCommand { public: - TrexRpcCmdTestAdd() : TrexRpcCommand("rpc_test_add") {} + TrexRpcCmdTestAdd() : TrexRpcCommand("test_add") {} protected: virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result); }; @@ -46,7 +46,7 @@ protected: */ class TrexRpcCmdTestSub : public TrexRpcCommand { public: - TrexRpcCmdTestSub() : TrexRpcCommand("rpc_test_sub") {} ; + TrexRpcCmdTestSub() : TrexRpcCommand("test_sub") {} ; protected: virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result); }; @@ -57,7 +57,7 @@ protected: */ class TrexRpcCmdPing : public TrexRpcCommand { public: - TrexRpcCmdPing() : TrexRpcCommand("rpc_ping") {}; + TrexRpcCmdPing() : TrexRpcCommand("ping") {}; protected: virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result); }; @@ -68,7 +68,7 @@ protected: */ class TrexRpcCmdGetReg : public TrexRpcCommand { public: - TrexRpcCmdGetReg() : TrexRpcCommand("rpc_get_reg_cmds") {}; + TrexRpcCmdGetReg() : TrexRpcCommand("get_reg_cmds") {}; protected: virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result); }; @@ -79,7 +79,7 @@ protected: */ class TrexRpcCmdGetStatus : public TrexRpcCommand { public: - TrexRpcCmdGetStatus() : TrexRpcCommand("rpc_get_status") {}; + TrexRpcCmdGetStatus() : TrexRpcCommand("get_status") {}; protected: virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result); }; diff --git a/src/rpc-server/src/trex_rpc_jsonrpc_v2_parser.cpp b/src/rpc-server/src/trex_rpc_jsonrpc_v2_parser.cpp index c11c603f..be1eb2f8 100644 --- a/src/rpc-server/src/trex_rpc_jsonrpc_v2_parser.cpp +++ b/src/rpc-server/src/trex_rpc_jsonrpc_v2_parser.cpp @@ -80,13 +80,15 @@ public: case TrexRpcCommand::RPC_CMD_PARAM_COUNT_ERR: case TrexRpcCommand::RPC_CMD_PARAM_PARSE_ERR: - response["error"]["code"] = JSONRPC_V2_ERR_INVALID_PARAMS; - response["error"]["message"] = "Bad paramters for method"; + response["error"]["code"] = JSONRPC_V2_ERR_INVALID_PARAMS; + response["error"]["message"] = "Bad paramters for method"; + response["error"]["specific_err"] = result["specific_err"]; break; case TrexRpcCommand::RPC_CMD_INTERNAL_ERR: - response["error"]["code"] = JSONRPC_V2_ERR_INTERNAL_ERROR; - response["error"]["message"] = "Internal Server Error"; + response["error"]["code"] = JSONRPC_V2_ERR_INTERNAL_ERROR; + response["error"]["message"] = "Internal Server Error"; + response["error"]["specific_err"] = result["specific_err"]; break; } |