From: Marcin Siodelski Date: Tue, 9 Jan 2018 13:49:14 +0000 (+0100) Subject: [5468] Implemented getting leases by subnet id and all leases. X-Git-Tag: trac5469_base~9 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=27322572027a3bc569dfd81eebac53c0e99f73c0;p=thirdparty%2Fkea.git [5468] Implemented getting leases by subnet id and all leases. This change affects Memfile, MySQL and Postgres backend. These functions aren't implemented for CQL at this time. --- diff --git a/src/lib/dhcpsrv/cql_lease_mgr.cc b/src/lib/dhcpsrv/cql_lease_mgr.cc index 9722ffe665..5036044b75 100644 --- a/src/lib/dhcpsrv/cql_lease_mgr.cc +++ b/src/lib/dhcpsrv/cql_lease_mgr.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2017 Deutsche Telekom AG. +// Copyright (C) 2015-2018 Deutsche Telekom AG. // // Authors: Razvan Becheriu // Andrei Pavel @@ -1620,6 +1620,16 @@ CqlLeaseMgr::getLease4(const ClientId &clientid, SubnetID subnet_id) const { return (result); } +Lease4Collection +CqlLeaseMgr::getLeases4(SubnetID) const { + isc_throw(NotImplemented, "getLeases4(subnet_id) is not implemented"); +} + +Lease4Collection +CqlLeaseMgr::getLeases4() const { + isc_throw(NotImplemented, "getLeases4() is not implemented"); +} + Lease6Ptr CqlLeaseMgr::getLease6(Lease::Type lease_type, const IOAddress &addr) const { std::string addr_data = addr.toText(); diff --git a/src/lib/dhcpsrv/cql_lease_mgr.h b/src/lib/dhcpsrv/cql_lease_mgr.h index 878f209393..4f5e24ea3e 100644 --- a/src/lib/dhcpsrv/cql_lease_mgr.h +++ b/src/lib/dhcpsrv/cql_lease_mgr.h @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2017 Deutsche Telekom AG. +// Copyright (C) 2015-2018 Deutsche Telekom AG. // // Authors: Razvan Becheriu // Andrei Pavel @@ -183,6 +183,18 @@ public: const HWAddr& hwaddr, SubnetID subnet_id) const override; + /// @brief Returns all IPv4 leases for the particular subnet identifier. + /// + /// @param subnet_id subnet identifier. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4(SubnetID subnet_id) const; + + /// @brief Returns all IPv4 leases. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4() const; + /// @brief Returns existing IPv4 lease for specified client-id /// /// There can be at most one lease for a given HW address in a single @@ -198,6 +210,18 @@ public: virtual Lease4Ptr getLease4(const ClientId& clientid, SubnetID subnet_id) const override; + /// @brief Returns all IPv4 leases for the particular subnet identifier. + /// + /// @param subnet_id subnet identifier. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4(SubnetID subnet_id) const; + + /// @brief Returns all IPv4 leases. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4() const; + /// @brief Returns existing IPv6 lease for a given IPv6 address. /// /// For a given address, we assume that there will be only one lease. diff --git a/src/lib/dhcpsrv/dhcpsrv_messages.mes b/src/lib/dhcpsrv/dhcpsrv_messages.mes index 22b66a5519..ad5a7d40f3 100644 --- a/src/lib/dhcpsrv/dhcpsrv_messages.mes +++ b/src/lib/dhcpsrv/dhcpsrv_messages.mes @@ -1,4 +1,4 @@ -# Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC") +# Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC") # # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this @@ -459,6 +459,10 @@ A debug message issued when the server has found expired-reclaimed leases to be removed. The number of leases to be removed is logged in the message. +% DHCPSRV_MEMFILE_GET4 obtaining all IPv4 leases +A debug message issued when the server is attempting to obtain all IPv4 +leases from the memory file database. + % DHCPSRV_MEMFILE_GET_ADDR4 obtaining IPv4 lease for address %1 A debug message issued when the server is attempting to obtain an IPv4 lease from the memory file database for the specified address. @@ -502,6 +506,10 @@ A debug message issued when the server is attempting to obtain an IPv6 lease from the memory file database for a client with the specified IAID (Identity Association ID), Subnet ID and DUID (DHCP Unique Identifier). +% DHCPSRV_MEMFILE_GET_SUBID4 obtaining IPv4 leases for subnet ID %1 +A debug message issued when the server is attempting to obtain all IPv4 +leases for a given subnet identifier from the memory file database. + % DHCPSRV_MEMFILE_GET_SUBID_CLIENTID obtaining IPv4 lease for subnet ID %1 and client ID %2 A debug message issued when the server is attempting to obtain an IPv4 lease from the memory file database for a client with the specified @@ -685,6 +693,10 @@ An error message indicating that communication with the MySQL database server has been lost. When this occurs the server exits immediately with a non-zero exit code. This is most likely due to a network issue. +% DHCPSRV_MYSQL_GET4 obtaining all IPv4 leases +A debug message issued when the server is attempting to obtain all IPv4 +leases from the MySQL database. + % DHCPSRV_MYSQL_GET_ADDR4 obtaining IPv4 lease for address %1 A debug message issued when the server is attempting to obtain an IPv4 lease from the MySQL database for the specified address. @@ -723,6 +735,10 @@ A debug message issued when the server is attempting to obtain an IPv6 lease from the MySQL database for a client with the specified IAID (Identity Association ID), Subnet ID and DUID (DHCP Unique Identifier). +% DHCPSRV_MYSQL_GET_SUBID4 obtaining IPv4 leases for subnet ID %1 +A debug message issued when the server is attempting to obtain all IPv4 +leases for a given subnet identifier from the MySQL database. + % DHCPSRV_MYSQL_GET_SUBID_CLIENTID obtaining IPv4 lease for subnet ID %1 and client ID %2 A debug message issued when the server is attempting to obtain an IPv4 lease from the MySQL database for a client with the specified subnet ID @@ -839,6 +855,10 @@ An error message indicating that communication with the MySQL database server has been lost. When this occurs the server exits immediately with a non-zero exit code. This is most likely due to a network issue. +% DHCPSRV_PGSQL_GET4 obtaining all IPv4 leases +A debug message issued when the server is attempting to obtain all IPv4 +leases from the PostgreSQL database. + % DHCPSRV_PGSQL_GET_ADDR4 obtaining IPv4 lease for address %1 A debug message issued when the server is attempting to obtain an IPv4 lease from the PostgreSQL database for the specified address. @@ -877,6 +897,10 @@ A debug message issued when the server is attempting to obtain an IPv6 lease from the PostgreSQL database for a client with the specified IAID (Identity Association ID), Subnet ID and DUID (DHCP Unique Identifier). +% DHCPSRV_PGSQL_GET_SUBID4 obtaining IPv4 leases for subnet ID %1 +A debug message issued when the server is attempting to obtain all IPv4 +leases for a given subnet identifier from the PostgreSQL database. + % DHCPSRV_PGSQL_GET_SUBID_CLIENTID obtaining IPv4 lease for subnet ID %1 and client ID %2 A debug message issued when the server is attempting to obtain an IPv4 lease from the PostgreSQL database for a client with the specified subnet ID diff --git a/src/lib/dhcpsrv/lease_mgr.h b/src/lib/dhcpsrv/lease_mgr.h index cfeffa0783..9bac9e6fc3 100644 --- a/src/lib/dhcpsrv/lease_mgr.h +++ b/src/lib/dhcpsrv/lease_mgr.h @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -261,6 +261,18 @@ public: virtual Lease4Ptr getLease4(const ClientId& clientid, SubnetID subnet_id) const = 0; + /// @brief Returns all IPv4 leases for the particular subnet identifier. + /// + /// @param subnet_id subnet identifier. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4(SubnetID subnet_id) const = 0; + + /// @brief Returns all IPv4 leases. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4() const = 0; + /// @brief Returns existing IPv6 lease for a given IPv6 address. /// /// For a given address, we assume that there will be only one lease. diff --git a/src/lib/dhcpsrv/memfile_lease_mgr.cc b/src/lib/dhcpsrv/memfile_lease_mgr.cc index cb58084c54..d5f8ef743d 100644 --- a/src/lib/dhcpsrv/memfile_lease_mgr.cc +++ b/src/lib/dhcpsrv/memfile_lease_mgr.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -733,6 +733,36 @@ Memfile_LeaseMgr::getLease4(const ClientId& client_id, return (Lease4Ptr(new Lease4(**lease))); } +Lease4Collection +Memfile_LeaseMgr::getLeases4(SubnetID subnet_id) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MEMFILE_GET_SUBID4) + .arg(subnet_id); + + Lease4Collection collection; + const Lease4StorageSubnetIdIndex& idx = storage4_.get(); + std::pair l = + idx.equal_range(subnet_id); + + for (auto lease = l.first; lease != l.second; ++lease) { + collection.push_back(Lease4Ptr(new Lease4(**lease))); + } + + return (collection); +} + +Lease4Collection +Memfile_LeaseMgr::getLeases4() const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MEMFILE_GET4); + + Lease4Collection collection; + for (auto lease = storage4_.begin(); lease != storage4_.end(); ++lease ) { + collection.push_back(Lease4Ptr(new Lease4(**lease))); + } + + return (collection); +} + Lease6Ptr Memfile_LeaseMgr::getLease6(Lease::Type type, const isc::asiolink::IOAddress& addr) const { diff --git a/src/lib/dhcpsrv/memfile_lease_mgr.h b/src/lib/dhcpsrv/memfile_lease_mgr.h index 3d76f94468..a82fa3cd5b 100644 --- a/src/lib/dhcpsrv/memfile_lease_mgr.h +++ b/src/lib/dhcpsrv/memfile_lease_mgr.h @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -220,6 +220,18 @@ public: virtual Lease4Ptr getLease4(const ClientId& clientid, SubnetID subnet_id) const; + /// @brief Returns all IPv4 leases for the particular subnet identifier. + /// + /// @param subnet_id subnet identifier. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4(SubnetID subnet_id) const; + + /// @brief Returns all IPv4 leases. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4() const; + /// @brief Returns existing IPv6 lease for a given IPv6 address. /// /// This function returns a copy of the lease. The modification in the diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.cc b/src/lib/dhcpsrv/mysql_lease_mgr.cc index 63d4cabdc3..4c4fe3c5f7 100644 --- a/src/lib/dhcpsrv/mysql_lease_mgr.cc +++ b/src/lib/dhcpsrv/mysql_lease_mgr.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -96,6 +96,12 @@ tagged_statements = { { {MySqlLeaseMgr::DELETE_LEASE6_STATE_EXPIRED, "DELETE FROM lease6 " "WHERE state = ? AND expire < ?"}, + {MySqlLeaseMgr::GET_LEASE4, + "SELECT address, hwaddr, client_id, " + "valid_lifetime, expire, subnet_id, " + "fqdn_fwd, fqdn_rev, hostname, " + "state " + "FROM lease4"}, {MySqlLeaseMgr::GET_LEASE4_ADDR, "SELECT address, hwaddr, client_id, " "valid_lifetime, expire, subnet_id, " @@ -131,6 +137,13 @@ tagged_statements = { { "state " "FROM lease4 " "WHERE hwaddr = ? AND subnet_id = ?"}, + {MySqlLeaseMgr::GET_LEASE4_SUBID, + "SELECT address, hwaddr, client_id, " + "valid_lifetime, expire, subnet_id, " + "fqdn_fwd, fqdn_rev, hostname, " + "state " + "FROM lease4 " + "WHERE subnet_id = ?"}, {MySqlLeaseMgr::GET_LEASE4_EXPIRE, "SELECT address, hwaddr, client_id, " "valid_lifetime, expire, subnet_id, " @@ -1492,9 +1505,13 @@ void MySqlLeaseMgr::getLeaseCollection(StatementIndex stindex, LeaseCollection& result, bool single) const { - // Bind the selection parameters to the statement - int status = mysql_stmt_bind_param(conn_.statements_[stindex], bind); - checkError(status, stindex, "unable to bind WHERE clause parameter"); + int status; + + if (bind) { + // Bind the selection parameters to the statement + int status = mysql_stmt_bind_param(conn_.statements_[stindex], bind); + checkError(status, stindex, "unable to bind WHERE clause parameter"); + } // Set up the MYSQL_BIND array for the data being returned and bind it to // the statement. @@ -1735,6 +1752,36 @@ MySqlLeaseMgr::getLease4(const ClientId& clientid, SubnetID subnet_id) const { return (result); } +Lease4Collection +MySqlLeaseMgr::getLeases4(SubnetID subnet_id) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MYSQL_GET_SUBID4) + .arg(subnet_id); + + // Set up the WHERE clause value + MYSQL_BIND inbind[1]; + memset(inbind, 0, sizeof(inbind)); + + // Subnet ID + inbind[0].buffer_type = MYSQL_TYPE_LONG; + inbind[0].buffer = reinterpret_cast(&subnet_id); + inbind[0].is_unsigned = MLM_TRUE; + + // ... and get the data + Lease4Collection result; + getLeaseCollection(GET_LEASE4_SUBID, inbind, result); + + return (result); +} + +Lease4Collection +MySqlLeaseMgr::getLeases4() const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MYSQL_GET4); + + Lease4Collection result; + getLeaseCollection(GET_LEASE4, 0, result); + + return (result); +} Lease6Ptr MySqlLeaseMgr::getLease6(Lease::Type lease_type, diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.h b/src/lib/dhcpsrv/mysql_lease_mgr.h index 5c47e8ede9..0c274a46ed 100644 --- a/src/lib/dhcpsrv/mysql_lease_mgr.h +++ b/src/lib/dhcpsrv/mysql_lease_mgr.h @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -193,6 +193,18 @@ public: virtual Lease4Ptr getLease4(const ClientId& clientid, SubnetID subnet_id) const; + /// @brief Returns all IPv4 leases for the particular subnet identifier. + /// + /// @param subnet_id subnet identifier. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4(SubnetID subnet_id) const; + + /// @brief Returns all IPv4 leases. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4() const; + /// @brief Returns existing IPv6 lease for a given IPv6 address. /// /// For a given address, we assume that there will be only one lease. @@ -416,11 +428,13 @@ public: DELETE_LEASE4_STATE_EXPIRED, // Delete expired lease4 in a given state DELETE_LEASE6, // Delete from lease6 by address DELETE_LEASE6_STATE_EXPIRED, // Delete expired lease6 in a given state + GET_LEASE4, // Get all IPv4 leases GET_LEASE4_ADDR, // Get lease4 by address GET_LEASE4_CLIENTID, // Get lease4 by client ID GET_LEASE4_CLIENTID_SUBID, // Get lease4 by client ID & subnet ID GET_LEASE4_HWADDR, // Get lease4 by HW address GET_LEASE4_HWADDR_SUBID, // Get lease4 by HW address & subnet ID + GET_LEASE4_SUBID, // Get IPv4 leases by subnet ID GET_LEASE4_EXPIRE, // Get lease4 by expiration. GET_LEASE6_ADDR, // Get lease6 by address GET_LEASE6_DUID_IAID, // Get lease6 by DUID and IAID diff --git a/src/lib/dhcpsrv/pgsql_lease_mgr.cc b/src/lib/dhcpsrv/pgsql_lease_mgr.cc index c2d0d43f39..3d8c30cfdc 100644 --- a/src/lib/dhcpsrv/pgsql_lease_mgr.cc +++ b/src/lib/dhcpsrv/pgsql_lease_mgr.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2018 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -55,6 +55,14 @@ PgSqlTaggedStatement tagged_statements[] = { "DELETE FROM lease6 " "WHERE state = $1 AND expire < $2"}, + // GET_LEASE4 + { 0, { OID_NONE }, + "get_lease4", + "SELECT address, hwaddr, client_id, " + "valid_lifetime, extract(epoch from expire)::bigint, subnet_id, " + "fqdn_fwd, fqdn_rev, hostname, state " + "FROM lease4"}, + // GET_LEASE4_ADDR { 1, { OID_INT8 }, "get_lease4_addr", @@ -100,6 +108,15 @@ PgSqlTaggedStatement tagged_statements[] = { "FROM lease4 " "WHERE hwaddr = $1 AND subnet_id = $2"}, + // GET_LEASE4_SUBID + { 1, { OID_INT8 }, + "get_lease4_subid", + "SELECT address, hwaddr, client_id, " + "valid_lifetime, extract(epoch from expire)::bigint, subnet_id, " + "fqdn_fwd, fqdn_rev, hostname, state " + "FROM lease4 " + "WHERE subnet_id = $1"}, + // GET_LEASE4_EXPIRE { 3, { OID_INT8, OID_TIMESTAMP, OID_INT8 }, "get_lease4_expire", @@ -892,11 +909,11 @@ void PgSqlLeaseMgr::getLeaseCollection(StatementIndex stindex, Exchange& exchange, LeaseCollection& result, bool single) const { - PgSqlResult r(PQexecPrepared(conn_, tagged_statements[stindex].name, - tagged_statements[stindex].nbparams, - &bind_array.values_[0], - &bind_array.lengths_[0], - &bind_array.formats_[0], 0)); + const int n = tagged_statements[stindex].nbparams; + PgSqlResult r(PQexecPrepared(conn_, tagged_statements[stindex].name, n, + n > 0 ? &bind_array.values_[0] : NULL, + n > 0 ? &bind_array.lengths_[0] : NULL, + n > 0 ? &bind_array.formats_[0] : NULL, 0)); conn_.checkStatementError(r, tagged_statements[stindex]); @@ -1072,6 +1089,38 @@ PgSqlLeaseMgr::getLease4(const ClientId&, const HWAddr&, SubnetID) const { " called, but it is not implemented"); } +Lease4Collection +PgSqlLeaseMgr::getLeases4(SubnetID subnet_id) const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_PGSQL_GET_SUBID4) + .arg(subnet_id); + + // Set up the WHERE clause value + PsqlBindArray bind_array; + + // SUBNET_ID + std::string subnet_id_str = boost::lexical_cast(subnet_id); + bind_array.add(subnet_id_str); + + // ... and get the data + Lease4Collection result; + getLeaseCollection(GET_LEASE4_SUBID, bind_array, result); + + return (result); +} + +Lease4Collection +PgSqlLeaseMgr::getLeases4() const { + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_PGSQL_GET4); + + // Provide empty binding array because our query has no parameters in + // WHERE clause. + PsqlBindArray bind_array; + Lease4Collection result; + getLeaseCollection(GET_LEASE4, bind_array, result); + + return (result); +} + Lease6Ptr PgSqlLeaseMgr::getLease6(Lease::Type lease_type, const isc::asiolink::IOAddress& addr) const { diff --git a/src/lib/dhcpsrv/pgsql_lease_mgr.h b/src/lib/dhcpsrv/pgsql_lease_mgr.h index 01b9ae41a9..8599d52379 100644 --- a/src/lib/dhcpsrv/pgsql_lease_mgr.h +++ b/src/lib/dhcpsrv/pgsql_lease_mgr.h @@ -1,4 +1,4 @@ -// Copyright (C) 2013-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2013-2018 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -161,6 +161,17 @@ public: virtual Lease4Ptr getLease4(const ClientId& client_id, const HWAddr& hwaddr, SubnetID subnet_id) const; + /// @brief Returns all IPv4 leases for the particular subnet identifier. + /// + /// @param subnet_id subnet identifier. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4(SubnetID subnet_id) const; + + /// @brief Returns all IPv4 leases. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4() const; /// @brief Returns existing IPv4 lease for specified client-id /// @@ -408,11 +419,13 @@ public: DELETE_LEASE4_STATE_EXPIRED,// Delete expired lease4s in certain state. DELETE_LEASE6, // Delete from lease6 by address DELETE_LEASE6_STATE_EXPIRED,// Delete expired lease6s in certain state. + GET_LEASE4, // Get all IPv4 leases GET_LEASE4_ADDR, // Get lease4 by address GET_LEASE4_CLIENTID, // Get lease4 by client ID GET_LEASE4_CLIENTID_SUBID, // Get lease4 by client ID & subnet ID GET_LEASE4_HWADDR, // Get lease4 by HW address GET_LEASE4_HWADDR_SUBID, // Get lease4 by HW address & subnet ID + GET_LEASE4_SUBID, // Get IPv4 leases by subnet ID GET_LEASE4_EXPIRE, // Get expired lease4 GET_LEASE6_ADDR, // Get lease6 by address GET_LEASE6_DUID_IAID, // Get lease6 by DUID and IAID diff --git a/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.cc index 3a0c65783a..e9cdcbf942 100644 --- a/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2018 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -1219,6 +1219,33 @@ GenericLeaseMgrTest::testGetLease4ClientIdSubnetId() { EXPECT_FALSE(returned); } +void +GenericLeaseMgrTest::testGetLeases4SubnetId() { + // Get the leases to be used for the test and add to the database. + vector leases = createLeases4(); + for (size_t i = 0; i < leases.size(); ++i) { + EXPECT_TRUE(lmptr_->addLease(leases[i])); + } + + // There should be exactly two leases for the subnet id that the second + // lease belongs to. + Lease4Collection returned = lmptr_->getLeases4(leases[1]->subnet_id_); + ASSERT_EQ(2, returned.size()); +} + +void +GenericLeaseMgrTest::testGetLeases4() { + // Get the leases to be used for the test and add to the database + vector leases = createLeases4(); + for (size_t i = 0; i < leases.size(); ++i) { + EXPECT_TRUE(lmptr_->addLease(leases[i])); + } + + // All leases should be returned. + Lease4Collection returned = lmptr_->getLeases4(); + ASSERT_EQ(leases.size(), returned.size()); +} + void GenericLeaseMgrTest::testGetLeases6DuidIaid() { // Get the leases to be used for the test. diff --git a/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.h b/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.h index 1a4b55277c..07b7948cc3 100644 --- a/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.h +++ b/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.h @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2018 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -200,6 +200,12 @@ public: /// a combination of client and subnet IDs. void testGetLease4ClientIdSubnetId(); + /// @brief Test method which returns all IPv4 leases for Subnet ID. + void testGetLeases4SubnetId(); + + /// @brief Test method which returns all IPv4 leases. + void testGetLeases4(); + /// @brief Basic Lease4 Checks /// /// Checks that the addLease, getLease4(by address), getLease4(hwaddr,subnet_id), diff --git a/src/lib/dhcpsrv/tests/lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/lease_mgr_unittest.cc index 05dccca6bc..373eb1a0b9 100644 --- a/src/lib/dhcpsrv/tests/lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/lease_mgr_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -129,6 +129,22 @@ public: return (Lease4Ptr()); } + /// @brief Returns all IPv4 leases for the particular subnet identifier. + /// + /// @param subnet_id subnet identifier. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4(SubnetID) const { + return (Lease4Collection()); + } + + /// @brief Returns all IPv4 leases. + /// + /// @return Lease collection (may be empty if no IPv4 lease found). + virtual Lease4Collection getLeases4() const { + return (Lease4Collection()); + } + /// @brief Returns existing IPv6 lease for a given IPv6 address. /// /// @param addr address of the searched lease diff --git a/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc index b4eafa44b0..df4d586c3b 100644 --- a/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -917,6 +917,18 @@ TEST_F(MemfileLeaseMgrTest, getLease4ClientIdSubnetId) { testGetLease4ClientIdSubnetId(); } +// This test checks that all IPv4 leases for a specified subnet id are returned. +TEST_F(MemfileLeaseMgrTest, getLeases4SubnetId) { + startBackend(V4); + testGetLeases4SubnetId(); +} + +// This test checks that all IPv4 leases are returned. +TEST_F(MemfileLeaseMgrTest, getLeases4) { + startBackend(V4); + testGetLeases4(); +} + /// @brief Basic Lease6 Checks /// /// Checks that the addLease, getLease6 (by address) and deleteLease (with an diff --git a/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc index bb2b5463f3..609ddabe6e 100644 --- a/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -321,6 +321,16 @@ TEST_F(MySqlLeaseMgrTest, getLease4ClientIdSubnetId) { testGetLease4ClientIdSubnetId(); } +// This test checks that all IPv4 leases for a specified subnet id are returned. +TEST_F(MySqlLeaseMgrTest, getLeases4SubnetId) { + testGetLeases4SubnetId(); +} + +// This test checks that all IPv4 leases are returned. +TEST_F(MySqlLeaseMgrTest, getLeases4) { + testGetLeases4(); +} + /// @brief Basic Lease4 Checks /// /// Checks that the addLease, getLease4(by address), getLease4(hwaddr,subnet_id), diff --git a/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc index 6b59fcdb2c..fa84bbb99b 100644 --- a/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-2018 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -280,6 +280,16 @@ TEST_F(PgSqlLeaseMgrTest, getLease4ClientIdSubnetId) { testGetLease4ClientIdSubnetId(); } +// This test checks that all IPv4 leases for a specified subnet id are returned. +TEST_F(PgSqlLeaseMgrTest, getLeases4SubnetId) { + testGetLeases4SubnetId(); +} + +// This test checks that all IPv4 leases are returned. +TEST_F(PgSqlLeaseMgrTest, getLeases4) { + testGetLeases4(); +} + /// @brief Basic Lease4 Checks /// /// Checks that the addLease, getLease4(by address), getLease4(hwaddr,subnet_id),