Commit ffaff5b5 authored by Tomek Mrugalski's avatar Tomek Mrugalski 🛰

[5280] lease4-wipe, lease6-wipe commands implemented.

parent 574356fa
......@@ -122,8 +122,8 @@ private:
/// - lease6-del
/// - lease4-update
/// - lease6-update
/// - lease4-del-all
/// - lease6-del-all
/// - lease4-wipe
/// - lease6-wipe
/// @throw Unexpected if CommandMgr is not available (should not happen)
void registerCommands();
......@@ -139,8 +139,8 @@ private:
/// - lease6-del
/// - lease4-update
/// - lease6-update
/// - lease4-del-all
/// - lease6-del-all
/// - lease4-wipe
/// - lease6-wipe
///
/// @throw Unexpected if CommandMgr is not available (should not happen)
void deregisterCommands();
......@@ -358,9 +358,9 @@ void LeaseCmdsImpl::registerCommands() {
CommandMgr::instance().registerCommand("lease6-update",
boost::bind(&LeaseCmdsImpl::lease6UpdateHandler, _1, _2));
CommandMgr::instance().registerCommand("lease4-del-all",
CommandMgr::instance().registerCommand("lease4-wipe",
boost::bind(&LeaseCmdsImpl::lease4WipeHandler, _1, _2));
CommandMgr::instance().registerCommand("lease6-del-all",
CommandMgr::instance().registerCommand("lease6-wipe",
boost::bind(&LeaseCmdsImpl::lease6WipeHandler, _1, _2));
}
......@@ -378,8 +378,8 @@ void LeaseCmdsImpl::deregisterCommands() {
CommandMgr::instance().deregisterCommand("lease4-update");
CommandMgr::instance().deregisterCommand("lease6-update");
CommandMgr::instance().deregisterCommand("lease4-del-all");
CommandMgr::instance().deregisterCommand("lease6-del-all");
CommandMgr::instance().deregisterCommand("lease4-wipe");
CommandMgr::instance().deregisterCommand("lease6-wipe");
}
ConstElementPtr
......@@ -776,13 +776,49 @@ LeaseCmdsImpl::lease6UpdateHandler(const string& , ConstElementPtr params) {
}
ConstElementPtr
LeaseCmdsImpl::lease4WipeHandler(const string& cmd, ConstElementPtr args) {
return (createAnswer(CONTROL_RESULT_ERROR, "not implemented yet."));
LeaseCmdsImpl::lease4WipeHandler(const string& /*cmd*/, ConstElementPtr params) {
try {
// We need the lease to be specified.
if (!params) {
isc_throw(isc::BadValue, "no parameters specified for lease4-wipe command");
}
SimpleParser parser;
SubnetID id = parser.getUint32(params, "subnet-id");
size_t num = LeaseMgrFactory::instance().wipeLeases4(id);
stringstream tmp;
tmp << "Deleted " << num << " IPv4 lease(s).";
return (createAnswer(num ? CONTROL_RESULT_SUCCESS : CONTROL_RESULT_EMPTY,
tmp.str()));
} catch (const std::exception& ex) {
return (createAnswer(CONTROL_RESULT_ERROR, ex.what()));
}
}
ConstElementPtr
LeaseCmdsImpl::lease6WipeHandler(const string& cmd, ConstElementPtr args) {
return (createAnswer(CONTROL_RESULT_ERROR, "not implemented yet."));
LeaseCmdsImpl::lease6WipeHandler(const string& /*cmd*/, ConstElementPtr params) {
try {
// We need the lease to be specified.
if (!params) {
isc_throw(isc::BadValue, "no parameters specified for lease6-wipe command");
}
SimpleParser parser;
SubnetID id = parser.getUint32(params, "subnet-id");
size_t num = LeaseMgrFactory::instance().wipeLeases6(id);
stringstream tmp;
tmp << "Deleted " << num << " IPv6 lease(s).";
return (createAnswer(num ? CONTROL_RESULT_SUCCESS : CONTROL_RESULT_EMPTY,
tmp.str()));
} catch (const std::exception& ex) {
return (createAnswer(CONTROL_RESULT_ERROR, ex.what()));
}
}
LeaseCmds::LeaseCmds()
......
......@@ -423,11 +423,11 @@ public:
// Simple test that checks the library really registers the commands.
TEST_F(LeaseCmdsTest, commands) {
vector<string> cmds = { "lease4-add", "lease6-add",
"lease4-get", "lease6-get",
"lease4-del", "lease6-del",
vector<string> cmds = { "lease4-add", "lease6-add",
"lease4-get", "lease6-get",
"lease4-del", "lease6-del",
"lease4-update", "lease6-update",
"lease4-del-all", "lease6-del-all" };
"lease4-wipe", "lease6-wipe" };
testCommands(cmds);
}
......@@ -1756,4 +1756,117 @@ TEST_F(LeaseCmdsTest, Lease6DelByDUID) {
EXPECT_FALSE(lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8::1")));
}
// Checks that lease4-wipe detects missing parmameter properly.
TEST_F(LeaseCmdsTest, Lease4WipeMissingParam) {
// Initialize lease manager (false = v4, true = add a lease)
initLeaseMgr(false, true);
// Query for valid, existing lease.
string cmd =
"{\n"
" \"command\": \"lease4-wipe\",\n"
" \"arguments\": {"
" }\n"
"}";
string exp_rsp = "missing parameter 'subnet-id' (<string>:3:19)";
testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
}
// Checks that lease4-wipe can remove leases.
TEST_F(LeaseCmdsTest, Lease4Wipe) {
// Initialize lease manager (false = v4, true = add a lease)
initLeaseMgr(false, true);
// Query for valid, existing lease.
string cmd =
"{\n"
" \"command\": \"lease4-wipe\",\n"
" \"arguments\": {"
" \"subnet-id\": 44"
" }\n"
"}";
string exp_rsp = "Deleted 1 IPv4 lease(s).";
testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);
// Make sure the lease is really gone.
EXPECT_FALSE(lmptr_->getLease4(IOAddress("192.0.2.1")));
}
// Checks that lease4-wipe properly reports when no leases were deleted.
TEST_F(LeaseCmdsTest, Lease4WipeNoLeases) {
// Initialize lease manager (false = v4, false = no leases)
initLeaseMgr(false, false);
// Query for valid, existing lease.
string cmd =
"{\n"
" \"command\": \"lease4-wipe\",\n"
" \"arguments\": {"
" \"subnet-id\": 44"
" }\n"
"}";
string exp_rsp = "Deleted 0 IPv4 lease(s).";
testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);
}
// Checks that lease4-wipe detects missing parmameter properly.
TEST_F(LeaseCmdsTest, Lease6WipeMissingParam) {
// Initialize lease manager (true = v6, true = add a lease)
initLeaseMgr(true, true);
// Query for valid, existing lease.
string cmd =
"{\n"
" \"command\": \"lease6-wipe\",\n"
" \"arguments\": {"
" }\n"
"}";
string exp_rsp = "missing parameter 'subnet-id' (<string>:3:19)";
testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
}
// Checks that lease4-wipe can remove leases.
TEST_F(LeaseCmdsTest, Lease6Wipe) {
initLeaseMgr(true, true); // (true = v6, true = create a lease)
// Now send the command.
string cmd =
"{\n"
" \"command\": \"lease6-wipe\",\n"
" \"arguments\": {"
" \"subnet-id\": 66\n"
" }\n"
"}";
string exp_rsp = "Deleted 1 IPv6 lease(s).";
// The status expected is success. The lease should be deleted.
testCommand(cmd, CONTROL_RESULT_SUCCESS, exp_rsp);
// Make sure the lease is really gone.
EXPECT_FALSE(lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8::1")));
}
// Checks that lease4-wipe properly reports when no leases were deleted.
TEST_F(LeaseCmdsTest, Lease6WipeNoLeases) {
// Initialize lease manager (false = v4, false = no leases)
initLeaseMgr(true, false);
// Query for valid, existing lease.
string cmd =
"{\n"
" \"command\": \"lease6-wipe\",\n"
" \"arguments\": {"
" \"subnet-id\": 66"
" }\n"
"}";
string exp_rsp = "Deleted 0 IPv6 lease(s).";
testCommand(cmd, CONTROL_RESULT_EMPTY, exp_rsp);
}
} // end of anonymous namespace
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