Browse Source

[4303] Added class holding configuration of host identifiers.

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.
Marcin Siodelski 9 years ago
parent
commit
af0439c035

+ 1 - 0
src/lib/dhcpsrv/Makefile.am

@@ -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

+ 35 - 0
src/lib/dhcpsrv/cfg_host_reservations.cc

@@ -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();
+}
+
+}
+}

+ 79 - 0
src/lib/dhcpsrv/cfg_host_reservations.h

@@ -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

+ 1 - 0
src/lib/dhcpsrv/tests/Makefile.am

@@ -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

+ 63 - 0
src/lib/dhcpsrv/tests/cfg_host_reservations_unittest.cc

@@ -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