Add supoprt for D release use-case.
[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/framework.h"
34
35 #define MANUAL_NOTIFICATION_RPC_SCHEMA_XPATH         "/nts-network-function:invoke-notification"
36
37 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);
38 static sr_subscription_ctx_t *manual_notification_subscription = 0;
39
40 int manual_notification_feature_get_status(void) {
41     return (manual_notification_subscription != 0);
42 }
43
44 int manual_notification_feature_start(sr_session_ctx_t *current_session) {
45     assert_session();
46     assert(current_session);
47
48     if(manual_notification_subscription == 0) {
49         int rc = sr_rpc_subscribe(current_session, MANUAL_NOTIFICATION_RPC_SCHEMA_XPATH, manual_notification_pm_cb, 0, 0, SR_SUBSCR_CTX_REUSE, &manual_notification_subscription);
50         if(rc != SR_ERR_OK) {
51             log_error("error from sr_rpc_subscribe: %s\n", sr_strerror(rc));
52             return NTS_ERR_FAILED;
53         }
54     }
55
56     return NTS_ERR_OK;
57 }
58
59 int manual_notification_feature_stop(void) {
60     assert_session();
61
62     if(manual_notification_subscription) {
63         int rc = sr_unsubscribe(manual_notification_subscription);
64         if(rc != SR_ERR_OK) {
65             log_error("error from sr_rpc_subscribe: %s\n", sr_strerror(rc));
66             return NTS_ERR_FAILED;
67         }
68
69         manual_notification_subscription = 0;
70     }
71
72     return NTS_ERR_OK;
73 }
74
75
76 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) {
77     int rc;
78
79     *output_cnt = 1;
80     rc = sr_new_values(*output_cnt, output);
81     if(SR_ERR_OK != rc) {
82         return rc;
83     }
84
85     rc = sr_val_set_xpath(output[0], MANUAL_NOTIFICATION_RPC_SCHEMA_XPATH"/status");
86     if(SR_ERR_OK != rc) {
87         return rc;
88     }
89
90     LYD_FORMAT notif_format = LYD_UNKNOWN;
91     char *notif_object = 0;
92     for(int i = 0; i < input_cnt; i++) {
93         if(strstr(input[i].xpath, "notification-format") != 0) {
94             if(input[i].data.enum_val[0] == 'x') {
95                 notif_format = LYD_XML;
96             }
97             else if(input[i].data.enum_val[0] == 'j') {
98                 notif_format = LYD_JSON;
99             }
100         }
101         else if(strstr(input[i].xpath, "notification-object") != 0) {
102             notif_object = input[i].data.string_val;
103         }
104     }
105
106     struct lyd_node *notif = 0;
107     notif = lyd_parse_mem(session_context, notif_object, notif_format, LYD_OPT_NOTIF, 0);
108     if(notif) {
109         rc = sr_event_notif_send_tree(session, notif);
110     }
111     else {
112         rc = SR_ERR_VALIDATION_FAILED;
113     }
114
115     lyd_free_withsiblings(notif);
116
117     if(rc != SR_ERR_OK) {
118         rc = sr_val_build_str_data(output[0], SR_ENUM_T, "%s", "ERROR");
119     }
120     else {
121         rc = sr_val_build_str_data(output[0], SR_ENUM_T, "%s", "SUCCESS");
122     }
123
124     return rc;
125 }