1 /*************************************************************************
3 * Copyright 2020 highstreet technologies GmbH and others
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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 ***************************************************************************/
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"
30 #include <sysrepo/values.h>
32 #include "core/session.h"
33 #include "core/xpath.h"
34 #include "core/framework.h"
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;
39 int manual_notification_feature_get_status(void) {
40 return (manual_notification_subscription != 0);
43 int manual_notification_feature_start(sr_session_ctx_t *current_session) {
45 assert(current_session);
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);
50 log_error("error from sr_rpc_subscribe: %s\n", sr_strerror(rc));
51 return NTS_ERR_FAILED;
58 int manual_notification_feature_stop(void) {
61 if(manual_notification_subscription) {
62 int rc = sr_unsubscribe(manual_notification_subscription);
64 log_error("error from sr_rpc_subscribe: %s\n", sr_strerror(rc));
65 return NTS_ERR_FAILED;
68 manual_notification_subscription = 0;
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) {
79 rc = sr_new_values(*output_cnt, output);
84 rc = sr_val_set_xpath(output[0], NTS_NF_RPC_MANUAL_NOTIF_SCHEMA_XPATH"/status");
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;
96 else if(input[i].data.enum_val[0] == 'j') {
97 notif_format = LYD_JSON;
100 else if(strstr(input[i].xpath, "notification-object") != 0) {
101 notif_object = input[i].data.string_val;
105 struct lyd_node *notif = 0;
106 notif = lyd_parse_mem(session_context, notif_object, notif_format, LYD_OPT_NOTIF, 0);
108 rc = sr_event_notif_send_tree(session, notif);
111 rc = SR_ERR_VALIDATION_FAILED;
114 lyd_free_withsiblings(notif);
116 if(rc != SR_ERR_OK) {
117 rc = sr_val_build_str_data(output[0], SR_ENUM_T, "%s", "ERROR");
120 rc = sr_val_build_str_data(output[0], SR_ENUM_T, "%s", "SUCCESS");