Commit 1b19c92b authored by Marcin Siodelski's avatar Marcin Siodelski
Browse files

[#689] Updated HA hooks lib to use lease6-bulk-apply command.

parent 65021b84
......@@ -96,6 +96,34 @@ CommandCreator::createLease4GetPage(const Lease4Ptr& last_lease4,
return (command);
}
ConstElementPtr
CommandCreator::createLease6BulkApply(const Lease6CollectionPtr& leases,
const Lease6CollectionPtr& deleted_leases) {
ElementPtr deleted_leases_list = Element::createList();
for (auto lease = deleted_leases->begin(); lease != deleted_leases->end();
++lease) {
ElementPtr lease_as_json = (*lease)->toElement();
insertLeaseExpireTime(lease_as_json);
deleted_leases_list->add(lease_as_json);
}
ElementPtr leases_list = Element::createList();
for (auto lease = leases->begin(); lease != leases->end();
++lease) {
ElementPtr lease_as_json = (*lease)->toElement();
insertLeaseExpireTime(lease_as_json);
leases_list->add(lease_as_json);
}
ElementPtr args = Element::createMap();
args->set("deleted-leases", deleted_leases_list);
args->set("leases", leases_list);
ConstElementPtr command = config::createCommand("lease6-bulk-apply", args);
insertService(command, HAServerType::DHCPv6);
return (command);
}
ConstElementPtr
CommandCreator::createLease6Update(const Lease6& lease6) {
ElementPtr lease_as_json = lease6.toElement();
......
......@@ -80,6 +80,17 @@ public:
createLease4GetPage(const dhcp::Lease4Ptr& lease4,
const uint32_t limit);
/// @brief Creates lease6-bulk-update command.
///
/// @param leases Pointer to the collection of leases to be created
/// or/and updated.
/// @param deleted_leases Pointer to the collection of leases to be
/// deleted.
/// @return Pointer to the JSON representation of the command.
static data::ConstElementPtr
createLease6BulkApply(const dhcp::Lease6CollectionPtr& leases,
const dhcp::Lease6CollectionPtr& deleted_leases);
/// @brief Creates lease6-update command.
///
/// It adds "force-create" parameter to the lease information to force
......
......@@ -732,17 +732,9 @@ HAService::asyncSendLeaseUpdates(const dhcp::Pkt6Ptr& query,
// Count contacted servers.
++sent_num;
// Lease updates for deleted leases.
for (auto l = deleted_leases->begin(); l != deleted_leases->end(); ++l) {
asyncSendLeaseUpdate(query, conf, CommandCreator::createLease6Delete(**l),
parking_lot);
}
// Lease updates for new allocations and updated leases.
for (auto l = leases->begin(); l != leases->end(); ++l) {
asyncSendLeaseUpdate(query, conf, CommandCreator::createLease6Update(**l),
parking_lot);
}
// Send new/updated leases and deleted leases in one command.
asyncSendLeaseUpdate(query, conf, CommandCreator::createLease6BulkApply(leases, deleted_leases),
parking_lot);
}
return (sent_num);
......
......@@ -303,6 +303,39 @@ TEST(CommandCreatorTest, createLease6Delete) {
EXPECT_EQ(lease_as_json->str(), arguments->str());
}
// This test verifies that the lease6-bulk-apply command is correct.
TEST(CommandCreatorTest, createLease6BulkApply) {
Lease6Ptr lease = createLease6();
Lease6Ptr deleted_lease = createLease6();
Lease6CollectionPtr leases(new Lease6Collection());
Lease6CollectionPtr deleted_leases(new Lease6Collection());
leases->push_back(lease);
deleted_leases->push_back(lease);
ConstElementPtr command = CommandCreator::createLease6BulkApply(leases, deleted_leases);
ConstElementPtr arguments;
ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "lease6-bulk-apply",
"dhcp6", arguments));
// Verify deleted-leases.
auto deleted_leases_json = arguments->get("deleted-leases");
ASSERT_TRUE(deleted_leases_json);
ASSERT_EQ(Element::list, deleted_leases_json->getType());
ASSERT_EQ(1, deleted_leases_json->size());
auto lease_as_json = deleted_leases_json->get(0);
EXPECT_EQ(leaseAsJson(createLease6())->str(), lease_as_json->str());
// Verify leases.
auto leases_json = arguments->get("leases");
ASSERT_TRUE(leases_json);
ASSERT_EQ(Element::list, leases_json->getType());
ASSERT_EQ(1, leases_json->size());
lease_as_json = leases_json->get(0);
EXPECT_EQ(leaseAsJson(createLease6())->str(), lease_as_json->str());
}
// This test verifies that the lease6-get-all command is correct.
TEST(CommandCreatorTest, createLease6GetAll) {
ConstElementPtr command = CommandCreator::createLease6GetAll();
......
......@@ -226,15 +226,21 @@ public:
///
/// @param str1 First string which must be included in the request.
/// @param str2 Second string which must be included in the request.
/// @param str3 Third string which must be included in the request.
/// It is optional and defaults to empty string which means "do not
/// match".
///
/// @return Pointer to the request found, or null pointer if there is
/// no such request.
ConstPostHttpRequestJsonPtr
findRequest(const std::string& str1, const std::string& str2) {
findRequest(const std::string& str1, const std::string& str2,
const std::string& str3 = "") {
for (auto r = requests_.begin(); r < requests_.end(); ++r) {
std::string request_as_string = (*r)->toString();
if (request_as_string.find(str1) != std::string::npos) {
if (request_as_string.find(str2) != std::string::npos) {
if (str3.empty() ||
(request_as_string.find(str3) != std::string::npos))
return (*r);
}
}
......@@ -1332,31 +1338,23 @@ TEST_F(HAServiceTest, sendSuccessfulUpdates6) {
// to be successful.
EXPECT_TRUE(unpark_called);
// The server 2 should have received two commands.
EXPECT_EQ(2, factory2_->getResponseCreator()->getReceivedRequests().size());
// Check that the server 2 has received lease6-update command.
auto update_request2 = factory2_->getResponseCreator()->findRequest("lease6-update",
"2001:db8:1::cafe");
EXPECT_TRUE(update_request2);
// The server 2 should have received one command.
EXPECT_EQ(1, factory2_->getResponseCreator()->getReceivedRequests().size());
// Check that the server 2 has received lease6-del command.
auto delete_request2 = factory2_->getResponseCreator()->findRequest("lease6-del",
// Check that the server 2 has received lease6-bulk-apply command.
auto update_request2 = factory2_->getResponseCreator()->findRequest("lease6-bulk-apply",
"2001:db8:1::cafe",
"2001:db8:1::efac");
EXPECT_TRUE(delete_request2);
EXPECT_TRUE(update_request2);
// Lease updates should be successfully sent to server3.
EXPECT_EQ(2, factory3_->getResponseCreator()->getReceivedRequests().size());
// Check that the server 3 has received lease6-update command.
auto update_request3 = factory3_->getResponseCreator()->findRequest("lease6-update",
"2001:db8:1::cafe");
EXPECT_TRUE(update_request3);
EXPECT_EQ(1, factory3_->getResponseCreator()->getReceivedRequests().size());
// Check that the server 3 has received lease6-del command.
auto delete_request3 = factory3_->getResponseCreator()->findRequest("lease6-del",
// Check that the server 3 has received lease6-bulk-apply command.
auto update_request3 = factory3_->getResponseCreator()->findRequest("lease6-bulk-apply",
"2001:db8:1::cafe",
"2001:db8:1::efac");
EXPECT_TRUE(delete_request3);
EXPECT_TRUE(update_request3);
}
// Test scenario when lease updates are sent successfully to the backup server
......@@ -1379,28 +1377,20 @@ TEST_F(HAServiceTest, sendUpdatesPartnerDown6) {
// to be successful.
EXPECT_TRUE(unpark_called);
// Server 2 should not receive lease6-update.
auto update_request2 = factory2_->getResponseCreator()->findRequest("lease6-update",
"2001:db8:1::cafe");
EXPECT_FALSE(update_request2);
// Server 2 should not receive lease6-del.
auto delete_request2 = factory2_->getResponseCreator()->findRequest("lease6-del",
// Server 2 should not receive lease6-bulk-apply.
auto update_request2 = factory2_->getResponseCreator()->findRequest("lease6-bulk-apply",
"2001:db8:1::cafe",
"2001:db8:1::efac");
EXPECT_FALSE(delete_request2);
EXPECT_FALSE(update_request2);
// Lease updates should be successfully sent to server3.
EXPECT_EQ(2, factory3_->getResponseCreator()->getReceivedRequests().size());
// Check that the server 3 has received lease6-update command.
auto update_request3 = factory3_->getResponseCreator()->findRequest("lease6-update",
"2001:db8:1::cafe");
EXPECT_TRUE(update_request3);
EXPECT_EQ(1, factory3_->getResponseCreator()->getReceivedRequests().size());
// Check that the server 3 has received lease6-del command.
auto delete_request3 = factory3_->getResponseCreator()->findRequest("lease6-del",
// Check that the server 3 has received lease6-bulk-apply command.
auto update_request3 = factory3_->getResponseCreator()->findRequest("lease6-bulk-apply",
"2001:db8:1::cafe",
"2001:db8:1::efac");
EXPECT_TRUE(delete_request3);
EXPECT_TRUE(update_request3);
}
// Test scenario when one of the servers to which updates are sent is offline.
......@@ -1416,28 +1406,20 @@ TEST_F(HAServiceTest, sendUpdatesActiveServerOffline6) {
" is dropped";
}, false, 2);
// Server 2 should not receive lease6-update.
auto update_request2 = factory2_->getResponseCreator()->findRequest("lease6-update",
"2001:db8:1::cafe");
EXPECT_FALSE(update_request2);
// Server 2 should not receive lease6-del.
auto delete_request2 = factory2_->getResponseCreator()->findRequest("lease6-del",
// Server 2 should not receive lease6-bulk-apply.
auto update_request2 = factory2_->getResponseCreator()->findRequest("lease6-bulk-apply",
"2001:db8:1::cafe",
"2001:db8:1::efac");
EXPECT_FALSE(delete_request2);
EXPECT_FALSE(update_request2);
// Lease updates should be successfully sent to server3.
EXPECT_EQ(2, factory3_->getResponseCreator()->getReceivedRequests().size());
// Check that the server 3 has received lease6-update command.
auto update_request3 = factory3_->getResponseCreator()->findRequest("lease6-update",
"2001:db8:1::cafe");
EXPECT_TRUE(update_request3);
EXPECT_EQ(1, factory3_->getResponseCreator()->getReceivedRequests().size());
// Check that the server 3 has received lease6-del command.
auto delete_request3 = factory3_->getResponseCreator()->findRequest("lease6-del",
// Check that the server 3 has received lease6-bulk-apply command.
auto update_request3 = factory3_->getResponseCreator()->findRequest("lease6-bulk-apply",
"2001:db8:1::cafe",
"2001:db8:1::efac");
EXPECT_TRUE(delete_request3);
EXPECT_TRUE(update_request3);
}
// Test scenario when one of the servers to which updates are sent is offline.
......@@ -1455,28 +1437,20 @@ TEST_F(HAServiceTest, sendUpdatesBackupServerOffline6) {
EXPECT_TRUE(unpark_called);
// The server 2 should have received two commands.
EXPECT_EQ(2, factory2_->getResponseCreator()->getReceivedRequests().size());
// The server 2 should have received one command.
EXPECT_EQ(1, factory2_->getResponseCreator()->getReceivedRequests().size());
// Check that the server 2 has received lease6-update command.
auto update_request2 = factory2_->getResponseCreator()->findRequest("lease6-update",
"2001:db8:1::cafe");
// Check that the server 2 has received lease6-bulk-apply command.
auto update_request2 = factory2_->getResponseCreator()->findRequest("lease6-bulk-apply",
"2001:db8:1::cafe",
"2001:db8:1::efac");
EXPECT_TRUE(update_request2);
// Check that the server 2 has received lease6-del command.
auto delete_request2 = factory2_->getResponseCreator()->findRequest("lease6-del",
// Server 3 should not receive lease6-bulk-apply.
auto update_request3 = factory3_->getResponseCreator()->findRequest("lease6-bulk-apply",
"2001:db8:1::cafe",
"2001:db8:1::efac");
EXPECT_TRUE(delete_request2);
// Server 3 should not receive lease6-update.
auto update_request3 = factory3_->getResponseCreator()->findRequest("lease6-update",
"2001:db8:1::cafe");
EXPECT_FALSE(update_request3);
// Server 3 should not receive lease6-del.
auto delete_request3 = factory3_->getResponseCreator()->findRequest("lease6-del",
"2001:db8:1::efac");
EXPECT_FALSE(delete_request3);
}
// Test scenario when one of the servers to which a lease update is sent
......@@ -1498,30 +1472,22 @@ TEST_F(HAServiceTest, sendUpdatesControlResultError6) {
}, false, 2);
// The updates should be sent to server 2 and this server should return error code.
EXPECT_EQ(2, factory2_->getResponseCreator()->getReceivedRequests().size());
// Server 2 should receive lease6-update.
auto update_request2 = factory2_->getResponseCreator()->findRequest("lease6-update",
"2001:db8:1::cafe");
EXPECT_TRUE(update_request2);
EXPECT_EQ(1, factory2_->getResponseCreator()->getReceivedRequests().size());
// Server 2 should receive lease6-del.
auto delete_request2 = factory2_->getResponseCreator()->findRequest("lease6-del",
// Server 2 should receive lease6-bulk-apply.
auto update_request2 = factory2_->getResponseCreator()->findRequest("lease6-bulk-apply",
"2001:db8:1::cafe",
"2001:db8:1::efac");
EXPECT_TRUE(delete_request2);
EXPECT_TRUE(update_request2);
// Lease updates should be successfully sent to server3.
EXPECT_EQ(2, factory3_->getResponseCreator()->getReceivedRequests().size());
// Check that the server 3 has received lease6-update command.
auto update_request3 = factory3_->getResponseCreator()->findRequest("lease6-update",
"2001:db8:1::cafe");
EXPECT_TRUE(update_request3);
EXPECT_EQ(1, factory3_->getResponseCreator()->getReceivedRequests().size());
// Check that the server 3 has received lease6-del command.
auto delete_request3 = factory3_->getResponseCreator()->findRequest("lease6-del",
// Check that the server 3 has received lease6-bulk-apply command.
auto update_request3 = factory3_->getResponseCreator()->findRequest("lease6-bulk-apply",
"2001:db8:1::cafe",
"2001:db8:1::efac");
EXPECT_TRUE(delete_request3);
EXPECT_TRUE(update_request3);
}
// This test verifies that the heartbeat command is processed successfully.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment