diff options
-rw-r--r-- | src/bin/dhcp4/ctrl_dhcp4_srv.cc | 33 | ||||
-rw-r--r-- | src/bin/dhcp6/ctrl_dhcp6_srv.cc | 33 | ||||
-rw-r--r-- | src/hooks/dhcp/high_availability/command_creator.cc | 14 | ||||
-rw-r--r-- | src/hooks/dhcp/high_availability/command_creator.h | 6 | ||||
-rw-r--r-- | src/hooks/dhcp/high_availability/ha_impl.cc | 4 | ||||
-rw-r--r-- | src/hooks/dhcp/high_availability/ha_service.cc | 6 | ||||
-rw-r--r-- | src/hooks/dhcp/high_availability/tests/command_creator_unittest.cc | 56 |
7 files changed, 91 insertions, 61 deletions
diff --git a/src/bin/dhcp4/ctrl_dhcp4_srv.cc b/src/bin/dhcp4/ctrl_dhcp4_srv.cc index af15fbbe23..8bea0fce97 100644 --- a/src/bin/dhcp4/ctrl_dhcp4_srv.cc +++ b/src/bin/dhcp4/ctrl_dhcp4_srv.cc @@ -490,11 +490,12 @@ ControlledDhcpv4Srv::commandDhcpDisableHandler(const std::string&, ConstElementPtr args) { std::ostringstream message; int64_t max_period = 0; - int64_t handle_id = 0; + std::string handle_id; NetworkState::ControllerType type = NetworkState::COMMAND; - // Parse arguments to see if the 'max-period' parameter has been specified. + // Parse arguments to see if the 'max-period' of 'handle-id' parameters have + // been specified. if (args) { // Arguments must be a map. if (args->getType() != Element::map) { @@ -524,16 +525,12 @@ ControlledDhcpv4Srv::commandDhcpDisableHandler(const std::string&, ConstElementPtr handle_id_element = args->get("handle-id"); // handle-id is optional. if (handle_id_element) { - // It must be an integer, if specified. - if (handle_id_element->getType() != Element::integer) { - message << "'handle-id' argument must be a number"; + // It must be a string, if specified. + if (handle_id_element->getType() != Element::string) { + message << "'handle-id' argument must be a string"; } else { - // It must be positive integer. - handle_id = handle_id_element->intValue(); - if (handle_id <= 0) { - message << "'handle-id' must be positive integer"; - } + handle_id = handle_id_element->stringValue(); type = NetworkState::HA; } } @@ -560,11 +557,11 @@ ConstElementPtr ControlledDhcpv4Srv::commandDhcpEnableHandler(const std::string&, ConstElementPtr args) { std::ostringstream message; - int64_t handle_id = 0; + std::string handle_id; NetworkState::ControllerType type = NetworkState::COMMAND; - // Parse arguments to see if the 'max-period' parameter has been specified. + // Parse arguments to see if the 'handle-id' parameter has been specified. if (args) { // Arguments must be a map. if (args->getType() != Element::map) { @@ -574,16 +571,12 @@ ControlledDhcpv4Srv::commandDhcpEnableHandler(const std::string&, ConstElementPtr handle_id_element = args->get("handle-id"); // handle-id is optional. if (handle_id_element) { - // It must be an integer, if specified. - if (handle_id_element->getType() != Element::integer) { - message << "'handle-id' argument must be a number"; + // It must be a string, if specified. + if (handle_id_element->getType() != Element::string) { + message << "'handle-id' argument must be a string"; } else { - // It must be positive integer. - handle_id = handle_id_element->intValue(); - if (handle_id <= 0) { - message << "'handle-id' must be positive integer"; - } + handle_id = handle_id_element->stringValue(); type = NetworkState::HA; } } diff --git a/src/bin/dhcp6/ctrl_dhcp6_srv.cc b/src/bin/dhcp6/ctrl_dhcp6_srv.cc index e771b8549f..f497ccbe14 100644 --- a/src/bin/dhcp6/ctrl_dhcp6_srv.cc +++ b/src/bin/dhcp6/ctrl_dhcp6_srv.cc @@ -493,11 +493,12 @@ ControlledDhcpv6Srv::commandDhcpDisableHandler(const std::string&, ConstElementPtr args) { std::ostringstream message; int64_t max_period = 0; - int64_t handle_id = 0; + std::string handle_id; NetworkState::ControllerType type = NetworkState::COMMAND; - // Parse arguments to see if the 'max-period' parameter has been specified. + // Parse arguments to see if the 'max-period' of 'handle-id' parameters have + // been specified. if (args) { // Arguments must be a map. if (args->getType() != Element::map) { @@ -527,16 +528,12 @@ ControlledDhcpv6Srv::commandDhcpDisableHandler(const std::string&, ConstElementPtr handle_id_element = args->get("handle-id"); // handle-id is optional. if (handle_id_element) { - // It must be an integer, if specified. - if (handle_id_element->getType() != Element::integer) { - message << "'handle-id' argument must be a number"; + // It must be a string, if specified. + if (handle_id_element->getType() != Element::string) { + message << "'handle-id' argument must be a string"; } else { - // It must be positive integer. - handle_id = handle_id_element->intValue(); - if (handle_id <= 0) { - message << "'handle-id' must be positive integer"; - } + handle_id = handle_id_element->stringValue(); type = NetworkState::HA; } } @@ -563,11 +560,11 @@ ConstElementPtr ControlledDhcpv6Srv::commandDhcpEnableHandler(const std::string&, ConstElementPtr args) { std::ostringstream message; - int64_t handle_id = 0; + std::string handle_id; NetworkState::ControllerType type = NetworkState::COMMAND; - // Parse arguments to see if the 'max-period' parameter has been specified. + // Parse arguments to see if the 'handle-id' parameter has been specified. if (args) { // Arguments must be a map. if (args->getType() != Element::map) { @@ -577,16 +574,12 @@ ControlledDhcpv6Srv::commandDhcpEnableHandler(const std::string&, ConstElementPtr handle_id_element = args->get("handle-id"); // handle-id is optional. if (handle_id_element) { - // It must be an integer, if specified. - if (handle_id_element->getType() != Element::integer) { - message << "'handle-id' argument must be a number"; + // It must be a string, if specified. + if (handle_id_element->getType() != Element::string) { + message << "'handle-id' argument must be a string"; } else { - // It must be positive integer. - handle_id = handle_id_element->intValue(); - if (handle_id <= 0) { - message << "'handle-id' must be positive integer"; - } + handle_id = handle_id_element->stringValue(); type = NetworkState::HA; } } diff --git a/src/hooks/dhcp/high_availability/command_creator.cc b/src/hooks/dhcp/high_availability/command_creator.cc index 2fa8a1ff25..2a3dc1158b 100644 --- a/src/hooks/dhcp/high_availability/command_creator.cc +++ b/src/hooks/dhcp/high_availability/command_creator.cc @@ -18,12 +18,14 @@ namespace isc { namespace ha { ConstElementPtr -CommandCreator::createDHCPDisable(const unsigned int max_period, +CommandCreator::createDHCPDisable(const std::string& service_name, + const unsigned int max_period, const HAServerType& server_type) { ElementPtr args; + args = Element::createMap(); + args->set("handle-id", Element::create(service_name)); // max-period is optional. A value of 0 means that it is not specified. if (max_period > 0) { - args = Element::createMap(); args->set("max-period", Element::create(static_cast<long int>(max_period))); } ConstElementPtr command = config::createCommand("dhcp-disable", args); @@ -32,8 +34,12 @@ CommandCreator::createDHCPDisable(const unsigned int max_period, } ConstElementPtr -CommandCreator::createDHCPEnable(const HAServerType& server_type) { - ConstElementPtr command = config::createCommand("dhcp-enable"); +CommandCreator::createDHCPEnable(const std::string& service_name, + const HAServerType& server_type) { + ElementPtr args; + args = Element::createMap(); + args->set("handle-id", Element::create(service_name)); + ConstElementPtr command = config::createCommand("dhcp-enable", args); insertService(command, server_type); return (command); } diff --git a/src/hooks/dhcp/high_availability/command_creator.h b/src/hooks/dhcp/high_availability/command_creator.h index e9ce338d4a..4d89e81eef 100644 --- a/src/hooks/dhcp/high_availability/command_creator.h +++ b/src/hooks/dhcp/high_availability/command_creator.h @@ -25,7 +25,8 @@ public: /// /// @return Pointer to the JSON representation of the command. static data::ConstElementPtr - createDHCPDisable(const unsigned int max_period, + createDHCPDisable(const std::string& service_name, + const unsigned int max_period, const HAServerType& server_type); /// @brief Creates dhcp-enable command for DHCP server. @@ -33,7 +34,8 @@ public: /// @param server_type type of the DHCP server, i.e. v4 or v6. /// @return Pointer to the JSON representation of the command. static data::ConstElementPtr - createDHCPEnable(const HAServerType& server_type); + createDHCPEnable(const std::string& service_name, + const HAServerType& server_type); /// @brief Creates ha-reset command. /// diff --git a/src/hooks/dhcp/high_availability/ha_impl.cc b/src/hooks/dhcp/high_availability/ha_impl.cc index 84d92eb91e..02e2f140d3 100644 --- a/src/hooks/dhcp/high_availability/ha_impl.cc +++ b/src/hooks/dhcp/high_availability/ha_impl.cc @@ -264,9 +264,7 @@ void HAImpl::commandProcessed(hooks::CalloutHandle& callout_handle) { std::string command_name; callout_handle.getArgument("name", command_name); - if (command_name == "dhcp-enable") { - service_->adjustNetworkState(); - } else if (command_name == "status-get") { + if (command_name == "status-get") { // Get the response. ConstElementPtr response; callout_handle.getArgument("response", response); diff --git a/src/hooks/dhcp/high_availability/ha_service.cc b/src/hooks/dhcp/high_availability/ha_service.cc index 8f62d659e7..ab0b602f3f 100644 --- a/src/hooks/dhcp/high_availability/ha_service.cc +++ b/src/hooks/dhcp/high_availability/ha_service.cc @@ -1683,7 +1683,8 @@ HAService::asyncDisableDHCPService(HttpClient& http_client, HostHttpHeader(remote_config->getUrl().getHostname())); remote_config->addBasicAuthHttpHeader(request); - request->setBodyAsJson(CommandCreator::createDHCPDisable(max_period, + request->setBodyAsJson(CommandCreator::createDHCPDisable(server_name, + max_period, server_type_)); request->finalize(); @@ -1757,7 +1758,8 @@ HAService::asyncEnableDHCPService(HttpClient& http_client, (HttpRequest::Method::HTTP_POST, "/", HttpVersion::HTTP_11(), HostHttpHeader(remote_config->getUrl().getHostname())); remote_config->addBasicAuthHttpHeader(request); - request->setBodyAsJson(CommandCreator::createDHCPEnable(server_type_)); + request->setBodyAsJson(CommandCreator::createDHCPEnable(server_name, + server_type_)); request->finalize(); // Response object should also be created because the HTTP client needs diff --git a/src/hooks/dhcp/high_availability/tests/command_creator_unittest.cc b/src/hooks/dhcp/high_availability/tests/command_creator_unittest.cc index 406c4290eb..ad6cfb523d 100644 --- a/src/hooks/dhcp/high_availability/tests/command_creator_unittest.cc +++ b/src/hooks/dhcp/high_availability/tests/command_creator_unittest.cc @@ -150,24 +150,42 @@ testCommandBasics(const ConstElementPtr& command, // This test verifies that the dhcp-disable command is correct. TEST(CommandCreatorTest, createDHCPDisable4) { // Create command with max-period value set to 20. - ConstElementPtr command = CommandCreator::createDHCPDisable(20, HAServerType::DHCPv4); + ConstElementPtr command = CommandCreator::createDHCPDisable("this-server", + 20, HAServerType::DHCPv4); ConstElementPtr arguments; ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-disable", "dhcp4", arguments)); + ASSERT_EQ(2, arguments->size()); ConstElementPtr max_period = arguments->get("max-period"); ASSERT_TRUE(max_period); ASSERT_EQ(Element::integer, max_period->getType()); EXPECT_EQ(20, max_period->intValue()); + ConstElementPtr handle_id = arguments->get("handle-id"); + ASSERT_TRUE(handle_id); + ASSERT_EQ("this-server", handle_id->stringValue()); // Repeat the test but this time the max-period is not specified. - command = CommandCreator::createDHCPDisable(0, HAServerType::DHCPv4); - ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-disable", "dhcp4")); + command = CommandCreator::createDHCPDisable("this-server", 0, + HAServerType::DHCPv4); + ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-disable", "dhcp4", + arguments)); + ASSERT_EQ(1, arguments->size()); + handle_id = arguments->get("handle-id"); + ASSERT_TRUE(handle_id); + ASSERT_EQ("this-server", handle_id->stringValue()); } // This test verifies that the dhcp-enable command is correct. TEST(CommandCreatorTest, createDHCPEnable4) { - ConstElementPtr command = CommandCreator::createDHCPEnable(HAServerType::DHCPv4); - ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-enable", "dhcp4")); + ConstElementPtr arguments; + ConstElementPtr command = CommandCreator::createDHCPEnable("this-server", + HAServerType::DHCPv4); + ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-enable", "dhcp4", + arguments)); + ASSERT_EQ(1, arguments->size()); + ConstElementPtr handle_id = arguments->get("handle-id"); + ASSERT_TRUE(handle_id); + ASSERT_EQ("this-server", handle_id->stringValue()); } // This test verifies that the ha-reset command sent to DHCPv4 server is correct. @@ -266,25 +284,43 @@ TEST(CommandCreatorTest, createLease4GetPageZeroLimit) { // correct. TEST(CommandCreatorTest, createDHCPDisable6) { // Create command with max-period value set to 20. - ConstElementPtr command = CommandCreator::createDHCPDisable(20, HAServerType::DHCPv6); + ConstElementPtr command = CommandCreator::createDHCPDisable("this-server", + 20, HAServerType::DHCPv6); ConstElementPtr arguments; ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-disable", "dhcp6", arguments)); + ASSERT_EQ(2, arguments->size()); ConstElementPtr max_period = arguments->get("max-period"); ASSERT_TRUE(max_period); ASSERT_EQ(Element::integer, max_period->getType()); EXPECT_EQ(20, max_period->intValue()); + ConstElementPtr handle_id = arguments->get("handle-id"); + ASSERT_TRUE(handle_id); + ASSERT_EQ("this-server", handle_id->stringValue()); // Repeat the test but this time the max-period is not specified. - command = CommandCreator::createDHCPDisable(0, HAServerType::DHCPv6); - ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-disable", "dhcp6")); + command = CommandCreator::createDHCPDisable("this-server", 0, + HAServerType::DHCPv6); + ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-disable", "dhcp6", + arguments)); + ASSERT_EQ(1, arguments->size()); + handle_id = arguments->get("handle-id"); + ASSERT_TRUE(handle_id); + ASSERT_EQ("this-server", handle_id->stringValue()); } // This test verifies that the dhcp-enable command (DHCPv6 case) is // correct. TEST(CommandCreatorTest, createDHCPEnable6) { - ConstElementPtr command = CommandCreator::createDHCPEnable(HAServerType::DHCPv6); - ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-enable", "dhcp6")); + ConstElementPtr arguments; + ConstElementPtr command = CommandCreator::createDHCPEnable("this-server", + HAServerType::DHCPv6); + ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "dhcp-enable", "dhcp6", + arguments)); + ASSERT_EQ(1, arguments->size()); + ConstElementPtr handle_id = arguments->get("handle-id"); + ASSERT_TRUE(handle_id); + ASSERT_EQ("this-server", handle_id->stringValue()); } // This test verifies that the ha-reset command sent to DHCPv6 server is correct. |