diff options
Diffstat (limited to 'saltstack-adapter/saltstack-adapter-provider/src')
6 files changed, 150 insertions, 20 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 cc4ce95c1..3469103b5 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,8 +84,8 @@ public class ConnectionBuilder { * @param cmd Commands to execute * @return command execution status */ - public SaltstackResult connectNExecute(String cmd) throws IOException { - return connectNExecute(cmd, -1, -1); + public SaltstackResult connectNExecute(String cmd, long execTimeout) throws IOException { + return connectNExecute(cmd, -1, -1, execTimeout); } /** @@ -98,12 +98,16 @@ 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, long execTimeout) throws IOException{ SaltstackResult result = new SaltstackResult(); OutputStream out = null; OutputStream errs = null; + if (execTimeout >= 0) { + sshConnection.setExecTimeout(execTimeout); + } + try { if (retryCount != -1) { result = sshConnection.connectWithRetry(retryCount, retryDelay); @@ -117,7 +121,7 @@ public class ConnectionBuilder { String errFilePath = "/tmp/" + RandomStringUtils.random(5, true, true); out = new FileOutputStream(outFilePath); errs = new FileOutputStream(errFilePath); - result = sshConnection.execCommand(cmd, out, errs); + result = sshConnection.execCommand(cmd, out, errs, result); sshConnection.disconnect(); if (result.getSshExitStatus() != 0) { return sortExitStatus(result.getSshExitStatus(), errFilePath, cmd); 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 0b6a5bb22..e4bceb5ba 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 @@ -318,7 +318,7 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { reqID = messageProcessor.reqId(params); String commandToExecute = messageProcessor.reqCmd(params); slsExec = messageProcessor.reqIsSLSExec(params); - testResult = execCommand(ctx, params, commandToExecute); + testResult = execCommand(ctx, params, commandToExecute, -1); testResult = messageProcessor.parseResponse(ctx, reqID, testResult, slsExec); checkResponseStatus(testResult, ctx, reqID, slsExec); } catch (IOException e) { @@ -344,8 +344,9 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { reqID = messageProcessor.reqId(params); String slsName = messageProcessor.reqSlsName(params); String applyTo = messageProcessor.reqApplyToDevices(params); + long execTimeout = messageProcessor.reqExecTimeout(params); String commandToExecute = putToCommands(slsName, applyTo); - testResult = execCommand(ctx, params, commandToExecute); + testResult = execCommand(ctx, params, commandToExecute, execTimeout); testResult = messageProcessor.parseResponse(ctx, reqID, testResult, true); checkResponseStatus(testResult, ctx, reqID, true); } catch (IOException e) { @@ -371,8 +372,9 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { reqID = messageProcessor.reqId(params); String slsFile = messageProcessor.reqSlsFile(params); String applyTo = messageProcessor.reqApplyToDevices(params); + long execTimeout = messageProcessor.reqExecTimeout(params); String commandToExecute = putToCommands(ctx, slsFile, applyTo); - testResult = execCommand(ctx, params, commandToExecute); + testResult = execCommand(ctx, params, commandToExecute, execTimeout); testResult = messageProcessor.parseResponse(ctx, reqID, testResult, true); checkResponseStatus(testResult, ctx, reqID, true); } catch (IOException e) { @@ -394,7 +396,8 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { } - public SaltstackResult execCommand(SvcLogicContext ctx, Map<String, String> params, String commandToExecute) + public SaltstackResult execCommand(SvcLogicContext ctx, Map<String, String> params, String commandToExecute, + long execTimeout) throws SvcLogicException{ SaltstackResult testResult = new SaltstackResult(); @@ -403,13 +406,13 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { 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); + testResult = sshClient.connectNExecute(commandToExecute, retryCount, retryDelay, execTimeout); } else { testResult = testServer.mockReqExec(params); } } else { if (!testMode) { - testResult = sshClient.connectNExecute(commandToExecute); + testResult = sshClient.connectNExecute(commandToExecute, execTimeout); } else { testResult = testServer.mockReqExec(params); } 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 96ed7d2d6..fd66eb100 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 @@ -50,6 +50,7 @@ class SshConnection { public static final int DEFAULT_CONNECTION_RETRY_COUNT = 5; private static final EELFLogger logger = EELFManager.getInstance().getApplicationLogger(); private static final long AUTH_TIMEOUT = 60000; + //TODO : change back to 120000 private static final long EXEC_TIMEOUT = 120000; private String host; private int port; @@ -165,16 +166,17 @@ class SshConnection { this.timeout = timeout; } - public SaltstackResult execCommand(String cmd, OutputStream out, OutputStream err) { - return execCommand(cmd, out, err, false); + 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) { - return execCommand(cmd, out, out, true); + 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 = new SaltstackResult(); + private SaltstackResult execCommand(String cmd, OutputStream out, OutputStream err, + boolean usePty, SaltstackResult result ) { + try { if (logger.isDebugEnabled()) { logger.debug("SSH: executing command"); 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 0a6e4eb89..16ab8dca4 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 @@ -72,6 +72,7 @@ public class SaltstackMessageParser { private static final String SLS_FILE_LOCATION = "slsFile"; private static final String SLS_NAME = "slsName"; private static final String MINION_TO_APPLY = "applyTo"; + private static final String EXEC_TIMEOUT_TO_APPLY = "execTimeout"; private static final String LOCAL_PARAMETERS_OPT_KEY = "LocalParameters"; private static final String FILE_PARAMETERS_OPT_KEY = "FileParameters"; @@ -240,6 +241,21 @@ public class SaltstackMessageParser { /** * Method that validates that the Map has enough information * to query Saltstack server for a result. If so, it returns + * the appropriate minions/vnfc to execute the SLS file to. + */ + public long reqExecTimeout(Map<String, String> params) { + + if (params.get(SaltstackMessageParser.EXEC_TIMEOUT_TO_APPLY) == null) { + return -1; + } else if (params.get(SaltstackMessageParser.EXEC_TIMEOUT_TO_APPLY).equalsIgnoreCase("")) { + return -1; + } + return Long.parseLong(params.get(SaltstackMessageParser.EXEC_TIMEOUT_TO_APPLY)); + } + + /** + * Method that validates that the Map has enough information + * to query Saltstack server for a result. If so, it returns * the appropriate IsSLSExec true or false. */ public boolean reqIsSLSExec(Map<String, String> params) throws SvcLogicException { @@ -333,10 +349,10 @@ public class SaltstackMessageParser { if (slsExec) { if (!retCodeFound) return new SaltstackResult(SaltstackResultCodes.COMMAND_EXEC_FAILED_STATUS.getValue(), - "error in parsing response Json after SLS file execution in server"); + "error in executing configuration at the server"); if (!executionStatus) return new SaltstackResult(SaltstackResultCodes.COMMAND_EXEC_FAILED_STATUS.getValue(), - "error in parsing response Json after SLS file execution in server"); + "error in executing configuration at the server"); } saltstackResult.setStatusCode(SaltstackResultCodes.FINAL_SUCCESS.getValue()); return saltstackResult; @@ -355,8 +371,8 @@ public class SaltstackMessageParser { String name = (String) key; String value = prop.getProperty(name); if (value != null && value.trim().length() > 0) { - ctx.setAttribute(pfx + name, value.trim()); - LOGGER.info("+++ " + pfx + name + ": [" + value + "]"); + ctx.setAttribute(pfx + "." + name, value.trim()); + LOGGER.info("+++ " + pfx + "." + name + ": [" + value + "]"); } } } catch (Exception e) { 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 0622a4716..48f5c20b8 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 @@ -799,4 +799,107 @@ public class TestSaltstackAdapterImpl { fail(e.getMessage() + " Unknown exception encountered "); } } + + @Test + public void reqExecCommand_shouldSetSuccessReal() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "127.0.0.1"); + params.put("Port", "22"); + params.put("User", "sdn"); + params.put("Password", "foo"); + params.put("Id", "test1"); + params.put("cmd", "ls -l"); + params.put("slsExec", "false"); + params.put("execTimeout", "12000"); + adapter = new SaltstackAdapterImpl(); + try { + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("250", status); + assertEquals(TestId, "test1"); + } catch (Exception e){ + //if local ssh is not enabled + return; + } + } + + @Test + public void reqExecCommand_shouldSetSuccessRealCommand() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "<IP>"); + params.put("Port", "2222"); + params.put("User", "root"); + params.put("Password", "vagrant"); + params.put("Id", "test1"); + params.put("cmd", "cd /srv/salt/; salt '*' state.apply vim --out=json --static"); + params.put("slsExec", "true"); + params.put("execTimeout", "12000"); + + adapter = new SaltstackAdapterImpl(); + try { + adapter.reqExecCommand(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } catch (Exception e){ + //if saltstack ssh IP is not enabled + return; + } + } + + @Test + public void reqExecCommand_shouldSetSuccessRealSSL() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "10.251.92.17"); + params.put("Port", "2222"); + params.put("User", "root"); + params.put("Password", "vagrant"); + params.put("Id", "test1"); + params.put("slsName", "vim"); + params.put("execTimeout", "12000"); + params.put("applyTo", "minion1"); + + adapter = new SaltstackAdapterImpl(); + try { + adapter.reqExecSLS(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } catch (Exception e){ + //if saltstack ssh IP is not enabled + return; + } + } + + @Test + public void reqExecCommand_shouldSetSuccessSSLFile() throws SvcLogicException, + IllegalStateException, IllegalArgumentException { + + params.put("HostName", "10.251.92.17"); + params.put("Port", "2222"); + params.put("User", "root"); + params.put("Password", "vagrant"); + params.put("Id", "test1"); + params.put("execTimeout", "12000"); + params.put("applyTo", "minion1"); + params.put("slsFile", "src/test/resources/config.sls"); + + adapter = new SaltstackAdapterImpl(); + try { + adapter.reqExecSLSFile(params, svcContext); + String status = svcContext.getAttribute("org.onap.appc.adapter.saltstack.result.code"); + TestId = svcContext.getAttribute("org.onap.appc.adapter.saltstack.Id"); + assertEquals("200", status); + assertEquals(TestId, "test1"); + } catch (Exception e){ + //if saltstack ssh IP is not enabled + return; + } + } } diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/resources/config.sls b/saltstack-adapter/saltstack-adapter-provider/src/test/resources/config.sls new file mode 100644 index 000000000..aff05939b --- /dev/null +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/resources/config.sls @@ -0,0 +1,2 @@ +vim: + pkg.installed |