From: Tomek Mrugalski Date: Wed, 24 Oct 2012 11:17:45 +0000 (+0200) Subject: [2324] Memfile_LeaseMgr is moved to a separate file X-Git-Tag: trac2487_base~21^2~9^2~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=daaa9544987cf61adb4a16fb955885019cce8885;p=thirdparty%2Fkea.git [2324] Memfile_LeaseMgr is moved to a separate file --- diff --git a/src/lib/dhcp/tests/Makefile.am b/src/lib/dhcp/tests/Makefile.am index 1f15112ab5..cb9941c11f 100644 --- a/src/lib/dhcp/tests/Makefile.am +++ b/src/lib/dhcp/tests/Makefile.am @@ -28,7 +28,6 @@ TESTS += libdhcp++_unittests libdhcpsrv_unittests libdhcp___unittests_SOURCES = run_unittests.cc libdhcp___unittests_SOURCES += libdhcp++_unittest.cc libdhcp___unittests_SOURCES += iface_mgr_unittest.cc -libdhcp___unittests_SOURCES += lease_mgr_unittest.cc libdhcp___unittests_SOURCES += option6_iaaddr_unittest.cc libdhcp___unittests_SOURCES += option6_ia_unittest.cc libdhcp___unittests_SOURCES += option6_addrlst_unittest.cc @@ -46,15 +45,17 @@ libdhcpsrv_unittests_SOURCES = run_unittests.cc libdhcpsrv_unittests_SOURCES += cfgmgr_unittest.cc triplet_unittest.cc libdhcpsrv_unittests_SOURCES += pool_unittest.cc subnet_unittest.cc libdhcpsrv_unittests_SOURCES += addr_utilities_unittest.cc +libdhcpsrv_unittests_SOURCES += memfile_lease_mgr.cc memfile_lease_mgr.h +libdhcpsrv_unittests_SOURCES += lease_mgr_unittest.cc libdhcpsrv_unittests_SOURCES += alloc_engine_unittest.cc - libdhcpsrv_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES) $(LOG4CPLUS_INCLUDES) libdhcpsrv_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS) libdhcpsrv_unittests_CXXFLAGS = $(AM_CXXFLAGS) libdhcpsrv_unittests_LDADD = $(GTEST_LDADD) libdhcpsrv_unittests_LDADD += $(top_builddir)/src/lib/exceptions/libb10-exceptions.la libdhcpsrv_unittests_LDADD += $(top_builddir)/src/lib/asiolink/libb10-asiolink.la +libdhcpsrv_unittests_LDADD += $(top_builddir)/src/lib/dhcp/libb10-dhcp++.la libdhcpsrv_unittests_LDADD += $(top_builddir)/src/lib/dhcp/libb10-dhcpsrv.la libdhcpsrv_unittests_LDADD += $(top_builddir)/src/lib/log/libb10-log.la diff --git a/src/lib/dhcp/tests/lease_mgr_unittest.cc b/src/lib/dhcp/tests/lease_mgr_unittest.cc index 35436ff3ad..8446160051 100644 --- a/src/lib/dhcp/tests/lease_mgr_unittest.cc +++ b/src/lib/dhcp/tests/lease_mgr_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2011-2012 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC") // // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above @@ -19,286 +19,13 @@ #include #include #include - -#include -#include -#include -#include -#include -#include +#include "memfile_lease_mgr.h" using namespace std; using namespace isc; using namespace isc::asiolink; using namespace isc::dhcp; - -using namespace boost; -using namespace boost::multi_index; - -// This is a concrete implementation of a Lease database. -// It does not do anything useful now, and is used for abstract LeaseMgr -// class testing. It may later evolve into more useful backend if the -// need arises. We can reuse code from memfile benchmark. See code in -// tests/tools/dhcp-ubench/memfile_bench.{cc|h} -class Memfile_LeaseMgr : public LeaseMgr { -public: - - /// @brief The sole lease manager constructor - /// - /// dbconfig is a generic way of passing parameters. Parameters - /// are passed in the "name=value" format, separated by spaces. - /// Values may be enclosed in double quotes, if needed. - /// - /// @param dbconfig database configuration - Memfile_LeaseMgr(const std::string& dbconfig); - - /// @brief Destructor (closes file) - virtual ~Memfile_LeaseMgr(); - - /// @brief Adds an IPv4 lease. - /// - /// @param lease lease to be added - virtual bool addLease(Lease4Ptr lease); - - /// @brief Adds an IPv6 lease. - /// - /// @param lease lease to be added - virtual bool addLease(Lease6Ptr lease); - - /// @brief Returns existing IPv4 lease for specified IPv4 address. - /// - /// @param addr address of the searched lease - /// - /// @return a collection of leases - virtual Lease4Ptr getLease4(isc::asiolink::IOAddress addr) const; - - /// @brief Returns existing IPv4 lease for specific address and subnet - /// @param addr address of the searched lease - /// @param subnet_id ID of the subnet the lease must belong to - /// - /// @return smart pointer to the lease (or NULL if a lease is not found) - virtual Lease4Ptr getLease4(isc::asiolink::IOAddress addr, - SubnetID subnet_id) const; - - /// @brief Returns existing IPv4 leases for specified hardware address. - /// - /// Although in the usual case there will be only one lease, for mobile - /// clients or clients with multiple static/fixed/reserved leases there - /// can be more than one. Thus return type is a container, not a single - /// pointer. - /// - /// @param hwaddr hardware address of the client - /// - /// @return lease collection - virtual Lease4Collection getLease4(const HWAddr& hwaddr) const; - - /// @brief Returns existing IPv4 leases for specified hardware address - /// and a subnet - /// - /// There can be at most one lease for a given HW address in a single - /// pool, so this method with either return a single lease or NULL. - /// - /// @param hwaddr hardware address of the client - /// @param subnet_id identifier of the subnet that lease must belong to - /// - /// @return a pointer to the lease (or NULL if a lease is not found) - virtual Lease4Ptr getLease4(const HWAddr& hwaddr, - SubnetID subnet_id) const; - - /// @brief Returns existing IPv4 lease for specified client-id - /// - /// @param clientid client identifier - virtual Lease4Collection getLease4(const ClientId& clientid) 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 - /// pool, so this method with either return a single lease or NULL. - /// - /// @param clientid client identifier - /// @param subnet_id identifier of the subnet that lease must belong to - /// - /// @return a pointer to the lease (or NULL if a lease is not found) - virtual Lease4Ptr getLease4(const ClientId& clientid, - SubnetID subnet_id) const; - - /// @brief Returns existing IPv6 lease for a given IPv6 address. - /// - /// @param addr address of the searched lease - /// - /// @return smart pointer to the lease (or NULL if a lease is not found) - Lease6Ptr getLease6(isc::asiolink::IOAddress addr) const; - - /// @brief Returns existing IPv6 lease for a given DUID+IA combination - /// - /// @param duid client DUID - /// @param iaid IA identifier - /// - /// @return collection of IPv6 leases - Lease6Collection getLease6(const DUID& duid, uint32_t iaid) const; - - /// @brief Returns existing IPv6 lease for a given DUID+IA combination - /// - /// @param duid client DUID - /// @param iaid IA identifier - /// @param subnet_id identifier of the subnet the lease must belong to - /// - /// @return smart pointer to the lease (or NULL if a lease is not found) - Lease6Ptr getLease6(const DUID& duid, uint32_t iaid, SubnetID subnet_id) const; - - /// @brief Updates IPv4 lease. - /// - /// @param lease4 The lease to be updated. - /// - /// If no such lease is present, an exception will be thrown. - void updateLease4(Lease4Ptr lease4); - - /// @brief Updates IPv4 lease. - /// - /// @param lease4 The lease to be updated. - /// - /// If no such lease is present, an exception will be thrown. - void updateLease6(Lease6Ptr lease6); - - /// @brief Deletes a lease. - /// - /// @param addr IPv4 address of the lease to be deleted. - /// - /// @return true if deletion was successful, false if no such lease exists - bool deleteLease4(uint32_t addr); - - /// @brief Deletes a lease. - /// - /// @param addr IPv4 address of the lease to be deleted. - /// - /// @return true if deletion was successful, false if no such lease exists - bool deleteLease6(isc::asiolink::IOAddress addr); - - /// @brief Returns backend name. - /// - /// Each backend have specific name, e.g. "mysql" or "sqlite". - std::string getName() const { return "memfile"; } - - /// @brief Returns description of the backend. - /// - /// This description may be multiline text that describes the backend. - std::string getDescription() const; - - /// @brief Returns backend version. - std::string getVersion() const { return "test-version"; } - - using LeaseMgr::getParameter; - -protected: - - typedef multi_index_container< // this is a multi-index container... - Lease6Ptr, // it will hold shared_ptr to leases6 - indexed_by< // and will be sorted by - - // IPv6 address that are unique. That particular key is a member - // of the Lease6 structure, is of type IOAddress and can be accessed - // by doing &Lease6::addr_ - ordered_unique< member > - > - > Lease6Storage; // Let the whole contraption be called Lease6Storage. - - Lease6Storage storage6_; -}; - -Memfile_LeaseMgr::Memfile_LeaseMgr(const std::string& dbconfig) - : LeaseMgr(dbconfig) { -} - -Memfile_LeaseMgr::~Memfile_LeaseMgr() { -} - -bool Memfile_LeaseMgr::addLease(Lease4Ptr) { - return (false); -} - -bool Memfile_LeaseMgr::addLease(Lease6Ptr lease) { - if (getLease6(lease->addr_)) { - // there is a lease with specified address already - return (false); - } - storage6_.insert(lease); - return (true); -} - -Lease4Ptr Memfile_LeaseMgr::getLease4(isc::asiolink::IOAddress) const { - return (Lease4Ptr()); -} - -Lease4Collection Memfile_LeaseMgr::getLease4(const HWAddr& ) const { - return (Lease4Collection()); -} - -Lease4Ptr Memfile_LeaseMgr::getLease4(isc::asiolink::IOAddress , - SubnetID) const { - return (Lease4Ptr()); -} - -Lease4Ptr Memfile_LeaseMgr::getLease4(const HWAddr&, - SubnetID) const { - return (Lease4Ptr()); -} - - -Lease4Ptr Memfile_LeaseMgr::getLease4(const ClientId&, - SubnetID) const { - return (Lease4Ptr()); -} - -Lease4Collection Memfile_LeaseMgr::getLease4(const ClientId& ) const { - return (Lease4Collection()); -} - -Lease6Ptr Memfile_LeaseMgr::getLease6(isc::asiolink::IOAddress addr) const { - Lease6Storage::iterator l = storage6_.find(addr); - if (l == storage6_.end()) { - return (Lease6Ptr()); - } else { - return (*l); - } -} - -Lease6Collection Memfile_LeaseMgr::getLease6(const DUID& , uint32_t ) const { - return (Lease6Collection()); -} - -Lease6Ptr Memfile_LeaseMgr::getLease6(const DUID&, uint32_t, - SubnetID) const { - - return (Lease6Ptr()); -} - -void Memfile_LeaseMgr::updateLease4(Lease4Ptr ) { -} - -void Memfile_LeaseMgr::updateLease6(Lease6Ptr ) { - -} - -bool Memfile_LeaseMgr::deleteLease4(uint32_t ) { - return (false); -} - -bool Memfile_LeaseMgr::deleteLease6(isc::asiolink::IOAddress addr) { - Lease6Storage::iterator l = storage6_.find(addr); - if (l == storage6_.end()) { - // no such lease - return (false); - } else { - storage6_.erase(l); - return (true); - } -} - -std::string Memfile_LeaseMgr::getDescription() const { - return (string("This is a dummy memfile backend implementation.\n" - "It does not offer any useful lease management and its only\n" - "purpose is to test abstract lease manager API.")); -} +using namespace isc::dhcp::test; // Memfile_LeaseMgr namespace { // empty class for now, but may be extended once Addr6 becomes bigger diff --git a/src/lib/dhcp/tests/memfile_lease_mgr.cc b/src/lib/dhcp/tests/memfile_lease_mgr.cc new file mode 100644 index 0000000000..d0a8b9994e --- /dev/null +++ b/src/lib/dhcp/tests/memfile_lease_mgr.cc @@ -0,0 +1,113 @@ +// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC") +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH +// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, +// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +// PERFORMANCE OF THIS SOFTWARE. + +#include "memfile_lease_mgr.h" + +using namespace isc::dhcp; +using namespace isc::dhcp::test; + +Memfile_LeaseMgr::Memfile_LeaseMgr(const std::string& dbconfig) + : LeaseMgr(dbconfig) { +} + +Memfile_LeaseMgr::~Memfile_LeaseMgr() { +} + +bool Memfile_LeaseMgr::addLease(Lease4Ptr) { + return (false); +} + +bool Memfile_LeaseMgr::addLease(Lease6Ptr lease) { + if (getLease6(lease->addr_)) { + // there is a lease with specified address already + return (false); + } + storage6_.insert(lease); + return (true); +} + +Lease4Ptr Memfile_LeaseMgr::getLease4(isc::asiolink::IOAddress) const { + return (Lease4Ptr()); +} + +Lease4Collection Memfile_LeaseMgr::getLease4(const HWAddr& ) const { + return (Lease4Collection()); +} + +Lease4Ptr Memfile_LeaseMgr::getLease4(isc::asiolink::IOAddress , + SubnetID) const { + return (Lease4Ptr()); +} + +Lease4Ptr Memfile_LeaseMgr::getLease4(const HWAddr&, + SubnetID) const { + return (Lease4Ptr()); +} + + +Lease4Ptr Memfile_LeaseMgr::getLease4(const ClientId&, + SubnetID) const { + return (Lease4Ptr()); +} + +Lease4Collection Memfile_LeaseMgr::getLease4(const ClientId& ) const { + return (Lease4Collection()); +} + +Lease6Ptr Memfile_LeaseMgr::getLease6(isc::asiolink::IOAddress addr) const { + Lease6Storage::iterator l = storage6_.find(addr); + if (l == storage6_.end()) { + return (Lease6Ptr()); + } else { + return (*l); + } +} + +Lease6Collection Memfile_LeaseMgr::getLease6(const DUID& , uint32_t ) const { + return (Lease6Collection()); +} + +Lease6Ptr Memfile_LeaseMgr::getLease6(const DUID&, uint32_t, + SubnetID) const { + + return (Lease6Ptr()); +} + +void Memfile_LeaseMgr::updateLease4(Lease4Ptr ) { +} + +void Memfile_LeaseMgr::updateLease6(Lease6Ptr ) { + +} + +bool Memfile_LeaseMgr::deleteLease4(uint32_t ) { + return (false); +} + +bool Memfile_LeaseMgr::deleteLease6(isc::asiolink::IOAddress addr) { + Lease6Storage::iterator l = storage6_.find(addr); + if (l == storage6_.end()) { + // no such lease + return (false); + } else { + storage6_.erase(l); + return (true); + } +} + +std::string Memfile_LeaseMgr::getDescription() const { + return (std::string("This is a dummy memfile backend implementation.\n" + "It does not offer any useful lease management and its only\n" + "purpose is to test abstract lease manager API.")); +} diff --git a/src/lib/dhcp/tests/memfile_lease_mgr.h b/src/lib/dhcp/tests/memfile_lease_mgr.h new file mode 100644 index 0000000000..d39e3eb1d8 --- /dev/null +++ b/src/lib/dhcp/tests/memfile_lease_mgr.h @@ -0,0 +1,206 @@ +// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC") +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH +// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, +// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +// PERFORMANCE OF THIS SOFTWARE. + +#ifndef MEMFILE_LEASE_MGR_H +#define MEMFILE_LEASE_MGR_H + +#include +#include +#include +#include +#include + +namespace isc { +namespace dhcp { +namespace test { + +// This is a concrete implementation of a Lease database. +// +// It is for testing purposes only. It is NOT a production code. +// +// It does not do anything useful now, and is used for abstract LeaseMgr +// class testing. It may later evolve into more useful backend if the +// need arises. We can reuse code from memfile benchmark. See code in +// tests/tools/dhcp-ubench/memfile_bench.{cc|h} +class Memfile_LeaseMgr : public LeaseMgr { +public: + + /// @brief The sole lease manager constructor + /// + /// dbconfig is a generic way of passing parameters. Parameters + /// are passed in the "name=value" format, separated by spaces. + /// Values may be enclosed in double quotes, if needed. + /// + /// @param dbconfig database configuration + Memfile_LeaseMgr(const std::string& dbconfig); + + /// @brief Destructor (closes file) + virtual ~Memfile_LeaseMgr(); + + /// @brief Adds an IPv4 lease. + /// + /// @param lease lease to be added + virtual bool addLease(Lease4Ptr lease); + + /// @brief Adds an IPv6 lease. + /// + /// @param lease lease to be added + virtual bool addLease(Lease6Ptr lease); + + /// @brief Returns existing IPv4 lease for specified IPv4 address. + /// + /// @param addr address of the searched lease + /// + /// @return a collection of leases + virtual Lease4Ptr getLease4(isc::asiolink::IOAddress addr) const; + + /// @brief Returns existing IPv4 lease for specific address and subnet + /// @param addr address of the searched lease + /// @param subnet_id ID of the subnet the lease must belong to + /// + /// @return smart pointer to the lease (or NULL if a lease is not found) + virtual Lease4Ptr getLease4(isc::asiolink::IOAddress addr, + SubnetID subnet_id) const; + + /// @brief Returns existing IPv4 leases for specified hardware address. + /// + /// Although in the usual case there will be only one lease, for mobile + /// clients or clients with multiple static/fixed/reserved leases there + /// can be more than one. Thus return type is a container, not a single + /// pointer. + /// + /// @param hwaddr hardware address of the client + /// + /// @return lease collection + virtual Lease4Collection getLease4(const HWAddr& hwaddr) const; + + /// @brief Returns existing IPv4 leases for specified hardware address + /// and a subnet + /// + /// There can be at most one lease for a given HW address in a single + /// pool, so this method with either return a single lease or NULL. + /// + /// @param hwaddr hardware address of the client + /// @param subnet_id identifier of the subnet that lease must belong to + /// + /// @return a pointer to the lease (or NULL if a lease is not found) + virtual Lease4Ptr getLease4(const HWAddr& hwaddr, + SubnetID subnet_id) const; + + /// @brief Returns existing IPv4 lease for specified client-id + /// + /// @param clientid client identifier + virtual Lease4Collection getLease4(const ClientId& clientid) 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 + /// pool, so this method with either return a single lease or NULL. + /// + /// @param clientid client identifier + /// @param subnet_id identifier of the subnet that lease must belong to + /// + /// @return a pointer to the lease (or NULL if a lease is not found) + virtual Lease4Ptr getLease4(const ClientId& clientid, + SubnetID subnet_id) const; + + /// @brief Returns existing IPv6 lease for a given IPv6 address. + /// + /// @param addr address of the searched lease + /// + /// @return smart pointer to the lease (or NULL if a lease is not found) + Lease6Ptr getLease6(isc::asiolink::IOAddress addr) const; + + /// @brief Returns existing IPv6 lease for a given DUID+IA combination + /// + /// @param duid client DUID + /// @param iaid IA identifier + /// + /// @return collection of IPv6 leases + Lease6Collection getLease6(const DUID& duid, uint32_t iaid) const; + + /// @brief Returns existing IPv6 lease for a given DUID+IA combination + /// + /// @param duid client DUID + /// @param iaid IA identifier + /// @param subnet_id identifier of the subnet the lease must belong to + /// + /// @return smart pointer to the lease (or NULL if a lease is not found) + Lease6Ptr getLease6(const DUID& duid, uint32_t iaid, SubnetID subnet_id) const; + + /// @brief Updates IPv4 lease. + /// + /// @param lease4 The lease to be updated. + /// + /// If no such lease is present, an exception will be thrown. + void updateLease4(Lease4Ptr lease4); + + /// @brief Updates IPv4 lease. + /// + /// @param lease4 The lease to be updated. + /// + /// If no such lease is present, an exception will be thrown. + void updateLease6(Lease6Ptr lease6); + + /// @brief Deletes a lease. + /// + /// @param addr IPv4 address of the lease to be deleted. + /// + /// @return true if deletion was successful, false if no such lease exists + bool deleteLease4(uint32_t addr); + + /// @brief Deletes a lease. + /// + /// @param addr IPv4 address of the lease to be deleted. + /// + /// @return true if deletion was successful, false if no such lease exists + bool deleteLease6(isc::asiolink::IOAddress addr); + + /// @brief Returns backend name. + /// + /// Each backend have specific name, e.g. "mysql" or "sqlite". + std::string getName() const { return "memfile"; } + + /// @brief Returns description of the backend. + /// + /// This description may be multiline text that describes the backend. + std::string getDescription() const; + + /// @brief Returns backend version. + std::string getVersion() const { return "test-version"; } + + using LeaseMgr::getParameter; + +protected: + + typedef boost::multi_index_container< // this is a multi-index container... + Lease6Ptr, // it will hold shared_ptr to leases6 + boost::multi_index::indexed_by< // and will be sorted by + // IPv6 address that are unique. That particular key is a member + // of the Lease6 structure, is of type IOAddress and can be accessed + // by doing &Lease6::addr_ + boost::multi_index::ordered_unique< + boost::multi_index::member + > + > + > Lease6Storage; // Let the whole contraption be called Lease6Storage. + + Lease6Storage storage6_; +}; + +}; // end of isc::dhcp::test namespace +}; // end of isc::dhcp namespace +}; // end of isc namespace + +#endif // MEMFILE_LEASE_MGR_H