csv_lease_file4.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef CSV_LEASE_FILE4_H
  7. #define CSV_LEASE_FILE4_H
  8. #include <asiolink/io_address.h>
  9. #include <dhcp/duid.h>
  10. #include <dhcpsrv/lease.h>
  11. #include <dhcpsrv/subnet.h>
  12. #include <dhcpsrv/lease_file_stats.h>
  13. #include <util/versioned_csv_file.h>
  14. #include <stdint.h>
  15. #include <string>
  16. #include <time.h>
  17. namespace isc {
  18. namespace dhcp {
  19. /// @brief Provides methods to access CSV file with DHCPv4 leases.
  20. ///
  21. /// This class contains methods customized to read and write DHCPv4 leases from
  22. /// and to the CSV file. It expects that the CSV file being parsed contains a
  23. /// set of columns with well known names (initialized in the class constructor).
  24. ///
  25. /// @todo This class doesn't validate the lease values read from the file.
  26. /// The @c Lease4 is a structure that should be itself responsible for this
  27. /// validation (see http://kea.isc.org/ticket/2405). However, when #2405
  28. /// is implemented, the @c next function may need to be updated to use the
  29. /// validation capablity of @c Lease4.
  30. class CSVLeaseFile4 : public isc::util::VersionedCSVFile, public LeaseFileStats {
  31. public:
  32. /// @brief Constructor.
  33. ///
  34. /// Initializes columns of the lease file.
  35. ///
  36. /// @param filename Name of the lease file.
  37. CSVLeaseFile4(const std::string& filename);
  38. /// @brief Opens a lease file.
  39. ///
  40. /// This function calls the base class open to do the
  41. /// work of opening a file. It is used to clear any
  42. /// statistics associated with any previous use of the file
  43. /// While it doesn't throw any exceptions of its own
  44. /// the base class may do so.
  45. virtual void open(const bool seek_to_end = false);
  46. /// @brief Appends the lease record to the CSV file.
  47. ///
  48. /// This function doesn't throw exceptions itself. In theory, exceptions
  49. /// are possible when the index of the indexes of the values being written
  50. /// to the file are invalid. However, this would have been a programming
  51. /// error.
  52. ///
  53. /// @param lease Structure representing a DHCPv4 lease.
  54. void append(const Lease4& lease);
  55. /// @brief Reads next lease from the CSV file.
  56. ///
  57. /// If this function hits an error during lease read, it sets the error
  58. /// message using @c CSVFile::setReadMsg and returns false. The error
  59. /// string may be read using @c CSVFile::getReadMsg.
  60. ///
  61. /// This function is exception safe.
  62. ///
  63. /// @param [out] lease Pointer to the lease read from CSV file or
  64. /// NULL pointer if lease hasn't been read.
  65. ///
  66. /// @return Boolean value indicating that the new lease has been
  67. /// read from the CSV file (if true), or that the error has occurred
  68. /// (false).
  69. ///
  70. /// @todo Make sure that the values read from the file are correct.
  71. /// The appropriate @c Lease4 validation mechanism should be used once
  72. /// ticket http://kea.isc.org/ticket/2405 is implemented.
  73. bool next(Lease4Ptr& lease);
  74. private:
  75. /// @brief Initializes columns of the CSV file holding leases.
  76. ///
  77. /// This function initializes the following columns:
  78. /// - address
  79. /// - hwaddr
  80. /// - client_id
  81. /// - valid_lifetime
  82. /// - expire
  83. /// - subnet_id
  84. /// - fqdn_fwd
  85. /// - fqdn_rev
  86. /// - hostname
  87. /// - state
  88. void initColumns();
  89. ///
  90. /// @name Methods which read specific lease fields from the CSV row.
  91. ///
  92. //@{
  93. ///
  94. /// @brief Reads lease address from the CSV file row.
  95. ///
  96. /// @param row CSV file row holding lease information.
  97. asiolink::IOAddress readAddress(const util::CSVRow& row);
  98. /// @brief Reads HW address from the CSV file row.
  99. ///
  100. /// @param row CSV file row holding lease information.
  101. HWAddr readHWAddr(const util::CSVRow& row);
  102. /// @brief Reads client identifier from the CSV file row.
  103. ///
  104. /// @param row CSV file row holding lease information.
  105. ClientIdPtr readClientId(const util::CSVRow& row);
  106. /// @brief Reads valid lifetime from the CSV file row.
  107. ///
  108. /// @param row CSV file row holding lease information.
  109. uint32_t readValid(const util::CSVRow& row);
  110. /// @brief Reads cltt value from the CSV file row.
  111. ///
  112. /// @param row CSV file row holding lease information.
  113. time_t readCltt(const util::CSVRow& row);
  114. /// @brief Reads subnet id from the CSV file row.
  115. ///
  116. /// @param row CSV file row holding lease information.
  117. SubnetID readSubnetID(const util::CSVRow& row);
  118. /// @brief Reads the FQDN forward flag from the CSV file row.
  119. ///
  120. /// @param row CSV file row holding lease information.
  121. bool readFqdnFwd(const util::CSVRow& row);
  122. /// @brief Reads the FQDN reverse flag from the CSV file row.
  123. ///
  124. /// @param row CSV file row holding lease information.
  125. bool readFqdnRev(const util::CSVRow& row);
  126. /// @brief Reads hostname from the CSV file row.
  127. ///
  128. /// @param row CSV file row holding lease information.
  129. std::string readHostname(const util::CSVRow& row);
  130. /// @brief Reads lease state from the CSV file row.
  131. ///
  132. /// @param row CSV file row holding lease information.
  133. uint32_t readState(const util::CSVRow& row);
  134. //@}
  135. };
  136. } // namespace isc::dhcp
  137. } // namespace isc
  138. #endif // CSV_LEASE_FILE4_H