cql_lease_mgr.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. // Copyright (C) 2015 - 2016 Deutsche Telekom AG.
  2. //
  3. // Author: Razvan Becheriu <razvan.becheriu@qualitance.com>
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. #ifndef CQL_LEASE_MGR_H
  17. #define CQL_LEASE_MGR_H
  18. #include <dhcp/hwaddr.h>
  19. #include <dhcpsrv/lease_mgr.h>
  20. #include <dhcpsrv/cql_connection.h>
  21. #include <boost/scoped_ptr.hpp>
  22. #include <boost/utility.hpp>
  23. #include <cassandra.h>
  24. #include <vector>
  25. namespace isc {
  26. namespace dhcp {
  27. /// @brief Structure used to bind C++ input values to dynamic SQL parameters
  28. /// The structure contains a vector which store the input values,
  29. /// This vector is passed directly into the
  30. /// CQL execute call.
  31. ///
  32. /// Note that the data values are stored as pointers. These pointers need
  33. /// to be valid for the duration of the CQL statement execution. In other
  34. /// words populating them with pointers to values that go out of scope
  35. /// before statement is executed is a bad idea.
  36. struct CqlDataArray {
  37. /// Add void pointer to a vector of pointers to the data values.
  38. std::vector<void*> values_;
  39. void add(void* value) {
  40. values_.push_back(value);
  41. }
  42. /// Remove void pointer from a vector of pointers to the data values.
  43. void remove(int index) {
  44. if (values_.size() <= index) {
  45. isc_throw(BadValue, "Index " << index << " out of bounds: [0, " <<
  46. (values_.size() - 1) << "]");
  47. }
  48. values_.erase(values_.begin() + index);
  49. }
  50. /// Get size.
  51. size_t size() {
  52. return values_.size();
  53. }
  54. };
  55. class CqlVersionExchange;
  56. class CqlLeaseExchange;
  57. class CqlLease4Exchange;
  58. class CqlLease6Exchange;
  59. /// @brief Cassandra Exchange
  60. ///
  61. /// This class provides the basic conversion functions between Cassandra CQL and
  62. /// base types.
  63. class CqlExchange : public virtual SqlExchange {
  64. public:
  65. // Time conversion methods.
  66. static void
  67. convertToDatabaseTime(const time_t& cltt,
  68. const uint32_t& valid_lifetime,
  69. uint64_t& expire) {
  70. // Calculate expiry time. Store it in the 64-bit value so as we can
  71. // detect overflows.
  72. int64_t expire_time = static_cast<int64_t>(cltt) +
  73. static_cast<int64_t>(valid_lifetime);
  74. if (expire_time > DatabaseConnection::MAX_DB_TIME) {
  75. isc_throw(BadValue, "Time value is too large: " << expire_time);
  76. }
  77. expire = expire_time;
  78. }
  79. static void
  80. convertFromDatabaseTime(const uint64_t& expire,
  81. const uint32_t& valid_lifetime,
  82. time_t& cltt) {
  83. // Convert to local time
  84. cltt = expire - static_cast<int64_t>(valid_lifetime);
  85. }
  86. };
  87. /// @brief Cassandra Lease Manager
  88. ///
  89. /// This class provides the \ref isc::dhcp::LeaseMgr interface to the Cassandra
  90. /// database. Use of this backend presupposes that a CQL database is available
  91. /// and that the Kea schema has been created within it.
  92. class CqlLeaseMgr : public LeaseMgr {
  93. public:
  94. /// @brief Constructor
  95. ///
  96. /// Uses the following keywords in the parameters passed to it to
  97. /// connect to the database:
  98. /// - name - Name of the database to which to connect (mandatory)
  99. /// - host - Host to which to connect (optional, defaults to "localhost")
  100. /// - user - Username under which to connect (optional)
  101. /// - password - Password for "user" on the database (optional)
  102. ///
  103. /// If the database is successfully opened, the version number in the
  104. /// schema_version table will be checked against hard-coded value in
  105. /// the implementation file.
  106. ///
  107. /// Finally, all the SQL commands are pre-compiled.
  108. ///
  109. /// @param parameters A data structure relating keywords and values
  110. /// concerned with the database.
  111. ///
  112. /// @throw isc::dhcp::NoDatabaseName Mandatory database name not given
  113. /// @throw isc::dhcp::DbOpenError Error opening the database
  114. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  115. /// failed.
  116. CqlLeaseMgr(const DatabaseConnection::ParameterMap& parameters);
  117. /// @brief Destructor (closes database)
  118. virtual ~CqlLeaseMgr();
  119. /// @brief Local version of getDBVersion() class method
  120. static std::string getDBVersion();
  121. /// @brief Adds an IPv4 lease
  122. ///
  123. /// @param lease lease to be added
  124. ///
  125. /// @result true if the lease was added, false if not (because a lease
  126. /// with the same address was already there).
  127. ///
  128. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  129. /// failed.
  130. virtual bool addLease(const Lease4Ptr& lease);
  131. /// @brief Adds an IPv6 lease
  132. ///
  133. /// @param lease lease to be added
  134. ///
  135. /// @result true if the lease was added, false if not (because a lease
  136. /// with the same address was already there).
  137. ///
  138. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  139. /// failed.
  140. virtual bool addLease(const Lease6Ptr& lease);
  141. /// @brief Returns an IPv4 lease for specified IPv4 address
  142. ///
  143. /// This method return a lease that is associated with a given address.
  144. /// For other query types (by hardware addr, by Client ID) there can be
  145. /// several leases in different subnets (e.g. for mobile clients that
  146. /// got address in different subnets). However, for a single address
  147. /// there can be only one lease, so this method returns a pointer to
  148. /// a single lease, not a container of leases.
  149. ///
  150. /// @param addr address of the searched lease
  151. ///
  152. /// @return smart pointer to the lease (or NULL if a lease is not found)
  153. ///
  154. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  155. /// failed.
  156. virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
  157. /// @brief Returns existing IPv4 leases for specified hardware address.
  158. ///
  159. /// Although in the usual case there will be only one lease, for mobile
  160. /// clients or clients with multiple static/fixed/reserved leases there
  161. /// can be more than one. Thus return type is a container, not a single
  162. /// pointer.
  163. ///
  164. /// @param hwaddr hardware address of the client
  165. ///
  166. /// @return lease collection
  167. ///
  168. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  169. /// failed.
  170. virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const;
  171. /// @brief Returns existing IPv4 leases for specified hardware address
  172. /// and a subnet
  173. ///
  174. /// There can be at most one lease for a given HW address in a single
  175. /// pool, so this method with either return a single lease or NULL.
  176. ///
  177. /// @param hwaddr hardware address of the client
  178. /// @param subnet_id identifier of the subnet that lease must belong to
  179. ///
  180. /// @return a pointer to the lease (or NULL if a lease is not found)
  181. ///
  182. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  183. /// failed.
  184. virtual Lease4Ptr getLease4(const isc::dhcp::HWAddr& hwaddr,
  185. SubnetID subnet_id) const;
  186. /// @brief Returns existing IPv4 leases for specified client-id
  187. ///
  188. /// Although in the usual case there will be only one lease, for mobile
  189. /// clients or clients with multiple static/fixed/reserved leases there
  190. /// can be more than one. Thus return type is a container, not a single
  191. /// pointer.
  192. ///
  193. /// @param clientid client identifier
  194. ///
  195. /// @return lease collection
  196. ///
  197. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  198. /// failed.
  199. virtual Lease4Collection getLease4(const ClientId& clientid) const;
  200. /// @brief Returns IPv4 lease for the specified client identifier, HW
  201. /// address and subnet identifier.
  202. ///
  203. /// @param client_id A client identifier.
  204. /// @param hwaddr Hardware address.
  205. /// @param subnet_id A subnet identifier.
  206. ///
  207. /// @return A pointer to the lease or NULL if the lease is not found.
  208. /// @throw isc::NotImplemented On every call as this function is currently
  209. /// not implemented for the PostgreSQL backend.
  210. virtual Lease4Ptr getLease4(const ClientId& client_id, const HWAddr& hwaddr,
  211. SubnetID subnet_id) const;
  212. /// @brief Returns existing IPv4 lease for specified client-id
  213. ///
  214. /// There can be at most one lease for a given HW address in a single
  215. /// pool, so this method with either return a single lease or NULL.
  216. ///
  217. /// @param clientid client identifier
  218. /// @param subnet_id identifier of the subnet that lease must belong to
  219. ///
  220. /// @return a pointer to the lease (or NULL if a lease is not found)
  221. ///
  222. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  223. /// failed.
  224. virtual Lease4Ptr getLease4(const ClientId& clientid,
  225. SubnetID subnet_id) const;
  226. /// @brief Returns existing IPv6 lease for a given IPv6 address.
  227. ///
  228. /// For a given address, we assume that there will be only one lease.
  229. /// The assumption here is that there will not be site or link-local
  230. /// addresses used, so there is no way of having address duplication.
  231. ///
  232. /// @param type specifies lease type: (NA, TA or PD)
  233. /// @param addr address of the searched lease
  234. ///
  235. /// @return smart pointer to the lease (or NULL if a lease is not found)
  236. ///
  237. /// @throw isc::BadValue record retrieved from database had an invalid
  238. /// lease type field.
  239. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  240. /// failed.
  241. virtual Lease6Ptr getLease6(Lease::Type type,
  242. const isc::asiolink::IOAddress& addr) const;
  243. /// @brief Returns existing IPv6 leases for a given DUID+IA combination
  244. ///
  245. /// Although in the usual case there will be only one lease, for mobile
  246. /// clients or clients with multiple static/fixed/reserved leases there
  247. /// can be more than one. Thus return type is a container, not a single
  248. /// pointer.
  249. ///
  250. /// @param type specifies lease type: (NA, TA or PD)
  251. /// @param duid client DUID
  252. /// @param iaid IA identifier
  253. ///
  254. /// @return smart pointer to the lease (or NULL if a lease is not found)
  255. ///
  256. /// @throw isc::BadValue record retrieved from database had an invalid
  257. /// lease type field.
  258. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  259. /// failed.
  260. virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
  261. uint32_t iaid) const;
  262. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  263. ///
  264. /// @param type specifies lease type: (NA, TA or PD)
  265. /// @param duid client DUID
  266. /// @param iaid IA identifier
  267. /// @param subnet_id subnet id of the subnet the lease belongs to
  268. ///
  269. /// @return lease collection (may be empty if no lease is found)
  270. ///
  271. /// @throw isc::BadValue record retrieved from database had an invalid
  272. /// lease type field.
  273. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  274. /// failed.
  275. virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
  276. uint32_t iaid, SubnetID subnet_id) const;
  277. /// @brief Returns a collection of expired DHCPv6 leases.
  278. ///
  279. /// This method returns at most @c max_leases expired leases. The leases
  280. /// returned haven't been reclaimed, i.e. the database query must exclude
  281. /// reclaimed leases from the results returned.
  282. ///
  283. /// @param [out] expired_leases A container to which expired leases returned
  284. /// by the database backend are added.
  285. /// @param max_leases A maximum number of leases to be returned. If this
  286. /// value is set to 0, all expired (but not reclaimed) leases are returned.
  287. virtual void getExpiredLeases6(Lease6Collection& ,
  288. const size_t ) const;
  289. /// @brief Returns a collection of expired DHCPv4 leases.
  290. ///
  291. /// This method returns at most @c max_leases expired leases. The leases
  292. /// returned haven't been reclaimed, i.e. the database query must exclude
  293. /// reclaimed leases from the results returned.
  294. ///
  295. /// @param [out] expired_leases A container to which expired leases returned
  296. /// by the database backend are added.
  297. /// @param max_leases A maximum number of leases to be returned. If this
  298. /// value is set to 0, all expired (but not reclaimed) leases are returned.
  299. virtual void getExpiredLeases4(Lease4Collection& ,
  300. const size_t ) const;
  301. /// @brief Updates IPv4 lease.
  302. ///
  303. /// Updates the record of the lease in the database (as identified by the
  304. /// address) with the data in the passed lease object.
  305. ///
  306. /// @param lease4 The lease to be updated.
  307. ///
  308. /// @throw isc::dhcp::NoSuchLease Attempt to update a lease that did not
  309. /// exist.
  310. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  311. /// failed.
  312. virtual void updateLease4(const Lease4Ptr& lease4);
  313. /// @brief Updates IPv6 lease.
  314. ///
  315. /// Updates the record of the lease in the database (as identified by the
  316. /// address) with the data in the passed lease object.
  317. ///
  318. /// @param lease6 The lease to be updated.
  319. ///
  320. /// @throw isc::dhcp::NoSuchLease Attempt to update a lease that did not
  321. /// exist.
  322. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  323. /// failed.
  324. virtual void updateLease6(const Lease6Ptr& lease6);
  325. /// @brief Deletes a lease.
  326. ///
  327. /// @param addr Address of the lease to be deleted. This can be an IPv4
  328. /// address or an IPv6 address.
  329. ///
  330. /// @return true if deletion was successful, false if no such lease exists
  331. ///
  332. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  333. /// failed.
  334. virtual bool deleteLease(const isc::asiolink::IOAddress& addr);
  335. /// @brief Deletes all expired and reclaimed DHCPv4 leases.
  336. ///
  337. /// @param secs Number of seconds since expiration of leases before
  338. /// they can be removed. Leases which have expired later than this
  339. /// time will not be deleted.
  340. ///
  341. /// @return Number of leases deleted.
  342. virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t );
  343. /// @brief Deletes all expired and reclaimed DHCPv6 leases.
  344. ///
  345. /// @param secs Number of seconds since expiration of leases before
  346. /// they can be removed. Leases which have expired later than this
  347. /// time will not be deleted.
  348. ///
  349. /// @return Number of leases deleted.
  350. virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t );
  351. /// @brief Return backend type
  352. ///
  353. /// @return Type of the backend.
  354. virtual std::string getType() const {
  355. return (std::string("cql"));
  356. }
  357. /// @brief Returns name of the database.
  358. ///
  359. /// @return database name
  360. virtual std::string getName() const;
  361. /// @brief Returns description of the backend.
  362. ///
  363. /// This description may be multiline text that describes the backend.
  364. ///
  365. /// @return Description of the backend.
  366. virtual std::string getDescription() const;
  367. /// @brief Returns backend version.
  368. ///
  369. /// @return Version number as a pair of unsigned integers. "first" is the
  370. /// major version number, "second" the minor number.
  371. ///
  372. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  373. /// failed.
  374. virtual std::pair<uint32_t, uint32_t> getVersion() const;
  375. /// @brief Commit Transactions
  376. ///
  377. /// This is a no-op for Cassandra.
  378. virtual void commit();
  379. /// @brief Rollback Transactions
  380. ///
  381. /// This is a no-op for Cassandra.
  382. virtual void rollback();
  383. /// @brief Statement Tags
  384. ///
  385. /// The contents of the enum are indexes into the list of compiled SQL
  386. /// statements
  387. enum StatementIndex {
  388. DELETE_LEASE4, // Delete from lease4 by address
  389. DELETE_LEASE4_STATE_EXPIRED,// Delete expired lease4s in certain state
  390. DELETE_LEASE6, // Delete from lease6 by address
  391. DELETE_LEASE6_STATE_EXPIRED,// Delete expired lease6s in certain state
  392. GET_LEASE4_ADDR, // Get lease4 by address
  393. GET_LEASE4_CLIENTID, // Get lease4 by client ID
  394. GET_LEASE4_CLIENTID_SUBID, // Get lease4 by client ID & subnet ID
  395. GET_LEASE4_HWADDR, // Get lease4 by HW address
  396. GET_LEASE4_HWADDR_SUBID, // Get lease4 by HW address & subnet ID
  397. GET_LEASE4_EXPIRE, // Get expired lease4
  398. GET_LEASE6_ADDR, // Get lease6 by address
  399. GET_LEASE6_DUID_IAID, // Get lease6 by DUID and IAID
  400. GET_LEASE6_DUID_IAID_SUBID, // Get lease6 by DUID, IAID and subnet ID
  401. GET_LEASE6_EXPIRE, // Get expired lease6
  402. GET_VERSION, // Obtain version number
  403. INSERT_LEASE4, // Add entry to lease4 table
  404. INSERT_LEASE6, // Add entry to lease6 table
  405. UPDATE_LEASE4, // Update a Lease4 entry
  406. UPDATE_LEASE6, // Update a Lease6 entry
  407. NUM_STATEMENTS // Number of statements
  408. };
  409. /// @brief Binds data specified in data
  410. ///
  411. /// This method calls one of cass_value_bind_{none,bool,int32,int64,string,bytes}.
  412. /// It is used to bind C++ data types used by Kea into formats used by Cassandra.
  413. ///
  414. /// @param statement Statement object representing the query
  415. /// @param stindex Index of statement being executed
  416. /// @param data array that has been created for the type of lease in question.
  417. /// @param exchange Exchange object to use
  418. static void bindData(CassStatement* statement, const StatementIndex stindex,
  419. CqlDataArray& data, const SqlExchange& exchange);
  420. /// @brief Returns type of data for specific parameter.
  421. ///
  422. /// Returns type of a given parameter of a given statement.
  423. ///
  424. /// @param stindex Index of statement being executed.
  425. /// @param pindex Index of the parameter for a given statement.
  426. /// @param exchange Exchange object to use
  427. static ExchangeDataType getDataType(const StatementIndex stindex, int pindex,
  428. const SqlExchange& exchange);
  429. /// @brief Retrieves data returned by Cassandra.
  430. ///
  431. /// This method calls one of cass_value_get_{none,bool,int32,int64,string,bytes}.
  432. /// It is used to retrieve data returned by Cassandra into standard C++ types
  433. /// used by Kea.
  434. ///
  435. /// @param row row of data returned by CQL library
  436. /// @param pindex Index of statement being executed
  437. /// @param data array that has been created for the type of lease in question.
  438. /// @param size a structure that holds information about size
  439. /// @param dindex data index (specifies which entry in size array is used)
  440. /// @param exchange Exchange object to use
  441. static void getData(const CassRow* row, const int pindex, CqlDataArray& data,
  442. CqlDataArray& size, const int dindex, const SqlExchange& exchange);
  443. private:
  444. /// @brief Add Lease Common Code
  445. ///
  446. /// This method performs the common actions for both flavours (V4 and V6)
  447. /// of the addLease method. It binds the contents of the lease object to
  448. /// the prepared statement and adds it to the database.
  449. ///
  450. /// @param stindex Index of statement being executed
  451. /// @param data array that has been created for the type
  452. /// of lease in question.
  453. /// @param exchange Exchange object to use
  454. ///
  455. /// @return true if the lease was added, false if it was not added because
  456. /// a lease with that address already exists in the database.
  457. ///
  458. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  459. /// failed.
  460. bool addLeaseCommon(StatementIndex stindex, CqlDataArray& data,
  461. CqlLeaseExchange& exchange);
  462. /// @brief Get Lease Collection Common Code
  463. ///
  464. /// This method performs the common actions for obtaining multiple leases
  465. /// from the database.
  466. ///
  467. /// @param stindex Index of statement being executed
  468. /// @param data array containing the where clause input parameters
  469. /// @param exchange Exchange object to use
  470. /// @param result Returned collection of Leases Note that any leases in
  471. /// the collection when this method is called are not erased: the
  472. /// new data is appended to the end.
  473. /// @param single If true, only a single data item is to be retrieved.
  474. /// If more than one is present, a MultipleRecords exception will
  475. /// be thrown.
  476. ///
  477. /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
  478. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  479. /// failed.
  480. /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
  481. /// from the database where only one was expected.
  482. template <typename Exchange, typename LeaseCollection>
  483. void getLeaseCollection(StatementIndex stindex, CqlDataArray& data_array,
  484. Exchange& exchange, LeaseCollection& result,
  485. bool single = false) const;
  486. /// @brief Gets Lease4 Collection
  487. ///
  488. /// Gets a collection of Lease4 objects. This is just an interface to
  489. /// the get lease collection common code.
  490. ///
  491. /// @param stindex Index of statement being executed
  492. /// @param data array containing the where clause input parameters
  493. /// @param result LeaseCollection object returned. Note that any leases in
  494. /// the collection when this method is called are not erased: the
  495. /// new data is appended to the end.
  496. ///
  497. /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
  498. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  499. /// failed.
  500. /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
  501. /// from the database where only one was expected.
  502. void getLeaseCollection(StatementIndex stindex, CqlDataArray& data,
  503. Lease4Collection& result) const {
  504. getLeaseCollection(stindex, data, exchange4_, result);
  505. }
  506. /// @brief Get Lease6 Collection
  507. ///
  508. /// Gets a collection of Lease6 objects. This is just an interface to
  509. /// the get lease collection common code.
  510. ///
  511. /// @param stindex Index of statement being executed
  512. /// @param data array containing input parameters for the query
  513. /// @param result LeaseCollection object returned. Note that any existing
  514. /// data in the collection is erased first.
  515. ///
  516. /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
  517. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  518. /// failed.
  519. /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
  520. /// from the database where only one was expected.
  521. void getLeaseCollection(StatementIndex stindex, CqlDataArray& data,
  522. Lease6Collection& result) const {
  523. getLeaseCollection(stindex, data, exchange6_, result);
  524. }
  525. /// @brief Get Lease4 Common Code
  526. ///
  527. /// This method performs the common actions for the various getLease4()
  528. /// methods. It acts as an interface to the getLeaseCollection() method,
  529. /// but retrieveing only a single lease.
  530. ///
  531. /// @param stindex Index of statement being executed
  532. /// @param data array containing input parameters for the query
  533. /// @param lease Lease4 object returned
  534. void getLease(StatementIndex stindex, CqlDataArray& data,
  535. Lease4Ptr& result) const;
  536. /// @brief Get Lease6 Common Code
  537. ///
  538. /// This method performs the common actions for the various getLease4()
  539. /// methods. It acts as an interface to the getLeaseCollection() method,
  540. /// but retrieveing only a single lease.
  541. ///
  542. /// @param stindex Index of statement being executed
  543. /// @param data array containing input parameters for the query
  544. /// @param lease Lease6 object returned
  545. void getLease(StatementIndex stindex, CqlDataArray& data,
  546. Lease6Ptr& result) const;
  547. /// @brief Get expired leases common code.
  548. ///
  549. /// This method retrieves expired and not reclaimed leases from the
  550. /// lease database. The returned leases are ordered by the expiration
  551. /// time. The maximum number of leases to be returned is specified
  552. /// as an argument.
  553. ///
  554. /// @param [out] expired_leases Reference to the container where the
  555. /// retrieved leases are put.
  556. /// @param max_leases Maximum number of leases to be returned.
  557. /// @param statement_index One of the @c GET_LEASE4_EXPIRE or
  558. /// @c GET_LEASE6_EXPIRE.
  559. ///
  560. /// @tparam One of the @c Lease4Collection or @c Lease6Collection.
  561. template<typename LeaseCollection>
  562. void getExpiredLeasesCommon(LeaseCollection& expired_leases,
  563. const size_t max_leases,
  564. StatementIndex statement_index) const;
  565. /// @brief Update lease common code
  566. ///
  567. /// Holds the common code for updating a lease. It binds the parameters
  568. /// to the prepared statement, executes it, then checks how many rows
  569. /// were affected.
  570. ///
  571. /// @param stindex Index of prepared statement to be executed
  572. /// @param data array containing lease values and where clause
  573. /// parameters for the update.
  574. /// @param lease Pointer to the lease object whose record is being updated.
  575. ///
  576. /// @throw NoSuchLease Could not update a lease because no lease matches
  577. /// the address given.
  578. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  579. /// failed.
  580. template <typename LeasePtr>
  581. void updateLeaseCommon(StatementIndex stindex, CqlDataArray& data,
  582. const LeasePtr& lease, CqlLeaseExchange& exchange);
  583. /// @brief Delete lease common code
  584. ///
  585. /// Holds the common code for deleting a lease. It binds the parameters
  586. /// to the prepared statement, executes the statement and checks to
  587. /// see how many rows were deleted.
  588. ///
  589. /// @param stindex Index of prepared statement to be executed
  590. /// @param data array containing lease values and where clause
  591. /// parameters for the delete
  592. ///
  593. /// @return Number of deleted leases.
  594. ///
  595. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  596. /// failed.
  597. bool deleteLeaseCommon(StatementIndex stindex, CqlDataArray& data,
  598. CqlLeaseExchange& exchange);
  599. /// @brief Delete expired-reclaimed leases.
  600. ///
  601. /// @param secs Number of seconds since expiration of leases before
  602. /// they can be removed. Leases which have expired later than this
  603. /// time will not be deleted.
  604. /// @param statement_index One of the @c DELETE_LEASE4_STATE_EXPIRED or
  605. /// @c DELETE_LEASE6_STATE_EXPIRED.
  606. ///
  607. /// @return Number of leases deleted.
  608. uint64_t deleteExpiredReclaimedLeasesCommon(const uint32_t secs,
  609. StatementIndex statement_index);
  610. /// CQL queries used by CQL backend
  611. static CqlTaggedStatement tagged_statements_[];
  612. /// Database connection object
  613. CqlConnection dbconn_;
  614. /// The exchange objects are used for transfer of data to/from the database.
  615. /// They are pointed-to objects as the contents may change in "const" calls,
  616. /// while the rest of this object does not. (At alternative would be to
  617. /// declare them as "mutable".)
  618. boost::scoped_ptr<CqlLease4Exchange> exchange4_; ///< Exchange object
  619. boost::scoped_ptr<CqlLease6Exchange> exchange6_; ///< Exchange object
  620. boost::scoped_ptr<CqlVersionExchange> versionExchange_; ///< Exchange object
  621. };
  622. }; // end of isc::dhcp namespace
  623. }; // end of isc namespace
  624. #endif // CQL_LEASE_MGR_H