Sonar fix: SshApiCallNode.java
[ccsdk/sli/plugins.git] / sshapi-call-node / provider / src / main / java / org / onap / ccsdk / sli / plugins / sshapicall / SshApiCallNode.java
index e4b082c..146188f 100644 (file)
@@ -3,9 +3,12 @@
  * ONAP : APPC
  * ================================================================================
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- * Copyright (C) 2017 Amdocs
  * =============================================================================
+ * Copyright (C) 2018 Samsung Electronics. All rights
+                       reserved.
+ * ================================================================================
+ * Modifications Copyright © 2018 IBM
+ * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
 
 package org.onap.ccsdk.sli.plugins.sshapicall;
 
-//import com.fasterxml.jackson.databind.ObjectMapper;
-
 import java.io.ByteArrayOutputStream;
-import java.util.Collections;
-import java.util.HashSet;
 import java.util.Map;
-import java.util.Set;
 
-import com.google.common.base.Strings;
-import org.json.JSONObject;
 import org.onap.appc.adapter.ssh.SshAdapter;
 import org.onap.appc.adapter.ssh.SshConnection;
 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
@@ -52,25 +48,45 @@ public class SshApiCallNode implements SvcLogicJavaPlugin {
     /**
      * Output parameter - SSH command execution status.
      */
-    String PARAM_OUT_status = "status";
+    private String PARAM_OUT_status = "sshApi.call.node.status";
 
     /**
      * Output parameter - content of SSH command stdout.
      */
-    String PARAM_OUT_stdout = "stdout";
+    private String PARAM_OUT_stdout = "sshApi.call.node.stdout";
 
     /**
      * Output parameter - content of SSH command stderr.
      */
-    String PARAM_OUT_stderr = "stderr";
+    private String PARAM_OUT_stderr = "sshApi.call.node.stderr";
+
+    /**
+     * Testing parameter - content of mocked SSH.
+     */
+    private boolean PARAM_TEST_MODE = false;
+    private String PARAM_OUT_MESSAGE = "TestOut";
+
 
     /**
      * Default success status.
      */
-    int DEF_SUCCESS_STATUS = 0;
+    private int DEF_SUCCESS_STATUS = 0;
+
+
 
     private SshAdapter sshAdapter;
 
+    /**
+     * Used for jUnit test and testing interface
+     */
+    public SshApiCallNode(boolean mode) {
+        PARAM_TEST_MODE = mode;
+    }
+
+    public SshApiCallNode() {
+        PARAM_TEST_MODE = false;
+    }
+
     public void setSshAdapter(SshAdapter sshAdapter) {
         this.sshAdapter = sshAdapter;
     }
@@ -81,7 +97,6 @@ public class SshApiCallNode implements SvcLogicJavaPlugin {
      * <table border="1">
      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th><th>example values</th></thead>
      *  <tbody>
-     *      <tr><td>templateFileName</td><td>Optional</td><td>full path to template file that can be used to build a request</td><td>/sdncopt/bvc/sshapi/templates/vnf_service-configuration-operation_minimal.json</td></tr>
      *      <tr><td>Url</td><td>Mandatory</td><td>url to make the SSH connection request to.</td></tr>
      *      <tr><td>Port</td><td>Mandatory</td><td>port to make the SSH connection request to.</td></tr>
      *      <tr><td>User</td><td>Optional</td><td>user name to use for ssh basic authentication</td><td>sdnc_ws</td></tr>
@@ -90,8 +105,8 @@ public class SshApiCallNode implements SvcLogicJavaPlugin {
      *      <tr><td>ExecTimeout</td><td>Optional</td><td>SSH command execution timeout</td><td>plain_key</td></tr>
      *      <tr><td>Retry</td><td>Optional</td><td>Make ssh connection with default retry policy</td><td>plain_key</td></tr>
      *      <tr><td>Cmd</td><td>Mandatory</td><td>ssh command to be executed on the server.</td><td>get post put delete patch</td></tr>
-     *      <tr><td>ResponsePrefix</td><td>Optional</td><td>location the response will be written to in context memory</td><td>tmp.sshapi.result</td></tr>
-     *      <tr><td>ResponseType</td><td>Optional</td><td>If we know the response is to be in a specific format (supported are JSON, XML and NONE) </td><td>tmp.sshapi.result</td></tr>
+     *      <tr><td>ResponsePrefix</td><td>Optional</td><td>location the response will be written to in context memory</td></tr>
+     *      <tr><td>ResponseType</td><td>Optional</td><td>If we know the response is to be in a specific format (supported are JSON, XML and NONE) </td></tr>
      *      <tr><td>listName[i]</td><td>Optional</td><td>Used for processing XML responses with repeating elements.</td>vpn-information.vrf-details<td></td></tr>
      *      <tr><td>ConvertResponse </td><td>Optional</td><td>whether the response should be converted</td><td>true or false</td></tr>
      *      <tr><td>AuthType</td><td>Optional</td><td>Type of authentiation to be used BASIC or sshKey based</td><td>true or false</td></tr>
@@ -109,20 +124,42 @@ public class SshApiCallNode implements SvcLogicJavaPlugin {
      */
 
     public void execCommand(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
+        execSshCommand(params, ctx, false);
+    }
+
+    private void execSshCommand(Map<String, String> params, SvcLogicContext ctx, boolean withPty) throws SvcLogicException {
         ParseParam parser = new ParseParam();
         Parameters p = parser.getParameters(params);
         logger.debug("=> Connecting to SSH server...");
-        SshConnection sshConnection = getSshConnection(p);
-        sshConnection.connect();
+        SshConnection sshConnection = null;
+        int status = -1;
+        ByteArrayOutputStream stdout = new ByteArrayOutputStream();
+        ByteArrayOutputStream stderr = new ByteArrayOutputStream();
+        String stdoutRes = "";
+        String stderrRes = "";
         try {
-            logger.debug("=> Connected to SSH server...");
-            logger.debug("=> Running SSH command...");
-            sshConnection.setExecTimeout(p.sshExecTimeout);
-            ByteArrayOutputStream stdout = new ByteArrayOutputStream();
-            ByteArrayOutputStream stderr = new ByteArrayOutputStream();
-            int status = sshConnection.execCommand(parser.makeCommand(params), stdout, stderr);
-            String stdoutRes = stdout.toString();
-            String stderrRes = stderr.toString();
+            if (!PARAM_TEST_MODE) {
+                sshConnection = getSshConnection(p);
+                sshConnection.connect();
+                logger.debug("=> Connected to SSH server...");
+                logger.debug("=> Running SSH command...");
+                sshConnection.setExecTimeout(p.sshExecTimeout);
+                if (withPty) {
+                    status = sshConnection.execCommandWithPty(parser.makeCommand(params), stdout);
+                    stderr = stdout;
+                }
+                else
+                    status = sshConnection.execCommand(parser.makeCommand(params), stdout, stderr);
+                stdoutRes = stdout.toString();
+                stderrRes = stderr.toString();
+
+            } else {
+                if (("true").equalsIgnoreCase(params.get("TestFail")))
+                    status = 202;
+                else
+                    status = DEF_SUCCESS_STATUS;
+                stdoutRes = params.get(PARAM_OUT_MESSAGE);
+            }
             logger.debug("=> executed SSH command");
 
             if(status == DEF_SUCCESS_STATUS) {
@@ -131,21 +168,22 @@ public class SshApiCallNode implements SvcLogicJavaPlugin {
             ctx.setAttribute(PARAM_OUT_status, String.format("%01d", status));
             ctx.setAttribute(PARAM_OUT_stdout, stdoutRes);
             ctx.setAttribute(PARAM_OUT_stderr, stderrRes);
+        } catch (Exception e){
+            throw new SvcLogicException("Exception in SSH adaptor : " + e.getMessage());
         } finally {
-            sshConnection.disconnect();
+            if (sshConnection != null)
+               sshConnection.disconnect();
         }
     }
 
-    private SshConnection getSshConnection(Parameters p) throws SvcLogicException{
+    private SshConnection getSshConnection(Parameters p) throws SvcLogicException {
         if (p.authtype == AuthType.BASIC)
             return sshAdapter.getConnection(p.sshapiUrl, p.sshapiPort, p.sshapiUser, p.sshapiPassword);
         // This is not supported yet in the API, patch has already been added to APPC
         else if (p.authtype == AuthType.KEY){
-            //return sshAdapter.getConnection(p.sshapiUrl, p.sshapiPort, p.sshKey);
             throw new SvcLogicException("SSH Key based Auth method not supported");
         }
         else if (p.authtype == AuthType.NONE){
-            //return sshAdapter.getConnection(p.sshapiUrl, p.sshapiPort, p.sshKey);
             throw new SvcLogicException("SSH Auth type required, BASIC auth in support");
         }
         else if (p.authtype == AuthType.UNSPECIFIED){
@@ -164,7 +202,6 @@ public class SshApiCallNode implements SvcLogicJavaPlugin {
      * <table border="1">
      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th><th>example values</th></thead>
      *  <tbody>
-     *      <tr><td>templateFileName</td><td>Optional</td><td>full path to template file that can be used to build a request</td><td>/sdncopt/bvc/sshapi/templates/vnf_service-configuration-operation_minimal.json</td></tr>
      *      <tr><td>Url</td><td>Mandatory</td><td>url to make the SSH connection request to.</td></tr>
      *      <tr><td>Port</td><td>Mandatory</td><td>port to make the SSH connection request to.</td></tr>
      *      <tr><td>User</td><td>Optional</td><td>user name to use for ssh basic authentication</td><td>sdnc_ws</td></tr>
@@ -173,8 +210,8 @@ public class SshApiCallNode implements SvcLogicJavaPlugin {
      *      <tr><td>ExecTimeout</td><td>Optional</td><td>SSH command execution timeout</td><td>plain_key</td></tr>
      *      <tr><td>Retry</td><td>Optional</td><td>Make ssh connection with default retry policy</td><td>plain_key</td></tr>
      *      <tr><td>Cmd</td><td>Mandatory</td><td>ssh command to be executed on the server.</td><td>get post put delete patch</td></tr>
-     *      <tr><td>ResponsePrefix</td><td>Optional</td><td>location the response will be written to in context memory</td><td>tmp.sshapi.result</td></tr>
-     *      <tr><td>ResponseType</td><td>Optional</td><td>If we know the response is to be in a specific format (supported are JSON, XML and NONE) </td><td>tmp.sshapi.result</td></tr>
+     *      <tr><td>ResponsePrefix</td><td>Optional</td><td>location the response will be written to in context memory</td></tr>
+     *      <tr><td>ResponseType</td><td>Optional</td><td>If we know the response is to be in a specific format (supported are JSON, XML and NONE) </td></tr>
      *      <tr><td>listName[i]</td><td>Optional</td><td>Used for processing XML responses with repeating elements.</td>vpn-information.vrf-details<td></td></tr>
      *      <tr><td>ConvertResponse </td><td>Optional</td><td>whether the response should be converted</td><td>true or false</td></tr>
      *      <tr><td>AuthType</td><td>Optional</td><td>Type of authentiation to be used BASIC or sshKey based</td><td>true or false</td></tr>
@@ -193,16 +230,17 @@ public class SshApiCallNode implements SvcLogicJavaPlugin {
 
     public void execWithStatusCheck(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
         execCommand(params, ctx);
+        parseResponse(ctx);
+    }
+
+    private void parseResponse (SvcLogicContext ctx) throws SvcLogicException {
         int status = Integer.parseInt(ctx.getAttribute(PARAM_OUT_status));
         if(status != DEF_SUCCESS_STATUS) {
             StringBuilder errmsg = new StringBuilder();
             errmsg.append("SSH command returned error status [").append(status).append(']');
-            String stderrRes = ctx.getAttribute(PARAM_OUT_stderr);
-            String stdoutRes = ctx.getAttribute(PARAM_OUT_stdout);
-            if((stderrRes != null) && !stderrRes.isEmpty()) {
-                errmsg.append(". Error: [").append(stderrRes).append(']');
-            } else if ((stdoutRes != null) && !stdoutRes.isEmpty()) {
-                errmsg.append(". Error: [").append(stdoutRes).append(']');
+            String stderr = ctx.getAttribute(PARAM_OUT_stderr);
+            if((stderr != null) && !stderr.isEmpty()) {
+                errmsg.append(". Error: [").append(stderr).append(']');
             }
             throw new SvcLogicException(errmsg.toString());
         }
@@ -214,7 +252,6 @@ public class SshApiCallNode implements SvcLogicJavaPlugin {
      * <table border="1">
      *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th><th>example values</th></thead>
      *  <tbody>
-     *      <tr><td>templateFileName</td><td>Optional</td><td>full path to template file that can be used to build a request</td><td>/sdncopt/bvc/sshapi/templates/vnf_service-configuration-operation_minimal.json</td></tr>
      *      <tr><td>Url</td><td>Mandatory</td><td>url to make the SSH connection request to.</td></tr>
      *      <tr><td>Port</td><td>Mandatory</td><td>port to make the SSH connection request to.</td></tr>
      *      <tr><td>User</td><td>Optional</td><td>user name to use for ssh basic authentication</td><td>sdnc_ws</td></tr>
@@ -223,8 +260,8 @@ public class SshApiCallNode implements SvcLogicJavaPlugin {
      *      <tr><td>ExecTimeout</td><td>Optional</td><td>SSH command execution timeout</td><td>plain_key</td></tr>
      *      <tr><td>Retry</td><td>Optional</td><td>Make ssh connection with default retry policy</td><td>plain_key</td></tr>
      *      <tr><td>Cmd</td><td>Mandatory</td><td>ssh command to be executed on the server.</td><td>get post put delete patch</td></tr>
-     *      <tr><td>ResponsePrefix</td><td>Optional</td><td>location the response will be written to in context memory</td><td>tmp.sshapi.result</td></tr>
-     *      <tr><td>ResponseType</td><td>Optional</td><td>If we know the response is to be in a specific format (supported are JSON, XML and NONE) </td><td>tmp.sshapi.result</td></tr>
+     *      <tr><td>ResponsePrefix</td><td>Optional</td><td>location the response will be written to in context memory</td></tr>
+     *      <tr><td>ResponseType</td><td>Optional</td><td>If we know the response is to be in a specific format (supported are JSON, XML and NONE) </td></tr>
      *      <tr><td>listName[i]</td><td>Optional</td><td>Used for processing XML responses with repeating elements.</td>vpn-information.vrf-details<td></td></tr>
      *      <tr><td>ConvertResponse </td><td>Optional</td><td>whether the response should be converted</td><td>true or false</td></tr>
      *      <tr><td>AuthType</td><td>Optional</td><td>Type of authentiation to be used BASIC or sshKey based</td><td>true or false</td></tr>
@@ -241,6 +278,6 @@ public class SshApiCallNode implements SvcLogicJavaPlugin {
      * @param ctx Reference to context memory
      */
     public void execCommandWithPty(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
-
+        execSshCommand(params, ctx, true);
     }
 }