diff options
-rwxr-xr-x | linux/ws_main.py | 12 | ||||
-rwxr-xr-x | linux_dpdk/ws_main.py | 6 | ||||
-rw-r--r-- | src/gtest/rpc_test.cpp | 34 | ||||
-rw-r--r-- | src/rpc-server/commands/trex_rpc_cmd_general.cpp (renamed from src/rpc-server/src/commands/trex_rpc_cmd_general.cpp) | 10 | ||||
-rw-r--r-- | src/rpc-server/commands/trex_rpc_cmd_stream.cpp | 155 | ||||
-rw-r--r-- | src/rpc-server/commands/trex_rpc_cmd_test.cpp (renamed from src/rpc-server/src/commands/trex_rpc_cmd_test.cpp) | 61 | ||||
-rw-r--r-- | src/rpc-server/commands/trex_rpc_cmds.h | 62 | ||||
-rw-r--r-- | src/rpc-server/include/trex_rpc_cmd_api.h | 90 | ||||
-rw-r--r-- | src/rpc-server/src/commands/trex_rpc_cmds.h | 89 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_cmd.cpp | 152 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_cmd_api.h | 142 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_cmds_table.cpp (renamed from src/rpc-server/src/trex_rpc_cmds_table.cpp) | 3 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_cmds_table.h (renamed from src/rpc-server/include/trex_rpc_cmds_table.h) | 0 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_exception_api.h (renamed from src/rpc-server/include/trex_rpc_exception_api.h) | 3 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_jsonrpc_v2_parser.cpp (renamed from src/rpc-server/src/trex_rpc_jsonrpc_v2_parser.cpp) | 10 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_jsonrpc_v2_parser.h (renamed from src/rpc-server/include/trex_rpc_jsonrpc_v2_parser.h) | 0 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_req_resp_server.cpp (renamed from src/rpc-server/src/trex_rpc_req_resp_server.cpp) | 0 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_req_resp_server.h (renamed from src/rpc-server/include/trex_rpc_req_resp_server.h) | 0 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_server.cpp (renamed from src/rpc-server/src/trex_rpc_server.cpp) | 0 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_server_api.h (renamed from src/rpc-server/include/trex_rpc_server_api.h) | 0 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_server_mock.cpp (renamed from src/rpc-server/src/trex_rpc_server_mock.cpp) | 2 |
21 files changed, 580 insertions, 251 deletions
diff --git a/linux/ws_main.py b/linux/ws_main.py index 505853b6..e2364be4 100755 --- a/linux/ws_main.py +++ b/linux/ws_main.py @@ -139,23 +139,25 @@ net_src = SrcGroup(dir='src/common/Network/Packet', 'VLANHeader.cpp']); # RPC code -rpc_server_src = SrcGroup(dir='src/rpc-server/src', +rpc_server_src = SrcGroup(dir='src/rpc-server/', src_list=[ 'trex_rpc_server.cpp', 'trex_rpc_req_resp_server.cpp', 'trex_rpc_jsonrpc_v2_parser.cpp', 'trex_rpc_cmds_table.cpp', + 'trex_rpc_cmd.cpp', 'commands/trex_rpc_cmd_test.cpp', 'commands/trex_rpc_cmd_general.cpp', + 'commands/trex_rpc_cmd_stream.cpp', ]) # RPC mock server (test) -rpc_server_mock_src = SrcGroup(dir='src/rpc-server/src', +rpc_server_mock_src = SrcGroup(dir='src/rpc-server/', src_list=[ 'trex_rpc_server_mock.cpp', - '../../gtest/rpc_test.cpp', + '../gtest/rpc_test.cpp', ]) # JSON package @@ -222,7 +224,7 @@ cxxflags_base =['-DWIN_UCODE_SIM', includes_path =''' ../src/pal/linux/ ../src/ - ../src/rpc-server/include + ../src/rpc-server/ ../external_libs/json/ ../external_libs/zmq/include/ ../external_libs/yaml-cpp/include/ @@ -351,7 +353,7 @@ build_types = [ build_option(name = "bp-sim", src = bp, debug_mode= RELEASE_,platform = PLATFORM_32, is_pie = False), build_option(name = "bp-sim", src = bp, debug_mode= RELEASE_,platform = PLATFORM_64, is_pie = False), - # build_option(name = "mock-rpc-server", use = ['zmq'], src = rpc_server_mock, debug_mode= DEBUG_,platform = PLATFORM_64, is_pie = False), + build_option(name = "mock-rpc-server", use = ['zmq'], src = rpc_server_mock, debug_mode= DEBUG_,platform = PLATFORM_64, is_pie = False), ] diff --git a/linux_dpdk/ws_main.py b/linux_dpdk/ws_main.py index 92364d75..24ffe18e 100755 --- a/linux_dpdk/ws_main.py +++ b/linux_dpdk/ws_main.py @@ -137,15 +137,17 @@ json_src = SrcGroup(dir='external_libs/json', ]) # RPC code -rpc_server_src = SrcGroup(dir='src/rpc-server/src', +rpc_server_src = SrcGroup(dir='src/rpc-server/', src_list=[ 'trex_rpc_server.cpp', 'trex_rpc_req_resp_server.cpp', 'trex_rpc_jsonrpc_v2_parser.cpp', 'trex_rpc_cmds_table.cpp', + 'trex_rpc_cmd.cpp', 'commands/trex_rpc_cmd_test.cpp', 'commands/trex_rpc_cmd_general.cpp', + 'commands/trex_rpc_cmd_stream.cpp', ]) # JSON package @@ -399,7 +401,7 @@ common_flags_old = common_flags + [ includes_path =''' ../src/pal/linux_dpdk/ ../src/ - ../src/rpc-server/include + ../src/rpc-server/ ../external_libs/yaml-cpp/include/ ../external_libs/zmq/include/ diff --git a/src/gtest/rpc_test.cpp b/src/gtest/rpc_test.cpp index 068457f3..a3df2a67 100644 --- a/src/gtest/rpc_test.cpp +++ b/src/gtest/rpc_test.cpp @@ -127,7 +127,7 @@ TEST_F(RpcTest, test_add_command) { string resp_str; /* simple add - missing paramters */ - req_str = "{\"jsonrpc\": \"2.0\", \"method\": \"rpc_test_add\", \"id\": 488}"; + req_str = "{\"jsonrpc\": \"2.0\", \"method\": \"test_add\", \"id\": 488}"; resp_str = send_msg(req_str); EXPECT_TRUE(reader.parse(resp_str, response, false)); @@ -136,7 +136,7 @@ TEST_F(RpcTest, test_add_command) { EXPECT_EQ(response["error"]["code"], -32602); /* simple add that works */ - req_str = "{\"jsonrpc\": \"2.0\", \"method\": \"rpc_test_add\", \"params\": {\"x\": 17, \"y\": -13} , \"id\": \"itay\"}"; + req_str = "{\"jsonrpc\": \"2.0\", \"method\": \"test_add\", \"params\": {\"x\": 17, \"y\": -13} , \"id\": \"itay\"}"; resp_str = send_msg(req_str); EXPECT_TRUE(reader.parse(resp_str, response, false)); @@ -145,7 +145,7 @@ TEST_F(RpcTest, test_add_command) { EXPECT_EQ(response["result"], 4); /* add with bad paratemers types */ - req_str = "{\"jsonrpc\": \"2.0\", \"method\": \"rpc_test_add\", \"params\": {\"x\": \"blah\", \"y\": -13} , \"id\": 17}"; + req_str = "{\"jsonrpc\": \"2.0\", \"method\": \"test_add\", \"params\": {\"x\": \"blah\", \"y\": -13} , \"id\": 17}"; resp_str = send_msg(req_str); EXPECT_TRUE(reader.parse(resp_str, response, false)); @@ -154,7 +154,7 @@ TEST_F(RpcTest, test_add_command) { EXPECT_EQ(response["error"]["code"], -32602); /* add with invalid count of parameters */ - req_str = "{\"jsonrpc\": \"2.0\", \"method\": \"rpc_test_add\", \"params\": {\"y\": -13} , \"id\": 17}"; + req_str = "{\"jsonrpc\": \"2.0\", \"method\": \"test_add\", \"params\": {\"y\": -13} , \"id\": 17}"; resp_str = send_msg(req_str); EXPECT_TRUE(reader.parse(resp_str, response, false)); @@ -164,7 +164,7 @@ TEST_F(RpcTest, test_add_command) { /* big numbers */ - req_str = "{\"jsonrpc\": \"2.0\", \"method\": \"rpc_test_add\", \"params\": {\"x\": 4827371, \"y\": -39181273} , \"id\": \"itay\"}"; + req_str = "{\"jsonrpc\": \"2.0\", \"method\": \"test_add\", \"params\": {\"x\": 4827371, \"y\": -39181273} , \"id\": \"itay\"}"; resp_str = send_msg(req_str); EXPECT_TRUE(reader.parse(resp_str, response, false)); @@ -183,12 +183,12 @@ TEST_F(RpcTest, batch_rpc_test) { string resp_str; req_str = "[ \ - {\"jsonrpc\": \"2.0\", \"method\": \"rpc_test_add\", \"params\": {\"x\": 22, \"y\": 17}, \"id\": \"1\"}, \ - {\"jsonrpc\": \"2.0\", \"method\": \"rpc_test_sub\", \"params\": {\"x\": 22, \"y\": 17}, \"id\": \"2\"}, \ - {\"jsonrpc\": \"2.0\", \"method\": \"rpc_test_add\", \"params\": {\"x\": 22, \"y\": \"itay\"}, \"id\": \"2\"}, \ + {\"jsonrpc\": \"2.0\", \"method\": \"test_add\", \"params\": {\"x\": 22, \"y\": 17}, \"id\": \"1\"}, \ + {\"jsonrpc\": \"2.0\", \"method\": \"test_sub\", \"params\": {\"x\": 22, \"y\": 17}, \"id\": \"2\"}, \ + {\"jsonrpc\": \"2.0\", \"method\": \"test_add\", \"params\": {\"x\": 22, \"y\": \"itay\"}, \"id\": \"2\"}, \ {\"foo\": \"boo\"}, \ {\"jsonrpc\": \"2.0\", \"method\": \"test_rpc_sheker\", \"params\": {\"name\": \"myself\"}, \"id\": 5}, \ - {\"jsonrpc\": \"2.0\", \"method\": \"rpc_test_add\", \"params\": {\"x\": 22, \"y\": 17} } \ + {\"jsonrpc\": \"2.0\", \"method\": \"test_add\", \"params\": {\"x\": 22, \"y\": 17} } \ ]"; resp_str = send_msg(req_str); @@ -224,3 +224,19 @@ TEST_F(RpcTest, batch_rpc_test) { return; } + +TEST_F(RpcTest, add_stream) { + Json::Value request; + Json::Value response; + Json::Reader reader; + + string req_str; + string resp_str; + + req_str = "{'stream':{'port_id':7,'stream_id':12,'enable':True,'start':True,'Is':10.0,'packet':[0,1,2,3,4]," + "'vm_data':[{'Name':'ip_cnt','Size':4,'big_edian':True,'type':'inc','core_mask':'split','init_val':'10.0.0.7','min':'10.0.0.1','max':'10.0.0.10',}]," + "'vm_program':[{'op_core':['read_to_reg_mem','write_reg_offet','write_rand_offset'],'read_name':'nameofopecodetoread','pkt_offset':20}]," + "'mode':{'type':'continues','pps':1000},'next_stream':17,'next_stream_loop':100,'rx_stats':{'enable':True,'rx_stream_id':71,'seq_enable':True,'latency':True}}}"; + + resp_str = send_msg(req_str); +} diff --git a/src/rpc-server/src/commands/trex_rpc_cmd_general.cpp b/src/rpc-server/commands/trex_rpc_cmd_general.cpp index 193ce8db..ac35babf 100644 --- a/src/rpc-server/src/commands/trex_rpc_cmd_general.cpp +++ b/src/rpc-server/commands/trex_rpc_cmd_general.cpp @@ -28,14 +28,11 @@ using namespace std; * get status * */ -TrexRpcCommand::rpc_cmd_rc_e +trex_rpc_cmd_rc_e TrexRpcCmdGetStatus::_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); - } + check_param_count(params, 0, result); Json::Value §ion = result["result"]; @@ -44,6 +41,7 @@ TrexRpcCmdGetStatus::_run(const Json::Value ¶ms, Json::Value &result) { section["general"]["build_time"] = get_build_time(); section["general"]["version_user"] = VERSION_USER; section["general"]["uptime"] = TrexRpcServer::get_server_uptime(); - return (RPC_CMD_OK); + + return (TREX_RPC_CMD_OK); } diff --git a/src/rpc-server/commands/trex_rpc_cmd_stream.cpp b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp new file mode 100644 index 00000000..58226a6b --- /dev/null +++ b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp @@ -0,0 +1,155 @@ +/* + Itay Marom + Cisco Systems, Inc. +*/ + +/* +Copyright (c) 2015-2015 Cisco Systems, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +#include "trex_rpc_cmds.h" +#include <../linux_dpdk/version.h> +#include <trex_rpc_server_api.h> + +using namespace std; + +/** + * Stateless stream mode + * abstract class + */ +class TrexStreamMode { +public: + enum mode_e { + CONTINUOUS, + SINGLE_BURST, + MULTI_BURST + }; + + virtual mode_e get_runtime_type() = 0; + virtual ~TrexStreamMode() {} +}; + +/** + * stream mode continuous + * + * @author imarom (30-Aug-15) + */ +class TrexStreamModeContinuous : public TrexStreamMode { +public: + mode_e get_runtime_type() { + return (CONTINUOUS); + } +private: + uint32_t pps; +}; + +/** + * single burst mode + * + */ +class TrexStreamModeSingleBurst : public TrexStreamMode { +public: + mode_e get_runtime_type() { + return (SINGLE_BURST); + } +private: + + uint32_t packets; + uint32_t pps; +}; + +class TrexStreamModeMultiBurst : public TrexStreamMode { +public: + + mode_e get_runtime_type() { + return (MULTI_BURST); + } + +private: + + uint32_t pps; + double ibg_usec; + uint32_t number_of_bursts; + uint32_t pkts_per_burst; +}; + + +/** + * Stateless Stream + * + */ +class TrexStatelessStream { + friend class TrexRpcCmdAddStream; + +public: + +private: + /* config */ + uint32_t stream_id; + uint8_t port_id; + double isg_usec; + uint32_t next_stream_id; + uint32_t loop_count; + + /* indicators */ + bool enable; + bool start; + + /* pkt */ + uint8_t *pkt; + uint16_t pkt_len; + + /* stream mode */ + TrexStreamMode *mode; + + /* VM */ + + /* RX check */ + struct { + bool enable; + bool seq_enable; + bool latency; + uint32_t stream_id; + + } rx_check; + +}; +/** + * add new stream + * + */ +trex_rpc_cmd_rc_e +TrexRpcCmdAddStream::_run(const Json::Value ¶ms, Json::Value &result) { + + TrexStatelessStream stream; + + check_param_count(params, 1, result); + check_field_type(params, "stream", FIELD_TYPE_OBJ, result); + + Json::Value §ion = result["stream"]; + + /* create a new steram and populate it */ + + check_field_type(section, "stream_id", FIELD_TYPE_INT, result); + stream.stream_id = section["stream_id"].asInt(); + + check_field_type(section, "port_id", FIELD_TYPE_INT, result); + stream.port_id = section["port_id"].asInt(); + + check_field_type(section, "Is", FIELD_TYPE_DOUBLE, result); + stream.isg_usec = section["Is"].asDouble(); + + return (TREX_RPC_CMD_OK); +} + diff --git a/src/rpc-server/src/commands/trex_rpc_cmd_test.cpp b/src/rpc-server/commands/trex_rpc_cmd_test.cpp index e2dc8959..e67de286 100644 --- a/src/rpc-server/src/commands/trex_rpc_cmd_test.cpp +++ b/src/rpc-server/commands/trex_rpc_cmd_test.cpp @@ -29,31 +29,18 @@ using namespace std; * add command * */ -TrexRpcCommand::rpc_cmd_rc_e +trex_rpc_cmd_rc_e TrexRpcCmdTestAdd::_run(const Json::Value ¶ms, Json::Value &result) { const Json::Value &x = params["x"]; const Json::Value &y = params["y"]; - - /* 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()) { - 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); - } + + check_param_count(params, 2, result); + check_field_type(params, "x", FIELD_TYPE_INT, result); + check_field_type(params, "y", FIELD_TYPE_INT, result); result["result"] = x.asInt() + y.asInt(); - return (RPC_CMD_OK); + return (TREX_RPC_CMD_OK); } /** @@ -61,56 +48,42 @@ TrexRpcCmdTestAdd::_run(const Json::Value ¶ms, Json::Value &result) { * * @author imarom (16-Aug-15) */ -TrexRpcCommand::rpc_cmd_rc_e +trex_rpc_cmd_rc_e TrexRpcCmdTestSub::_run(const Json::Value ¶ms, Json::Value &result) { const Json::Value &x = params["x"]; const Json::Value &y = params["y"]; - /* 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()) { - return (TrexRpcCommand::RPC_CMD_PARAM_PARSE_ERR); - } + check_param_count(params, 2, result); + check_field_type(params, "x", TrexRpcCommand::FIELD_TYPE_INT, result); + check_field_type(params, "y", TrexRpcCommand::FIELD_TYPE_INT, result); result["result"] = x.asInt() - y.asInt(); - return (RPC_CMD_OK); + return (TREX_RPC_CMD_OK); } /** * ping command */ -TrexRpcCommand::rpc_cmd_rc_e +trex_rpc_cmd_rc_e 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); - } + check_param_count(params, 0, result); result["result"] = "ACK"; - return (RPC_CMD_OK); + return (TREX_RPC_CMD_OK); } /** * query command */ -TrexRpcCommand::rpc_cmd_rc_e +trex_rpc_cmd_rc_e TrexRpcCmdGetReg::_run(const Json::Value ¶ms, Json::Value &result) { vector<string> cmds; /* validate count */ - if (params.size() != 0) { - generate_err_param_count(result, 0, params.size()); - return (TrexRpcCommand::RPC_CMD_PARAM_COUNT_ERR); - } - + check_param_count(params, 0, result); TrexRpcCommandsTable::get_instance().query(cmds); @@ -121,6 +94,6 @@ TrexRpcCmdGetReg::_run(const Json::Value ¶ms, Json::Value &result) { result["result"] = test; - return (RPC_CMD_OK); + return (TREX_RPC_CMD_OK); } diff --git a/src/rpc-server/commands/trex_rpc_cmds.h b/src/rpc-server/commands/trex_rpc_cmds.h new file mode 100644 index 00000000..5c425856 --- /dev/null +++ b/src/rpc-server/commands/trex_rpc_cmds.h @@ -0,0 +1,62 @@ +/* + Itay Marom + Cisco Systems, Inc. +*/ + +/* +Copyright (c) 2015-2015 Cisco Systems, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#ifndef __TREX_RPC_CMD_H__ +#define __TREX_RPC_CMD_H__ + +#include <trex_rpc_cmd_api.h> +#include <json/json.h> + +/* all the RPC commands decl. goes here */ + +/******************* test section ************/ + +/** + * syntactic sugar for creating a simple command + */ +#define TREX_RPC_CMD_DEFINE(class_name, cmd_name) \ + class class_name : public TrexRpcCommand { \ + public: \ + class_name () : TrexRpcCommand(cmd_name) {} \ + protected: \ + virtual trex_rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result); \ + } + + +/** + * test cmds + */ +TREX_RPC_CMD_DEFINE(TrexRpcCmdTestAdd, "test_add"); +TREX_RPC_CMD_DEFINE(TrexRpcCmdTestSub, "test_sub"); + +/** + * general cmds + */ +TREX_RPC_CMD_DEFINE(TrexRpcCmdPing, "ping"); +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetReg, "get_reg_cmds"); +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetStatus, "get_status"); + +/** + * stream cmds + */ +TREX_RPC_CMD_DEFINE(TrexRpcCmdAddStream, "add_stream"); + +#endif /* __TREX_RPC_CMD_H__ */ diff --git a/src/rpc-server/include/trex_rpc_cmd_api.h b/src/rpc-server/include/trex_rpc_cmd_api.h deleted file mode 100644 index c773b15f..00000000 --- a/src/rpc-server/include/trex_rpc_cmd_api.h +++ /dev/null @@ -1,90 +0,0 @@ -/* - Itay Marom - Cisco Systems, Inc. -*/ - -/* -Copyright (c) 2015-2015 Cisco Systems, Inc. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -#ifndef __TREX_RPC_CMD_API_H__ -#define __TREX_RPC_CMD_API_H__ - -#include <string> -#include <vector> -#include <json/json.h> - -/** - * interface for RPC command - * - * @author imarom (13-Aug-15) - */ -class TrexRpcCommand { -public: - - /** - * describe different types of rc for run() - */ - enum rpc_cmd_rc_e { - RPC_CMD_OK, - RPC_CMD_PARAM_COUNT_ERR = 1, - RPC_CMD_PARAM_PARSE_ERR, - RPC_CMD_INTERNAL_ERR - }; - - /** - * method name and params - */ - TrexRpcCommand(const std::string &method_name) : m_name(method_name) { - - } - - rpc_cmd_rc_e run(const Json::Value ¶ms, Json::Value &result) { - return _run(params, result); - } - - const std::string &get_name() { - return m_name; - } - - virtual ~TrexRpcCommand() {} - -protected: - - /** - * implemented by the dervied class - * - */ - 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; -}; - -#endif /* __TREX_RPC_CMD_API_H__ */ - diff --git a/src/rpc-server/src/commands/trex_rpc_cmds.h b/src/rpc-server/src/commands/trex_rpc_cmds.h deleted file mode 100644 index e37e1cda..00000000 --- a/src/rpc-server/src/commands/trex_rpc_cmds.h +++ /dev/null @@ -1,89 +0,0 @@ -/* - Itay Marom - Cisco Systems, Inc. -*/ - -/* -Copyright (c) 2015-2015 Cisco Systems, Inc. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -#ifndef __TREX_RPC_CMD_H__ -#define __TREX_RPC_CMD_H__ - -#include <trex_rpc_cmd_api.h> -#include <json/json.h> - -/* all the RPC commands decl. goes here */ - -/******************* test section ************/ - -/** - * add - * - */ -class TrexRpcCmdTestAdd : public TrexRpcCommand { -public: - TrexRpcCmdTestAdd() : TrexRpcCommand("test_add") {} -protected: - virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result); -}; - -/** - * sub - * - */ -class TrexRpcCmdTestSub : public TrexRpcCommand { -public: - TrexRpcCmdTestSub() : TrexRpcCommand("test_sub") {} ; -protected: - virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result); -}; - -/** - * ping - * - */ -class TrexRpcCmdPing : public TrexRpcCommand { -public: - TrexRpcCmdPing() : TrexRpcCommand("ping") {}; -protected: - virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result); -}; - -/** - * get all registered commands - * - */ -class TrexRpcCmdGetReg : public TrexRpcCommand { -public: - TrexRpcCmdGetReg() : TrexRpcCommand("get_reg_cmds") {}; -protected: - virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result); -}; - -/** - * get status - * - */ -class TrexRpcCmdGetStatus : public TrexRpcCommand { -public: - TrexRpcCmdGetStatus() : TrexRpcCommand("get_status") {}; -protected: - virtual rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result); -}; - - -/**************** test section end *************/ -#endif /* __TREX_RPC_CMD_H__ */ diff --git a/src/rpc-server/trex_rpc_cmd.cpp b/src/rpc-server/trex_rpc_cmd.cpp new file mode 100644 index 00000000..1ad94fb5 --- /dev/null +++ b/src/rpc-server/trex_rpc_cmd.cpp @@ -0,0 +1,152 @@ +/* + Itay Marom + Cisco Systems, Inc. +*/ + +/* +Copyright (c) 2015-2015 Cisco Systems, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +#include <trex_rpc_cmd_api.h> + +trex_rpc_cmd_rc_e +TrexRpcCommand::run(const Json::Value ¶ms, Json::Value &result) { + trex_rpc_cmd_rc_e rc; + + /* the internal run can throw a parser error / other error */ + try { + rc = _run(params, result); + } catch (TrexRpcCommandException &e) { + return e.get_rc(); + } + + return (rc); +} + +void +TrexRpcCommand::check_param_count(const Json::Value ¶ms, int expected, Json::Value &result) { + + if (params.size() != expected) { + std::stringstream ss; + ss << "method expects '" << expected << "' paramteres, '" << params.size() << "' provided"; + generate_err(result, ss.str()); + throw TrexRpcCommandException(TREX_RPC_CMD_PARAM_COUNT_ERR); + } +} + +const char * +TrexRpcCommand::type_to_str(field_type_e type) { + switch (type) { + case FIELD_TYPE_BOOL: + return "bool"; + case FIELD_TYPE_INT: + return "int"; + case FIELD_TYPE_DOUBLE: + return "double"; + case FIELD_TYPE_OBJ: + return "object"; + case FIELD_TYPE_STR: + return "string"; + case FILED_TYPE_ARRAY: + return "array"; + + default: + return "UNKNOWN"; + } +} + +const char * +TrexRpcCommand::json_type_to_name(const Json::Value &value) { + + switch(value.type()) { + case Json::nullValue: + return "null"; + case Json::uintValue: + return "uint"; + case Json::realValue: + return "real"; + case Json::stringValue: + return "string"; + case Json::booleanValue: + return "boolean"; + case Json::arrayValue: + return "array"; + case Json::objectValue: + return "object"; + + default: + return "UNKNOWN"; + } + +} +void +TrexRpcCommand::check_field_type(const Json::Value &parent, const std::string &name, field_type_e type, Json::Value &result) { + std::stringstream ss; + + /* check if field exists , does not add the field because its const */ + const Json::Value &field = parent[name]; + + /* first check if field exists */ + if (field == Json::Value::null) { + ss << "field '" << name << "' missing"; + generate_err(result, ss.str()); + throw (TrexRpcCommandException(TREX_RPC_CMD_PARAM_PARSE_ERR)); + } + + bool rc = true; + + switch (type) { + case FIELD_TYPE_BOOL: + if (!field.isBool()) { + rc = false; + } + break; + + case FIELD_TYPE_INT: + if (!field.isInt()) { + rc = false; + } + break; + + case FIELD_TYPE_DOUBLE: + if (!field.isDouble()) { + rc = false; + } + break; + + case FIELD_TYPE_OBJ: + if (!field.isObject()) { + rc = false; + } + break; + + case FIELD_TYPE_STR: + if (!field.isString()) { + rc = false; + } + break; + } + + if (!rc) { + ss << "error at offset: " << field.getOffsetStart() << " - '" << name << "' is '" << json_type_to_name(field) << "', expecting '" << type_to_str(type) << "'"; + generate_err(result, ss.str()); + throw (TrexRpcCommandException(TREX_RPC_CMD_PARAM_PARSE_ERR)); + } + +} + +void +TrexRpcCommand::generate_err(Json::Value &result, const std::string &msg) { + result["specific_err"] = msg; +} diff --git a/src/rpc-server/trex_rpc_cmd_api.h b/src/rpc-server/trex_rpc_cmd_api.h new file mode 100644 index 00000000..34e6ba0a --- /dev/null +++ b/src/rpc-server/trex_rpc_cmd_api.h @@ -0,0 +1,142 @@ +/* + Itay Marom + Cisco Systems, Inc. +*/ + +/* +Copyright (c) 2015-2015 Cisco Systems, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#ifndef __TREX_RPC_CMD_API_H__ +#define __TREX_RPC_CMD_API_H__ + +#include <string> +#include <vector> +#include <json/json.h> +#include <trex_rpc_exception_api.h> + +/** + * describe different types of rc for run() + */ +typedef enum trex_rpc_cmd_rc_ { + TREX_RPC_CMD_OK, + TREX_RPC_CMD_PARAM_COUNT_ERR = 1, + TREX_RPC_CMD_PARAM_PARSE_ERR, + TREX_RPC_CMD_INTERNAL_ERR +} trex_rpc_cmd_rc_e; + +/** + * simple exception for RPC command processing + * + * @author imarom (23-Aug-15) + */ +class TrexRpcCommandException : TrexRpcException { +public: + TrexRpcCommandException(trex_rpc_cmd_rc_e rc) : m_rc(rc) { + + } + + trex_rpc_cmd_rc_e get_rc() { + return m_rc; + + } + +protected: + trex_rpc_cmd_rc_e m_rc; +}; + +/** + * interface for RPC command + * + * @author imarom (13-Aug-15) + */ +class TrexRpcCommand { +public: + + /** + * method name and params + */ + TrexRpcCommand(const std::string &method_name) : m_name(method_name) { + + } + + /** + * entry point for executing RPC command + * + */ + trex_rpc_cmd_rc_e run(const Json::Value ¶ms, Json::Value &result); + + const std::string &get_name() { + return m_name; + } + + virtual ~TrexRpcCommand() {} + +protected: + + /** + * different types of fields + */ + enum field_type_e { + FIELD_TYPE_INT, + FIELD_TYPE_DOUBLE, + FIELD_TYPE_BOOL, + FIELD_TYPE_STR, + FIELD_TYPE_OBJ, + FILED_TYPE_ARRAY + }; + + /** + * implemented by the dervied class + * + */ + virtual trex_rpc_cmd_rc_e _run(const Json::Value ¶ms, Json::Value &result) = 0; + + /** + * check param count + */ + void check_param_count(const Json::Value ¶ms, int expected, Json::Value &result); + + /** + * check field type + * + */ + //void check_field_type(const Json::Value &field, field_type_e type, Json::Value &result); + void check_field_type(const Json::Value &parent, const std::string &name, field_type_e type, Json::Value &result); + + /** + * error generating functions + * + */ + void generate_err(Json::Value &result, const std::string &msg); + + /** + * translate enum to string + * + */ + const char * type_to_str(field_type_e type); + + /** + * translate JSON values to string + * + */ + const char * json_type_to_name(const Json::Value &value); + + /* RPC command name */ + std::string m_name; +}; + +#endif /* __TREX_RPC_CMD_API_H__ */ + diff --git a/src/rpc-server/src/trex_rpc_cmds_table.cpp b/src/rpc-server/trex_rpc_cmds_table.cpp index 04a56389..ac419bfd 100644 --- a/src/rpc-server/src/trex_rpc_cmds_table.cpp +++ b/src/rpc-server/trex_rpc_cmds_table.cpp @@ -33,6 +33,9 @@ TrexRpcCommandsTable::TrexRpcCommandsTable() { register_command(new TrexRpcCmdPing()); register_command(new TrexRpcCmdGetReg()); register_command(new TrexRpcCmdGetStatus()); + + /* stream commands */ + register_command(new TrexRpcCmdAddStream()); } TrexRpcCommandsTable::~TrexRpcCommandsTable() { diff --git a/src/rpc-server/include/trex_rpc_cmds_table.h b/src/rpc-server/trex_rpc_cmds_table.h index a41944f1..a41944f1 100644 --- a/src/rpc-server/include/trex_rpc_cmds_table.h +++ b/src/rpc-server/trex_rpc_cmds_table.h diff --git a/src/rpc-server/include/trex_rpc_exception_api.h b/src/rpc-server/trex_rpc_exception_api.h index 8783c219..e349b980 100644 --- a/src/rpc-server/include/trex_rpc_exception_api.h +++ b/src/rpc-server/trex_rpc_exception_api.h @@ -32,6 +32,9 @@ limitations under the License. class TrexRpcException : public std::runtime_error { public: + TrexRpcException() : std::runtime_error("") { + + } TrexRpcException(const std::string &what) : std::runtime_error(what) { } }; diff --git a/src/rpc-server/src/trex_rpc_jsonrpc_v2_parser.cpp b/src/rpc-server/trex_rpc_jsonrpc_v2_parser.cpp index be1eb2f8..bf6056d5 100644 --- a/src/rpc-server/src/trex_rpc_jsonrpc_v2_parser.cpp +++ b/src/rpc-server/trex_rpc_jsonrpc_v2_parser.cpp @@ -71,21 +71,21 @@ public: virtual void _execute(Json::Value &response) { Json::Value result; - TrexRpcCommand::rpc_cmd_rc_e rc = m_cmd->run(m_params, result); + trex_rpc_cmd_rc_e rc = m_cmd->run(m_params, result); switch (rc) { - case TrexRpcCommand::RPC_CMD_OK: + case TREX_RPC_CMD_OK: response["result"] = result["result"]; break; - case TrexRpcCommand::RPC_CMD_PARAM_COUNT_ERR: - case TrexRpcCommand::RPC_CMD_PARAM_PARSE_ERR: + case TREX_RPC_CMD_PARAM_COUNT_ERR: + case TREX_RPC_CMD_PARAM_PARSE_ERR: 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: + case TREX_RPC_CMD_INTERNAL_ERR: response["error"]["code"] = JSONRPC_V2_ERR_INTERNAL_ERROR; response["error"]["message"] = "Internal Server Error"; response["error"]["specific_err"] = result["specific_err"]; diff --git a/src/rpc-server/include/trex_rpc_jsonrpc_v2_parser.h b/src/rpc-server/trex_rpc_jsonrpc_v2_parser.h index 3367ad6a..3367ad6a 100644 --- a/src/rpc-server/include/trex_rpc_jsonrpc_v2_parser.h +++ b/src/rpc-server/trex_rpc_jsonrpc_v2_parser.h diff --git a/src/rpc-server/src/trex_rpc_req_resp_server.cpp b/src/rpc-server/trex_rpc_req_resp_server.cpp index 7484758d..7484758d 100644 --- a/src/rpc-server/src/trex_rpc_req_resp_server.cpp +++ b/src/rpc-server/trex_rpc_req_resp_server.cpp diff --git a/src/rpc-server/include/trex_rpc_req_resp_server.h b/src/rpc-server/trex_rpc_req_resp_server.h index f12d0540..f12d0540 100644 --- a/src/rpc-server/include/trex_rpc_req_resp_server.h +++ b/src/rpc-server/trex_rpc_req_resp_server.h diff --git a/src/rpc-server/src/trex_rpc_server.cpp b/src/rpc-server/trex_rpc_server.cpp index 366bfc5b..366bfc5b 100644 --- a/src/rpc-server/src/trex_rpc_server.cpp +++ b/src/rpc-server/trex_rpc_server.cpp diff --git a/src/rpc-server/include/trex_rpc_server_api.h b/src/rpc-server/trex_rpc_server_api.h index 6bb81c73..6bb81c73 100644 --- a/src/rpc-server/include/trex_rpc_server_api.h +++ b/src/rpc-server/trex_rpc_server_api.h diff --git a/src/rpc-server/src/trex_rpc_server_mock.cpp b/src/rpc-server/trex_rpc_server_mock.cpp index fd4f051c..98d1f35d 100644 --- a/src/rpc-server/src/trex_rpc_server_mock.cpp +++ b/src/rpc-server/trex_rpc_server_mock.cpp @@ -44,7 +44,7 @@ int main(int argc, char *argv[]) { // gtest ? if (argc > 1) { - if ( (string(argv[1]) != "--ut") || (argc != 2) ) { + if (string(argv[1]) != "--ut") { cout << "\n[Usage] " << argv[0] << ": " << " [--ut]\n\n"; exit(-1); } |