1ad907bec3c38d9fd79d5047d74123b021e26593
[ric-plt/o1.git] / agent / pkg / nbi / helper.c
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
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
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
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 ==================================================================================
18 */
19
20 #include <string.h>
21 #include "helper.h"
22 #include "_cgo_export.h"
23 #include <sysrepo.h>
24 #include <libyang/libyang.h>
25
26
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);
29 }
30
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;
33     int rc = SR_ERR_OK;
34     sr_change_oper_t oper;
35     sr_val_t *old_value = NULL;
36     sr_val_t *new_value = NULL;
37     sr_val_t *val = NULL;
38     char *json_str = NULL;
39     struct lyd_node *root = NULL;
40     struct lyd_node *node = NULL;
41
42     rc = sr_get_changes_iter(session, "//." , &it);
43     if (rc != SR_ERR_OK) {
44         goto cleanup;
45     }
46
47     while ((rc = sr_get_change_next(session, it, &oper, &old_value, &new_value)) == SR_ERR_OK) {
48         *operation = oper;
49         val = oper == SR_OP_CREATED ? new_value : old_value;
50         char *leaf = sr_val_to_str(val);
51         if (root) {
52             lyd_new_path(root, NULL, val->xpath, (void *) leaf, LYD_NEW_PATH_UPDATE, &node);
53         } else {
54             lyd_new_path(NULL, sr_get_context(sr_session_get_connection(session)), val->xpath, (void *) leaf, LYD_NEW_PATH_UPDATE, &root);
55         }
56
57         sr_free_val(old_value);
58         sr_free_val(new_value);
59         if (leaf) {
60             free(leaf);
61         }
62     }
63
64     if (root) {
65         lyd_print_mem(&json_str, root, LYD_JSON, LYD_PRINT_WITHSIBLINGS);
66         printf("\n%s\n", json_str);
67     }
68
69 cleanup:
70     sr_free_change_iter(it);
71     return json_str;
72 }
73
74 char * get_data_json(sr_session_ctx_t *session, const char *module_name) {
75     struct lyd_node *data;
76     char *json_str = NULL;
77     int rc;
78     char xpath[100];
79
80     sprintf(xpath, "/%s:ric", module_name);
81
82     rc = sr_get_data(session, xpath, 0, 0, 0, &data);
83     if (rc != SR_ERR_OK) {
84         fprintf(stdout, "\nsr_get_data failed: %d\n", rc);
85         return NULL;
86     }
87
88     if (data) {
89         lyd_print_mem(&json_str, data, LYD_JSON, LYD_PRINT_WITHSIBLINGS);
90     }
91     return json_str;
92 }
93
94 sr_val_t *get_val(sr_val_t *val, size_t i) {
95         return &val[i];
96 }
97
98 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) {
99     return nbiGnbStateCB(session, (char *)module_name, (char *)xpath, (char *)req_xpath, req_id, (char **)parent);
100 }
101
102 void create_new_path(sr_session_ctx_t *session, char **parent, char *key, char *value) {
103     struct lyd_node **p = (struct lyd_node **)parent;
104     struct lyd_node *node;
105
106     if (*parent == NULL) {
107         lyd_new_path(NULL, sr_get_context(sr_session_get_connection(session)), key, value, LYD_NEW_PATH_UPDATE, &node);
108     } else {
109         lyd_new_path(*p, sr_get_context(sr_session_get_connection(session)), key, value, LYD_NEW_PATH_UPDATE, &node);
110     }
111 }