From 021cae43d3b22dcbd41e7b34ca4215c167714ab4 Mon Sep 17 00:00:00 2001 From: Ganesh Chandrasekaran Date: Mon, 23 Jul 2018 14:27:22 +0900 Subject: Use ByteStream instead of FileStream Issue-ID: CCSDK-393 Change-Id: Id004f0bce1768e00f3e326efc204532acdb22546 Signed-off-by: Ganesh Chandrasekaran --- .../adaptors/saltstack/impl/ConnectionBuilder.java | 51 +++++++------------ .../saltstack/impl/SaltstackAdapterImpl.java | 45 +++++++++-------- .../sli/adaptors/saltstack/impl/SshConnection.java | 8 +-- .../saltstack/model/SaltstackMessageParser.java | 57 ++++++++++------------ .../adaptors/saltstack/model/SaltstackResult.java | 8 +-- .../saltstack/model/SaltstackServerEmulator.java | 24 ++++----- .../blueprint/saltstack-adapter-blueprint.xml | 13 +++-- .../main/resources/saltstack-adapter.properties | 5 -- .../ccsdk/adapter/impl/TestConnectionBuilder.java | 3 -- .../adapter/impl/TestSaltstackAdapterImpl.java | 20 ++++---- ...TestSaltstackAdapterPropertiesProviderImpl.java | 2 - .../onap/ccsdk/adapter/model/TestJsonParser.java | 6 +-- .../resources/org/onap/ccsdk/default.properties | 13 +---- .../src/test/resources/test-invalid.json | 4 +- .../src/test/resources/test.json | 4 +- 15 files changed, 118 insertions(+), 145 deletions(-) (limited to 'saltstack-adapter/saltstack-adapter-provider/src') diff --git a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/ConnectionBuilder.java b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/ConnectionBuilder.java index f6b3b70cb..6e5feb4ee 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/ConnectionBuilder.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/ConnectionBuilder.java @@ -26,18 +26,11 @@ package org.onap.ccsdk.sli.adaptors.saltstack.impl; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.RandomStringUtils; import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResult; import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResultCodes; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.OutputStream; -import java.io.StringWriter; /** * Returns a custom SSH client @@ -98,11 +91,11 @@ public class ConnectionBuilder { * @return command execution status */ public SaltstackResult connectNExecute(String cmd, int retryCount, int retryDelay, long execTimeout) - throws IOException{ + throws IOException { SaltstackResult result = new SaltstackResult(); - OutputStream out = null; - OutputStream errs = null; + ByteArrayOutputStream out = null; + ByteArrayOutputStream errs = null; if (execTimeout >= 0) { sshConnection.setExecTimeout(execTimeout); } @@ -116,69 +109,59 @@ public class ConnectionBuilder { if (result.getStatusCode() != SaltstackResultCodes.SUCCESS.getValue()) { return result; } - String outFilePath = "/tmp/" + RandomStringUtils.random(5, true, true); - String errFilePath = "/tmp/" + RandomStringUtils.random(5, true, true); - out = new FileOutputStream(outFilePath); - errs = new FileOutputStream(errFilePath); + out = new ByteArrayOutputStream(); + errs = new ByteArrayOutputStream(); result = sshConnection.execCommand(cmd, out, errs, result); sshConnection.disconnect(); if (result.getSshExitStatus() != 0) { - return sortExitStatus(result.getSshExitStatus(), errFilePath, cmd); + return sortExitStatus(result.getSshExitStatus(), errs.toString(), cmd); } if (result.getStatusCode() != SaltstackResultCodes.SUCCESS.getValue()) { return result; } result.setStatusMessage("Success"); - result.setOutputFileName(outFilePath); + result.setOutputMessage(out); } catch (Exception io) { logger.error("Caught Exception", io); result.setStatusCode(SaltstackResultCodes.UNKNOWN_EXCEPTION.getValue()); result.setStatusMessage(io.getMessage()); } finally { - if( out != null ) + if (out != null) { out.close(); - if( errs != null ) + } + if (errs != null) { errs.close(); + } } return result; } - public SaltstackResult sortExitStatus(int exitStatus, String errFilePath, String cmd) { + public SaltstackResult sortExitStatus(int exitStatus, String errMess, String cmd) { SaltstackResult result = new SaltstackResult(); - String err = ""; - StringWriter writer = new StringWriter(); - try { - IOUtils.copy(new FileInputStream(new File(errFilePath)), writer, "UTF-8"); - err = writer.toString(); - } catch (FileNotFoundException e){ - logger.info("Error stream file doesn't exist"); - } catch (IOException e){ - logger.info("Error stream file doesn't exist"); - } if (exitStatus == 255 || exitStatus == 1) { String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString() + "]. Exit Code " + exitStatus + " and Error message : " + - "Malformed configuration. " + err; + "Malformed configuration. " + errMess; logger.error(errMessage); result.setStatusCode(SaltstackResultCodes.INVALID_COMMAND.getValue()); result.setStatusMessage(errMessage); } else if (exitStatus == 5 || exitStatus == 65) { String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString() + "]. Exit Code " + exitStatus + " and Error message : " + - "Host not allowed to connect. " + err; + "Host not allowed to connect. " + errMess; logger.error(errMessage); result.setStatusCode(SaltstackResultCodes.USER_UNAUTHORIZED.getValue()); result.setStatusMessage(errMessage); } else if (exitStatus == 67 || exitStatus == 73) { String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString() + "]. Exit Code " + exitStatus + " and Error message : " + - "Key exchange failed. " + err; + "Key exchange failed. " + errMess; logger.error(errMessage); result.setStatusCode(SaltstackResultCodes.CERTIFICATE_ERROR.getValue()); result.setStatusMessage(errMessage); } else { String errMessage = "Error executing command [" + cmd + "] over SSH [" + sshConnection.toString() - + "]. Exit Code " + exitStatus + " and Error message : " + err; + + "]. Exit Code " + exitStatus + " and Error message : " + errMess; logger.error(errMessage); result.setStatusCode(SaltstackResultCodes.UNKNOWN_EXCEPTION.getValue()); result.setStatusMessage(errMessage); diff --git a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SaltstackAdapterImpl.java b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SaltstackAdapterImpl.java index 5e0cc77eb..a48b67ad6 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SaltstackAdapterImpl.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SaltstackAdapterImpl.java @@ -108,11 +108,11 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { /** * This default constructor is used as a work around because the activator wasn't getting called */ - public SaltstackAdapterImpl() throws SvcLogicException{ + public SaltstackAdapterImpl() throws SvcLogicException { initialize(new SaltstackAdapterPropertiesProviderImpl()); } - public SaltstackAdapterImpl(SaltstackAdapterPropertiesProvider propProvider) throws SvcLogicException{ + public SaltstackAdapterImpl(SaltstackAdapterPropertiesProvider propProvider) throws SvcLogicException { initialize(propProvider); } @@ -152,7 +152,7 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { /** * initialize the Saltstack adapter based on default and over-ride configuration data */ - private void initialize(SaltstackAdapterPropertiesProvider propProvider) throws SvcLogicException{ + private void initialize(SaltstackAdapterPropertiesProvider propProvider) throws SvcLogicException { Properties props = propProvider.getProperties(); @@ -176,7 +176,7 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { logger.info("Creating ssh client connection"); // set path to keystore file String sshHost = props.getProperty(SS_SERVER_HOSTNAME); - String sshPort = reqServerPort(props) ; + String sshPort = reqServerPort(props); String sshUserName = props.getProperty(SS_SERVER_USERNAME); String sshPassword = props.getProperty(SS_SERVER_PASSWD); sshClient = new ConnectionBuilder(sshHost, sshPort, sshUserName, sshPassword); @@ -212,8 +212,9 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { private String reqServerPort(Properties props) { // use default port if null - if (props.getProperty(SS_SERVER_PORT) == null) + if (props.getProperty(SS_SERVER_PORT) == null) { return "22"; + } return props.getProperty(SS_SERVER_PORT); } @@ -235,11 +236,11 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { private String parseEnvParam(JSONObject envParams) { StringBuilder envParamBuilder = new StringBuilder(); if (envParams != null) { - for(Object key : envParams.keySet()) { - if(envParamBuilder.length() > 0) { + for (Object key : envParams.keySet()) { + if (envParamBuilder.length() > 0) { envParamBuilder.append(", "); } - envParamBuilder.append(key+"="+envParams.get((String) key)); + envParamBuilder.append(key + "=" + envParams.get((String) key)); logger.info("EnvParameters : " + envParamBuilder); } } @@ -249,7 +250,7 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { private String parseFileParam(JSONObject fileParams) { StringBuilder fileParamBuilder = new StringBuilder(); if (fileParams != null) { - for(Object key : fileParams.keySet()) { + for (Object key : fileParams.keySet()) { fileParamBuilder.append("echo -e \"" + fileParams.get((String) key) + "\" > /srv/salt/" + key).append("; "); logger.info("FileParameters : " + fileParamBuilder); } @@ -258,7 +259,7 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { } private String putToCommands(SvcLogicContext ctx, String slsFileName, - String applyTo, JSONObject envParams, JSONObject fileParams) throws SvcLogicException { + String applyTo, JSONObject envParams, JSONObject fileParams) throws SvcLogicException { StringBuilder constructedCommand = new StringBuilder(); try { @@ -283,10 +284,10 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { } catch (FileNotFoundException e) { doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(), "Input SLS file " + - "not found in path : " + slsFileName+". "+ e.getMessage()); + "not found in path : " + slsFileName + ". " + e.getMessage()); } catch (IOException e) { doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(), "Input SLS file " + - "error in path : " + slsFileName +". "+ e.getMessage()); + "error in path : " + slsFileName + ". " + e.getMessage()); } catch (StringIndexOutOfBoundsException e) { doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(), "Input file " + "is not of type .sls"); @@ -294,10 +295,14 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { return constructedCommand.toString(); } - private String stripExtension (String str) { - if (str == null) return null; + private String stripExtension(String str) { + if (str == null) { + return null; + } int pos = str.lastIndexOf("."); - if (pos == -1) return str; + if (pos == -1) { + return str; + } return str.substring(0, pos); } @@ -348,7 +353,7 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { checkResponseStatus(testResult, ctx, reqID, slsExec); } catch (IOException e) { doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(), - "IOException in file stream : "+ e.getMessage()); + "IOException in file stream : " + e.getMessage()); } } @@ -379,7 +384,7 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { checkResponseStatus(testResult, ctx, reqID, true); } catch (IOException e) { doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(), - "IOException in file stream : "+ e.getMessage()); + "IOException in file stream : " + e.getMessage()); } catch (JSONException e) { doFailure(ctx, SaltstackResultCodes.INVALID_COMMAND.getValue(), e.getMessage()); } @@ -412,13 +417,13 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { checkResponseStatus(testResult, ctx, reqID, true); } catch (IOException e) { doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(), - "IOException in file stream : "+ e.getMessage()); + "IOException in file stream : " + e.getMessage()); } } public SaltstackResult execCommand(SvcLogicContext ctx, Map params, String commandToExecute, long execTimeout) - throws SvcLogicException{ + throws SvcLogicException { SaltstackResult testResult = new SaltstackResult(); try { @@ -439,7 +444,7 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { } } catch (IOException e) { doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(), - "IOException in file stream : "+ e.getMessage()); + "IOException in file stream : " + e.getMessage()); } return testResult; } diff --git a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SshConnection.java b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SshConnection.java index 62724c364..71ca5cf7c 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SshConnection.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SshConnection.java @@ -163,19 +163,19 @@ class SshConnection { public void setExecTimeout(long timeout) { //convert seconds to milliseconds - this.timeout = timeout*1000; + this.timeout = timeout * 1000; } - public SaltstackResult execCommand(String cmd, OutputStream out, OutputStream err, SaltstackResult result ) { + public SaltstackResult execCommand(String cmd, OutputStream out, OutputStream err, SaltstackResult result) { return execCommand(cmd, out, err, false, result); } - public SaltstackResult execCommandWithPty(String cmd, OutputStream out, SaltstackResult result ) { + public SaltstackResult execCommandWithPty(String cmd, OutputStream out, SaltstackResult result) { return execCommand(cmd, out, out, true, result); } private SaltstackResult execCommand(String cmd, OutputStream out, OutputStream err, - boolean usePty, SaltstackResult result ) { + boolean usePty, SaltstackResult result) { try { if (logger.isDebugEnabled()) { diff --git a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackMessageParser.java b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackMessageParser.java index 8548efc06..2d810aee7 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackMessageParser.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackMessageParser.java @@ -31,16 +31,14 @@ package org.onap.ccsdk.sli.adaptors.saltstack.model; import com.google.common.base.Strings; import org.json.JSONException; -import org.json.JSONArray; import org.json.JSONObject; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Collections; @@ -79,8 +77,9 @@ public class SaltstackMessageParser { */ public String reqPortResult(Map params) throws SvcLogicException { // use default port if null - if (params.get(SS_AGENT_PORT_KEY) == null) + if (params.get(SS_AGENT_PORT_KEY) == null) { return "22"; + } return params.get(SS_AGENT_PORT_KEY); } @@ -200,7 +199,7 @@ public class SaltstackMessageParser { public JSONObject reqEnvParameters(Map params) throws JSONException { JSONObject jsonPayload = new JSONObject(); - final String[] optionalTestParam = { SaltstackMessageParser.ENV_PARAMETERS_OPT_KEY }; + final String[] optionalTestParam = {SaltstackMessageParser.ENV_PARAMETERS_OPT_KEY}; parseParam(params, optionalTestParam, jsonPayload); return (JSONObject) jsonPayload.remove(SaltstackMessageParser.ENV_PARAMETERS_OPT_KEY); @@ -214,7 +213,7 @@ public class SaltstackMessageParser { public JSONObject reqFileParameters(Map params) throws JSONException { JSONObject jsonPayload = new JSONObject(); - final String[] optionalTestParam = { SaltstackMessageParser.FILE_PARAMETERS_OPT_KEY }; + final String[] optionalTestParam = {SaltstackMessageParser.FILE_PARAMETERS_OPT_KEY}; parseParam(params, optionalTestParam, jsonPayload); return (JSONObject) jsonPayload.remove(SaltstackMessageParser.FILE_PARAMETERS_OPT_KEY); @@ -308,19 +307,13 @@ public class SaltstackMessageParser { public SaltstackResult parseResponse(SvcLogicContext ctx, String pfx, SaltstackResult saltstackResult, boolean slsExec) throws IOException { int code = saltstackResult.getStatusCode(); - InputStream in = null; boolean executionStatus = true, retCodeFound = false; if (code != SaltstackResultCodes.SUCCESS.getValue()) { return saltstackResult; } + ByteArrayOutputStream str = saltstackResult.getOutputMessage(); try { - File file = new File(saltstackResult.getOutputFileName()); - in = new FileInputStream(file); - byte[] data = new byte[(int) file.length()]; - in.read(data); - String str = new String(data, "UTF-8"); - in.close(); - Map mm = JsonParser.convertToProperties(str); + Map mm = JsonParser.convertToProperties(str.toString()); if (mm != null) { for (Map.Entry entry : mm.entrySet()) { if (entry.getKey().contains("retcode")) { @@ -333,18 +326,19 @@ public class SaltstackMessageParser { LOGGER.info("+++ " + pfx + "." + entry.getKey() + ": [" + entry.getValue() + "]"); } } - } catch (FileNotFoundException e) { - return new SaltstackResult(SaltstackResultCodes.INVALID_RESPONSE_FILE.getValue(), "error parsing response file " - + saltstackResult.getOutputFileName() + " : " + e.getMessage()); } catch (org.codehaus.jettison.json.JSONException e) { + if (slsExec) { + return new SaltstackResult(SaltstackResultCodes.INVALID_RESPONSE.getValue(), "error parsing response file" + + " : Output has to be in JSON format"); + } LOGGER.info("Output not in JSON format"); return putToProperties(ctx, pfx, saltstackResult); } catch (Exception e) { - return new SaltstackResult(SaltstackResultCodes.INVALID_RESPONSE_FILE.getValue(), "error parsing response file " - + saltstackResult.getOutputFileName() + " : " + e.getMessage()); + return new SaltstackResult(SaltstackResultCodes.INVALID_RESPONSE_FILE.getValue(), "error parsing response file" + + " : " + e.getMessage()); } finally { - if (in != null) { - in.close(); + if (str != null) { + str.close(); } } if (slsExec) { @@ -363,12 +357,14 @@ public class SaltstackMessageParser { public SaltstackResult putToProperties(SvcLogicContext ctx, String pfx, SaltstackResult saltstackResult) throws IOException { - InputStream in = null; + + ByteArrayOutputStream buffer = saltstackResult.getOutputMessage(); + InputStream inputStream = null; try { - File file = new File(saltstackResult.getOutputFileName()); - in = new FileInputStream(file); + byte[] bytes = buffer.toByteArray(); Properties prop = new Properties(); - prop.load(in); + inputStream = new ByteArrayInputStream(bytes); + prop.load(inputStream); ctx.setAttribute(pfx + "completeResult", prop.toString()); for (Object key : prop.keySet()) { String name = (String) key; @@ -379,11 +375,12 @@ public class SaltstackMessageParser { } } } catch (Exception e) { - saltstackResult = new SaltstackResult(SaltstackResultCodes.INVALID_RESPONSE_FILE.getValue(), "Error parsing response file = " - + saltstackResult.getOutputFileName() + ". Error = " + e.getMessage()); + saltstackResult = new SaltstackResult(SaltstackResultCodes.INVALID_RESPONSE_FILE.getValue(), "Error parsing response file." + + " Error = " + e.getMessage()); } finally { - if (in != null) { - in.close(); + if (buffer != null && inputStream != null) { + buffer.close(); + inputStream.close(); } } saltstackResult.setStatusCode(SaltstackResultCodes.FINAL_SUCCESS.getValue()); diff --git a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackResult.java b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackResult.java index b29dd8e80..727cfe314 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackResult.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackResult.java @@ -24,6 +24,8 @@ package org.onap.ccsdk.sli.adaptors.saltstack.model; +import java.io.ByteArrayOutputStream; + /** * Simple class to store code and message returned by POST/GET to an Saltstack Server */ @@ -34,7 +36,7 @@ public class SaltstackResult { private int statusCode; private String statusMessage; private String results; - private String out; + private ByteArrayOutputStream out; private int sshExitStatus; public SaltstackResult() { @@ -58,11 +60,11 @@ public class SaltstackResult { this.results = results; } - public String getOutputFileName() { + public ByteArrayOutputStream getOutputMessage() { return out; } - public void setOutputFileName(String out) { + public void setOutputMessage(ByteArrayOutputStream out) { this.out = out; } diff --git a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackServerEmulator.java b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackServerEmulator.java index 55beb2294..bac2cfe5d 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackServerEmulator.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackServerEmulator.java @@ -34,12 +34,13 @@ package org.onap.ccsdk.sli.adaptors.saltstack.model; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; -import org.apache.commons.lang.StringUtils; -import org.json.JSONObject; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; public class SaltstackServerEmulator { @@ -60,13 +61,10 @@ public class SaltstackServerEmulator { result = rejectRequest(result, "Mocked: Fail"); } else { String fileName = params.get(SALTSTATE_FILE_NAME); - if (fileName == null) - result = acceptRequest(result, ""); - else - result = acceptRequest(result, fileName); + result = acceptRequest(result, fileName); } } catch (Exception e) { - logger.error("JSONException caught", e); + logger.error("Exception caught", e); rejectRequest(result, e.getMessage()); } return result; @@ -78,10 +76,14 @@ public class SaltstackServerEmulator { return result; } - private SaltstackResult acceptRequest(SaltstackResult result, String fileName) { + private SaltstackResult acceptRequest(SaltstackResult result, String fileName) throws IOException { result.setStatusCode(SaltstackResultCodes.SUCCESS.getValue()); result.setStatusMessage("Success"); - result.setOutputFileName(fileName); + Path path = Paths.get(fileName); + byte[] data = Files.readAllBytes(path); + ByteArrayOutputStream byteOut = new ByteArrayOutputStream(data.length); + byteOut.write(data, 0, data.length); + result.setOutputMessage(byteOut); return result; } } \ No newline at end of file diff --git a/saltstack-adapter/saltstack-adapter-provider/src/main/resources/org/opendaylight/blueprint/saltstack-adapter-blueprint.xml b/saltstack-adapter/saltstack-adapter-provider/src/main/resources/org/opendaylight/blueprint/saltstack-adapter-blueprint.xml index 446ab3e44..e360f8184 100755 --- a/saltstack-adapter/saltstack-adapter-provider/src/main/resources/org/opendaylight/blueprint/saltstack-adapter-blueprint.xml +++ b/saltstack-adapter/saltstack-adapter-provider/src/main/resources/org/opendaylight/blueprint/saltstack-adapter-blueprint.xml @@ -20,19 +20,22 @@ ============LICENSE_END========================================================= --> - - + - + - org.onap.ccsdk.sli.adaptors.saltstack.SaltstackAdapter + org.onap.ccsdk.sli.adaptors.saltstack.SaltstackAdapter + diff --git a/saltstack-adapter/saltstack-adapter-provider/src/main/resources/saltstack-adapter.properties b/saltstack-adapter/saltstack-adapter-provider/src/main/resources/saltstack-adapter.properties index 0b077524d..1755f7adf 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/main/resources/saltstack-adapter.properties +++ b/saltstack-adapter/saltstack-adapter-provider/src/main/resources/saltstack-adapter.properties @@ -21,7 +21,6 @@ # # ============LICENSE_END========================================================= ### - # # Default properties for the APP-C TestService Adapter # @@ -31,17 +30,13 @@ # to supply configuration options org.onap.appc.bootstrap.file=appc.properties org.onap.appc.bootstrap.path=${user.home},/opt/opendaylight/current/properties - appc.application.name=APPC - # # Define the message resource bundle name to be loaded org.onap.appc.resources=org.onap/appc/i18n/MessageResources # # The name of the adapter. org.onap.appc.provider.adaptor.name=org.onap.appc.appc_saltstack_adapter - - # Default truststore path and password org.onap.appc.adapter.saltstack.trustStore=/opt/opendaylight/tls-client/mykeystore.js org.onap.appc.adapter.saltstack.trustStore.trustPasswd=changeit diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestConnectionBuilder.java b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestConnectionBuilder.java index 266147aad..8cd7af25e 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestConnectionBuilder.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestConnectionBuilder.java @@ -29,14 +29,11 @@ import org.junit.Before; import org.junit.Test; import org.onap.ccsdk.sli.adaptors.saltstack.impl.ConnectionBuilder; import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackResult; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; -import org.onap.ccsdk.sli.core.sli.SvcLogicException; import java.util.HashMap; import java.util.Map; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; public class TestConnectionBuilder { diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterImpl.java b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterImpl.java index a0d36046c..50530ecd8 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterImpl.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterImpl.java @@ -801,7 +801,7 @@ public class TestSaltstackAdapterImpl { TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); assertEquals("200", status); assertEquals(TestId, "test1"); - } catch (Exception e){ + } catch (Exception e) { //if local ssh is not enabled System.out.print(e.getMessage()); } @@ -829,7 +829,7 @@ public class TestSaltstackAdapterImpl { assertEquals(TestId, "test1"); TestId = svcContext.getAttribute("test1.minion1"); assertEquals(TestId, "true"); - } catch (Exception e){ + } catch (Exception e) { //if saltstack ssh IP is not enabled System.out.print(e.getMessage()); } @@ -855,7 +855,7 @@ public class TestSaltstackAdapterImpl { TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); assertEquals("200", status); assertEquals(TestId, "test1"); - } catch (Exception e){ + } catch (Exception e) { //if saltstack ssh IP is not enabled System.out.print(e.getMessage()); } @@ -881,7 +881,7 @@ public class TestSaltstackAdapterImpl { TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); assertEquals("200", status); assertEquals(TestId, "test1"); - } catch (Exception e){ + } catch (Exception e) { //if saltstack ssh IP is not enabled System.out.print(e.getMessage()); } @@ -908,7 +908,7 @@ public class TestSaltstackAdapterImpl { TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); assertEquals("200", status); assertEquals(TestId, "test1"); - } catch (Exception e){ + } catch (Exception e) { //if saltstack ssh IP is not enabled System.out.print(e.getMessage()); } @@ -936,7 +936,7 @@ public class TestSaltstackAdapterImpl { TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); assertEquals("200", status); assertEquals(TestId, "test1"); - } catch (Exception e){ + } catch (Exception e) { //if saltstack ssh IP is not enabled System.out.print(e.getMessage()); } @@ -964,7 +964,7 @@ public class TestSaltstackAdapterImpl { TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); assertEquals("200", status); assertEquals(TestId, "test1"); - } catch (Exception e){ + } catch (Exception e) { //if saltstack ssh IP is not enabled System.out.print(e.getMessage()); } @@ -992,7 +992,7 @@ public class TestSaltstackAdapterImpl { TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); assertEquals("200", status); assertEquals(TestId, "test1"); - } catch (Exception e){ + } catch (Exception e) { //if saltstack ssh IP is not enabled System.out.print(e.getMessage()); } @@ -1018,7 +1018,7 @@ public class TestSaltstackAdapterImpl { TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); assertEquals("200", status); assertEquals(TestId, "test1"); - } catch (Exception e){ + } catch (Exception e) { //if saltstack ssh IP is not enabled System.out.print(e.getMessage()); } @@ -1046,7 +1046,7 @@ public class TestSaltstackAdapterImpl { TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); assertEquals("200", status); assertEquals(TestId, "test1"); - } catch (Exception e){ + } catch (Exception e) { //if saltstack ssh IP is not enabled System.out.print(e.getMessage()); } diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterPropertiesProviderImpl.java b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterPropertiesProviderImpl.java index 29d08801b..57590baec 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterPropertiesProviderImpl.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterPropertiesProviderImpl.java @@ -29,7 +29,6 @@ import org.junit.Before; import org.junit.Test; import org.onap.ccsdk.sli.adaptors.saltstack.SaltstackAdapterPropertiesProvider; import org.onap.ccsdk.sli.adaptors.saltstack.impl.SaltstackAdapterImpl; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import org.onap.ccsdk.sli.core.sli.SvcLogicException; import java.util.Properties; @@ -258,7 +257,6 @@ public class TestSaltstackAdapterPropertiesProviderImpl { } - @Test public void reqExecCommand_setPropertiesElsePortNull() throws SvcLogicException, IllegalStateException, IllegalArgumentException { diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/model/TestJsonParser.java b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/model/TestJsonParser.java index d082285f5..71798fbc8 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/model/TestJsonParser.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/model/TestJsonParser.java @@ -8,9 +8,9 @@ * 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 - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -23,8 +23,8 @@ package org.onap.ccsdk.adapter.model; import org.codehaus.jettison.json.JSONException; import org.junit.Test; -import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.onap.ccsdk.sli.adaptors.saltstack.model.JsonParser; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/resources/org/onap/ccsdk/default.properties b/saltstack-adapter/saltstack-adapter-provider/src/test/resources/org/onap/ccsdk/default.properties index 3e7e2bcc4..e5650e853 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/test/resources/org/onap/ccsdk/default.properties +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/resources/org/onap/ccsdk/default.properties @@ -21,7 +21,6 @@ # # ============LICENSE_END========================================================= ### - # # Default properties for the APP-C Provider Adapter # @@ -31,9 +30,7 @@ # to supply configuration options org.onap.appc.bootstrap.file=appc.properties org.onap.appc.bootstrap.path=/opt/onap/appc/data/properties,${user.home},. - appc.application.name=APPC - # # Define the message resource bundle name to be loaded org.onap.appc.resources=org/onap/appc/i18n/MessageResources @@ -53,15 +50,13 @@ org.onap.appc.security.logger=org.onap.appc.security # means that the upper bound on the pool is unbounded. org.onap.appc.provider.min.pool=1 org.onap.appc.provider.max.pool=0 - # # The following properties are used to configure the retry logic for connection to the # IaaS provider(s). The retry delay property is the amount of time, in seconds, the # application waits between retry attempts. The retry limit is the number of retries # that are allowed before the request is failed. -org.onap.appc.provider.retry.delay = 30 -org.onap.appc.provider.retry.limit = 10 - +org.onap.appc.provider.retry.delay=30 +org.onap.appc.provider.retry.limit=10 # # The trusted hosts list for SSL access when a certificate is not provided. # @@ -85,21 +80,17 @@ org.onap.appc.openstack.poll.interval=20 # the values for a different provider. Any number of providers can be defined in this # way. # - # Don't change these 2 right now since they are hard coded in the DG #provider1.type=appc #provider1.name=appc - #These you can change #provider1.identity=appc #provider1.tenant1.name=appc #provider1.tenant1.userid=appc #provider1.tenant1.password=appc - # After a change to the provider make sure to recheck these values with an api call to provider1.identity/tokens test.expected-regions=1 test.expected-endpoints=1 - #Your OpenStack IP #test.ip=192.168.1.2 # Your OpenStack Platform's Keystone Port (default is 5000) diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/resources/test-invalid.json b/saltstack-adapter/saltstack-adapter-provider/src/test/resources/test-invalid.json index 53158cada..d16bfccdf 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/test/resources/test-invalid.json +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/resources/test-invalid.json @@ -6,8 +6,8 @@ "equipment-id": "Server1", "server-model": "Unknown", "server-id": "Server1", -"test-node" : { -"test-inner-node" : "Test-Value" +"test-node": { +"test-inner-node": "Test-Value" } } ], diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/resources/test.json b/saltstack-adapter/saltstack-adapter-provider/src/test/resources/test.json index cd76486aa..81131d6f4 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/test/resources/test.json +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/resources/test.json @@ -7,8 +7,8 @@ "equipment-id": "Server1", "server-model": "Unknown", "server-id": "Server1", - "test-node" : { - "test-inner-node" : "Test-Value" + "test-node": { + "test-inner-node": "Test-Value" } } ], -- cgit 1.2.3-korg