ytree.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. //#include <libyang/libyang.h>
  4. //#include <libyang/tree_schema.h>
  5. #include <sysrepo.h>
  6. #include <sysrepo/xpath.h>
  7. #include <faux/faux.h>
  8. #include <faux/argv.h>
  9. #define NODETYPE_CONF (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST)
  10. static void process_node(const struct lysc_node *node, size_t level);
  11. static void iterate_nodes(const struct lysc_node *node, size_t level);
  12. static int
  13. sr_ly_module_is_internal(const struct lys_module *ly_mod)
  14. {
  15. if (!ly_mod->revision) {
  16. return 0;
  17. }
  18. if (!strcmp(ly_mod->name, "ietf-yang-metadata") && !strcmp(ly_mod->revision, "2016-08-05")) {
  19. return 1;
  20. } else if (!strcmp(ly_mod->name, "yang") && !strcmp(ly_mod->revision, "2021-04-07")) {
  21. return 1;
  22. } else if (!strcmp(ly_mod->name, "ietf-inet-types") && !strcmp(ly_mod->revision, "2013-07-15")) {
  23. return 1;
  24. } else if (!strcmp(ly_mod->name, "ietf-yang-types") && !strcmp(ly_mod->revision, "2013-07-15")) {
  25. return 1;
  26. }
  27. return 0;
  28. }
  29. int
  30. sr_module_is_internal(const struct lys_module *ly_mod)
  31. {
  32. if (!ly_mod->revision) {
  33. return 0;
  34. }
  35. if (sr_ly_module_is_internal(ly_mod)) {
  36. return 1;
  37. }
  38. if (!strcmp(ly_mod->name, "ietf-datastores") && !strcmp(ly_mod->revision, "2018-02-14")) {
  39. return 1;
  40. } else if (!strcmp(ly_mod->name, "ietf-yang-schema-mount")) {
  41. return 1;
  42. } else if (!strcmp(ly_mod->name, "ietf-yang-library")) {
  43. return 1;
  44. } else if (!strcmp(ly_mod->name, "ietf-netconf")) {
  45. return 1;
  46. } else if (!strcmp(ly_mod->name, "ietf-netconf-with-defaults") && !strcmp(ly_mod->revision, "2011-06-01")) {
  47. return 1;
  48. } else if (!strcmp(ly_mod->name, "ietf-origin") && !strcmp(ly_mod->revision, "2018-02-14")) {
  49. return 1;
  50. } else if (!strcmp(ly_mod->name, "ietf-netconf-notifications") && !strcmp(ly_mod->revision, "2012-02-06")) {
  51. return 1;
  52. } else if (!strcmp(ly_mod->name, "sysrepo")) {
  53. return 1;
  54. } else if (!strcmp(ly_mod->name, "sysrepo-monitoring")) {
  55. return 1;
  56. } else if (!strcmp(ly_mod->name, "sysrepo-plugind")) {
  57. return 1;
  58. } else if (!strcmp(ly_mod->name, "ietf-netconf-acm")) {
  59. return 1;
  60. }
  61. return 0;
  62. }
  63. static void identityref(struct lysc_ident *ident)
  64. {
  65. LY_ARRAY_COUNT_TYPE u = 0;
  66. if (!ident)
  67. return;
  68. if (!ident->derived) {
  69. printf(" %s", ident->name);
  70. return;
  71. }
  72. LY_ARRAY_FOR(ident->derived, u) {
  73. identityref(ident->derived[u]);
  74. }
  75. }
  76. static void process_node(const struct lysc_node *node, size_t level)
  77. {
  78. if (!node)
  79. return;
  80. printf("%*c %s [%s]",
  81. (int)(level * 2), ' ',
  82. node->name,
  83. lys_nodetype2str(node->nodetype));
  84. if (node->nodetype & LYS_LIST) {
  85. const struct lysc_node *iter = NULL;
  86. LY_LIST_FOR(lysc_node_child(node), iter) {
  87. if (lysc_is_key(iter))
  88. printf(" %s", iter->name);
  89. }
  90. } else if (node->nodetype & LYS_LEAF) {
  91. const struct lysc_node_leaf *leaf =
  92. (const struct lysc_node_leaf *)node;
  93. const struct lysc_type *type = leaf->type;
  94. if (type->basetype == LY_TYPE_IDENT) {
  95. struct lysc_type_identityref *iref =
  96. (struct lysc_type_identityref *)type;
  97. LY_ARRAY_COUNT_TYPE u = 0;
  98. LY_ARRAY_FOR(iref->bases, u) {
  99. identityref(iref->bases[u]);
  100. }
  101. }
  102. }
  103. printf("\n");
  104. iterate_nodes(lysc_node_child(node), level + 1);
  105. }
  106. static void iterate_nodes(const struct lysc_node *node, size_t level)
  107. {
  108. const struct lysc_node *iter = NULL;
  109. if (!node)
  110. return;
  111. LY_LIST_FOR(node, iter) {
  112. if (!(iter->nodetype & (
  113. LYS_CONTAINER |
  114. LYS_LIST |
  115. LYS_LEAF |
  116. LYS_LEAFLIST
  117. )))
  118. continue;
  119. if (!(iter->flags & LYS_CONFIG_W))
  120. continue;
  121. process_node(iter, level);
  122. }
  123. }
  124. void show(const struct ly_ctx *ctx)
  125. {
  126. struct lys_module *module = NULL;
  127. uint32_t i = 0;
  128. // Iterate all modules
  129. i = 0;
  130. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  131. if (sr_module_is_internal(module))
  132. continue;
  133. if (!module->compiled)
  134. continue;
  135. if (!module->implemented)
  136. continue;
  137. if (!module->compiled->data)
  138. continue;
  139. printf("%s\n", module->name);
  140. iterate_nodes(module->compiled->data, 1);
  141. }
  142. }
  143. const struct lysc_node *ppath2path_node(const struct lys_module *module,
  144. const struct lysc_node *parent, const faux_argv_t *argv)
  145. {
  146. const struct lysc_node *node = NULL;
  147. const char *search = faux_argv_index(argv, 0);
  148. printf("searching for %s\n", search);
  149. node = lys_find_child(node, module, search, 0,
  150. NODETYPE_CONF, 0);
  151. if (node)
  152. printf("%s\n", search);
  153. return node;
  154. }
  155. void ppath2path(const struct ly_ctx *ctx, const char *ppath)
  156. {
  157. faux_argv_t *argv = NULL;
  158. struct lys_module *module = NULL;
  159. uint32_t i = 0;
  160. argv = faux_argv_new();
  161. faux_argv_parse(argv, ppath);
  162. // Iterate all modules
  163. i = 0;
  164. while ((module = ly_ctx_get_module_iter(ctx, &i))) {
  165. if (sr_module_is_internal(module))
  166. continue;
  167. if (!module->compiled)
  168. continue;
  169. if (!module->implemented)
  170. continue;
  171. if (!module->compiled->data)
  172. continue;
  173. printf("Module %s\n", module->name);
  174. if (ppath2path_node(module, NULL, argv)) {
  175. printf("Found\n");
  176. }
  177. }
  178. }
  179. int main(void)
  180. {
  181. int ret = -1;
  182. int err = SR_ERR_OK;
  183. sr_conn_ctx_t *conn = NULL;
  184. sr_session_ctx_t *sess = NULL;
  185. const struct ly_ctx *ctx = NULL;
  186. err = sr_connect(SR_CONN_DEFAULT, &conn);
  187. if (err) {
  188. printf("Error\n");
  189. goto out;
  190. }
  191. err = sr_session_start(conn, SR_DS_RUNNING, &sess);
  192. if (err) {
  193. printf("Error2\n");
  194. goto out;
  195. }
  196. ctx = sr_acquire_context(conn);
  197. if (!ctx) {
  198. printf("Cannot acquire context\n");
  199. goto out;
  200. }
  201. ppath2path(ctx, "interfaces interface eth0 type");
  202. // show(ctx);
  203. // printf("Ok\n");
  204. ret = 0;
  205. out:
  206. sr_release_context(conn);
  207. sr_disconnect(conn);
  208. return 0;
  209. }