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

Additional Inherited Members

- Public Types inherited from isc::util::VersionedCSVFile
enum  InputSchemaState { CURRENT, NEEDS_UPGRADE, NEEDS_DOWNGRADE }
 Possible input file schema states. More...
 
- Static Public Member Functions inherited from isc::util::CSVFile
static CSVRow EMPTY_ROW ()
 Represents empty row. More...
 
- Protected Member Functions inherited from isc::util::VersionedCSVFile
void columnCountError (const CSVRow &row, const std::string &reason)
 Convenience method for adding an error message. More...
 
virtual bool validateHeader (const CSVRow &header)
 Validates the header of a VersionedCSVFile. More...
 
- 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. More...
 
virtual bool validate (const CSVRow &row)
 Validate the row read from a file. More...
 
- Protected Attributes inherited from isc::dhcp::LeaseFileStats
uint32_t read_errs_
 Number of errors when reading. More...
 
uint32_t read_leases_
 Number of leases read. More...
 
uint32_t reads_
 Number of attempts to read a lease. More...
 
uint32_t write_errs_
 Number of errors when writing. More...
 
uint32_t write_leases_
 Number of lease written. More...
 
uint32_t writes_
 Number of attempts to write a lease. More...
 

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).

Todo:
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 (see http://oldkea.isc.org/ticket/2405). However, when #2405 is implemented, the next function may need to be updated to use the validation capability of Lease4.

Definition at line 34 of file csv_lease_file4.h.

Constructor & Destructor Documentation

◆ CSVLeaseFile4()

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

Constructor.

Initializes columns of the lease file.

Parameters
filenameName of the lease file.

Definition at line 18 of file csv_lease_file4.cc.

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.

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

Definition at line 33 of file csv_lease_file4.cc.

References isc::dhcp::Lease::addr_, isc::dhcp::Lease::basicStatesToText(), isc::dhcp::Lease4::client_id_, isc::dhcp::Lease::cltt_, isc::dhcp::Lease::fqdn_fwd_, isc::dhcp::Lease::fqdn_rev_, isc::util::CSVFile::getColumnCount(), isc::util::CSVFile::getColumnIndex(), isc::data::UserContext::getContext(), isc::dhcp::Lease::hostname_, isc::dhcp::Lease::hwaddr_, isc_throw, isc::dhcp::Lease::state_, isc::dhcp::Lease::STATE_DECLINED, isc::dhcp::Lease::subnet_id_, isc::asiolink::IOAddress::toText(), isc::dhcp::Lease::valid_lft_, isc::dhcp::LeaseFileStats::write_errs_, isc::dhcp::LeaseFileStats::write_leases_, isc::util::CSVRow::writeAt(), isc::util::CSVRow::writeAtEscaped(), 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.

Parameters
[out]leasePointer to the lease read from CSV file or NULL pointer if lease hasn't been read.
Returns
Boolean value indicating that the new lease has been read from the CSV file (if true), or that the error has occurred (false).
Todo:
Make sure that the values read from the file are correct. The appropriate Lease4 validation mechanism should be used once ticket http://oldkea.isc.org/ticket/2405 is implemented.

Definition at line 87 of file csv_lease_file4.cc.

References isc::util::VersionedCSVFile::addColumn(), isc::dhcp::Lease::basicStatesToText(), isc::dhcp::HWAddr::fromText(), isc::dhcp::ClientId::fromText(), isc::util::CSVFile::getColumnIndex(), isc::dhcp::HWAddr::hwaddr_, isc_throw, isc::dhcp::LeaseFileStats::read_errs_, isc::dhcp::LeaseFileStats::read_leases_, isc::util::CSVRow::readAndConvertAt(), isc::util::CSVRow::readAt(), isc::util::CSVRow::readAtEscaped(), isc::dhcp::LeaseFileStats::reads_, isc::util::VersionedCSVFile::setMinimumValidColumns(), 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)
virtual

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 csv_lease_file4.cc.

References isc::dhcp::LeaseFileStats::clearStatistics().

+ Here is the call graph for this function:

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