Add VES stndDefined PM and subscription for O-DU.
[sim/o1-interface.git] / ntsimulator / ntsim-ng / features / ves_file_ready / ves_file_ready.c
index af79d0e..76c8cfc 100644 (file)
 
 #include "core/framework.h"
 #include "core/session.h"
-
-#define FILE_READY_RPC_SCHEMA_XPATH         "/nts-network-function:invoke-ves-pm-file-ready"
+#include "core/xpath.h"
 
 static int ves_file_ready_invoke_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);
-static int ves_file_ready_send_message(sr_session_ctx_t *session, const char *file_location);
+static int ves_file_ready_send_message(sr_session_ctx_t *session, const char *file_location, int port);
 static cJSON* ves_create_file_ready_fields(const char* file_location);
-static void ves_file_ready_vsftp_daemon_init(void);
-static void ves_file_ready_vsftp_daemon_deinit(void);
 
 static sr_subscription_ctx_t *ves_file_ready_subscription = 0;
 
@@ -50,13 +47,14 @@ int ves_file_ready_feature_start(sr_session_ctx_t *current_session) {
     assert_session();
 
     if(ves_file_ready_subscription == 0) {
-        int rc = sr_rpc_subscribe(current_session, FILE_READY_RPC_SCHEMA_XPATH, ves_file_ready_invoke_pm_cb, 0, 0, SR_SUBSCR_CTX_REUSE, &ves_file_ready_subscription);
+        int rc = sr_rpc_subscribe(current_session, NTS_NF_RPC_FILE_READY_SCHEMA_XPATH, ves_file_ready_invoke_pm_cb, 0, 0, SR_SUBSCR_CTX_REUSE, &ves_file_ready_subscription);
         if(rc != SR_ERR_OK) {
             log_error("error from sr_rpc_subscribe: %s\n", sr_strerror(rc));
             return NTS_ERR_FAILED;
         }
 
-        ves_file_ready_vsftp_daemon_init();
+        sftp_daemon_init();
+        vsftp_daemon_init();
     }
 
     return NTS_ERR_OK;
@@ -72,23 +70,72 @@ int ves_file_ready_feature_stop(void) {
             return NTS_ERR_FAILED;
         }
 
-        ves_file_ready_vsftp_daemon_deinit();
+        vsftp_daemon_deinit();
+        sftp_daemon_deinit();
         ves_file_ready_subscription = 0;
     }
 
     return NTS_ERR_OK;
 }
 
-static void ves_file_ready_vsftp_daemon_init(void) {
-    system("/usr/sbin/vsftpd &");
-}
+static int ves_file_ready_invoke_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) {
+    int ssh_base_port = 0;
+    int tls_base_port = 0;
+    sr_session_ctx_t *current_session = 0;
 
-static void ves_file_ready_vsftp_daemon_deinit(void) {
-    system("killall -9 vsftpd");
-}
+    int rc = sr_session_start(session_connection, SR_DS_RUNNING, &current_session);
+    if(rc != SR_ERR_OK) {
+        log_error("could not start sysrepo session\n");
+        return NTS_ERR_FAILED;
+    }
 
-static int ves_file_ready_invoke_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) {
-    int rc;
+    nts_mount_point_addressing_method_t mp = nts_mount_point_addressing_method_get(current_session);
+    if(mp == UNKNOWN_MAPPING) {
+        log_error("mount-point-addressing-method failed\n");
+        sr_session_stop(current_session);
+        return NTS_ERR_FAILED;
+    }
+    else if(mp == DOCKER_MAPPING) {
+        ssh_base_port = STANDARD_NETCONF_PORT;
+        tls_base_port = ssh_base_port + framework_environment.settings.ssh_connections;
+    }
+    else {
+        ssh_base_port = framework_environment.host.ssh_base_port;
+        tls_base_port = framework_environment.host.tls_base_port;       
+    }
+
+    int failed = 0;
+
+    if((framework_environment.settings.ssh_connections + framework_environment.settings.tls_connections) > 1) {
+        for(int port = ssh_base_port; port < ssh_base_port + framework_environment.settings.ssh_connections; port++) {
+            int rc = ves_file_ready_send_message(current_session, input[0].data.string_val, port);
+            if(rc != NTS_ERR_OK) {
+                log_error("ves_file_ready_send_message failed\n");
+                failed++;
+            }
+        }
+
+        for(int port = tls_base_port; port < tls_base_port + framework_environment.settings.tls_connections; port++) {
+            int rc = ves_file_ready_send_message(current_session, input[0].data.string_val, port);
+            if(rc != NTS_ERR_OK) {
+                log_error("ves_file_ready_send_message failed\n");
+                failed++;
+            }
+        }
+    }
+    else {
+        int rc = ves_file_ready_send_message(current_session, input[0].data.string_val, 0);
+        if(rc != NTS_ERR_OK) {
+            log_error("ves_file_ready_send_message failed\n");
+            failed++;
+        }
+    }
+
+    rc = sr_session_stop(current_session);
+    if(rc != SR_ERR_OK) {
+        log_error("could not stop sysrepo session\n");
+        return NTS_ERR_FAILED;
+    }
 
     *output_cnt = 1;
     rc = sr_new_values(*output_cnt, output);
@@ -96,13 +143,12 @@ static int ves_file_ready_invoke_pm_cb(sr_session_ctx_t *session, const char *pa
         return rc;
     }
 
-    rc = sr_val_set_xpath(output[0], FILE_READY_RPC_SCHEMA_XPATH"/status");
+    rc = sr_val_set_xpath(output[0], NTS_NF_RPC_FILE_READY_SCHEMA_XPATH"/status");
     if(SR_ERR_OK != rc) {
         return rc;
     }
-
-    rc = ves_file_ready_send_message(session, input[0].data.string_val);
-    if(rc != NTS_ERR_OK) {
+    
+    if(failed != 0) {
         rc = sr_val_build_str_data(output[0], SR_ENUM_T, "%s", "ERROR");
     }
     else {
@@ -112,7 +158,7 @@ static int ves_file_ready_invoke_pm_cb(sr_session_ctx_t *session, const char *pa
     return rc;
 }
 
-static int ves_file_ready_send_message(sr_session_ctx_t *session, const char *file_location) {
+static int ves_file_ready_send_message(sr_session_ctx_t *session, const char *file_location, int port) {
     assert(session);
     assert(file_location);
 
@@ -138,8 +184,7 @@ static int ves_file_ready_send_message(sr_session_ctx_t *session, const char *fi
         return NTS_ERR_FAILED;
     }
 
-    char *hostname_string = framework_environment.settings.hostname;
-    cJSON *common_event_header = ves_create_common_event_header("notification", "Notification-gnb_Nokia-FileReady", hostname_string, "Normal", sequence_number++);
+    cJSON *common_event_header = ves_create_common_event_header("notification", "Notification-gnb_Nokia-FileReady", framework_environment.settings.hostname, port, "Normal", sequence_number++);
     if(common_event_header == 0) {
         log_error("could not create cJSON object\n");
         cJSON_Delete(post_data_json);
@@ -173,7 +218,7 @@ static int ves_file_ready_send_message(sr_session_ctx_t *session, const char *fi
     }
 
 
-    ves_details_t *ves_details = ves_endpoint_details_get(session);
+    ves_details_t *ves_details = ves_endpoint_details_get(session, 0);
     if(!ves_details) {
         log_error("ves_endpoint_details_get failed\n");
         free(post_data);