cql_connection.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #include <dhcpsrv/cql_connection.h>
  17. #include <string>
  18. using namespace std;
  19. namespace isc {
  20. namespace dhcp {
  21. CqlConnection::CqlConnection(const ParameterMap& parameters) :
  22. DatabaseConnection(parameters), cluster_(NULL), session_(NULL),
  23. tagged_statements_(NULL) {
  24. }
  25. CqlConnection::~CqlConnection() {
  26. // Free up the prepared statements, ignoring errors.
  27. // Session and connection resources are deallocated.
  28. CassError rc = CASS_OK;
  29. std::string error;
  30. for (int i = 0; i < statements_.size(); i++) {
  31. if (statements_[i]) {
  32. cass_prepared_free(statements_[i]);
  33. }
  34. statements_[i] = NULL;
  35. }
  36. if (session_) {
  37. CassFuture* close_future = cass_session_close(session_);
  38. cass_future_wait(close_future);
  39. checkStatementError(error, close_future, "could not close connection to DB");
  40. rc = cass_future_error_code(close_future);
  41. cass_future_free(close_future);
  42. cass_session_free(session_);
  43. session_ = NULL;
  44. }
  45. if (cluster_) {
  46. cass_cluster_free(cluster_);
  47. cluster_ = NULL;
  48. }
  49. // We're closing the connection anyway. Let's not throw at this
  50. // stage
  51. if (rc != CASS_OK) {
  52. isc_throw(DbOpenError, error);
  53. }
  54. }
  55. void
  56. CqlConnection::openDatabase() {
  57. CassError rc;
  58. // Set up the values of the parameters
  59. const char* contact_points = "127.0.0.1";
  60. string scontact_points;
  61. try {
  62. scontact_points = getParameter("contact-points");
  63. contact_points = scontact_points.c_str();
  64. } catch (...) {
  65. // No host. Fine, we'll use "localhost".
  66. }
  67. const char* port = NULL;
  68. string sport;
  69. try {
  70. sport = getParameter("port");
  71. port = sport.c_str();
  72. } catch (...) {
  73. // No port. Fine, we'll use "default".
  74. }
  75. const char* user = NULL;
  76. string suser;
  77. try {
  78. suser = getParameter("user");
  79. user = suser.c_str();
  80. } catch (...) {
  81. // No user. Fine, we'll use NULL.
  82. }
  83. const char* password = NULL;
  84. string spassword;
  85. try {
  86. spassword = getParameter("password");
  87. password = spassword.c_str();
  88. } catch (...) {
  89. // No password. Fine, we'll use NULL.
  90. }
  91. const char* keyspace = "keatest";
  92. string skeyspace;
  93. try {
  94. skeyspace = getParameter("keyspace");
  95. keyspace = skeyspace.c_str();
  96. } catch (...) {
  97. // No keyspace name. Fine, we'll use default "keatest".
  98. }
  99. cluster_ = cass_cluster_new();
  100. cass_cluster_set_contact_points(cluster_, contact_points);
  101. if (user != NULL && password != NULL) {
  102. cass_cluster_set_credentials(cluster_, user, password);
  103. }
  104. if (port != NULL) {
  105. int port_number;
  106. try {
  107. port_number = boost::lexical_cast<int>(port);
  108. } catch (const std::exception& ex) {
  109. isc_throw(DbOperationError, "Invalid int data: " << port
  110. << " : " << ex.what());
  111. }
  112. cass_cluster_set_port(cluster_, port_number);
  113. }
  114. session_ = cass_session_new();
  115. CassFuture* connect_future = cass_session_connect_keyspace(session_,
  116. cluster_, keyspace);
  117. cass_future_wait(connect_future);
  118. std::string error;
  119. checkStatementError(error, connect_future, "could not connect to DB");
  120. rc = cass_future_error_code(connect_future);
  121. cass_future_free(connect_future);
  122. if (rc != CASS_OK) {
  123. cass_session_free(session_);
  124. session_ = NULL;
  125. cass_cluster_free(cluster_);
  126. cluster_ = NULL;
  127. isc_throw(DbOpenError, error);
  128. }
  129. }
  130. void
  131. CqlConnection::prepareStatements(CqlTaggedStatement *statements) {
  132. CassError rc = CASS_OK;
  133. uint32_t size = 0;
  134. tagged_statements_ = statements;
  135. for (; tagged_statements_[size].params_; size++);
  136. statements_.resize(size);
  137. for (uint32_t i = 0; i < size; i++) {
  138. const char* query = tagged_statements_[i].text_;
  139. CassFuture* future = cass_session_prepare(session_, query);
  140. cass_future_wait(future);
  141. std::string error;
  142. checkStatementError(error, future, i, "could not prepare statement");
  143. rc = cass_future_error_code(future);
  144. if (rc != CASS_OK) {
  145. cass_future_free(future);
  146. statements_[i] = NULL;
  147. isc_throw(DbOperationError, error);
  148. } else {
  149. statements_[i] = cass_future_get_prepared(future);
  150. }
  151. cass_future_free(future);
  152. }
  153. }
  154. void
  155. CqlConnection::commit() {
  156. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_CQL_COMMIT);
  157. }
  158. void
  159. CqlConnection::rollback() {
  160. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_CQL_ROLLBACK);
  161. }
  162. void
  163. CqlConnection::checkStatementError(std::string& error, CassFuture* future,
  164. uint32_t stindex, const char* what) const {
  165. CassError rc;
  166. const char* errorMessage;
  167. size_t errorMessageSize;
  168. std::stringstream stream;
  169. stream << "no error for: " << tagged_statements_[stindex].name_;
  170. rc = cass_future_error_code(future);
  171. cass_future_error_message(future, &errorMessage, &errorMessageSize);
  172. if (rc != CASS_OK) {
  173. stream.str(std::string());
  174. stream << what << " for: " << tagged_statements_[stindex].name_
  175. << " reason: " << errorMessage << " error code: " << rc;
  176. }
  177. error = stream.str();
  178. }
  179. void
  180. CqlConnection::checkStatementError(std::string& error, CassFuture* future,
  181. const char* what) const {
  182. CassError rc;
  183. const char* errorMessage;
  184. size_t errorMessageSize;
  185. std::stringstream stream;
  186. stream << "no error";
  187. rc = cass_future_error_code(future);
  188. cass_future_error_message(future, &errorMessage, &errorMessageSize);
  189. if (rc != CASS_OK) {
  190. stream.str(std::string());
  191. stream << what << " reason: " << errorMessage << " error code: " << rc;
  192. }
  193. error = stream.str();
  194. }
  195. }; // end of isc::dhcp namespace
  196. }; // end of isc namespace