Add NF addressing method as ENV.
[sim/o1-interface.git] / ntsimulator / ntsim-ng / features / manual_notification / manual_notification.c
1 /*************************************************************************
2 *
3 * Copyright 2020 highstreet technologies GmbH and others
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 ***************************************************************************/
17
18 #define _GNU_SOURCE
19
20 #include "manual_notification.h"
21 #include "utils/log_utils.h"
22 #include "utils/sys_utils.h"
23 #include "utils/rand_utils.h"
24 #include "utils/http_client.h"
25 #include "utils/nts_utils.h"
26 #include <stdio.h>
27 #include <assert.h>
28
29 #include <sysrepo.h>
30 #include <sysrepo/values.h>
31
32 #include "core/session.h"
33 #include "core/xpath.h"
34 #include "core/framework.h"
35
36 static int manual_notification_pm_cb(sr_session_ctx_t *session, const char *path, const sr_val_t *input, const size_t input_cnt, sr_event_t event, uint32_t request_id, sr_val_t **output, size_t *output_cnt, void *private_data);
37 static sr_subscription_ctx_t *manual_notification_subscription = 0;
38
39 int manual_notification_feature_get_status(void) {
40     return (manual_notification_subscription != 0);
41 }
42
43 int manual_notification_feature_start(sr_session_ctx_t *current_session) {
44     assert_session();
45     assert(current_session);
46
47     if(manual_notification_subscription == 0) {
48         int rc = sr_rpc_subscribe(current_session, NTS_NF_RPC_MANUAL_NOTIF_SCHEMA_XPATH, manual_notification_pm_cb, 0, 0, SR_SUBSCR_CTX_REUSE, &manual_notification_subscription);
49         if(rc != SR_ERR_OK) {
50             log_error("error from sr_rpc_subscribe: %s\n", sr_strerror(rc));
51             return NTS_ERR_FAILED;
52         }
53     }
54
55     return NTS_ERR_OK;
56 }
57
58 int manual_notification_feature_stop(void) {
59     assert_session();
60
61     if(manual_notification_subscription) {
62         int rc = sr_unsubscribe(manual_notification_subscription);
63         if(rc != SR_ERR_OK) {
64             log_error("error from sr_rpc_subscribe: %s\n", sr_strerror(rc));
65             return NTS_ERR_FAILED;
66         }
67
68         manual_notification_subscription = 0;
69     }
70
71     return NTS_ERR_OK;
72 }
73
74
75 static int manual_notification_pm_cb(sr_session_ctx_t *session, const char *path, const sr_val_t *input, const size_t input_cnt, sr_event_t event, uint32_t request_id, sr_val_t **output, size_t *output_cnt, void *private_data) {
76     int rc;
77
78     *output_cnt = 1;
79     rc = sr_new_values(*output_cnt, output);
80     if(SR_ERR_OK != rc) {
81         return rc;
82     }
83
84     rc = sr_val_set_xpath(output[0], NTS_NF_RPC_MANUAL_NOTIF_SCHEMA_XPATH"/status");
85     if(SR_ERR_OK != rc) {
86         return rc;
87     }
88
89     LYD_FORMAT notif_format = LYD_UNKNOWN;
90     char *notif_object = 0;
91     for(int i = 0; i < input_cnt; i++) {
92         if(strstr(input[i].xpath, "notification-format") != 0) {
93             if(input[i].data.enum_val[0] == 'x') {
94                 notif_format = LYD_XML;
95             }
96             else if(input[i].data.enum_val[0] == 'j') {
97                 notif_format = LYD_JSON;
98             }
99         }
100         else if(strstr(input[i].xpath, "notification-object") != 0) {
101             notif_object = input[i].data.string_val;
102         }
103     }
104
105     struct lyd_node *notif = 0;
106     notif = lyd_parse_mem(session_context, notif_object, notif_format, LYD_OPT_NOTIF, 0);
107     if(notif) {
108         rc = sr_event_notif_send_tree(session, notif);
109     }
110     else {
111         rc = SR_ERR_VALIDATION_FAILED;
112     }
113
114     lyd_free_withsiblings(notif);
115
116     if(rc != SR_ERR_OK) {
117         rc = sr_val_build_str_data(output[0], SR_ENUM_T, "%s", "ERROR");
118     }
119     else {
120         rc = sr_val_build_str_data(output[0], SR_ENUM_T, "%s", "SUCCESS");
121     }
122
123     return rc;
124 }