You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

330 lines
10 KiB

  1. From 97cd849362b45ecbcb20194b5771c5ce777de6bc Mon Sep 17 00:00:00 2001
  2. From: Renato Westphal <renato@opensourcerouting.org>
  3. Date: Tue, 21 Apr 2020 21:27:47 -0300
  4. Subject: [PATCH] lib: create a wrapper function for all northbound callbacks
  5. The intention here is to keep the code more organized. These wrappers
  6. should be used by the northbound clients only, and never directly
  7. by any YANG backend code.
  8. Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
  9. ---
  10. lib/northbound.c | 222 +++++++++++++++++++++++-----------------
  11. lib/northbound_grpc.cpp | 3 +-
  12. 2 files changed, 131 insertions(+), 94 deletions(-)
  13. diff --git a/lib/northbound.c b/lib/northbound.c
  14. index 85e723d7cf..d10e4713f5 100644
  15. --- a/lib/northbound.c
  16. +++ b/lib/northbound.c
  17. @@ -62,11 +62,10 @@ static struct {
  18. */
  19. static bool transaction_in_progress;
  20. +static int nb_callback_pre_validate(const struct nb_node *nb_node,
  21. + const struct lyd_node *dnode);
  22. static int nb_callback_configuration(const enum nb_event event,
  23. struct nb_config_change *change);
  24. -static void nb_log_callback(const enum nb_event event,
  25. - enum nb_operation operation, const char *xpath,
  26. - const char *value);
  27. static struct nb_transaction *nb_transaction_new(struct nb_config *config,
  28. struct nb_config_cbs *changes,
  29. enum nb_client client,
  30. @@ -609,18 +608,7 @@ static int nb_candidate_validate_code(struct nb_config *candidate,
  31. if (!nb_node->cbs.pre_validate)
  32. goto next;
  33. - if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config,
  34. - DEBUG_MODE_ALL)) {
  35. - char xpath[XPATH_MAXLEN];
  36. -
  37. - yang_dnode_get_path(child, xpath,
  38. - sizeof(xpath));
  39. - nb_log_callback(NB_EV_VALIDATE,
  40. - NB_OP_PRE_VALIDATE, xpath,
  41. - NULL);
  42. - }
  43. -
  44. - ret = (*nb_node->cbs.pre_validate)(child);
  45. + ret = nb_callback_pre_validate(nb_node, child);
  46. if (ret != NB_OK)
  47. return NB_ERR_VALIDATION;
  48. @@ -791,14 +779,128 @@ int nb_running_lock_check(enum nb_client client, const void *user)
  49. return ret;
  50. }
  51. -static void nb_log_callback(const enum nb_event event,
  52. - enum nb_operation operation, const char *xpath,
  53. - const char *value)
  54. +static void nb_log_config_callback(const enum nb_event event,
  55. + enum nb_operation operation,
  56. + const struct lyd_node *dnode)
  57. {
  58. + const char *value;
  59. + char xpath[XPATH_MAXLEN];
  60. +
  61. + if (!DEBUG_MODE_CHECK(&nb_dbg_cbs_config, DEBUG_MODE_ALL))
  62. + return;
  63. +
  64. + yang_dnode_get_path(dnode, xpath, sizeof(xpath));
  65. + if (yang_snode_is_typeless_data(dnode->schema))
  66. + value = "(none)";
  67. + else
  68. + value = yang_dnode_get_string(dnode, NULL);
  69. +
  70. zlog_debug(
  71. "northbound callback: event [%s] op [%s] xpath [%s] value [%s]",
  72. nb_event_name(event), nb_operation_name(operation), xpath,
  73. - value ? value : "(NULL)");
  74. + value);
  75. +}
  76. +
  77. +static int nb_callback_create(const struct nb_node *nb_node,
  78. + enum nb_event event, const struct lyd_node *dnode,
  79. + union nb_resource *resource)
  80. +{
  81. + nb_log_config_callback(event, NB_OP_CREATE, dnode);
  82. +
  83. + return nb_node->cbs.create(event, dnode, resource);
  84. +}
  85. +
  86. +static int nb_callback_modify(const struct nb_node *nb_node,
  87. + enum nb_event event, const struct lyd_node *dnode,
  88. + union nb_resource *resource)
  89. +{
  90. + nb_log_config_callback(event, NB_OP_MODIFY, dnode);
  91. +
  92. + return nb_node->cbs.modify(event, dnode, resource);
  93. +}
  94. +
  95. +static int nb_callback_destroy(const struct nb_node *nb_node,
  96. + enum nb_event event,
  97. + const struct lyd_node *dnode)
  98. +{
  99. + nb_log_config_callback(event, NB_OP_DESTROY, dnode);
  100. +
  101. + return nb_node->cbs.destroy(event, dnode);
  102. +}
  103. +
  104. +static int nb_callback_move(const struct nb_node *nb_node, enum nb_event event,
  105. + const struct lyd_node *dnode)
  106. +{
  107. + nb_log_config_callback(event, NB_OP_MOVE, dnode);
  108. +
  109. + return nb_node->cbs.move(event, dnode);
  110. +}
  111. +
  112. +static int nb_callback_pre_validate(const struct nb_node *nb_node,
  113. + const struct lyd_node *dnode)
  114. +{
  115. + nb_log_config_callback(NB_EV_VALIDATE, NB_OP_PRE_VALIDATE, dnode);
  116. +
  117. + return nb_node->cbs.pre_validate(dnode);
  118. +}
  119. +
  120. +static void nb_callback_apply_finish(const struct nb_node *nb_node,
  121. + const struct lyd_node *dnode)
  122. +{
  123. + nb_log_config_callback(NB_EV_APPLY, NB_OP_APPLY_FINISH, dnode);
  124. +
  125. + nb_node->cbs.apply_finish(dnode);
  126. +}
  127. +
  128. +struct yang_data *nb_callback_get_elem(const struct nb_node *nb_node,
  129. + const char *xpath,
  130. + const void *list_entry)
  131. +{
  132. + DEBUGD(&nb_dbg_cbs_state,
  133. + "northbound callback (get_elem): xpath [%s] list_entry [%p]",
  134. + xpath, list_entry);
  135. +
  136. + return nb_node->cbs.get_elem(xpath, list_entry);
  137. +}
  138. +
  139. +const void *nb_callback_get_next(const struct nb_node *nb_node,
  140. + const void *parent_list_entry,
  141. + const void *list_entry)
  142. +{
  143. + DEBUGD(&nb_dbg_cbs_state,
  144. + "northbound callback (get_next): node [%s] parent_list_entry [%p] list_entry [%p]",
  145. + nb_node->xpath, parent_list_entry, list_entry);
  146. +
  147. + return nb_node->cbs.get_next(parent_list_entry, list_entry);
  148. +}
  149. +
  150. +int nb_callback_get_keys(const struct nb_node *nb_node, const void *list_entry,
  151. + struct yang_list_keys *keys)
  152. +{
  153. + DEBUGD(&nb_dbg_cbs_state,
  154. + "northbound callback (get_keys): node [%s] list_entry [%p]",
  155. + nb_node->xpath, list_entry);
  156. +
  157. + return nb_node->cbs.get_keys(list_entry, keys);
  158. +}
  159. +
  160. +const void *nb_callback_lookup_entry(const struct nb_node *nb_node,
  161. + const void *parent_list_entry,
  162. + const struct yang_list_keys *keys)
  163. +{
  164. + DEBUGD(&nb_dbg_cbs_state,
  165. + "northbound callback (lookup_entry): node [%s] parent_list_entry [%p]",
  166. + nb_node->xpath, parent_list_entry);
  167. +
  168. + return nb_node->cbs.lookup_entry(parent_list_entry, keys);
  169. +}
  170. +
  171. +int nb_callback_rpc(const struct nb_node *nb_node, const char *xpath,
  172. + const struct list *input, struct list *output)
  173. +{
  174. + DEBUGD(&nb_dbg_cbs_rpc, "northbound RPC: %s", xpath);
  175. +
  176. + return nb_node->cbs.rpc(xpath, input, output);
  177. }
  178. /*
  179. @@ -815,15 +917,6 @@ static int nb_callback_configuration(const enum nb_event event,
  180. union nb_resource *resource;
  181. int ret = NB_ERR;
  182. - if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config, DEBUG_MODE_ALL)) {
  183. - const char *value = "(none)";
  184. -
  185. - if (dnode && !yang_snode_is_typeless_data(dnode->schema))
  186. - value = yang_dnode_get_string(dnode, NULL);
  187. -
  188. - yang_dnode_get_path(dnode, xpath, sizeof(xpath));
  189. - nb_log_callback(event, operation, xpath, value);
  190. - }
  191. if (event == NB_EV_VALIDATE)
  192. resource = NULL;
  193. @@ -832,16 +925,16 @@ static int nb_callback_configuration(const enum nb_event event,
  194. switch (operation) {
  195. case NB_OP_CREATE:
  196. - ret = (*nb_node->cbs.create)(event, dnode, resource);
  197. + ret = nb_callback_create(nb_node, event, dnode, resource);
  198. break;
  199. case NB_OP_MODIFY:
  200. - ret = (*nb_node->cbs.modify)(event, dnode, resource);
  201. + ret = nb_callback_modify(nb_node, event, dnode, resource);
  202. break;
  203. case NB_OP_DESTROY:
  204. - ret = (*nb_node->cbs.destroy)(event, dnode);
  205. + ret = nb_callback_destroy(nb_node, event, dnode);
  206. break;
  207. case NB_OP_MOVE:
  208. - ret = (*nb_node->cbs.move)(event, dnode);
  209. + ret = nb_callback_move(nb_node, event, dnode);
  210. break;
  211. default:
  212. yang_dnode_get_path(dnode, xpath, sizeof(xpath));
  213. @@ -890,57 +983,6 @@ static int nb_callback_configuration(const enum nb_event event,
  214. return ret;
  215. }
  216. -struct yang_data *nb_callback_get_elem(const struct nb_node *nb_node,
  217. - const char *xpath,
  218. - const void *list_entry)
  219. -{
  220. - DEBUGD(&nb_dbg_cbs_state,
  221. - "northbound callback (get_elem): xpath [%s] list_entry [%p]",
  222. - xpath, list_entry);
  223. -
  224. - return nb_node->cbs.get_elem(xpath, list_entry);
  225. -}
  226. -
  227. -const void *nb_callback_get_next(const struct nb_node *nb_node,
  228. - const void *parent_list_entry,
  229. - const void *list_entry)
  230. -{
  231. - DEBUGD(&nb_dbg_cbs_state,
  232. - "northbound callback (get_next): node [%s] parent_list_entry [%p] list_entry [%p]",
  233. - nb_node->xpath, parent_list_entry, list_entry);
  234. -
  235. - return nb_node->cbs.get_next(parent_list_entry, list_entry);
  236. -}
  237. -
  238. -int nb_callback_get_keys(const struct nb_node *nb_node, const void *list_entry,
  239. - struct yang_list_keys *keys)
  240. -{
  241. - DEBUGD(&nb_dbg_cbs_state,
  242. - "northbound callback (get_keys): node [%s] list_entry [%p]",
  243. - nb_node->xpath, list_entry);
  244. -
  245. - return nb_node->cbs.get_keys(list_entry, keys);
  246. -}
  247. -
  248. -const void *nb_callback_lookup_entry(const struct nb_node *nb_node,
  249. - const void *parent_list_entry,
  250. - const struct yang_list_keys *keys)
  251. -{
  252. - DEBUGD(&nb_dbg_cbs_state,
  253. - "northbound callback (lookup_entry): node [%s] parent_list_entry [%p]",
  254. - nb_node->xpath, parent_list_entry);
  255. -
  256. - return nb_node->cbs.lookup_entry(parent_list_entry, keys);
  257. -}
  258. -
  259. -int nb_callback_rpc(const struct nb_node *nb_node, const char *xpath,
  260. - const struct list *input, struct list *output)
  261. -{
  262. - DEBUGD(&nb_dbg_cbs_rpc, "northbound RPC: %s", xpath);
  263. -
  264. - return nb_node->cbs.rpc(xpath, input, output);
  265. -}
  266. -
  267. static struct nb_transaction *
  268. nb_transaction_new(struct nb_config *config, struct nb_config_cbs *changes,
  269. enum nb_client client, const void *user, const char *comment)
  270. @@ -1058,7 +1100,6 @@ static void nb_transaction_apply_finish(struct nb_transaction *transaction)
  271. {
  272. struct nb_config_cbs cbs;
  273. struct nb_config_cb *cb;
  274. - char xpath[XPATH_MAXLEN];
  275. /* Initialize tree of 'apply_finish' callbacks. */
  276. RB_INIT(nb_config_cbs, &cbs);
  277. @@ -1075,6 +1116,8 @@ static void nb_transaction_apply_finish(struct nb_transaction *transaction)
  278. * be called though).
  279. */
  280. if (change->cb.operation == NB_OP_DESTROY) {
  281. + char xpath[XPATH_MAXLEN];
  282. +
  283. dnode = dnode->parent;
  284. if (!dnode)
  285. break;
  286. @@ -1111,15 +1154,8 @@ static void nb_transaction_apply_finish(struct nb_transaction *transaction)
  287. }
  288. /* Call the 'apply_finish' callbacks, sorted by their priorities. */
  289. - RB_FOREACH (cb, nb_config_cbs, &cbs) {
  290. - if (DEBUG_MODE_CHECK(&nb_dbg_cbs_config, DEBUG_MODE_ALL)) {
  291. - yang_dnode_get_path(cb->dnode, xpath, sizeof(xpath));
  292. - nb_log_callback(NB_EV_APPLY, NB_OP_APPLY_FINISH, xpath,
  293. - NULL);
  294. - }
  295. -
  296. - (*cb->nb_node->cbs.apply_finish)(cb->dnode);
  297. - }
  298. + RB_FOREACH (cb, nb_config_cbs, &cbs)
  299. + nb_callback_apply_finish(cb->nb_node, cb->dnode);
  300. /* Release memory. */
  301. while (!RB_EMPTY(nb_config_cbs, &cbs)) {
  302. diff --git a/lib/northbound_grpc.cpp b/lib/northbound_grpc.cpp
  303. index b195f1aeca..66bf05c1ab 100644
  304. --- a/lib/northbound_grpc.cpp
  305. +++ b/lib/northbound_grpc.cpp
  306. @@ -545,7 +545,8 @@ class NorthboundImpl final : public frr::Northbound::Service
  307. }
  308. // Execute callback registered for this XPath.
  309. - if (nb_node->cbs.rpc(xpath, input_list, output_list) != NB_OK) {
  310. + if (nb_callback_rpc(nb_node, xpath, input_list, output_list)
  311. + != NB_OK) {
  312. flog_warn(EC_LIB_NB_CB_RPC,
  313. "%s: rpc callback failed: %s", __func__,
  314. xpath);