Kea 2.7.0
isc::dhcp::CSVLeaseFile4 Class Reference

Provides methods to access CSV file with DHCPv4 leases. More...

#include <csv_lease_file4.h>

+ Inheritance diagram for isc::dhcp::CSVLeaseFile4:

Public Member Functions

 CSVLeaseFile4 (const std::string &filename)
void append (const Lease4 &lease)
 Appends the lease record to the CSV file.
bool next (Lease4Ptr &lease)
 Reads next lease from the CSV file.
virtual void open (const bool seek_to_end=false)
 Opens a lease file.
- Public Member Functions inherited from isc::util::VersionedCSVFile
 VersionedCSVFile (const std::string &filename)
virtual ~VersionedCSVFile ()
void addColumn (const std::string &col_name, const std::string &version, const std::string &default_value="")
 Adds metadata for a single column to the schema.
size_t getInputHeaderCount () const
 Returns the number of columns found in the input header.
enum InputSchemaState getInputSchemaState () const
 Fetches the state of the input file's schema.
std::string getInputSchemaVersion () const
 Returns the schema version of the physical file.
size_t getMinimumValidColumns () const
 Returns the minimum number of columns which must be present for the file to be considered valid.
std::string getSchemaVersion () const
 text version of current schema supported by the file's metadata
