]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[4303] Added class holding configuration of host identifiers.
authorMarcin Siodelski <marcin@isc.org>
Wed, 13 Apr 2016 10:23:14 +0000 (12:23 +0200)
committerMarcin Siodelski <marcin@isc.org>
Wed, 13 Apr 2016 10:23:14 +0000 (12:23 +0200)
The identifiers are held on an ordered list and the server will
use them to search for host reservations for each client using
this order.

src/lib/dhcpsrv/Makefile.am
src/lib/dhcpsrv/cfg_host_reservations.cc [new file with mode: 0644]
src/lib/dhcpsrv/cfg_host_reservations.h [new file with mode: 0644]
src/lib/dhcpsrv/tests/Makefile.am
src/lib/dhcpsrv/tests/cfg_host_reservations_unittest.cc [new file with mode: 0644]

index acc8e879bd6525499cd6341c6f617140198734ea..f6a364737c82753909c945afcd6ea5544ede4dfd 100755 (executable)
@@ -90,6 +90,7 @@ libkea_dhcpsrv_la_SOURCES += cfg_duid.cc cfg_duid.h
 libkea_dhcpsrv_la_SOURCES += cfg_hosts.cc cfg_hosts.h
 libkea_dhcpsrv_la_SOURCES += cfg_iface.cc cfg_iface.h
 libkea_dhcpsrv_la_SOURCES += cfg_expiration.cc cfg_expiration.h
+libkea_dhcpsrv_la_SOURCES += cfg_host_reservations.cc cfg_host_reservations.h
 libkea_dhcpsrv_la_SOURCES += cfg_option.cc cfg_option.h
 libkea_dhcpsrv_la_SOURCES += cfg_option_def.cc cfg_option_def.h
 libkea_dhcpsrv_la_SOURCES += cfg_rsoo.cc cfg_rsoo.h
diff --git a/src/lib/dhcpsrv/cfg_host_reservations.cc b/src/lib/dhcpsrv/cfg_host_reservations.cc
new file mode 100644 (file)
index 0000000..25bf62c
--- /dev/null
@@ -0,0 +1,35 @@
+// Copyright (C) 2016 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
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#include <exceptions/exceptions.h>
+#include <dhcpsrv/cfg_host_reservations.h>
+#include <algorithm>
+
+namespace isc {
+namespace dhcp {
+
+CfgHostReservations::CfgHostReservations()
+    : identifier_types_() {
+}
+
+void
+CfgHostReservations::addIdentifierType(const std::string& identifier_name) {
+    Host::IdentifierType identifier_type = Host::getIdentifierType(identifier_name);
+    if (std::find(identifier_types_.begin(), identifier_types_.end(),
+                  identifier_type) != identifier_types_.end()) {
+        isc_throw(isc::BadValue, "invalid host identifier name '"
+                  << identifier_name << "'");
+    }
+    identifier_types_.push_back(identifier_type);
+}
+
+void
+CfgHostReservations::clear() {
+    identifier_types_.clear();
+}
+
+}
+}
diff --git a/src/lib/dhcpsrv/cfg_host_reservations.h b/src/lib/dhcpsrv/cfg_host_reservations.h
new file mode 100644 (file)
index 0000000..0795eaf
--- /dev/null
@@ -0,0 +1,79 @@
+// Copyright (C) 2016 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
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#ifndef CFG_HOST_RESERVATIONS_H
+#define CFG_HOST_RESERVATIONS_H
+
+#include <dhcpsrv/host.h>
+#include <boost/shared_ptr.hpp>
+#include <list>
+#include <string>
+
+namespace isc {
+namespace dhcp {
+
+/// @brief Represents global configuration for host reservations.
+///
+/// This class represents server configuration pertaining to host
+/// reservations.
+///
+/// Currently it only holds the ordered list of host identifiers
+/// to be used to search for reservations for a particular host.
+/// An administrator selects which identifiers the server should
+/// use and in which order to search for host reservations to
+/// optimize performance of the server.
+class CfgHostReservations {
+public:
+
+    /// @brief Type of the container holding ordered list of identifiers.
+    typedef std::list<Host::IdentifierType> IdentifierTypes;
+
+    /// @brief Constructor.
+    ///
+    /// The default confguration:
+    /// - no identifiers selected for host reservations searches.
+    CfgHostReservations();
+
+    /// @brief Adds new identifier type to a collection of identifiers
+    /// to be used by the server to search for host reservations.
+    ///
+    /// @param identifier_type Name of the identifier to be added. It
+    /// must be one of the names supported by the @ref Host::getIdentifierType
+    /// function.
+    void addIdentifierType(const std::string& identifier_name);
+
+    /// @brief Returns const reference to ordered collection of identifiers
+    /// to be used by the server to search for host reservations.
+    const IdentifierTypes& getIdentifierTypes() const {
+        return (identifier_types_);
+    }
+
+    /// @brief Restores default configuration.
+    void clear();
+
+private:
+
+    /// @brief Holds ordered collection of identifiers to be used by the
+    /// server to search for host reservations for a client.
+    IdentifierTypes identifier_types_;
+
+};
+
+/// @name Pointers to the @ref CfgHostReservations objects.
+//@{
+/// @brief Pointer to the Non-const object.
+typedef boost::shared_ptr<CfgHostReservations> CfgHostReservationsPtr;
+
+/// @brief Pointer to the const object.
+typedef boost::shared_ptr<const CfgHostReservations>
+ConstCfgHostReservationsPtr;
+
+//@}
+
+}
+}
+
+#endif // CFG_HOST_RESERVATIONS_H
index 86890aa6d43a0562631b8faecaf92c01c08811dc..517079bfabe310f15f0bf43a98ecf64ca307c62a 100755 (executable)
@@ -68,6 +68,7 @@ libdhcpsrv_unittests_SOURCES += callout_handle_store_unittest.cc
 libdhcpsrv_unittests_SOURCES += cfg_db_access_unittest.cc
 libdhcpsrv_unittests_SOURCES += cfg_duid_unittest.cc
 libdhcpsrv_unittests_SOURCES += cfg_expiration_unittest.cc
