ytree.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. static void process_node(const struct lysc_node *node, size_t level);
  8. static void iterate_nodes(const struct lysc_node *node, size_t level);
  9. static int
  10. sr_ly_module_is_internal(const struct lys_module *ly_mod)
  11. {
  12. if (!ly_mod->revision) {
  13. return 0;
  14. }
  15. if (!strcmp(ly_mod->name, "ietf-yang-metadata") && !strcmp(ly_mod->revision, "2016-08-05")) {
  16. return 1;
  17. } else if (!strcmp(ly_mod->name, "yang") && !strcmp(ly_mod->revision, "2021-04-07")) {
  18. return 1;
  19. } else if (!strcmp(ly_mod->name, "ietf-inet-types") && !strcmp(ly_mod->revision, "2013-07-15")) {
  20. return 1;
  21. } else if (!strcmp(ly_mod->name, "ietf-yang-types") && !strcmp(ly_mod->revision, "2013-07-15")) {
  22. return 1;
  23. }
  24. return 0;
  25. }
  26. int
  27. sr_module_is_internal(const struct lys_module *ly_mod)
  28. {
  29. if (!ly_mod->revision) {
  30. return 0;
  31. }
  32. if (sr_ly_module_is_internal(ly_mod)) {
  33. return 1;
  34. }
  35. if (!strcmp(ly_mod->name, "ietf-datastores") && !strcmp(ly_mod->revision, "2018-02-14")) {
  36. return 1;
  37. } else if (!strcmp(ly_mod->name, "ietf-yang-schema-mount")) {
  38. return 1;
  39. } else if (!strcmp(ly_mod->name, "ietf-yang-library")) {
  40. return 1;
  41. } else if (!strcmp(ly_mod->name, "ietf-netconf")) {
  42. return 1;
  43. } else if (!strcmp(ly_mod->name, "ietf-netconf-with-defaults") && !strcmp(ly_mod->revision, "2011-06-01")) {
  44. return 1;
  45. } else if (!strcmp(ly_mod->name, "ietf-origin") && !strcmp(ly_mod->revision, "2018-02-14")) {
  46. return 1;
  47. } else if (!strcmp(ly_mod->name, "ietf-netconf-notifications") && !strcmp(ly_mod->revision, "2012-02-06")) {
  48. return 1;
  49. } else if (!strcmp(ly_mod->name, "sysrepo")) {
  50. return 1;
  51. } else if (!strcmp(ly_mod->name, "sysrepo-monitoring")) {
  52. return 1;
  53. } else if (!strcmp(ly_mod->name, "sysrepo-plugind")) {
  54. return 1;
  55. } else if (!strcmp(ly_mod->name, "ietf-netconf-acm")) {
  56. return 1;
  57. }
  58. return 0;
  59. }
  60. static void process_node(const struct lysc_node *node, size_t level)
  61. {
  62. if (!node)
  63. return;
  64. printf("%*c %s [%s]\n",
  65. (int)(level * 2), ' ',
  66. node->name,
  67. lys_nodetype2str(node->nodetype));
  68. iterate_nodes(lysc_node_child(node), level + 1);
  69. }
  70. static void iterate_nodes(const struct lysc_node *node, size_t level)
  71. {
  72. const struct lysc_node *iter = NULL;
  73. if (!node)
  74. return;
  75. LY_LIST_FOR(node, iter) {
  76. if (!(iter->nodetype & (
  77. LYS_CONTAINER |
  78. LYS_LIST |
  79. LYS_LEAF |
  80. LYS_LEAFLIST
  81. )))
  82. continue;
  83. if (!(iter->flags & LYS_CONFIG_W))
  84. continue;
  85. process_node(iter, level);
  86. }
  87. }
  88. int main(void)
  89. {
  90. int ret = -1;
  91. int err = SR_ERR_OK;
  92. sr_conn_ctx_t *conn = NULL;
  93. sr_session_ctx_t *sess = NULL;
  94. const struct ly_ctx *lyctx = NULL;
  95. uint32_t i = 0;
  96. struct lys_module *module = NULL;
  97. err = sr_connect(SR_CONN_DEFAULT, &conn);
  98. if (err) {
  99. printf("Error\n");
  100. goto out;
  101. }
  102. err = sr_session_start(conn, SR_DS_RUNNING, &sess);
  103. if (err) {
  104. printf("Error2\n");
  105. goto out;
  106. }
  107. lyctx = sr_acquire_context(conn);
  108. if (!lyctx) {
  109. printf("Cannot acquire context\n");
  110. goto out;
  111. }
  112. // Iterate all modules
  113. i = 0;
  114. while ((module = ly_ctx_get_module_iter(lyctx, &i))) {
  115. if (sr_module_is_internal(module))
  116. continue;
  117. if (!module->compiled)
  118. continue;
  119. if (!module->implemented)
  120. continue;
  121. if (!module->compiled->data)
  122. continue;
  123. printf("%s\n", module->name);
  124. iterate_nodes(module->compiled->data, 1);
  125. }
  126. // printf("Ok\n");
  127. ret = 0;
  128. out:
  129. sr_release_context(conn);
  130. sr_disconnect(conn);
  131. return 0;
  132. }