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/framework.h"
35 #define MANUAL_NOTIFICATION_RPC_SCHEMA_XPATH "/nts-network-function:invoke-notification"
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);
39 int manual_notification_feature_start(sr_session_ctx_t *current_session) {
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);
44 log_error("error from sr_rpc_subscribe: %s\n", sr_strerror(rc));
45 return NTS_ERR_FAILED;
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) {
55 rc = sr_new_values(*output_cnt, output);
60 rc = sr_val_set_xpath(output[0], MANUAL_NOTIFICATION_RPC_SCHEMA_XPATH"/status");
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;
72 else if(input[i].data.enum_val[0] == 'j') {
73 notif_format = LYD_JSON;
76 else if(strstr(input[i].xpath, "notification-object") != 0) {
77 notif_object = input[i].data.string_val;
81 struct lyd_node *notif = 0;
82 notif = lyd_parse_mem(session_context, notif_object, notif_format, LYD_OPT_NOTIF, 0);
84 rc = sr_event_notif_send_tree(session, notif);
87 rc = SR_ERR_VALIDATION_FAILED;
90 lyd_free_withsiblings(notif);
93 rc = sr_val_build_str_data(output[0], SR_ENUM_T, "%s", "ERROR");
96 rc = sr_val_build_str_data(output[0], SR_ENUM_T, "%s", "SUCCESS");