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