From: Marcin Siodelski Date: Tue, 17 Oct 2023 18:25:37 +0000 (+0200) Subject: [#3106] Include origin in ha-sync-complete-notify X-Git-Tag: Kea-2.5.5~133 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ccd4873dcf6fb620babbefe1777f925490d5a638;p=thirdparty%2Fkea.git [#3106] Include origin in ha-sync-complete-notify --- diff --git a/src/hooks/dhcp/high_availability/command_creator.cc b/src/hooks/dhcp/high_availability/command_creator.cc index 25395e0f14..a43447f170 100644 --- a/src/hooks/dhcp/high_availability/command_creator.cc +++ b/src/hooks/dhcp/high_availability/command_creator.cc @@ -256,10 +256,12 @@ CommandCreator::createMaintenanceNotify(const bool cancel, const HAServerType& s } ConstElementPtr -CommandCreator::createSyncCompleteNotify(const std::string& server_name, +CommandCreator::createSyncCompleteNotify(const unsigned int origin, + const std::string& server_name, const HAServerType& server_type) { auto args = Element::createMap(); args->set("server-name", Element::create(server_name)); + args->set("origin", Element::create(origin)); auto command = config::createCommand("ha-sync-complete-notify", 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 04c81acd0b..dc65cffcc9 100644 --- a/src/hooks/dhcp/high_availability/command_creator.h +++ b/src/hooks/dhcp/high_availability/command_creator.h @@ -180,12 +180,15 @@ public: /// @brief Creates ha-sync-complete-notify command. /// + /// @param origin a numeric value of the origin created from the + /// @c HAService identifier to enable the DHCP service. /// @param server_name name of the server sending the command allowing /// for associating the command with the relationship. /// @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 - createSyncCompleteNotify(const std::string& server_name, + createSyncCompleteNotify(const unsigned int origin, + const std::string& server_name, const HAServerType& server_type); /// @brief List of commands used by the High Availability in v4. diff --git a/src/hooks/dhcp/high_availability/ha_impl.cc b/src/hooks/dhcp/high_availability/ha_impl.cc index af0fdb1f2d..042ff901fe 100644 --- a/src/hooks/dhcp/high_availability/ha_impl.cc +++ b/src/hooks/dhcp/high_availability/ha_impl.cc @@ -603,7 +603,18 @@ HAImpl::syncCompleteNotifyHandler(hooks::CalloutHandle& callout_handle) { static_cast(parseCommand(args, command)); HAServicePtr service; + auto origin_value = NetworkState::HA_REMOTE_COMMAND+1; try { + if (args) { + auto origin = args->get("origin"); + if (origin) { + if (origin->getType() != Element::integer) { + isc_throw(BadValue, "'origin' must be an integer in the 'ha-sync-complete-notify' command"); + } + origin_value = origin->intValue(); + } + } + service = getHAServiceByServerName("ha-sync-complete-notify", args); } catch (const std::exception& ex) { @@ -614,7 +625,7 @@ HAImpl::syncCompleteNotifyHandler(hooks::CalloutHandle& callout_handle) { return; } - ConstElementPtr response = services_->get()->processSyncCompleteNotify(); + ConstElementPtr response = service->processSyncCompleteNotify(origin_value); callout_handle.setArgument("response", response); } diff --git a/src/hooks/dhcp/high_availability/ha_service.cc b/src/hooks/dhcp/high_availability/ha_service.cc index edd2c70408..837aa3f095 100644 --- a/src/hooks/dhcp/high_availability/ha_service.cc +++ b/src/hooks/dhcp/high_availability/ha_service.cc @@ -2916,7 +2916,8 @@ HAService::asyncSyncCompleteNotify(HttpClient& http_client, HostHttpHeader(remote_config->getUrl().getStrippedHostname())); remote_config->addBasicAuthHttpHeader(request); - request->setBodyAsJson(CommandCreator::createSyncCompleteNotify(config_->getThisServerConfig()->getName(), + request->setBodyAsJson(CommandCreator::createSyncCompleteNotify(NetworkState::HA_REMOTE_COMMAND+id_, + config_->getThisServerConfig()->getName(), server_type_)); request->finalize(); @@ -2989,11 +2990,11 @@ HAService::asyncSyncCompleteNotify(HttpClient& http_client, } ConstElementPtr -HAService::processSyncCompleteNotify() { +HAService::processSyncCompleteNotify(const unsigned int origin) { if (getCurrState() == HA_PARTNER_DOWN_ST) { sync_complete_notified_ = true; } else { - localEnableDHCPService(); + network_state_->enableService(origin); } return (createAnswer(CONTROL_RESULT_SUCCESS, "Server successfully notified about the synchronization completion.")); diff --git a/src/hooks/dhcp/high_availability/ha_service.h b/src/hooks/dhcp/high_availability/ha_service.h index fa6c5d49eb..93b2a6caea 100644 --- a/src/hooks/dhcp/high_availability/ha_service.h +++ b/src/hooks/dhcp/high_availability/ha_service.h @@ -1062,8 +1062,10 @@ public: /// In this state, the server will first have to check connectivity with /// the partner and transition to a state in which it will send lease updates. /// + /// @param origin a numeric value of the origin created from the + /// @c HAService identifier to enable the DHCP service. /// @return Pointer to the response to the ha-sync-complete-notify command. - data::ConstElementPtr processSyncCompleteNotify(); + data::ConstElementPtr processSyncCompleteNotify(const unsigned int origin); /// @brief Start the client and(or) listener instances. /// 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 3deeb7f66b..5ad5f8973c 100644 --- a/src/hooks/dhcp/high_availability/tests/command_creator_unittest.cc +++ b/src/hooks/dhcp/high_availability/tests/command_creator_unittest.cc @@ -535,25 +535,33 @@ TEST(CommandCreatorTest, createMaintenanceNotify6) { // This test verifies that the ha-sync-complete-notify command sent to a // DHCPv4 server is correct. TEST(CommandCreatorTest, createSyncCompleteNotify4) { - ConstElementPtr command = CommandCreator::createSyncCompleteNotify("server1", HAServerType::DHCPv4); + ConstElementPtr command = CommandCreator::createSyncCompleteNotify(1, "server1", HAServerType::DHCPv4); ConstElementPtr arguments; ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "ha-sync-complete-notify", "dhcp4", arguments)); auto server_name = arguments->get("server-name"); ASSERT_TRUE(server_name); ASSERT_EQ(Element::string, server_name->getType()); EXPECT_EQ("server1", server_name->stringValue()); + auto origin = arguments->get("origin"); + ASSERT_TRUE(origin); + EXPECT_EQ(Element::integer, origin->getType()); + EXPECT_EQ(1, origin->intValue()); } // This test verifies that the ha-sync-complete-notify command sent to a -// DHCPv4 server is correct. +// DHCPv6 server is correct. TEST(CommandCreatorTest, createSyncCompleteNotify6) { - ConstElementPtr command = CommandCreator::createSyncCompleteNotify("server1", HAServerType::DHCPv6); + ConstElementPtr command = CommandCreator::createSyncCompleteNotify(1, "server1", HAServerType::DHCPv6); ConstElementPtr arguments; ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "ha-sync-complete-notify", "dhcp6", arguments)); auto server_name = arguments->get("server-name"); ASSERT_TRUE(server_name); ASSERT_EQ(Element::string, server_name->getType()); EXPECT_EQ("server1", server_name->stringValue()); + auto origin = arguments->get("origin"); + ASSERT_TRUE(origin); + EXPECT_EQ(Element::integer, origin->getType()); + EXPECT_EQ(1, origin->intValue()); } } diff --git a/src/hooks/dhcp/high_availability/tests/ha_service_unittest.cc b/src/hooks/dhcp/high_availability/tests/ha_service_unittest.cc index 5280aa945a..efb958b00e 100644 --- a/src/hooks/dhcp/high_availability/tests/ha_service_unittest.cc +++ b/src/hooks/dhcp/high_availability/tests/ha_service_unittest.cc @@ -5027,11 +5027,11 @@ TEST_F(HAServiceTest, processSyncCompleteNotify) { // Simulate disabling the DHCP service for synchronization. if (service.network_state_->isServiceEnabled()) { - EXPECT_NO_THROW(service.network_state_->disableService(NetworkState::HA_LOCAL_COMMAND+1)); + EXPECT_NO_THROW(service.network_state_->disableService(NetworkState::HA_REMOTE_COMMAND+1)); } ConstElementPtr rsp; - EXPECT_NO_THROW(rsp = service.processSyncCompleteNotify()); + EXPECT_NO_THROW(rsp = service.processSyncCompleteNotify(NetworkState::HA_REMOTE_COMMAND+1)); ASSERT_TRUE(rsp); checkAnswer(rsp, CONTROL_RESULT_SUCCESS, @@ -5056,9 +5056,9 @@ TEST_F(HAServiceTest, processSyncCompleteNotify) { EXPECT_NO_THROW(service.transition(HA_LOAD_BALANCING_ST, HAService::NOP_EVT)); // Disable the service again. - EXPECT_NO_THROW(service.network_state_->disableService(NetworkState::HA_LOCAL_COMMAND+1)); + EXPECT_NO_THROW(service.network_state_->disableService(NetworkState::HA_REMOTE_COMMAND+1)); - EXPECT_NO_THROW(rsp = service.processSyncCompleteNotify()); + EXPECT_NO_THROW(rsp = service.processSyncCompleteNotify(NetworkState::HA_REMOTE_COMMAND+1)); ASSERT_TRUE(rsp); checkAnswer(rsp, CONTROL_RESULT_SUCCESS,