diff options
author | Ganesh Chandrasekaran <ganesh.c@samsung.com> | 2018-08-02 15:06:36 +0900 |
---|---|---|
committer | Ganesh Chandrasekaran <ganesh.c@samsung.com> | 2018-08-02 18:36:42 +0900 |
commit | 1476ff884265181b5934d71a7a0e59b705ee1aac (patch) | |
tree | f983cda14754b3007a37b0b079dcde7e2d3e2f17 /sshapi-call-node/provider/src/main/java | |
parent | bace9b5f98bfef3168134a747c8795196ce9a723 (diff) |
dependancy and tests cases added
Issue-ID: CCSDK-407
Change-Id: I0450875b0a29cc4612e0979e3d6e97b6c4b398f0
Signed-off-by: Ganesh Chandrasekaran <ganesh.c@samsung.com>
Diffstat (limited to 'sshapi-call-node/provider/src/main/java')
8 files changed, 55 insertions, 35 deletions
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 e4b082cda..10635be71 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 @@ -3,9 +3,10 @@ * 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. +* * ================================================================================ * 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 @@ -52,22 +53,22 @@ 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"; /** * Default success status. */ - int DEF_SUCCESS_STATUS = 0; + private int DEF_SUCCESS_STATUS = 0; private SshAdapter sshAdapter; @@ -81,7 +82,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 +90,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,18 +109,29 @@ 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; 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 = sshConnection.execCommand(parser.makeCommand(params), stdout, stderr); + int status; + if (withPty) { + status = sshConnection.execCommandWithPty(parser.makeCommand(params), stdout); + stderr = stdout; + } + else + status = sshConnection.execCommand(parser.makeCommand(params), stdout, stderr); String stdoutRes = stdout.toString(); String stderrRes = stderr.toString(); logger.debug("=> executed SSH command"); @@ -131,12 +142,15 @@ 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 @@ -164,7 +178,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 +186,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 +206,19 @@ public class SshApiCallNode implements SvcLogicJavaPlugin { public void execWithStatusCheck(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException { execCommand(params, ctx); + ParseParam parser = new ParseParam(); + String responsePrefix = parser.getStringParameters(params, parser.SSH_ResponsePrefix); + parseResponse(ctx, responsePrefix); + } + + private void parseResponse (SvcLogicContext ctx, String responsePrefix) 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 +230,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 +238,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 +256,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); } } diff --git a/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/AuthType.java b/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/AuthType.java index 2ec48c70c..806c80890 100644 --- a/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/AuthType.java +++ b/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/AuthType.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * openECOMP : SDN-C * ================================================================================ - * * Copyright (C) 2017 AT&T Intellectual Property. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2018 Samsung Electronics. All rights * reserved. diff --git a/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/Format.java b/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/Format.java index e50a96c6c..43c3c38d6 100644 --- a/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/Format.java +++ b/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/Format.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * openECOMP : SDN-C * ================================================================================ - * * Copyright (C) 2017 AT&T Intellectual Property. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2018 Samsung Electronics. All rights * reserved. diff --git a/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/JsonParser.java b/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/JsonParser.java index f40f1ad71..296192a5b 100644 --- a/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/JsonParser.java +++ b/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/JsonParser.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * openECOMP : SDN-C * ================================================================================ - * * Copyright (C) 2017 AT&T Intellectual Property. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2018 Samsung Electronics. All rights * reserved. diff --git a/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/Parameters.java b/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/Parameters.java index a22da5bcb..fa2371e88 100644 --- a/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/Parameters.java +++ b/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/Parameters.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * openECOMP : SDN-C * ================================================================================ - * * Copyright (C) 2017 AT&T Intellectual Property. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2018 Samsung Electronics. All rights * reserved. diff --git a/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/ParseParam.java b/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/ParseParam.java index caf49ef91..a72bc7bbe 100644 --- a/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/ParseParam.java +++ b/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/ParseParam.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * openECOMP : SDN-C * ================================================================================ - * * Copyright (C) 2017 AT&T Intellectual Property. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2018 Samsung Electronics. All rights * reserved. @@ -59,6 +59,7 @@ public class ParseParam { * Default SSH command timeout */ private final String FILE_PARAMETERS_OPT_KEY = "FileParameters"; + public final String SSH_ResponsePrefix = "ResponsePrefix"; /** * Default SSH connection port. */ @@ -74,7 +75,7 @@ public class ParseParam { p.sshExecTimeout = Long.parseLong(parseParam(paramMap, "ExecTimeout", false, Long.toString(dEF_timeout))); p.sshWithRetry = Boolean.valueOf(parseParam(paramMap, "Retry", false, "false")); p.cmd = parseParam(paramMap, "Cmd", true, null); - p.responsePrefix = parseParam(paramMap, "ResponsePrefix", false, null); + p.responsePrefix = parseParam(paramMap, SSH_ResponsePrefix, false, null); p.responseType = Format.fromString(parseParam(paramMap, "ResponseType", false, "none")); p.listNameList = getListNameList(paramMap); p.convertResponse = Boolean.valueOf(parseParam(paramMap, "ConvertResponse", false, "true")); @@ -83,6 +84,10 @@ public class ParseParam { return p; } + public String getStringParameters(Map<String, String> paramMap, String paramName) throws SvcLogicException { + return parseParam(paramMap, SSH_ResponsePrefix, false, null); + } + public void parseOutput (SvcLogicContext ctx, String outMessage) throws SvcLogicException { if (p.convertResponse) { if (p.responseType == Format.NONE) { diff --git a/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/XmlJsonUtil.java b/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/XmlJsonUtil.java index e7fc60edb..d68fbc6ac 100644 --- a/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/XmlJsonUtil.java +++ b/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/XmlJsonUtil.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * openECOMP : SDN-C * ================================================================================ - * * Copyright (C) 2017 AT&T Intellectual Property. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2018 Samsung Electronics. All rights * reserved. diff --git a/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/XmlParser.java b/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/XmlParser.java index c23aca6cf..e31754bcd 100644 --- a/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/XmlParser.java +++ b/sshapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/sshapicall/model/XmlParser.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * openECOMP : SDN-C * ================================================================================ - * * Copyright (C) 2017 AT&T Intellectual Property. + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2018 Samsung Electronics. All rights * reserved. |