+libdhcpsrv_unittests_SOURCES += cfg_host_reservations_unittest.cc
 libdhcpsrv_unittests_SOURCES += cfg_hosts_unittest.cc
 libdhcpsrv_unittests_SOURCES += cfg_iface_unittest.cc
 libdhcpsrv_unittests_SOURCES += cfg_mac_source_unittest.cc
diff --git a/src/lib/dhcpsrv/tests/cfg_host_reservations_unittest.cc b/src/lib/dhcpsrv/tests/cfg_host_reservations_unittest.cc
new file mode 100644 (file)
index 0000000..9b15288
--- /dev/null
@@ -0,0 +1,63 @@
+// Copyright (C) 2016 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
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#include <config.h>
+#include <dhcp/dhcp6.h>
+#include <dhcpsrv/cfg_host_reservations.h>
+#include <dhcpsrv/host.h>
+#include <gtest/gtest.h>
+#include <algorithm>
+
+using namespace isc;
+using namespace isc::dhcp;
+
+namespace {
+
+/// @brief Checks if specified identifier has been added.
+///
+/// @param cfg Object holding current configuration.
+/// @param id Identifier type which presence should be checked.
+bool
+identifierAdded(const CfgHostReservations& cfg, const Host::IdentifierType& id) {
+    CfgHostReservations::IdentifierTypes types = cfg.getIdentifierTypes();
+    return (std::find(types.begin(), types.end(), id) != types.end());
+}
+
+// This test checks default configuration.
+TEST(CfgHostReservationsTest, defaults) {
+    CfgHostReservations cfg;
+    EXPECT_TRUE(cfg.getIdentifierTypes().empty());
+}
+
+// This test verifies that identifier types can be added into an
+// ordered collection and then removed.
+TEST(CfgHostReservationsTest, addIdentifier) {
+    CfgHostReservations cfg;
+
+    // Only HW address added.
+    ASSERT_NO_THROW(cfg.addIdentifierType("hw-address"));
+    EXPECT_TRUE(identifierAdded(cfg, Host::IDENT_HWADDR));
+    EXPECT_FALSE(identifierAdded(cfg, Host::IDENT_DUID));
+    EXPECT_FALSE(identifierAdded(cfg, Host::IDENT_CIRCUIT_ID));
+
+    // HW address and DUID should be present.
+    ASSERT_NO_THROW(cfg.addIdentifierType("duid"));
+    EXPECT_TRUE(identifierAdded(cfg, Host::IDENT_HWADDR));
+    EXPECT_TRUE(identifierAdded(cfg, Host::IDENT_DUID));
+    EXPECT_FALSE(identifierAdded(cfg, Host::IDENT_CIRCUIT_ID));
+
+    // All three identifiers should be present now.
+    ASSERT_NO_THROW(cfg.addIdentifierType("circuit-id"));
+    EXPECT_TRUE(identifierAdded(cfg, Host::IDENT_HWADDR));
+    EXPECT_TRUE(identifierAdded(cfg, Host::IDENT_DUID));
+    EXPECT_TRUE(identifierAdded(cfg, Host::IDENT_CIRCUIT_ID));
+
+    // Let's clear and make sure no identifiers are present.
+    ASSERT_NO_THROW(cfg.clear());
+    EXPECT_TRUE(cfg.getIdentifierTypes().empty());
+}
+
+} // end of anonymous namespace