/*
 * SPDX-License-Identifier: LicenseRef-CSSL-1.0
 */

#include "connect-mplane.h"
#include "init-mplane.h"
#include "common/utils/assertions.h"

#include <libyang/libyang.h>
#include <nc_client.h>

static int my_auth_hostkey_check(const char *hostname, ssh_session session, void *priv)
{
  (void)hostname;
  (void)session;
  (void)priv;

  return 0;
}

bool connect_mplane(ru_session_t *ru_session)
{
  int port = NC_PORT_SSH;

  nc_client_ssh_set_username(ru_session->username);

  nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 1);  // ssh-key identification
  nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, -1);
  nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, -1);

  nc_client_ssh_set_auth_hostkey_check_clb(my_auth_hostkey_check, "DATA");  // host-key identification

  MP_LOG_I("RPC request to RU \"%s\" = <connect> with username \"%s\" and port ID \"%d\".\n", ru_session->ru_ip_add, ru_session->username, port);
  ru_session->session = nc_connect_ssh(ru_session->ru_ip_add, port, NULL);
  AssertError(ru_session->session != NULL, return false, "[MPLANE] RU IP address %s unreachable. Maybe M-plane is disabled on the RU?\n", ru_session->ru_ip_add);

  MP_LOG_I("Successfuly connected to RU \"%s\" with username \"%s\" and port ID \"%d\".\n", ru_session->ru_ip_add, ru_session->username, port);

  return true;
}

void disconnect_mplane(void *rus_disconnect)
{
  ru_session_list_t *ru_session_list = (ru_session_list_t *)rus_disconnect;
  free_ru_session_list(ru_session_list);

  nc_client_destroy();
}
