ndb_cassandra.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * ndb_cassandra.c
  3. *
  4. * Copyright (C) 2013 Indigital Telecom.
  5. *
  6. * Author: Luis Martin Gil
  7. * <[email protected]>
  8. * <[email protected]>
  9. *
  10. * This file is part of Kamailio, a free SIP server.
  11. *
  12. * kamailio is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version
  16. *
  17. * kamailio is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. *
  26. * History:
  27. * -------
  28. * 2013-11: Initial version luismartingil
  29. */
  30. /*! \file
  31. * \brief SIP-router indigital :: Module core
  32. * \ingroup cass
  33. */
  34. /*! \defgroup INdigital Telecom :: INdigital Telecom Cassandra writer.
  35. *
  36. */
  37. #include "../../sr_module.h"
  38. #include "../../mod_fix.h"
  39. #include "../../lib/kmi/mi.h" //register_my_mod function
  40. #include "../../lvalue.h"
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include "thrift_wrapper.h" // Getting the thrift interface
  44. MODULE_VERSION /* Module*/
  45. /* Module parameter variables */
  46. static str host = {NULL, 0};
  47. static int port = 0;
  48. /* Module management function prototypes */
  49. static int mod_init(void);
  50. static int child_init(int);
  51. static void destroy(void);
  52. /* Fixups functions */
  53. static int fixup_cass_insert(void** param, int param_no);
  54. static int fixup_cass_retrieve(void** param, int param_no);
  55. static int free_fixup_cass_retrieve(void** param, int param_no);
  56. /* Module exported functions */
  57. static int cass_insert_f(struct sip_msg *msg, char* keyspace, char* column_family,
  58. char* key, char* column, char* value);
  59. static int cass_retrieve_f(struct sip_msg *msg, char* keyspace, char* column_family,
  60. char* key, char* column, char* value);
  61. /* Exported functions */
  62. static cmd_export_t cmds[] = {
  63. {"cass_insert", (cmd_function)cass_insert_f, 5,
  64. fixup_cass_insert, 0,
  65. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  66. {"cass_retrieve", (cmd_function)cass_retrieve_f, 5,
  67. fixup_cass_retrieve, free_fixup_cass_retrieve,
  68. REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
  69. {0, 0, 0, 0, 0, 0}
  70. };
  71. /* Exported parameters */
  72. static param_export_t params[] = {
  73. {"host", PARAM_STR, &host},
  74. {"port", INT_PARAM, &port},
  75. {0, 0, 0}
  76. };
  77. static mi_export_t mi_cmds[] = {
  78. { 0, 0, 0, 0, 0}
  79. };
  80. /* Module interface */
  81. struct module_exports exports = {
  82. "ndb_cassandra",
  83. DEFAULT_DLFLAGS, /* dlopen flags */
  84. cmds, /* Exported functions */
  85. params, /* Exported parameters */
  86. 0, /* exported statistics */
  87. mi_cmds, /* exported MI functions */
  88. 0, /* exported pseudo-variables */
  89. 0, /* extra processes */
  90. mod_init, /* module initialization function */
  91. 0, /* response function*/
  92. destroy, /* destroy function */
  93. child_init /* per-child init function */
  94. };
  95. /* Module initialization function */
  96. static int mod_init(void) {
  97. if(register_mi_mod(exports.name, mi_cmds)!=0)
  98. {
  99. LM_ERR("failed to register MI commands\n");
  100. return -1;
  101. }
  102. return 0;
  103. }
  104. /* Child initialization function */
  105. static int child_init(int rank) {
  106. int rtn = 0;
  107. return(rtn);
  108. }
  109. static void destroy(void) {
  110. return;
  111. }
  112. /**/
  113. static int fixup_cass_insert(void** param, int param_no) {
  114. if (param_no == 1) {
  115. return fixup_spve_null(param, 1);
  116. }
  117. if (param_no == 2) {
  118. return fixup_spve_null(param, 1);
  119. }
  120. if (param_no == 3) {
  121. return fixup_spve_null(param, 1);
  122. }
  123. if (param_no == 4) {
  124. return fixup_spve_null(param, 1);
  125. }
  126. if (param_no == 5) {
  127. return fixup_spve_null(param, 1);
  128. }
  129. LM_ERR("invalid parameter number <%d>\n", param_no);
  130. return -1;
  131. }
  132. /**/
  133. static int fixup_cass_retrieve(void** param, int param_no) {
  134. if (param_no == 1) {
  135. return fixup_spve_null(param, 1);
  136. }
  137. if (param_no == 2) {
  138. return fixup_spve_null(param, 1);
  139. }
  140. if (param_no == 3) {
  141. return fixup_spve_null(param, 1);
  142. }
  143. if (param_no == 4) {
  144. return fixup_spve_null(param, 1);
  145. }
  146. if (param_no == 5) {
  147. if (fixup_pvar_null(param, 1) != 0) {
  148. LM_ERR("failed to fixup result pvar\n");
  149. return -1;
  150. }
  151. if (((pv_spec_t *)(*param))->setf == NULL) {
  152. LM_ERR("result pvar is not writeble\n");
  153. return -1;
  154. }
  155. return 0;
  156. }
  157. LM_ERR("invalid parameter number <%d>\n", param_no);
  158. return -1;
  159. }
  160. static int free_fixup_cass_retrieve(void** param, int param_no) {
  161. if (param_no == 1) {
  162. LM_WARN("free function has not been defined for spve\n");
  163. return 0;
  164. }
  165. if (param_no == 2) {
  166. LM_WARN("free function has not been defined for spve\n");
  167. return 0;
  168. }
  169. if (param_no == 3) {
  170. LM_WARN("free function has not been defined for spve\n");
  171. return 0;
  172. }
  173. if (param_no == 4) {
  174. LM_WARN("free function has not been defined for spve\n");
  175. return 0;
  176. }
  177. if (param_no == 5) {
  178. return fixup_free_pvar_null(param, 1);
  179. }
  180. LM_ERR("invalid parameter number <%d>\n", param_no);
  181. return -1;
  182. }
  183. /**/
  184. // **********************************************************
  185. // cass_insert_f
  186. // **********************************************************
  187. static int cass_insert_f(struct sip_msg *msg, char* keyspace, char* column_family,
  188. char* key, char* column, char* value) {
  189. str keyspace_string = {NULL, 0};
  190. if (fixup_get_svalue(msg, (gparam_p)keyspace, &keyspace_string) != 0) {
  191. LM_ERR("cannot get the keyspace value\n");goto error;}
  192. str column_family_string = {NULL, 0};
  193. if (fixup_get_svalue(msg, (gparam_p)column_family, &column_family_string) != 0) {
  194. LM_ERR("cannot get the column_family value\n");goto error;}
  195. str key_string = {NULL, 0};
  196. if (fixup_get_svalue(msg, (gparam_p)key, &key_string) != 0) {
  197. LM_ERR("cannot get the key value\n");goto error;}
  198. str column_string = {NULL, 0};
  199. if (fixup_get_svalue(msg, (gparam_p)column, &column_string) != 0) {
  200. LM_ERR("cannot get the column value\n");goto error;}
  201. str value_string = {NULL, 0};
  202. if (fixup_get_svalue(msg, (gparam_p)value, &value_string) != 0) {
  203. LM_ERR("cannot get the value value\n");goto error;}
  204. int ret;
  205. /* Doing the insert. */
  206. LM_DBG("Insert. %s['%s']['%s'] <== '%s' ",
  207. column_family_string.s,
  208. key_string.s,
  209. column_string.s,
  210. value_string.s);
  211. ret = insert_wrap(host.s, port,
  212. keyspace_string.s,
  213. column_family_string.s,
  214. key_string.s,
  215. column_string.s,
  216. &(value_string.s));
  217. LM_DBG("Insert. done!");
  218. return ret;
  219. error:
  220. return -1;
  221. }
  222. // cass_insert_f*********************************************
  223. // **********************************************************
  224. // cass_retrieve_f
  225. // **********************************************************
  226. static int cass_retrieve_f(struct sip_msg *msg, char* keyspace, char* column_family,
  227. char* key, char* column, char* value) {
  228. str keyspace_string = {NULL, 0};
  229. if (fixup_get_svalue(msg, (gparam_p)keyspace, &keyspace_string) != 0) {
  230. LM_ERR("cannot get the keyspace value\n");goto error;}
  231. str column_family_string = {NULL, 0};
  232. if (fixup_get_svalue(msg, (gparam_p)column_family, &column_family_string) != 0) {
  233. LM_ERR("cannot get the column_family value\n");goto error;}
  234. str key_string = {NULL, 0};
  235. if (fixup_get_svalue(msg, (gparam_p)key, &key_string) != 0) {
  236. LM_ERR("cannot get the key value\n");goto error;}
  237. str column_string = {NULL, 0};
  238. if (fixup_get_svalue(msg, (gparam_p)column, &column_string) != 0) {
  239. LM_ERR("cannot get the column value\n");goto error;}
  240. int ret;
  241. char *value_out = NULL;
  242. /* Doing the retrieve. */
  243. LM_DBG("Retrieve. %s['%s']['%s'] ==>",
  244. column_family_string.s,
  245. key_string.s,
  246. column_string.s);
  247. ret = retrieve_wrap(host.s, port,
  248. keyspace_string.s,
  249. column_family_string.s,
  250. key_string.s,
  251. column_string.s,
  252. &(value_out));
  253. if (ret > 0) {
  254. LM_DBG("Retrieve. done! value:'%s'", value_out);
  255. pv_spec_t* dst;
  256. pv_value_t val;
  257. val.rs.s = value_out;
  258. val.rs.len = strlen(value_out);
  259. val.flags = PV_VAL_STR;
  260. dst = (pv_spec_t *) value;
  261. dst->setf(msg, &dst->pvp, (int)EQ_T, &val);
  262. free(value_out);
  263. }
  264. return ret;
  265. error:
  266. return -1;
  267. }
  268. // cass_retrieve_f*******************************************