]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3106] Include origin in ha-sync-complete-notify
authorMarcin Siodelski <marcin@isc.org>
Tue, 17 Oct 2023 18:25:37 +0000 (20:25 +0200)
committerMarcin Siodelski <marcin@isc.org>
Wed, 29 Nov 2023 19:58:55 +0000 (20:58 +0100)
src/hooks/dhcp/high_availability/command_creator.cc
src/hooks/dhcp/high_availability/command_creator.h
src/hooks/dhcp/high_availability/ha_impl.cc
src/hooks/dhcp/high_availability/ha_service.cc
src/hooks/dhcp/high_availability/ha_service.h
src/hooks/dhcp/high_availability/tests/command_creator_unittest.cc
src/hooks/dhcp/high_availability/tests/ha_service_unittest.cc

index 25395e0f144f816b8ca435513b00985c8cd037d4..a43447f17059c9bfe347ef23e5be6c5df8c636c1 100644 (file)
@@ -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);
index 04c81acd0b1e521aed27dbe5ca810966bc9f2598..dc65cffcc987ba4db0436b95da1c12a2ba4005bc 100644 (file)
@@ -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.
index af0fdb1f2d6621119a4b0d405873cc04f12659ed..042ff901fe0020de6a6c52b38e356b8c7c0ee1d7 100644 (file)
@@ -603,7 +603,18 @@ HAImpl::syncCompleteNotifyHandler(hooks::CalloutHandle& callout_handle) {
     static_cast<void>(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);
 }
 
index edd2c70408e0db19ba2043ce61bd98f3028e29e7..837aa3f095163d04cd8fbb0a86512ddfb43d2d61 100644 (file)
@@ -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."));
index fa6c5d49eb0f989b6680b20424a18192d23942ba..93b2a6caea1afec53c006e32e1afdf66f945ae70 100644 (file)
@@ -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.
     ///
index 3deeb7f66b103294bd6532d75074862d403b5323..5ad5f8973cdc1d45c804379ca17b14028e8f83df 100644 (file)
@@ -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());
 }
 
 }
index 5280aa945a9025ebbe0294915b1bcd3629f2cc2a..efb958b00ec9bb2f83b49ee4c1b892478b6fb190 100644 (file)
@@ -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,