e76fa6105ddd8a560a9dff2c5f116ca65a0e0c15
[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
39 int manual_notification_feature_start(sr_session_ctx_t *current_session) {
40     assert_session();
41
42     int rc = sr_rpc_subscribe(current_session, MANUAL_NOTIFICATION_RPC_SCHEMA_XPATH, manual_notification_pm_cb, 0, 0, SR_SUBSCR_CTX_REUSE, &session_subscription);
43     if(rc != SR_ERR_OK) {
44         log_error("error from sr_rpc_subscribe: %s\n", sr_strerror(rc));
45         return NTS_ERR_FAILED;
46     }
47
48     return NTS_ERR_OK;
49 }
50
51 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) {
52     int rc;
53
54     *output_cnt = 1;
55     rc = sr_new_values(*output_cnt, output);
56     if(SR_ERR_OK != rc) {
57         return rc;
58     }
59
60     rc = sr_val_set_xpath(output[0], MANUAL_NOTIFICATION_RPC_SCHEMA_XPATH"/status");
61     if(SR_ERR_OK != rc) {
62         return rc;
63     }
64
65     LYD_FORMAT notif_format = LYD_UNKNOWN;
66     char *notif_object = 0;
67     for(int i = 0; i < input_cnt; i++) {
68         if(strstr(input[i].xpath, "notification-format") != 0) {
69             if(input[i].data.enum_val[0] == 'x') {
70                 notif_format = LYD_XML;
71             }
72             else if(input[i].data.enum_val[0] == 'j') {
73                 notif_format = LYD_JSON;
74             }
75         }
76         else if(strstr(input[i].xpath, "notification-object") != 0) {
77             notif_object = input[i].data.string_val;
78         }
79     }
80
81     struct lyd_node *notif = 0;
82     notif = lyd_parse_mem(session_context, notif_object, notif_format, LYD_OPT_NOTIF, 0);
83     if(notif) {
84         rc = sr_event_notif_send_tree(session, notif);
85     }
86     else {
87         rc = SR_ERR_VALIDATION_FAILED;
88     }
89
90     lyd_free_withsiblings(notif);
91
92     if(rc != SR_ERR_OK) {
93         rc = sr_val_build_str_data(output[0], SR_ENUM_T, "%s", "ERROR");
94     }
95     else {
96         rc = sr_val_build_str_data(output[0], SR_ENUM_T, "%s", "SUCCESS");
97     }
98
99     return rc;
100 }