From: Francis Dupont Date: Sun, 7 Oct 2018 21:09:23 +0000 (+0200) Subject: [153-netconf-control-socket] Extended command X-Git-Tag: 171-keactrl-tests-not-posix_base~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5191f3d1ea17ddf57cfaa70bb9efbc2f87c01228;p=thirdparty%2Fkea.git [153-netconf-control-socket] Extended command --- diff --git a/src/lib/cc/command_interpreter.cc b/src/lib/cc/command_interpreter.cc index bbdc3570fc..2d4d89b1e7 100644 --- a/src/lib/cc/command_interpreter.cc +++ b/src/lib/cc/command_interpreter.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2009-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2009-2018 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -26,6 +26,7 @@ const char *CONTROL_COMMAND = "command"; const char *CONTROL_RESULT = "result"; const char *CONTROL_TEXT = "text"; const char *CONTROL_ARGUMENTS = "arguments"; + const char *CONTROL_SERVICE = "service"; // Full version, with status, text and arguments ConstElementPtr @@ -134,17 +135,34 @@ answerToText(const ConstElementPtr& msg) { ConstElementPtr createCommand(const std::string& command) { - return (createCommand(command, ElementPtr())); + return (createCommand(command, ElementPtr(), "")); } ConstElementPtr createCommand(const std::string& command, ConstElementPtr arg) { + return (createCommand(command, arg, "")); +} + +ConstElementPtr +createCommand(const std::string& command, const std::string& service) { + return (createCommand(command, ElementPtr(), service)); +} + +ConstElementPtr +createCommand(const std::string& command, + ConstElementPtr arg, + const std::string& service) { ElementPtr query = Element::createMap(); ElementPtr cmd = Element::create(command); query->set(CONTROL_COMMAND, cmd); if (arg) { query->set(CONTROL_ARGUMENTS, arg); } + if (!service.empty()) { + ElementPtr services = Element::createList(); + services->add(Element::create(service)); + query->set(CONTROL_SERVICE, services); + } return (query); } diff --git a/src/lib/cc/command_interpreter.h b/src/lib/cc/command_interpreter.h index eeaa500517..1c29bdb384 100644 --- a/src/lib/cc/command_interpreter.h +++ b/src/lib/cc/command_interpreter.h @@ -1,4 +1,4 @@ -// Copyright (C) 2009-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2009-2018 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -121,6 +121,29 @@ isc::data::ConstElementPtr createCommand(const std::string& command); isc::data::ConstElementPtr createCommand(const std::string& command, isc::data::ConstElementPtr arg); +/// @brief Creates a standard config/command command message with no +/// argument and with the given service (of the form +/// { "command": "my_command", "service": [ service ] }) +/// +/// @param command The command string +/// @param service The target service. May be empty. +/// @return The created message + isc::data::ConstElementPtr createCommand(const std::string& command, + const std::string& service); + +/// @brief Creates a standard config/command command message with the +/// given argument and given service (of the form +/// { "command": "my_command", "arguments": arg, "service": [ service ] } +/// +/// @param command The command string +/// @param arg The optional argument for the command. This can be of +/// any Element type. May be NULL. +/// @param service The target service. May be empty. +/// @return The created message +isc::data::ConstElementPtr createCommand(const std::string& command, + isc::data::ConstElementPtr arg, + const std::string& service); + /// @brief Parses the given command into a string containing the actual /// command and an ElementPtr containing the optional argument. /// diff --git a/src/lib/cc/tests/command_interpreter_unittests.cc b/src/lib/cc/tests/command_interpreter_unittests.cc index df381d0f46..18e1f6ab2f 100644 --- a/src/lib/cc/tests/command_interpreter_unittests.cc +++ b/src/lib/cc/tests/command_interpreter_unittests.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2009-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2009-2018 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -105,6 +105,7 @@ TEST(CommandInterpreterTest, answerToText) { TEST(CommandInterpreterTest, createCommand) { ConstElementPtr command; ConstElementPtr arg; + string service; command = createCommand("my_command"); ASSERT_EQ("{ \"command\": \"my_command\" }", command->str()); @@ -123,6 +124,31 @@ TEST(CommandInterpreterTest, createCommand) { command = createCommand("foo", arg); ASSERT_EQ("{ \"arguments\": { \"a\": \"map\" }, \"command\": \"foo\" }", command->str()); + + command = createCommand("my_command", "my_service"); + ASSERT_EQ("{ \"command\": \"my_command\", " + "\"service\": [ \"my_service\" ] }", + command->str()); + + arg = el("1"); + command = createCommand("my_command", arg, "my_service"); + ASSERT_EQ("{ \"arguments\": 1, \"command\": \"my_command\", " + "\"service\": [ \"my_service\" ] }", + command->str()); + + arg = el("[ \"a\", \"b\" ]"); + command = createCommand("my_cmd", arg, "my_server"); + ASSERT_EQ("{ \"arguments\": [ \"a\", \"b\" ], " + "\"command\": \"my_cmd\", " + "\"service\": [ \"my_server\" ] }", + command->str()); + + arg = el("{ \"a\": \"map\" }"); + command = createCommand("foo", arg, "bar"); + ASSERT_EQ("{ \"arguments\": { \"a\": \"map\" }, " + "\"command\": \"foo\", " + "\"service\": [ \"bar\" ] }", + command->str()); } // This test checks whether parseCommand function is able to parse various valid