ytree.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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]",
  65. (int)(level * 2), ' ',
  66. node->name,
  67. lys_nodetype2str(node->nodetype));
  68. if (node->nodetype & LYS_LIST) {
  69. const struct lysc_node *iter = NULL;
  70. LY_LIST_FOR(lysc_node_child(node), iter) {
  71. if (lysc_is_key(iter))
  72. printf(" %s", iter->name);
  73. }
  74. }
  75. printf("\n");
  76. iterate_nodes(lysc_node_child(node), level + 1);
  77. }
  78. static void iterate_nodes(const struct lysc_node *node, size_t level)
  79. {
  80. const struct lysc_node *iter = NULL;
  81. if (!node)
  82. return;
  83. LY_LIST_FOR(node, iter) {
  84. if (!(iter->nodetype & (
  85. LYS_CONTAINER |
  86. LYS_LIST |
  87. LYS_LEAF |
  88. LYS_LEAFLIST
  89. )))
  90. continue;
  91. if (!(iter->flags & LYS_CONFIG_W))
  92. continue;
  93. process_node(iter, level);
  94. }
  95. }
  96. int main(void)
  97. {
  98. int ret = -1;
  99. int err = SR_ERR_OK;
  100. sr_conn_ctx_t *conn = NULL;
  101. sr_session_ctx_t *sess = NULL;
  102. const struct ly_ctx *lyctx = NULL;
  103. uint32_t i = 0;
  104. struct lys_module *module = NULL;
  105. err = sr_connect(SR_CONN_DEFAULT, &conn);
  106. if (err) {
  107. printf("Error\n");
  108. goto out;
  109. }
  110. err = sr_session_start(conn, SR_DS_RUNNING, &sess);
  111. if (err) {
  112. printf("Error2\n");
  113. goto out;
  114. }
  115. lyctx = sr_acquire_context(conn);
  116. if (!lyctx) {
  117. printf("Cannot acquire context\n");
  118. goto out;
  119. }
  120. // Iterate all modules
  121. i = 0;
  122. while ((module = ly_ctx_get_module_iter(lyctx, &i))) {
  123. if (sr_module_is_internal(module))
  124. continue;
  125. if (!module->compiled)
  126. continue;
  127. if (!module->implemented)
  128. continue;
  129. if (!module->compiled->data)
  130. continue;
  131. printf("%s\n", module->name);
  132. iterate_nodes(module->compiled->data, 1);
  133. }
  134. // printf("Ok\n");
  135. ret = 0;
  136. out:
  137. sr_release_context(conn);
  138. sr_disconnect(conn);
  139. return 0;
  140. }