ytree.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 identityref(struct lysc_ident *ident)
  61. {
  62. LY_ARRAY_COUNT_TYPE u = 0;
  63. if (!ident)
  64. return;
  65. if (!ident->derived) {
  66. printf(" %s", ident->name);
  67. return;
  68. }
  69. LY_ARRAY_FOR(ident->derived, u) {
  70. identityref(ident->derived[u]);
  71. }
  72. }
  73. static void process_node(const struct lysc_node *node, size_t level)
  74. {
  75. if (!node)
  76. return;
  77. printf("%*c %s [%s]",
  78. (int)(level * 2), ' ',
  79. node->name,
  80. lys_nodetype2str(node->nodetype));
  81. if (node->nodetype & LYS_LIST) {
  82. const struct lysc_node *iter = NULL;
  83. LY_LIST_FOR(lysc_node_child(node), iter) {
  84. if (lysc_is_key(iter))
  85. printf(" %s", iter->name);
  86. }
  87. } else if (node->nodetype & LYS_LEAF) {
  88. const struct lysc_node_leaf *leaf =
  89. (const struct lysc_node_leaf *)node;
  90. const struct lysc_type *type = leaf->type;
  91. if (type->basetype == LY_TYPE_IDENT) {
  92. struct lysc_type_identityref *iref =
  93. (struct lysc_type_identityref *)type;
  94. LY_ARRAY_COUNT_TYPE u = 0;
  95. LY_ARRAY_FOR(iref->bases, u) {
  96. identityref(iref->bases[u]);
  97. }
  98. }
  99. }
  100. printf("\n");
  101. iterate_nodes(lysc_node_child(node), level + 1);
  102. }
  103. static void iterate_nodes(const struct lysc_node *node, size_t level)
  104. {
  105. const struct lysc_node *iter = NULL;
  106. if (!node)
  107. return;
  108. LY_LIST_FOR(node, iter) {
  109. if (!(iter->nodetype & (
  110. LYS_CONTAINER |
  111. LYS_LIST |
  112. LYS_LEAF |
  113. LYS_LEAFLIST
  114. )))
  115. continue;
  116. if (!(iter->flags & LYS_CONFIG_W))
  117. continue;
  118. process_node(iter, level);
  119. }
  120. }
  121. int main(void)
  122. {
  123. int ret = -1;
  124. int err = SR_ERR_OK;
  125. sr_conn_ctx_t *conn = NULL;
  126. sr_session_ctx_t *sess = NULL;
  127. const struct ly_ctx *lyctx = NULL;
  128. uint32_t i = 0;
  129. struct lys_module *module = NULL;
  130. err = sr_connect(SR_CONN_DEFAULT, &conn);
  131. if (err) {
  132. printf("Error\n");
  133. goto out;
  134. }
  135. err = sr_session_start(conn, SR_DS_RUNNING, &sess);
  136. if (err) {
  137. printf("Error2\n");
  138. goto out;
  139. }
  140. lyctx = sr_acquire_context(conn);
  141. if (!lyctx) {
  142. printf("Cannot acquire context\n");
  143. goto out;
  144. }
  145. // Iterate all modules
  146. i = 0;
  147. while ((module = ly_ctx_get_module_iter(lyctx, &i))) {
  148. if (sr_module_is_internal(module))
  149. continue;
  150. if (!module->compiled)
  151. continue;
  152. if (!module->implemented)
  153. continue;
  154. if (!module->compiled->data)
  155. continue;
  156. printf("%s\n", module->name);
  157. iterate_nodes(module->compiled->data, 1);
  158. }
  159. // printf("Ok\n");
  160. ret = 0;
  161. out:
  162. sr_release_context(conn);
  163. sr_disconnect(conn);
  164. return 0;
  165. }