ytree.c 5.8 KB

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