size_t getValidColumnCount () const
 Returns the number of valid columns found in the header For newly created files this will always match the number of defined columns (i.e.
const VersionedColumnPtrgetVersionedColumn (const size_t index) const
 Fetch the column descriptor for a given index.
bool needsConversion () const
 Returns true if the input file schema state is not CURRENT.
bool next (CSVRow &row)
 Reads next row from the file file.
virtual void recreate ()
 Creates a new CSV file.
void setMinimumValidColumns (const std::string &column_name)
 Sets the minimum number of valid columns based on a given column.
- Public Member Functions inherited from isc::util::CSVFile
 CSVFile (const std::string &filename)
virtual ~CSVFile ()
void addColumn (const std::string &col_name)
 Adds new column name.
void append (const CSVRow &row) const
 Writes the CSV row into the file.
void close ()
 Closes the CSV file.
bool exists () const
 Checks if the CSV file exists and can be opened for reading.
void flush () const
 Flushes a file.
size_t getColumnCount () const
 Returns the number of columns in the file.
size_t getColumnIndex (const std::string &col_name) const
 Returns the index of the column having specified name.
std::string getColumnName (const size_t col_index) const
 Returns the name of the column.
std::string getFilename () const
 Returns the path to the CSV file.
std::string getReadMsg () const
 Returns the description of the last error returned by the CSVFile::next function.
bool next (CSVRow &row, const bool skip_validation=false)
 Reads next row from CSV file.
void setReadMsg (const std::string &read_msg)
 Sets error message after row validation.
- Public Member Functions inherited from isc::dhcp::LeaseFileStats
 LeaseFileStats ()
 ~LeaseFileStats ()
void clearStatistics ()
 Clears the statistics.
uint32_t getReadErrs () const
 Gets the number of errors when reading leases.
uint32_t getReadLeases () const
 Gets the number of leases read.
uint32_t getReads () const
 Gets the number of attempts to read a lease.
uint32_t getWriteErrs () const
 Gets the number of errors when writing leases.
uint32_t getWriteLeases () const
 Gets the number of leases written.
uint32_t getWrites () const
 Gets the number of attempts to write a lease.

Additional Inherited Members

- Public Types inherited from isc::util::VersionedCSVFile
 Possible input file schema states. More...
- Static Public Member Functions inherited from isc::util::CSVFile
static CSVRow EMPTY_ROW ()
 Represents empty row.
- Protected Member Functions inherited from isc::util::VersionedCSVFile
void columnCountError (const CSVRow &row, const std::string &reason)
 Convenience method for adding an error message.
virtual bool validateHeader (const CSVRow &header)
 Validates the header of a VersionedCSVFile.
- Protected Member Functions inherited from isc::util::CSVFile
void addColumnInternal (const std::string &col_name)
 Adds a column regardless if the file is open or not.
virtual bool validate (const CSVRow &row)
 Validate the row read from a file.
- Protected Attributes inherited from isc::dhcp::LeaseFileStats
uint32_t read_errs_
 Number of errors when reading.
uint32_t read_leases_
 Number of leases read.
uint32_t reads_
 Number of attempts to read a lease.
uint32_t write_errs_
 Number of errors when writing.
uint32_t write_leases_
 Number of lease written.
uint32_t writes_
 Number of attempts to write a lease.

Detailed Description

Provides methods to access CSV file with DHCPv4 leases.

This class contains methods customized to read and write DHCPv4 leases from and to the CSV file. It expects that the CSV file being parsed contains a set of columns with well known names (initialized in the class constructor).

This class doesn't validate the lease values read from the file. The Lease4 is a structure that should be itself responsible for this validation. However, the next function may need to be updated to use the validation capability of Lease4.

Definition at line 33 of file csv_lease_file4.h.

Constructor & Destructor Documentation

◆ CSVLeaseFile4()

isc::dhcp::CSVLeaseFile4::CSVLeaseFile4 ( const std::string & filename)


Initializes columns of the lease file.

filenameName of the lease file.

Definition at line 18 of file

Member Function Documentation

◆ append()

void isc::dhcp::CSVLeaseFile4::append ( const Lease4 & lease)

Appends the lease record to the CSV file.

This function doesn't throw exceptions itself. In theory, exceptions are possible when the index of the indexes of the values being written to the file are invalid. However, this would have been a programming error.

leaseStructure representing a DHCPv4 lease.
BadValueif the lease has no hardware address, no client id and is not in STATE_DECLINED.

Definition at line 33 of file

References isc::dhcp::Lease::basicStatesToText(), isc::util::CSVFile::getColumnCount(), isc::util::CSVFile::getColumnIndex(), isc_throw, isc::dhcp::Lease::STATE_DECLINED, isc::dhcp::LeaseFileStats::write_errs_, isc::dhcp::LeaseFileStats::write_leases_, and isc::dhcp::LeaseFileStats::writes_.

+ Here is the call graph for this function:

◆ next()

bool isc::dhcp::CSVLeaseFile4::next ( Lease4Ptr & lease)

Reads next lease from the CSV file.

If this function hits an error during lease read, it sets the error message using CSVFile::setReadMsg and returns false. The error string may be read using CSVFile::getReadMsg.

Treats rows without a hardware address or a client id when their state is not STATE_DECLINED as an error.

This function is exception safe.

[out]leasePointer to the lease read from CSV file or NULL pointer if lease hasn't been read.
Boolean value indicating that the new lease has been read from the CSV file (if true), or that the error has occurred (false).
Make sure that the values read from the file are correct. The appropriate Lease4 validation mechanism should be used.

Definition at line 87 of file

References isc::dhcp::Lease::basicStatesToText(), isc::util::CSVFile::EMPTY_ROW(), isc::dhcp::HWAddr::hwaddr_, isc_throw, isc::util::VersionedCSVFile::next(), isc::dhcp::LeaseFileStats::read_errs_, isc::dhcp::LeaseFileStats::read_leases_, isc::dhcp::LeaseFileStats::reads_, isc::util::CSVFile::setReadMsg(), isc::dhcp::Lease::STATE_DECLINED, and isc::asiolink::IOAddress::toText().

+ Here is the call graph for this function:

◆ open()

void isc::dhcp::CSVLeaseFile4::open ( const bool seek_to_end = false)

Opens a lease file.

This function calls the base class open to do the work of opening a file. It is used to clear any statistics associated with any previous use of the file While it doesn't throw any exceptions of its own the base class may do so.

Reimplemented from isc::util::VersionedCSVFile.

Definition at line 24 of file

References isc::dhcp::LeaseFileStats::clearStatistics(), and isc::util::VersionedCSVFile::open().

+ Here is the call graph for this function:

The documentation for this class was generated from the following files: