2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
22 #include "_cgo_export.h"
24 #include <libyang/libyang.h>
27 int module_change_cb(sr_session_ctx_t *session, const char *module_name, const char *xpath, sr_event_t event, uint32_t req_id, void *priv_ctx) {
28 return nbiModuleChangeCB(session, (char *)module_name, (char *)xpath, event, (int)req_id);
31 char * yang_data_sr2json(sr_session_ctx_t *session, const char *module_name, sr_event_t event, sr_change_oper_t *operation) {
32 sr_change_iter_t *it = NULL;
34 sr_change_oper_t oper;
35 sr_val_t *old_value = NULL;
36 sr_val_t *new_value = NULL;
38 char *json_str = NULL;
39 struct lyd_node *root = NULL;
41 rc = sr_get_changes_iter(session, "//." , &it);
42 if (rc != SR_ERR_OK) {
46 while ((rc = sr_get_change_next(session, it, &oper, &old_value, &new_value)) == SR_ERR_OK) {
48 val = oper == SR_OP_CREATED ? new_value : old_value;
49 char *leaf = sr_val_to_str(val);
51 lyd_new_path(root, NULL, val->xpath, (void *) leaf, 0, 1);
53 root = lyd_new_path(NULL, sr_get_context(sr_session_get_connection(session)), val->xpath, (void *) leaf, 0, 1);
56 sr_free_val(old_value);
57 sr_free_val(new_value);
64 lyd_print_mem(&json_str, root, LYD_JSON, LYP_WITHSIBLINGS | LYP_FORMAT);
65 printf("\n%s\n", json_str);
69 sr_free_change_iter(it);
73 char * get_data_json(sr_session_ctx_t *session, const char *module_name) {
74 struct lyd_node *data;
75 char *json_str = NULL;
79 sprintf(xpath, "/%s:ric", module_name);
81 rc = sr_get_data(session, xpath, 0, 0, 0, &data);
82 if (rc != SR_ERR_OK) {
83 fprintf(stdout, "\nsr_get_data failed: %d\n", rc);
88 lyd_print_mem(&json_str, data, LYD_JSON, LYP_WITHSIBLINGS | LYP_FORMAT);
93 sr_val_t *get_val(sr_val_t *val, size_t i) {
97 int gnb_status_cb(sr_session_ctx_t *session, const char *module_name, const char *xpath, const char *req_xpath, uint32_t req_id, struct lyd_node **parent, void *private_data) {
98 return nbiGnbStateCB(session, (char *)module_name, (char *)xpath, (char *)req_xpath, req_id, (char **)parent);
101 void create_new_path(sr_session_ctx_t *session, char **parent, char *key, char *value) {
102 struct lyd_node **p = (struct lyd_node **)parent;
104 if (*parent == NULL) {
105 *p = lyd_new_path(NULL, sr_get_context(sr_session_get_connection(session)), key, value, 0, 0);
107 lyd_new_path(*p, sr_get_context(sr_session_get_connection(session)), key, value, 0, 0);