Add NF addressing method as ENV.
[sim/o1-interface.git] / ntsimulator / ntsim-ng / features / web_cut_through / web_cut_through.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 "web_cut_through.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 "core/session.h"
30 #include "core/framework.h"
31 #include "core/xpath.h"
32
33 static int web_cut_through_status = 0;
34
35 int web_cut_through_feature_get_status(void) {
36     return web_cut_through_status;
37 }
38
39 int web_cut_through_feature_start(sr_session_ctx_t *current_session) {
40     assert(current_session);
41     assert_session();
42
43     if(web_cut_through_status == 0) {
44         //update ietf-system details
45         int rc = sr_set_item_str(current_session, IETF_SYSTEM_NAME_SCHEMA_XPATH, framework_environment.settings.hostname, 0, 0);
46         if(rc != SR_ERR_OK) {
47             log_error("sr_set_item_str failed\n");
48             return NTS_ERR_FAILED;
49         }
50
51         controller_details_t *controller_details = controller_details_get(current_session);
52         if(controller_details == 0) {
53             log_error("controller_details_get failed\n");
54             return NTS_ERR_FAILED;
55         }
56
57         char *web_ui = 0;
58         asprintf(&web_ui, "%s/odlux/index.html#/configuration/%s", controller_details->base_url, framework_environment.settings.hostname);
59         controller_details_free(controller_details);
60
61         if(web_ui == 0) {
62             log_error("asprintf failed\n");
63             return NTS_ERR_FAILED;
64         }
65
66         rc = sr_set_item_str(current_session, IETF_SYSTEM_WEB_UI_SCHEMA_XPATH, web_ui, 0, 0);
67         free(web_ui);
68         if(rc != SR_ERR_OK) {
69             log_error("sr_set_item_str failed\n");
70             return NTS_ERR_FAILED;
71         }
72
73         rc = sr_set_item_str(current_session, IETF_SYSTEM_CONTACT_SCHEMA_XPATH, "O-RAN-SC SIM project", 0, 0);
74         if(rc != SR_ERR_OK) {
75             log_error("sr_set_item_str failed\n");
76             return NTS_ERR_FAILED;
77         }
78
79         rc = sr_set_item_str(current_session, IETF_SYSTEM_HOSTNAME_SCHEMA_XPATH, framework_environment.settings.hostname, 0, 0);
80         if(rc != SR_ERR_OK) {
81             log_error("sr_set_item_str failed\n");
82             return NTS_ERR_FAILED;
83         }
84
85         rc = sr_set_item_str(current_session, IETF_SYSTEM_LOCATION_SCHEMA_XPATH, "Open Wireless Lab", 0, 0);
86         if(rc != SR_ERR_OK) {
87             log_error("sr_set_item_str failed\n");
88             return NTS_ERR_FAILED;
89         }
90
91         rc = sr_set_item_str(current_session, IETF_SYSTEM_TIMEZONE_NAME_SCHEMA_XPATH, "UTC", 0, 0);
92         if(rc != SR_ERR_OK) {
93             log_error("sr_set_item_str failed\n");
94             return NTS_ERR_FAILED;
95         }
96
97         rc = sr_set_item_str(current_session, IETF_SYSTEM_NTP_ENABLED_SCHEMA_XPATH, "false", 0, 0);
98         if(rc != SR_ERR_OK) {
99             log_error("sr_set_item_str failed\n");
100             return NTS_ERR_FAILED;
101         }
102
103         rc = sr_apply_changes(current_session, 0, 0);
104         if(rc != SR_ERR_OK) {
105             log_error("could not apply changes on datastore\n");
106             return NTS_ERR_FAILED;
107         }
108
109         web_cut_through_status = 1;
110     }
111
112     return NTS_ERR_OK;
113 }
114
115 int web_cut_through_feature_stop(sr_session_ctx_t *current_session) {
116     assert(current_session);
117     assert_session();
118
119     if(web_cut_through_status) {
120         //update ietf-system details
121         int rc = sr_delete_item(current_session, IETF_SYSTEM_NAME_SCHEMA_XPATH, 0);
122         if(rc != SR_ERR_OK) {
123             log_error("sr_delete_item failed\n");
124             return NTS_ERR_FAILED;
125         }
126
127         rc = sr_delete_item(current_session, IETF_SYSTEM_WEB_UI_SCHEMA_XPATH, 0);
128         if(rc != SR_ERR_OK) {
129             log_error("sr_delete_item failed\n");
130             return NTS_ERR_FAILED;
131         }
132
133         rc = sr_apply_changes(current_session, 0, 0);
134         if(rc != SR_ERR_OK) {
135             log_error("could not apply changes on datastore\n");
136             return NTS_ERR_FAILED;
137         }
138
139         web_cut_through_status = 0;
140     }
141
142     return NTS_ERR_OK;
143 }