Browse Source

[3562] Basic implementation of the HostReservationParser class.

Marcin Siodelski 10 years ago
parent
commit
8662473294

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

@@ -62,6 +62,7 @@ libkea_dhcpsrv_la_SOURCES += dhcp_config_parser.h
 libkea_dhcpsrv_la_SOURCES += dhcp_parsers.cc dhcp_parsers.h
 libkea_dhcpsrv_la_SOURCES += host.cc host.h
 libkea_dhcpsrv_la_SOURCES += host_container.h
+libkea_dhcpsrv_la_SOURCES += host_reservation_parser.cc host_reservation_parser.h
 libkea_dhcpsrv_la_SOURCES += key_from_key.h
 libkea_dhcpsrv_la_SOURCES += lease.cc lease.h
 libkea_dhcpsrv_la_SOURCES += lease_mgr.cc lease_mgr.h

+ 61 - 0
src/lib/dhcpsrv/host_reservation_parser.cc

@@ -0,0 +1,61 @@
+// Copyright (C) 2014 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 <asiolink/io_address.h>
+#include <dhcpsrv/host_reservation_parser.h>
+#include <boost/foreach.hpp>
+#include <string>
+
+using namespace isc::asiolink;
+
+namespace isc {
+namespace dhcp {
+
+HostReservationParser::HostReservationParser()
+    : DhcpConfigParser() {
+}
+
+void
+HostReservationParser::build(isc::data::ConstElementPtr reservation_data) {
+    std::string identifier;
+    std::string identifier_name;
+    std::string hostname;
+
+    BOOST_FOREACH(ConfigPair element, reservation_data->mapValue()) {
+        try {
+            if (element.first == "hw-address" || element.first == "duid") {
+                if (!identifier_name.empty()) {
+                    isc_throw(DhcpConfigError, "the 'hw-address' and 'duid'"
+                              " parameters are mutually exclusive");
+                }
+                identifier = element.second->stringValue();
+                identifier_name = element.first;
+
+            } else if (element.first == "hostname") {
+                hostname = element.second->stringValue();
+
+            }
+        } catch (const std::exception& ex) {
+            // Append line number where the error occurred.
+            isc_throw(DhcpConfigError, ex.what() << " ("
+                      << element.second->getPosition() << ")");
+        }
+    }
+
+    host_.reset(new Host(identifier, identifier_name, SubnetID(0), SubnetID(0),
+                         IOAddress("0.0.0.0"), hostname));
+}
+
+} // end of namespace isc::dhcp
+} // end of namespace isc

+ 54 - 0
src/lib/dhcpsrv/host_reservation_parser.h

@@ -0,0 +1,54 @@
+// Copyright (C) 2014 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 HOST_RESERVATION_PARSER_H
+#define HOST_RESERVATION_PARSER_H
+
+#include <cc/data.h>
+#include <dhcpsrv/dhcp_config_parser.h>
+#include <dhcpsrv/host.h>
+
+namespace isc {
+namespace dhcp {
+
+/// @brief Parser for a single host reservation entry.
+class HostReservationParser : public DhcpConfigParser {
+public:
+
+    /// @brief Default constructor.
+    HostReservationParser();
+
+    /// @brief Parses a single entry for host reservation.
+    ///
+    /// @param reservation_data Data element holding map with a host
+    /// reservation configuration.
+    ///
+    /// @throw DhcpConfigError If the configuration is invalid.
+    virtual void build(isc::data::ConstElementPtr reservation_data);
+
+    /// @brief Commit, unused.
+    virtual void commit() { }
+
+protected:
+
+    /// @brief Holds a pointer to @c Host object representing a parsed
+    /// host reservation configuration.
+    HostPtr host_;
+
+};
+
+}
+} // end of namespace isc
+
+#endif // HOST_RESERVATION_PARSER_H

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

@@ -67,6 +67,7 @@ libdhcpsrv_unittests_SOURCES += d2_udp_unittest.cc
 libdhcpsrv_unittests_SOURCES += daemon_unittest.cc
 libdhcpsrv_unittests_SOURCES += dbaccess_parser_unittest.cc
 libdhcpsrv_unittests_SOURCES += host_unittest.cc
+libdhcpsrv_unittests_SOURCES += host_reservation_parser_unittest.cc
 libdhcpsrv_unittests_SOURCES += lease_file_io.cc lease_file_io.h
 libdhcpsrv_unittests_SOURCES += lease_unittest.cc
 libdhcpsrv_unittests_SOURCES += lease_mgr_factory_unittest.cc

+ 68 - 0
src/lib/dhcpsrv/tests/host_reservation_parser_unittest.cc

@@ -0,0 +1,68 @@
+// Copyright (C) 2014 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 <config.h>
+
+#include <cc/data.h>
+#include <dhcpsrv/cfgmgr.h>
+#include <dhcpsrv/host.h>
+#include <dhcpsrv/host_reservation_parser.h>
+#include <dhcpsrv/testutils/config_result_check.h>
+#include <gtest/gtest.h>
+#include <string>
+
+using namespace isc::data;
+using namespace isc::dhcp;
+
+namespace {
+
+/// @brief Test fixture class for @c HostReservationParser.
+class HostReservationParserTest : public ::testing::Test {
+protected:
+
+    /// @brief Setup for each test.
+    ///
+    /// Clears the configuration in the @c CfgMgr.
+    virtual void SetUp();
+
+    /// @brief Cleans up after each test.
+    ///
+    /// Clears the configuration in the @c CfgMgr.
+    virtual void TearDown();
+
+};
+
+void
+HostReservationParserTest::SetUp() {
+    CfgMgr::instance().clear();
+}
+
+void
+HostReservationParserTest::TearDown() {
+    CfgMgr::instance().clear();
+}
+
+// This test verfies that the parser can parse the reservation entry for
+// which hw-address is a host identifier.
+TEST_F(HostReservationParserTest, hwaddr) {
+    std::string config = "{ \"hw-address\": \"01:02:03:04:05:06\","
+        "\"hostname\": \"\" }";
+
+    ElementPtr config_element = Element::fromJSON(config);
+
+    HostReservationParser parser;
+    ASSERT_NO_THROW(parser.build(config_element));
+}
+
+} // end of anonymous namespace