diff --git a/src/lib/dhcpsrv/csv_lease_file6.cc b/src/lib/dhcpsrv/csv_lease_file6.cc index 91578e779d..7157e2e79c 100644 --- a/src/lib/dhcpsrv/csv_lease_file6.cc +++ b/src/lib/dhcpsrv/csv_lease_file6.cc @@ -205,7 +205,22 @@ CSVLeaseFile6::readHWAddr(const CSVRow& row) { return (HWAddrPtr()); } +} +bool +CSVLeaseFile6::validateHeader(const isc::util::CSVRow& header) { + + if (!CSVFile::validateHeader(header)) { + + // One possible validation failure is that we're reading Kea 0.9 + // lease file that didn't have hwaddr column. Let's add it and + // try to revalidate. + isc::util::CSVRow copy = header; + copy.append("hwaddr"); + return CSVFile::validateHeader(copy); + } else { + return (true); + } } diff --git a/src/lib/dhcpsrv/csv_lease_file6.h b/src/lib/dhcpsrv/csv_lease_file6.h index 33e9ba3264..9cd8de497e 100644 --- a/src/lib/dhcpsrv/csv_lease_file6.h +++ b/src/lib/dhcpsrv/csv_lease_file6.h @@ -77,6 +77,18 @@ public: /// ticket http://kea.isc.org/ticket/2405 is implemented. bool next(Lease6Ptr& lease); +protected: + /// @brief This function validates the header of the Lease6 CSV file. + /// + /// It works similar to @c CSVFile::validateHeader, but if the validation + /// fails, it attempts to add hwaddr column and retry validation. + /// That's useful when attmepting to read CSV file generated in 0.9 + /// (did not have hwaddr field) in 0.9.1 or later code. + /// + /// @param header A row holding a header. + /// @return true if header matches the columns; false otherwise. + virtual bool validateHeader(const isc::util::CSVRow& header); + private: /// @brief Initializes columns of the CSV file holding leases. diff --git a/src/lib/util/csv_file.cc b/src/lib/util/csv_file.cc index d62acc66b3..7b29a38bf1 100644 --- a/src/lib/util/csv_file.cc +++ b/src/lib/util/csv_file.cc @@ -289,15 +289,8 @@ CSVFile::open() { // Check the header against the columns specified for the CSV file. if (!validateHeader(header)) { - // One possible validation failure is that we're reading Kea 0.9 - // lease file that didn't have hwaddr column. Let's add it and - // try to revalidate. - header.append("hwaddr"); - - if (!validateHeader(header)) { - isc_throw(CSVFileError, "invalid header '" << header - << "' in CSV file '" << filename_ << "'"); - } + isc_throw(CSVFileError, "invalid header '" << header + << "' in CSV file '" << filename_ << "'"); } // Everything is good, so if we haven't added any columns yet, diff --git a/src/lib/util/csv_file.h b/src/lib/util/csv_file.h index 561d182262..123948f8bd 100644 --- a/src/lib/util/csv_file.h +++ b/src/lib/util/csv_file.h @@ -444,7 +444,7 @@ protected: /// @return true if the column is valid; false otherwise. virtual bool validate(const CSVRow& row); -private: +protected: /// @brief This function validates the header of the CSV file. /// @@ -452,12 +452,16 @@ private: /// compare that they exactly match (including order) the header read /// from the file. /// - /// This function is called internally by @CSVFile::open. + /// This function is called internally by @CSVFile::open. Derived classes + /// may add extra validation steps. + /// + /// @todo There should be a support for optional columns (see ticket #3626). /// /// @param header A row holding a header. /// @return true if header matches the columns; false otherwise. - bool validateHeader(const CSVRow& header); + virtual bool validateHeader(const CSVRow& header); +private: /// @brief Sanity check if stream is open. /// /// Checks if the file stream is open so as IO operations can be performed