summaryrefslogtreecommitdiffstats
path: root/saltstack-adapter/saltstack-adapter-provider/src/main
diff options
context:
space:
mode:
authorGanesh Chandrasekaran <ganesh.c@samsung.com>2018-07-13 09:41:32 +0900
committerGanesh Chandrasekaran <ganesh.c@samsung.com>2018-07-16 16:24:56 +0900
commit3323c0829fa0492a5c7b3544a83cc24412c9934c (patch)
tree421b5d254821a24d63869b8c6651ea74a562d1ea /saltstack-adapter/saltstack-adapter-provider/src/main
parent3c36ac2ba101ec0cc596c15bfd45efcc731f442f (diff)
saltstack adaptor fixes from Sonar
Issue-ID: CCSDK-364 Change-Id: Ief9705370d84fcb99fcb718f03c31e6cbfd47363 Signed-off-by: Ganesh Chandrasekaran <ganesh.c@samsung.com>
Diffstat (limited to 'saltstack-adapter/saltstack-adapter-provider/src/main')
-rw-r--r--saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/ConnectionBuilder.java18
-rw-r--r--saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SaltstackAdapterImpl.java99
-rw-r--r--saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackMessageParser.java18
-rw-r--r--saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackServerEmulator.java41
4 files changed, 86 insertions, 90 deletions
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 65ab598d..cc4ce95c 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
@@ -84,7 +84,7 @@ public class ConnectionBuilder {
* @param cmd Commands to execute
* @return command execution status
*/
- public SaltstackResult connectNExecute(String cmd) {
+ public SaltstackResult connectNExecute(String cmd) throws IOException {
return connectNExecute(cmd, -1, -1);
}
@@ -98,9 +98,12 @@ public class ConnectionBuilder {
* @param retryCount number of count retry to make a SSH connection.
* @return command execution status
*/
- public SaltstackResult connectNExecute(String cmd, int retryCount, int retryDelay) {
+ public SaltstackResult connectNExecute(String cmd, int retryCount, int retryDelay)
+ throws IOException{
SaltstackResult result = new SaltstackResult();
+ OutputStream out = null;
+ OutputStream errs = null;
try {
if (retryCount != -1) {
result = sshConnection.connectWithRetry(retryCount, retryDelay);
@@ -112,12 +115,10 @@ public class ConnectionBuilder {
}
String outFilePath = "/tmp/" + RandomStringUtils.random(5, true, true);
String errFilePath = "/tmp/" + RandomStringUtils.random(5, true, true);
- OutputStream out = new FileOutputStream(outFilePath);
- OutputStream errs = new FileOutputStream(errFilePath);
+ out = new FileOutputStream(outFilePath);
+ errs = new FileOutputStream(errFilePath);
result = sshConnection.execCommand(cmd, out, errs);
sshConnection.disconnect();
- out.close();
- errs.close();
if (result.getSshExitStatus() != 0) {
return sortExitStatus(result.getSshExitStatus(), errFilePath, cmd);
}
@@ -130,6 +131,11 @@ public class ConnectionBuilder {
logger.error("Caught Exception", io);
result.setStatusCode(SaltstackResultCodes.UNKNOWN_EXCEPTION.getValue());
result.setStatusMessage(io.getMessage());
+ } finally {
+ if( out != null )
+ out.close();
+ if( errs != null )
+ errs.close();
}
return result;
}
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 84e5d4f1..0b6a5bb2 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
@@ -81,7 +81,7 @@ public class SaltstackAdapterImpl implements SaltstackAdapter {
private static final String SS_SERVER_HOSTNAME = "org.onap.appc.adapter.saltstack.host";
private static final String SS_SERVER_PORT = "org.onap.appc.adapter.saltstack.port";
private static final String SS_SERVER_USERNAME = "org.onap.appc.adapter.saltstack.userName";
- private static final String SS_SERVER_PASSWORD = "org.onap.appc.adapter.saltstack.userPasswd";
+ private static final String SS_SERVER_PASSWD = "org.onap.appc.adapter.saltstack.userPasswd";
private static final String SS_SERVER_SSH_KEY = "org.onap.appc.adapter.saltstack.sshKey";
/**
* The logger to be used
@@ -186,7 +186,7 @@ public class SaltstackAdapterImpl implements SaltstackAdapter {
String sshHost = props.getProperty(SS_SERVER_HOSTNAME);
String sshPort = props.getProperty(SS_SERVER_PORT);
String sshUserName = props.getProperty(SS_SERVER_USERNAME);
- String sshPassword = props.getProperty(SS_SERVER_PASSWORD);
+ String sshPassword = props.getProperty(SS_SERVER_PASSWD);
sshClient = new ConnectionBuilder(sshHost, sshPort, sshUserName, sshPassword);
} else if ("SSH_CERT".equalsIgnoreCase(clientType)) {
// set path to keystore file
@@ -200,13 +200,10 @@ public class SaltstackAdapterImpl implements SaltstackAdapter {
String sshKey = props.getProperty(SS_SERVER_SSH_KEY);
String sshHost = props.getProperty(SS_SERVER_HOSTNAME);
String sshUserName = props.getProperty(SS_SERVER_USERNAME);
- String sshPassword = props.getProperty(SS_SERVER_PASSWORD);
+ String sshPassword = props.getProperty(SS_SERVER_PASSWD);
String sshPort = props.getProperty(SS_SERVER_PORT);
logger.info("Creating ssh client with ssh KEY from " + sshKey);
sshClient = new ConnectionBuilder(sshHost, sshPort, sshUserName, sshPassword, sshKey);
- } else if ("NONE".equalsIgnoreCase(clientType)) {
- logger.info("No saltstack-adapter.properties defined so reading from DG props");
- sshClient = null;
} else {
logger.info("No saltstack-adapter.properties defined so reading from DG props");
sshClient = null;
@@ -317,12 +314,17 @@ public class SaltstackAdapterImpl implements SaltstackAdapter {
boolean slsExec;
SaltstackResult testResult;
setSSHClient(params);
- reqID = messageProcessor.reqId(params);
- String commandToExecute = messageProcessor.reqCmd(params);
- slsExec = messageProcessor.reqIsSLSExec(params);
- testResult = execCommand(params, commandToExecute);
- testResult = messageProcessor.parseResponse(ctx, reqID, testResult, slsExec);
- checkResponseStatus(testResult, ctx, reqID, slsExec);
+ try {
+ reqID = messageProcessor.reqId(params);
+ String commandToExecute = messageProcessor.reqCmd(params);
+ slsExec = messageProcessor.reqIsSLSExec(params);
+ testResult = execCommand(ctx, params, commandToExecute);
+ testResult = messageProcessor.parseResponse(ctx, reqID, testResult, slsExec);
+ checkResponseStatus(testResult, ctx, reqID, slsExec);
+ } catch (IOException e) {
+ doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(),
+ "IOException in file stream : "+ e.getMessage());
+ }
}
/**
@@ -338,13 +340,18 @@ public class SaltstackAdapterImpl implements SaltstackAdapter {
String reqID;
SaltstackResult testResult;
setSSHClient(params);
- reqID = messageProcessor.reqId(params);
- String slsName = messageProcessor.reqSlsName(params);
- String applyTo = messageProcessor.reqApplyToDevices(params);
- String commandToExecute = putToCommands(slsName, applyTo);
- testResult = execCommand(params, commandToExecute);
- testResult = messageProcessor.parseResponse(ctx, reqID, testResult, true);
- checkResponseStatus(testResult, ctx, reqID, true);
+ try {
+ reqID = messageProcessor.reqId(params);
+ String slsName = messageProcessor.reqSlsName(params);
+ String applyTo = messageProcessor.reqApplyToDevices(params);
+ String commandToExecute = putToCommands(slsName, applyTo);
+ testResult = execCommand(ctx, params, commandToExecute);
+ testResult = messageProcessor.parseResponse(ctx, reqID, testResult, true);
+ checkResponseStatus(testResult, ctx, reqID, true);
+ } catch (IOException e) {
+ doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(),
+ "IOException in file stream : "+ e.getMessage());
+ }
}
/**
@@ -360,13 +367,18 @@ public class SaltstackAdapterImpl implements SaltstackAdapter {
String reqID;
SaltstackResult testResult;
setSSHClient(params);
- reqID = messageProcessor.reqId(params);
- String slsFile = messageProcessor.reqSlsFile(params);
- String applyTo = messageProcessor.reqApplyToDevices(params);
- String commandToExecute = putToCommands(ctx, slsFile, applyTo);
- testResult = execCommand(params, commandToExecute);
- testResult = messageProcessor.parseResponse(ctx, reqID, testResult, true);
- checkResponseStatus(testResult, ctx, reqID, true);
+ try {
+ reqID = messageProcessor.reqId(params);
+ String slsFile = messageProcessor.reqSlsFile(params);
+ String applyTo = messageProcessor.reqApplyToDevices(params);
+ String commandToExecute = putToCommands(ctx, slsFile, applyTo);
+ testResult = execCommand(ctx, params, commandToExecute);
+ testResult = messageProcessor.parseResponse(ctx, reqID, testResult, true);
+ checkResponseStatus(testResult, ctx, reqID, true);
+ } catch (IOException e) {
+ doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(),
+ "IOException in file stream : "+ e.getMessage());
+ }
}
/**
@@ -382,22 +394,29 @@ public class SaltstackAdapterImpl implements SaltstackAdapter {
}
- public SaltstackResult execCommand(Map<String, String> params, String commandToExecute) {
- SaltstackResult testResult;
- if (params.get(CONNECTION_RETRY_DELAY) != null && params.get(CONNECTION_RETRY_COUNT) != null) {
- int retryDelay = Integer.parseInt(params.get(CONNECTION_RETRY_DELAY));
- int retryCount = Integer.parseInt(params.get(CONNECTION_RETRY_COUNT));
- if (!testMode) {
- testResult = sshClient.connectNExecute(commandToExecute, retryCount, retryDelay);
- } else {
- testResult = testServer.MockReqExec(params);
- }
- } else {
- if (!testMode) {
- testResult = sshClient.connectNExecute(commandToExecute);
+ public SaltstackResult execCommand(SvcLogicContext ctx, Map<String, String> params, String commandToExecute)
+ throws SvcLogicException{
+
+ SaltstackResult testResult = new SaltstackResult();
+ try {
+ if (params.get(CONNECTION_RETRY_DELAY) != null && params.get(CONNECTION_RETRY_COUNT) != null) {
+ int retryDelay = Integer.parseInt(params.get(CONNECTION_RETRY_DELAY));
+ int retryCount = Integer.parseInt(params.get(CONNECTION_RETRY_COUNT));
+ if (!testMode) {
+ testResult = sshClient.connectNExecute(commandToExecute, retryCount, retryDelay);
+ } else {
+ testResult = testServer.mockReqExec(params);
+ }
} else {
- testResult = testServer.MockReqExec(params);
+ if (!testMode) {
+ testResult = sshClient.connectNExecute(commandToExecute);
+ } else {
+ testResult = testServer.mockReqExec(params);
+ }
}
+ } catch (IOException e) {
+ doFailure(ctx, SaltstackResultCodes.IO_EXCEPTION.getValue(),
+ "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/model/SaltstackMessageParser.java b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/model/SaltstackMessageParser.java
index f282a338..0a6e4eb8 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
@@ -41,6 +41,7 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
+import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashSet;
@@ -289,15 +290,16 @@ public class SaltstackMessageParser {
* and returns an SaltstackResult object.
*/
public SaltstackResult parseResponse(SvcLogicContext ctx, String pfx,
- SaltstackResult saltstackResult, boolean slsExec) {
+ 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;
}
try {
File file = new File(saltstackResult.getOutputFileName());
- InputStream in = new FileInputStream(file);
+ in = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
in.read(data);
String str = new String(data, "UTF-8");
@@ -324,6 +326,9 @@ public class SaltstackMessageParser {
} catch (Exception e) {
return new SaltstackResult(SaltstackResultCodes.INVALID_RESPONSE_FILE.getValue(), "error parsing response file "
+ saltstackResult.getOutputFileName() + " : " + e.getMessage());
+ } finally {
+ if( in != null )
+ in.close();
}
if (slsExec) {
if (!retCodeFound)
@@ -337,10 +342,12 @@ public class SaltstackMessageParser {
return saltstackResult;
}
- public SaltstackResult putToProperties(SvcLogicContext ctx, String pfx, SaltstackResult saltstackResult) {
+ public SaltstackResult putToProperties(SvcLogicContext ctx, String pfx,
+ SaltstackResult saltstackResult) throws IOException{
+ InputStream in = null;
try {
File file = new File(saltstackResult.getOutputFileName());
- InputStream in = new FileInputStream(file);
+ in = new FileInputStream(file);
Properties prop = new Properties();
prop.load(in);
ctx.setAttribute(pfx + "completeResult", prop.toString());
@@ -355,6 +362,9 @@ public class SaltstackMessageParser {
} catch (Exception e) {
saltstackResult = new SaltstackResult(SaltstackResultCodes.INVALID_RESPONSE_FILE.getValue(), "Error parsing response file = "
+ saltstackResult.getOutputFileName() + ". Error = " + e.getMessage());
+ } finally {
+ if( in != null )
+ in.close();
}
saltstackResult.setStatusCode(SaltstackResultCodes.FINAL_SUCCESS.getValue());
return saltstackResult;
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 ecb36fb8..adbf9bd9 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
@@ -55,7 +55,7 @@ public class SaltstackServerEmulator {
* Returns an saltstack object result. The response code is always the ssh code 200 (i.e connection successful)
* payload is json string as would be sent back by Saltstack Server
**/
- public SaltstackResult MockReqExec(Map<String, String> params) {
+ public SaltstackResult mockReqExec(Map<String, String> params) {
SaltstackResult result = new SaltstackResult();
try {
@@ -75,45 +75,6 @@ public class SaltstackServerEmulator {
return result;
}
- /**
- * Method to emulate response from an Saltstack
- * Server when presented with a GET request
- * Returns an saltstack object result. The response code is always the ssh code 200 (i.e connection successful)
- * payload is json string as would be sent back by Saltstack Server
- **/
- public SaltstackResult Execute(String agentUrl) {
-
- Pattern pattern = Pattern.compile(".*?\\?Id=(.*?)&Type.*");
- Matcher matcher = pattern.matcher(agentUrl);
- String id = StringUtils.EMPTY;
- String vmAddress = "192.168.1.10";
-
- if (matcher.find()) {
- id = matcher.group(1);
- }
-
- SaltstackResult getResult = new SaltstackResult();
-
- JSONObject response = new JSONObject();
- response.put(STATUS_CODE, 200);
- response.put(STATUS_MESSAGE, "FINISHED");
-
- JSONObject results = new JSONObject();
-
- JSONObject vmResults = new JSONObject();
- vmResults.put(STATUS_CODE, 200);
- vmResults.put(STATUS_MESSAGE, "SUCCESS");
- vmResults.put("Id", id);
- results.put(vmAddress, vmResults);
-
- response.put("Results", results);
-
- getResult.setStatusCode(200);
- getResult.setStatusMessage(response.toString());
-
- return getResult;
- }
-
private SaltstackResult rejectRequest(SaltstackResult result, String Message) {
result.setStatusCode(SaltstackResultCodes.REJECTED.getValue());
result.setStatusMessage("Rejected");