Add supoprt for D release use-case.
[sim/o1-interface.git] / ntsimulator / ntsim-ng / features / netconf_call_home / netconf_call_home.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 "netconf_call_home.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 NETCONF_CALLHOME_ENABLED_SCHEMA_PATH        "/nts-network-function:simulation/network-function/netconf/call-home"
33 #define NETCONF_CALLHOME_CURL_SEND_PAYLOAD_FORMAT   "{\"odl-netconf-callhome-server:device\":[{\"odl-netconf-callhome-server:unique-id\":\"%s\",\"odl-netconf-callhome-server:ssh-host-key\":\"%s\",\"odl-netconf-callhome-server:credentials\":{\"odl-netconf-callhome-server:username\":\"netconf\",\"odl-netconf-callhome-server:passwords\":[\"netconf!\"]}}]}"
34 #define SDN_CONTROLLER_DETAILS_SCHEMA_PATH          "/nts-network-function:simulation/sdn-controller"
35
36 static int create_ssh_callhome_endpoint(sr_session_ctx_t *current_session, struct lyd_node *netconf_node);
37 static int create_tls_callhome_endpoint(sr_session_ctx_t *current_session, struct lyd_node *netconf_node);
38 static int send_odl_callhome_configuration(sr_session_ctx_t *current_session);
39
40 static int netconf_call_home_status = 0;
41
42 int netconf_call_home_feature_get_status(void) {
43     return netconf_call_home_status;
44 }
45
46 int netconf_call_home_feature_start(sr_session_ctx_t *current_session) {
47     assert(current_session);
48     assert_session();
49
50     sr_val_t *value = 0;
51
52     bool callhome_enabled = false;
53     int rc = sr_get_item(current_session, NETCONF_CALLHOME_ENABLED_SCHEMA_PATH, 0, &value);
54     if(rc == SR_ERR_OK) {
55         callhome_enabled = value->data.bool_val;
56         sr_free_val(value);
57     }
58     else {
59         // if value is not set yet, feature enable means we want to start call-home
60         callhome_enabled = true;
61     }
62
63     if(callhome_enabled == false) {
64         log_add_verbose(2, "NETCONF CallHome is not enabled, not configuring NETCONF Server.\n");
65         return NTS_ERR_OK;
66     }
67
68     struct lyd_node *netconf_node = 0;
69     netconf_node = lyd_new_path(NULL, session_context, "/ietf-netconf-server:netconf-server", 0, 0, 0);
70     if(netconf_node == 0) {
71         log_error("could not create a new lyd_node\n");
72         return NTS_ERR_FAILED;
73     }
74
75     rc = create_ssh_callhome_endpoint(current_session, netconf_node);
76     if(rc != NTS_ERR_OK) {
77         log_error("could not create SSH CallHome endpoint on the NETCONF Server\n");
78         return NTS_ERR_FAILED;
79     }
80
81     rc = create_tls_callhome_endpoint(current_session, netconf_node);
82     if(rc != NTS_ERR_OK) {
83         log_error("could not create TLS CallHome endpoint on the NETCONF Server\n");
84         return NTS_ERR_FAILED;
85     }
86
87     rc = sr_edit_batch(current_session, netconf_node, "merge");
88     if(rc != SR_ERR_OK) {
89         log_error("could not edit batch on datastore\n");
90         return NTS_ERR_FAILED;
91     }
92
93     rc = sr_validate(current_session, "ietf-netconf-server", 0);
94     if(rc != SR_ERR_OK) {
95         log_error("sr_validate issues on STARTUP\n");
96         return NTS_ERR_FAILED;
97     }
98
99     rc = sr_apply_changes(current_session, 0, 0);
100     if(rc != SR_ERR_OK) {
101         log_error("could not apply changes on datastore\n");
102         return NTS_ERR_FAILED;
103     }
104
105     lyd_free_withsiblings(netconf_node);
106     netconf_call_home_status = 1;
107
108     return NTS_ERR_OK;
109 }
110
111
112 static int create_ssh_callhome_endpoint(sr_session_ctx_t *current_session, struct lyd_node *netconf_node) {
113     assert(current_session);
114     assert(netconf_node);
115
116     controller_details_t *controller = controller_details_get(current_session);
117     if(controller == 0) {
118         log_error("controller_details_get failed\n");
119         return NTS_ERR_FAILED;
120     }
121
122     char *controller_ip = strdup(controller->ip);
123     uint16_t controller_callhome_port = controller->nc_callhome_port;
124     controller_details_free(controller);
125
126     if(controller_ip == 0) {
127         log_error("strdup failed\n");
128         return NTS_ERR_FAILED;
129     }
130
131     char xpath[500];
132     sprintf(xpath, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='default-client']/endpoints/endpoint[name='callhome-ssh']/ssh/tcp-client-parameters/keepalives/idle-time");
133     struct lyd_node *rcl = lyd_new_path(netconf_node, 0, xpath, "1", 0, LYD_PATH_OPT_NOPARENTRET);
134     if(rcl == 0) {
135         log_error("could not created yang path\n");
136         free(controller_ip);
137         return NTS_ERR_FAILED;
138     }
139
140     sprintf(xpath, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='default-client']/endpoints/endpoint[name='callhome-ssh']/ssh/tcp-client-parameters/keepalives/max-probes");
141     rcl = lyd_new_path(netconf_node, 0, xpath, "10", 0, LYD_PATH_OPT_NOPARENTRET);
142     if(rcl == 0) {
143         log_error("could not created yang path\n");
144         free(controller_ip);
145         return NTS_ERR_FAILED;
146     }
147
148     sprintf(xpath, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='default-client']/endpoints/endpoint[name='callhome-ssh']/ssh/tcp-client-parameters/keepalives/probe-interval");
149     rcl = lyd_new_path(netconf_node, 0, xpath, "5", 0, LYD_PATH_OPT_NOPARENTRET);
150     if(rcl == 0) {
151         log_error("could not created yang path\n");
152         free(controller_ip);
153         return NTS_ERR_FAILED;
154     }
155
156     sprintf(xpath, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='default-client']/endpoints/endpoint[name='callhome-ssh']/ssh/tcp-client-parameters/remote-address");
157     rcl = lyd_new_path(netconf_node, 0, xpath, controller_ip, 0, LYD_PATH_OPT_NOPARENTRET);
158     if(rcl == 0) {
159         log_error("could not created yang path\n");
160         free(controller_ip);
161         return NTS_ERR_FAILED;
162     }
163     free(controller_ip);
164
165     sprintf(xpath, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='default-client']/endpoints/endpoint[name='callhome-ssh']/ssh/tcp-client-parameters/remote-port");
166     char port[20];
167     sprintf(port, "%d", controller_callhome_port);
168     rcl = lyd_new_path(netconf_node, 0, xpath, port, 0, LYD_PATH_OPT_NOPARENTRET);
169     if(rcl == 0) {
170         log_error("could not created yang path\n");
171         return NTS_ERR_FAILED;
172     }
173
174     sprintf(xpath, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='default-client']/endpoints/endpoint[name='callhome-ssh']/ssh/ssh-server-parameters/server-identity/host-key[name='default-key']/public-key/keystore-reference");
175     rcl = lyd_new_path(netconf_node, 0, xpath, KS_KEY_NAME, 0, LYD_PATH_OPT_NOPARENTRET);
176     if(rcl == 0) {
177         log_error("could not created yang path\n");
178         return NTS_ERR_FAILED;
179     }
180
181     sprintf(xpath, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='default-client']/endpoints/endpoint[name='callhome-ssh']/ssh/ssh-server-parameters/client-authentication/supported-authentication-methods/publickey");
182     rcl = lyd_new_path(netconf_node, 0, xpath, "", 0, LYD_PATH_OPT_NOPARENTRET);
183     if(rcl == 0) {
184         log_error("could not created yang path\n");
185         return NTS_ERR_FAILED;
186     }
187
188     sprintf(xpath, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='default-client']/endpoints/endpoint[name='callhome-ssh']/ssh/ssh-server-parameters/client-authentication/supported-authentication-methods/passsword");
189     rcl = lyd_new_path(netconf_node, 0, xpath, "", 0, LYD_PATH_OPT_NOPARENTRET);
190     if(rcl == 0) {
191         log_error("could not created yang path\n");
192         return NTS_ERR_FAILED;
193     }
194
195     sprintf(xpath, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='default-client']/endpoints/endpoint[name='callhome-ssh']/ssh/ssh-server-parameters/client-authentication/supported-authentication-methods/other");
196     rcl = lyd_new_path(netconf_node, 0, xpath, "interactive", 0, LYD_PATH_OPT_NOPARENTRET);
197     if(rcl == 0) {
198         log_error("could not created yang path\n");
199         return NTS_ERR_FAILED;
200     }
201
202     sprintf(xpath, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='default-client']/endpoints/endpoint[name='callhome-ssh']/ssh/ssh-server-parameters/client-authentication/users");
203     rcl = lyd_new_path(netconf_node, 0, xpath, "", 0, LYD_PATH_OPT_NOPARENTRET);
204     if(rcl == 0) {
205         log_error("could not created yang path\n");
206         return NTS_ERR_FAILED;
207     }
208
209     sprintf(xpath, "/ietf-netconf-server:netconf-server/call-home/netconf-client[name='default-client']/connection-type/persistent");
210     rcl = lyd_new_path(netconf_node, 0, xpath, "", 0, LYD_PATH_OPT_NOPARENTRET);
211     if(rcl == 0) {
212         log_error("could not created yang path\n");
213         return NTS_ERR_FAILED;
214     }
215
216     int rc = send_odl_callhome_configuration(current_session);
217     if(rc != NTS_ERR_OK) {
218         log_add_verbose(2, "could not send ODL Call Home configuration.\n");
219     }
220
221     return NTS_ERR_OK;
222 }
223
224 static int create_tls_callhome_endpoint(sr_session_ctx_t *current_session, struct lyd_node *netconf_node) {
225     assert(current_session);
226     assert(netconf_node);
227
228     // checkAS future usage, TLS endpoint yet supported in ODL
229     
230     return NTS_ERR_OK;
231 }
232
233
234 static int send_odl_callhome_configuration(sr_session_ctx_t *current_session) {
235     assert(current_session);
236
237     char *public_ssh_key = read_key(SERVER_PUBLIC_SSH_KEY_PATH);
238     if(public_ssh_key == 0) {
239         log_error("could not read the public ssh key from file %s\n", SERVER_PUBLIC_SSH_KEY_PATH);
240         return NTS_ERR_FAILED;
241     }
242
243     char *ssh_key_string;
244     ssh_key_string = strtok(public_ssh_key, " ");
245     ssh_key_string = strtok(NULL, " ");
246     ssh_key_string[strlen(ssh_key_string) - 1] = 0; // trim the newline character
247
248     // checkAS we have hardcoded here the username and password of the NETCONF Server
249     char *odl_callhome_payload = 0;
250     asprintf(&odl_callhome_payload, NETCONF_CALLHOME_CURL_SEND_PAYLOAD_FORMAT, framework_environment.settings.hostname, ssh_key_string);
251     free(public_ssh_key);
252     if(odl_callhome_payload == 0) {
253         log_error("bad asprintf\n");
254         return NTS_ERR_FAILED;
255     }
256
257     controller_details_t *controller = controller_details_get(current_session);
258     if(controller == 0) {
259         log_error("controller_details_get failed\n");
260         return NTS_ERR_FAILED;
261     }
262     
263     char *url = 0;
264     asprintf(&url, "%s/rests/data/odl-netconf-callhome-server:netconf-callhome-server/allowed-devices/device=%s", controller->base_url, framework_environment.settings.hostname);
265     if(url == 0) {
266         log_error("bad asprintf\n");
267         controller_details_free(controller);
268         return NTS_ERR_FAILED;
269     }
270
271     int rc = http_request(url, controller->username, controller->password, "PUT", odl_callhome_payload, 0, 0);
272     if(rc != NTS_ERR_OK) {
273         log_error("http_request failed\n");
274     }
275     
276     free(url);
277     controller_details_free(controller);
278     free(odl_callhome_payload);
279
280     return rc;
281 }