csv_lease_file6.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_FILE6_H
  7. #define CSV_LEASE_FILE6_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. namespace isc {
  17. namespace dhcp {
  18. /// @brief Provides methods to access CSV file with DHCPv6 leases.
  19. ///
  20. /// This class contains methods customized to read and write DHCPv6 leases from
  21. /// and to the CSV file. It expects that the CSV file being parsed contains a
  22. /// set of columns with well known names (initialized in the class constructor).
  23. ///
  24. /// @todo This class doesn't validate the lease values read from the file.
  25. /// The @c Lease6 is a structure that should be itself responsible for this
  26. /// validation (see http://kea.isc.org/ticket/2405). However, when #2405
  27. /// is implemented, the @c next function may need to be updated to use the
  28. /// validation capablity of @c Lease6.
  29. class CSVLeaseFile6 : public isc::util::VersionedCSVFile, public LeaseFileStats {
  30. public:
  31. /// @brief Constructor.
  32. ///
  33. /// Initializes columns of the lease file.
  34. ///
  35. /// @param filename Name of the lease file.
  36. CSVLeaseFile6(const std::string& filename);
  37. /// @brief Opens a lease file.
  38. ///
  39. /// This function calls the base class open to do the
  40. /// work of opening a file. It is used to clear any
  41. /// statistics associated with any previous use of the file
  42. /// While it doesn't throw any exceptions of its own
  43. /// the base class may do so.
  44. virtual void open(const bool seek_to_end = false);
  45. /// @brief Appends the lease record to the CSV file.
  46. ///
  47. /// This function doesn't throw exceptions itself. In theory, exceptions
  48. /// are possible when the index of the indexes of the values being written
  49. /// to the file are invalid. However, this would have been a programming
  50. /// error.
  51. ///
  52. /// @param lease Structure representing a DHCPv6 lease.
  53. void append(const Lease6& lease);
  54. /// @brief Reads next lease from the CSV file.
  55. ///
  56. /// If this function hits an error during lease read, it sets the error
  57. /// message using @c CSVFile::setReadMsg and returns false. The error
  58. /// string may be read using @c CSVFile::getReadMsg.
  59. ///
  60. /// This function is exception safe.
  61. ///
  62. /// @param [out] lease Pointer to the lease read from CSV file or
  63. /// NULL pointer if lease hasn't been read.
  64. ///
  65. /// @return Boolean value indicating that the new lease has been
  66. /// read from the CSV file (if true), or that the error has occurred
  67. /// (false).
  68. ///
  69. /// @todo Make sure that the values read from the file are correct.
  70. /// The appropriate @c Lease6 validation mechanism should be used once
  71. /// ticket http://kea.isc.org/ticket/2405 is implemented.
  72. bool next(Lease6Ptr& lease);
  73. private:
  74. /// @brief Initializes columns of the CSV file holding leases.
  75. ///
  76. /// This function initializes the following columns:
  77. /// - address
  78. /// - duid
  79. /// - valid_lifetime
  80. /// - expire
  81. /// - subnet_id
  82. /// - pref_lifetime
  83. /// - lease_type
  84. /// - iaid
  85. /// - prefix_len
  86. /// - fqdn_fwd
  87. /// - fqdn_rev
  88. /// - hostname
  89. /// - hwaddr
  90. /// - state
  91. void initColumns();
  92. ///
  93. /// @name Methods which read specific lease fields from the CSV row.
  94. ///
  95. //@{
  96. ///
  97. /// @brief Reads lease type from the CSV file row.
  98. ///
  99. /// @param row CSV file row holding lease information.
  100. Lease::Type readType(const util::CSVRow& row);
  101. /// @brief Reads lease address from the CSV file row.
  102. ///
  103. /// @param row CSV file row holding lease information.
  104. asiolink::IOAddress readAddress(const util::CSVRow& row);
  105. /// @brief Reads DUID from the CSV file row.
  106. ///
  107. /// @param row CSV file row holding lease information.
  108. DuidPtr readDUID(const util::CSVRow& row);
  109. /// @brief Reads IAID from the CSV file row.
  110. ///
  111. /// @param row CSV file row holding lease information.
  112. uint32_t readIAID(const util::CSVRow& row);
  113. /// @brief Reads preferred lifetime from the CSV file row.
  114. ///
  115. /// @param row CSV file row holding lease information.
  116. uint32_t readPreferred(const util::CSVRow& row);
  117. /// @brief Reads valid lifetime from the CSV file row.
  118. ///
  119. /// @param row CSV file row holding lease information.
  120. uint32_t readValid(const util::CSVRow& row);
  121. /// @brief Reads cltt value from the CSV file row.
  122. ///
  123. /// @param row CSV file row holding lease information.
  124. uint32_t readCltt(const util::CSVRow& row);
  125. /// @brief Reads subnet id from the CSV file row.
  126. ///
  127. /// @param row CSV file row holding lease information.
  128. SubnetID readSubnetID(const util::CSVRow& row);
  129. /// @brief Reads prefix length from the CSV file row.
  130. ///
  131. /// @param row CSV file row holding lease information.
  132. uint8_t readPrefixLen(const util::CSVRow& row);
  133. /// @brief Reads the FQDN forward flag from the CSV file row.
  134. ///
  135. /// @param row CSV file row holding lease information.
  136. bool readFqdnFwd(const util::CSVRow& row);
  137. /// @brief Reads the FQDN reverse flag from the CSV file row.
  138. ///
  139. /// @param row CSV file row holding lease information.
  140. bool readFqdnRev(const util::CSVRow& row);
  141. /// @brief Reads hostname from the CSV file row.
  142. ///
  143. /// @param row CSV file row holding lease information.
  144. std::string readHostname(const util::CSVRow& row);
  145. /// @brief Reads HW address from the CSV file row.
  146. ///
  147. /// @param row CSV file row holding lease information.
  148. /// @return pointer to the HWAddr structure that was read
  149. HWAddrPtr readHWAddr(const util::CSVRow& row);
  150. /// @brief Reads lease state from the CSV file row.
  151. ///
  152. /// @param row CSV file row holding lease information.
  153. uint32_t readState(const util::CSVRow& row);
  154. //@}
  155. };
  156. } // namespace isc::dhcp
  157. } // namespace isc
  158. #endif // CSV_LEASE_FILE6_H