From 1476ff884265181b5934d71a7a0e59b705ee1aac Mon Sep 17 00:00:00 2001 From: Ganesh Chandrasekaran Date: Thu, 2 Aug 2018 15:06:36 +0900 Subject: dependancy and tests cases added Issue-ID: CCSDK-407 Change-Id: I0450875b0a29cc4612e0979e3d6e97b6c4b398f0 Signed-off-by: Ganesh Chandrasekaran --- sshapi-call-node/provider/ReadMe.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 sshapi-call-node/provider/ReadMe.md (limited to 'sshapi-call-node/provider/ReadMe.md') diff --git a/sshapi-call-node/provider/ReadMe.md b/sshapi-call-node/provider/ReadMe.md new file mode 100644 index 000000000..6c31ae0c2 --- /dev/null +++ b/sshapi-call-node/provider/ReadMe.md @@ -0,0 +1,32 @@ +SSHApi-Call-Node Plugin: + +Parameters List that managed in from Directed Graphs: + +Input: +Url -> Mandatory -> url to make the SSH connection request to. +Port -> Mandatory -> port to make the SSH connection request to. +AuthType -> Optional -> Type of authentiation to be used BASIC or sshKey based -> true or false +User -> Optional -> user name to use for ssh basic authentication -> sdnc_ws +Password -> Optional -> unencrypted password to use for ssh basic authentication -> plain_password +SshKey -> Optional -> Consumer SSH key to use for ssh authentication -> plain_key +ExecTimeout -> Optional -> SSH command execution timeout -> plain_key +Retry -> Optional -> Make ssh connection with default retry policy -> plain_key +Cmd -> Mandatory -> ssh command to be executed on the server. -> get post put delete patch +EnvParameters -> Optional -> A JSON dictionary which should list key value pairs to be passed to the command execution. These values would correspond to instance specific parameters that a command may need to execute an action. +FileParameters -> Optional -> A JSON dictionary where keys are filenames and values are contents of files. The SSH Server will utilize this feature to generate files with keys as filenames and values as content. This attribute can be used to generate files that a command may require as part of execution. +ConvertResponse -> Optional -> whether the response should be converted to properties -> true or false +ResponseType -> Optional -> If we know the response is to be in a specific format (supported are JSON, XML and NONE) +ResponsePrefix -> Optional -> location the response will be written to in context memory +listName[i] -> Optional -> Used for processing XML responses with repeating elements.vpn-information.vrf-details + +Output: +"'ResponsePrefix'.sshApi.call.node.status" -> SSH Exit status code is set in here. +"'ResponsePrefix'.sshApi.call.node.stdout" -> SSH command execution result is put in here. +"'ResponsePrefix'.sshApi.call.node.stderr" -> SSH execution failure message is put in here. + + + +API methods that are exposed: +1) execCommand +2) execWithStatusCheck: Throws exception if the exit status is not successful. +3) execCommandWithPty \ No newline at end of file -- cgit 1.2.3-korg From efd6f9e3d2f770f16a6cb93efc061fe502a56979 Mon Sep 17 00:00:00 2001 From: Ganesh Chandrasekaran Date: Fri, 3 Aug 2018 18:06:37 +0900 Subject: mocked testcases added Issue-ID: CCSDK-433 Change-Id: I9961df9131b4672a99601b31ed2e9312d0840d6b Signed-off-by: Ganesh Chandrasekaran --- sshapi-call-node/provider/ReadMe.md | 6 +- .../sli/plugins/sshapicall/SshApiCallNode.java | 64 ++++++++---- .../sli/plugins/sshapicall/TestSshApiCallNode.java | 107 +++++++++++++++++++++ 3 files changed, 155 insertions(+), 22 deletions(-) (limited to 'sshapi-call-node/provider/ReadMe.md') diff --git a/sshapi-call-node/provider/ReadMe.md b/sshapi-call-node/provider/ReadMe.md index 6c31ae0c2..beaadcda8 100644 --- a/sshapi-call-node/provider/ReadMe.md +++ b/sshapi-call-node/provider/ReadMe.md @@ -20,9 +20,9 @@ ResponsePrefix -> Optional -> location the response will be written listName[i] -> Optional -> Used for processing XML responses with repeating elements.vpn-information.vrf-details Output: -"'ResponsePrefix'.sshApi.call.node.status" -> SSH Exit status code is set in here. -"'ResponsePrefix'.sshApi.call.node.stdout" -> SSH command execution result is put in here. -"'ResponsePrefix'.sshApi.call.node.stderr" -> SSH execution failure message is put in here. +"sshApi.call.node.status" -> SSH Exit status code is set in here. +"sshApi.call.node.stdout" -> SSH command execution result is put in here. +"sshApi.call.node.stderr" -> SSH execution failure message is put in here. diff --git a/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/SshApiCallNode.java b/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/SshApiCallNode.java index 10635be71..452d94785 100644 --- a/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/SshApiCallNode.java +++ b/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/SshApiCallNode.java @@ -65,11 +65,29 @@ public class SshApiCallNode implements SvcLogicJavaPlugin { */ 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. */ private int DEF_SUCCESS_STATUS = 0; + /** + * Used for jUnit test and testing interface + */ + public SshApiCallNode(boolean mode) { + PARAM_TEST_MODE = mode; + } + + public SshApiCallNode() { + PARAM_TEST_MODE = false; + } + private SshAdapter sshAdapter; public void setSshAdapter(SshAdapter sshAdapter) { @@ -117,23 +135,33 @@ public class SshApiCallNode implements SvcLogicJavaPlugin { Parameters p = parser.getParameters(params); logger.debug("=> Connecting to SSH server..."); SshConnection sshConnection = null; + int status = -1; + ByteArrayOutputStream stdout = new ByteArrayOutputStream(); + ByteArrayOutputStream stderr = new ByteArrayOutputStream(); + String stdoutRes = "", stderrRes = ""; try { - sshConnection = getSshConnection(p); - sshConnection.connect(); - 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; - if (withPty) { - status = sshConnection.execCommandWithPty(parser.makeCommand(params), stdout); - stderr = stdout; + 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 (params.get("TestFail").equalsIgnoreCase("true")) + status = 202; + else + status = DEF_SUCCESS_STATUS; + stdoutRes = params.get(PARAM_OUT_MESSAGE); } - else - status = sshConnection.execCommand(parser.makeCommand(params), stdout, stderr); - String stdoutRes = stdout.toString(); - String stderrRes = stderr.toString(); logger.debug("=> executed SSH command"); if(status == DEF_SUCCESS_STATUS) { @@ -206,12 +234,10 @@ public class SshApiCallNode implements SvcLogicJavaPlugin { public void execWithStatusCheck(Map params, SvcLogicContext ctx) throws SvcLogicException { execCommand(params, ctx); - ParseParam parser = new ParseParam(); - String responsePrefix = parser.getStringParameters(params, parser.SSH_ResponsePrefix); - parseResponse(ctx, responsePrefix); + parseResponse(ctx); } - private void parseResponse (SvcLogicContext ctx, String responsePrefix) throws SvcLogicException { + 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(); diff --git a/sshapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/sshapicall/TestSshApiCallNode.java b/sshapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/sshapicall/TestSshApiCallNode.java index 3992dffe1..544057a6e 100644 --- a/sshapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/sshapicall/TestSshApiCallNode.java +++ b/sshapi-call-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/sshapicall/TestSshApiCallNode.java @@ -225,4 +225,111 @@ public class TestSshApiCallNode { params.put("ResponseType", "json"); adapter.execWithStatusCheck(params, svcContext); } + + @Test + public void testExecCommandResponse_validJSON() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("Url", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Cmd", "test"); + params.put("AuthType", "basic"); + params.put("ResponseType", "json"); + params.put("TestOut", "{\"equipment-data\":\"boo\"}"); + params.put("TestFail", "false"); + adapter = new SshApiCallNode(true); + adapter.execWithStatusCheck(params, svcContext); + assertEquals("boo", svcContext.getAttribute("equipment-data")); + } + + @Test + public void testExecCommandResponse_validXML() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("Url", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Cmd", "test"); + params.put("AuthType", "basic"); + params.put("ResponseType", "xml"); + params.put("TestOut", "4.0.0"); + params.put("TestFail", "false"); + adapter = new SshApiCallNode(true); + adapter.execWithStatusCheck(params, svcContext); + assertEquals("4.0.0", svcContext.getAttribute("modelVersion")); + } + + @Test + public void testExecCommandResponse_validJSONPrefix() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("Url", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Cmd", "test"); + params.put("AuthType", "basic"); + params.put("ResponseType", "json"); + params.put("TestOut", "{\"equipment-data\":\"boo\"}"); + params.put("ResponsePrefix", "test"); + params.put("TestFail", "false"); + adapter = new SshApiCallNode(true); + adapter.execWithStatusCheck(params, svcContext); + assertEquals("boo", svcContext.getAttribute("test.equipment-data")); + } + + @Test + public void testExecCommandResponse_validXMLPrefix() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("Url", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Cmd", "test"); + params.put("AuthType", "basic"); + params.put("ResponseType", "xml"); + params.put("TestOut", "4.0.0"); + params.put("ResponsePrefix", "test"); + params.put("TestFail", "false"); + adapter = new SshApiCallNode(true); + adapter.execWithStatusCheck(params, svcContext); + assertEquals("4.0.0", svcContext.getAttribute("test.modelVersion")); + } + + @Test(expected = SvcLogicException.class) + public void testExecCommandResponse_validXMLFail() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("Url", "test"); + params.put("Port", "10"); + params.put("User", "test"); + params.put("Password", "test"); + params.put("Cmd", "test"); + params.put("AuthType", "basic"); + params.put("ResponseType", "xml"); + params.put("TestOut", "4.0.0"); + params.put("TestFail", "true"); + params.put("ResponsePrefix", "test"); + adapter = new SshApiCallNode(true); + adapter.execWithStatusCheck(params, svcContext); + } + + @Test(expected = SvcLogicException.class) + public void testExecCommandResponse_validXMLPrefixKey() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + params = new HashMap<>(); + params.put("Url", "test"); + params.put("Port", "10"); + params.put("SshKey", "test"); + params.put("Cmd", "test"); + params.put("ResponseType", "xml"); + params.put("TestOut", "4.0.0"); + params.put("ResponsePrefix", "test"); + adapter.execWithStatusCheck(params, svcContext); + assertEquals("4.0.0", svcContext.getAttribute("test.modelVersion")); + } } -- cgit 1.2.3-korg