diff options
25 files changed, 601 insertions, 189 deletions
diff --git a/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/appc/adapter/ansible/impl/TestAnsibleAdapterImpl.java b/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/ccsdk/adapter/ansible/impl/TestAnsibleAdapterImpl.java index d96a709c..86bed7e4 100644 --- a/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/appc/adapter/ansible/impl/TestAnsibleAdapterImpl.java +++ b/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/ccsdk/adapter/ansible/impl/TestAnsibleAdapterImpl.java @@ -22,7 +22,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.appc.adapter.ansible.impl; +package org.onap.ccsdk.adapter.ansible.impl; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; diff --git a/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/appc/adapter/ansible/model/TestAnsibleAdapter.java b/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/ccsdk/adapter/ansible/model/TestAnsibleAdapter.java index aebc1c0d..6fc90d01 100644 --- a/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/appc/adapter/ansible/model/TestAnsibleAdapter.java +++ b/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/ccsdk/adapter/ansible/model/TestAnsibleAdapter.java @@ -21,7 +21,7 @@ * ECOMP is a trademark and service mark of AT&T Intellectual Property. * ============LICENSE_END========================================================= */ -package org.onap.appc.adapter.ansible.model; +package org.onap.ccsdk.adapter.ansible.model; import static org.junit.Assert.assertNotNull; diff --git a/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/appc/test/ExecutorHarness.java b/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/ccsdk/test/ExecutorHarness.java index 13b5fdfb..3555d7df 100644 --- a/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/appc/test/ExecutorHarness.java +++ b/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/ccsdk/test/ExecutorHarness.java @@ -23,7 +23,7 @@ */ -package org.onap.appc.test; +package org.onap.ccsdk.test; import java.lang.reflect.Field; import java.lang.reflect.Method; @@ -32,7 +32,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.onap.appc.test.InterceptLogger; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin; diff --git a/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/appc/test/InterceptLogger.java b/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/ccsdk/test/InterceptLogger.java index b101ecee..92235cb3 100644 --- a/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/appc/test/InterceptLogger.java +++ b/ansible-adapter/ansible-adapter-bundle/src/test/java/org/onap/ccsdk/test/InterceptLogger.java @@ -23,7 +23,7 @@ */ -package org.onap.appc.test; +package org.onap.ccsdk.test; import java.text.MessageFormat; import java.util.ArrayList; diff --git a/ansible-adapter/ansible-adapter-bundle/src/test/resources/org/onap/appc/default.properties b/ansible-adapter/ansible-adapter-bundle/src/test/resources/org/onap/ccsdk/default.properties index 2f8fb458..2f8fb458 100644 --- a/ansible-adapter/ansible-adapter-bundle/src/test/resources/org/onap/appc/default.properties +++ b/ansible-adapter/ansible-adapter-bundle/src/test/resources/org/onap/ccsdk/default.properties diff --git a/saltstack-adapter/README.md b/saltstack-adapter/README.md index 8e989a87..87c43f98 100644 --- a/saltstack-adapter/README.md +++ b/saltstack-adapter/README.md @@ -31,7 +31,8 @@ Create an Adaptor to communicate with the SaltStack server: ***Requirements and benefits of the chosen SSH method:*** 1) The SaltStack server should have it’s SSH enabled. -2) Such execution method will give the DGs and adaptor with more refined control on the SaltStack server. +2) Via ssh user account we should have the access to run saltstack command. +3) Such execution method will give the DGs and adaptor with more refined control on the SaltStack server. ================================================================================================================== @@ -50,7 +51,8 @@ Create an Adaptor to communicate with the SaltStack server: "User"; -> Saltstack server's SSH Password. Note: SSH_CERT based Auth is not supported in this method. -***Using Saltstack Adaptor Commands and params to pass in:*** reqExecCommand: +***Using Saltstack Adaptor Commands and params to pass in: reqExecCommand API:*** + Method to execute a single command on SaltState server and execute a SLS file located on the server. The command entered should request the output in JSON format, this can be done by appending json-out outputter as specified in https://docs.saltstack.com/en/latest/ref/output/all/salt.output.json_out.html#module-salt.output.json_out and https://docs.saltstack.com/en/2017.7/ref/cli/salt-call.html The response from Saltstack comes in json format and it is automatically put to context for DGs access, with a certain request-ID as prefix. If Id is not passed as part of input param, then a random Id will be generated and put to properties in "org.onap.appc.adapter.saltstack.Id" field. All the output message from the execution will be appended with reqId. @@ -72,8 +74,8 @@ here for instance, in 1.1) the user should check if $reqId.<minion-name> is set 2) Execute a SLS file located on the server : Example command will look like: Knowing the saltstack server has vim.sls file located at "/srv/salt" directory then user can execute the following commands: -1.1) Command to run the vim.sls file on saltstack server: "salt '*' state.apply vim --out=json --static" -1.2) Command to run the nettools.sls file on saltstack server: "salt '*' state.apply nettools --out=json --static" +1.1) Command to run the vim.sls file on saltstack server: cmd = "salt '*' state.apply vim --out=json --static" +1.2) Command to run the nettools.sls file on saltstack server: cmd = "cd /srv/salt/; salt '*' state.apply <sls-file-name> --out=json --static" Important thing to note: If the reqExecCommand is used to execute sls file then along with following, "HostName"; -> Saltstack server's host name IP address. "Port"; -> Saltstack server's port to make SSH connection to. @@ -81,15 +83,46 @@ Important thing to note: If the reqExecCommand is used to execute sls file then "User"; -> Saltstack server's SSH Password. the param should contain, "slsExec"; -> this variable should be set to true. + "execTimeout"; -> set large timeout if your SLS file will take large time to finish executing (in milliseconds). -In this case, params that will hold the command execution result for DG access are +In this case, params that will hold the command execution result for DG access in Key: Result code at: org.onap.appc.adapter.saltstack.result.code (On success: This will be 200, this means the command was executed successfully and also configuration change made using the SLS file was also successful) Message at: org.onap.appc.adapter.saltstack.message Both user inputted/auto generated req Id at: org.onap.appc.adapter.saltstack.Id The result code here will be the execution of configuration SLS file on the server. -NOTE: It would be better to use reqExecSLS, where you will only have to specify SLS file name on server. -***Using Saltstack Adaptor Commands and params to pass in:*** reqExecSLS: -Method to execute a single sls on SaltState server and execute a SLS file located on the server. The command entered should request the output in JSON format, this can be done by appending json-out outputter as specified in https://docs.saltstack.com/en/latest/ref/output/all/salt.output.json_out.html#module-salt.output.json_out and https://docs.saltstack.com/en/2017.7/ref/cli/salt-call.html +NOTE: It would be better to use reqExecSLS, where you will only have to specify SLS file name on server to execute it. + + +***Using Saltstack Adaptor Commands and params to pass in: reqExecSLS API:*** + +Method to execute a single sls on SaltState server (Where the SLS file already located on the server). The command entered will only be the SLS file name and the output will be in JSON format automatically. The response from Saltstack comes in json format and it is automatically put to context for DGs access, with a certain request-ID as prefix. -If Id is not passed as part of input param, then a random Id will be generated and put to properties in "org.onap.appc.adapter.saltstack.Id" field. All the output message from the execution will be appended with reqId. +If request Id (Id) is not passed as part of input param, then a random Id will be generated and put to properties in "org.onap.appc.adapter.saltstack.Id" field. All the output message from the execution will be appended with reqId. 1) Execute a single command on SaltState server : Example command will look like: + In the context set the "slsName" to "test.sls" + In the context set the "execTimeout"; -> set large timeout if your SLS file will take large time to finish executing (in milliseconds). + In the context set the "applyTo" to "minion1" //to the minions or VNFCs you want to apply the SLS file to. + "applyTo" can be empty or set to "*" is the SLS has to be applied to all the minions or VNFCs. +In this case, params that will hold the command execution result for DG access in Key: +Result code at: org.onap.appc.adapter.saltstack.result.code (On success: This will be 200, this means the command was executed successfully and also configuration change made using the SLS file was also successful) +Message at: org.onap.appc.adapter.saltstack.message +Both user inputted/auto generated req Id at: org.onap.appc.adapter.saltstack.Id +The result code here will be the execution of configuration SLS file on the server. + +***Using Saltstack Adaptor Commands and params to pass in: reqExecSLSFile API:*** + +Method to execute a single sls on SaltState server (Where the SLS file in the adaptor). The command entered will only be the SLS file location on the APPC/ODL container and the output from server will be in JSON format automatically. +The response from Saltstack comes in json format and it is automatically put to context for DGs access, with a certain request-ID as prefix. +If request Id (Id) is not passed as part of input param, then a random Id will be generated and put to properties in "org.onap.appc.adapter.saltstack.Id" field. All the output message from the execution will be appended with reqId. +1) Execute a single command on SaltState server : Example command will look like: + In the context set the "slsFile" to "/path/to/test.sls" //mention the path of the SLS file in ODL container. + In the context set the "execTimeout"; -> set large timeout if your SLS file will take large time to finish executing (in milliseconds). + In the context set the "applyTo" to "minion1" //to the minions or VNFCs you want to apply the SLS file to. + "applyTo" can be empty or set to "*" is the SLS has to be applied to all the minions or VNFCs. +In this case, params that will hold the command execution result for DG access in Key: +Result code at: org.onap.appc.adapter.saltstack.result.code (On success: This will be 200, this means the command was executed successfully and also configuration change made using the SLS file was also successful) +Message at: org.onap.appc.adapter.saltstack.message +Both user inputted/auto generated req Id at: org.onap.appc.adapter.saltstack.Id +The result code here will be the execution of configuration SLS file on the server. + + diff --git a/saltstack-adapter/saltstack-adapter-features/ccsdk-saltstack-adapter/pom.xml b/saltstack-adapter/saltstack-adapter-features/ccsdk-saltstack-adapter/pom.xml index 01b330e5..cf151ca5 100644 --- a/saltstack-adapter/saltstack-adapter-features/ccsdk-saltstack-adapter/pom.xml +++ b/saltstack-adapter/saltstack-adapter-features/ccsdk-saltstack-adapter/pom.xml @@ -47,4 +47,4 @@ </dependency> </dependencies> --> -</project>
\ No newline at end of file +</project> diff --git a/saltstack-adapter/saltstack-adapter-features/src/main/resources/features.xml b/saltstack-adapter/saltstack-adapter-features/src/main/resources/features.xml index 47db978b..5359d808 100644 --- a/saltstack-adapter/saltstack-adapter-features/src/main/resources/features.xml +++ b/saltstack-adapter/saltstack-adapter-features/src/main/resources/features.xml @@ -24,17 +24,17 @@ --> -<features name="appc-saltstack-adapter-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" +<features name="ccsdk-saltstack-adapter-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://karaf.apache.org/xmlns/features/v1.2.0 http://karaf.apache.org/xmlns/features/v1.2.0"> <repository>mvn:org.opendaylight.mdsal/features-mdsal/${odl.mdsal.features.version}/xml/features</repository> - <feature name='appc-saltstack-adapter' description="appc-saltstack-adapter" version='${project.version}'> + <feature name='ccsdk-saltstack-adapter' description="ccsdk-saltstack-adapter" version='${project.version}'> <feature version="${odl.mdsal.version}">odl-mdsal-broker</feature> <feature>sdnc-sli</feature> <bundle dependency="true">mvn:org.onap.appc/appc-common/${project.version}</bundle> - <bundle>mvn:org.onap.appc/appc-saltstack-adapter-provider/${project.version}</bundle> + <bundle>mvn:org.onap.ccsdk.sli.adaptors/saltstack-adapter-provider/${project.version}</bundle> </feature> </features> 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..3469103b 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) { - return connectNExecute(cmd, -1, -1); + public SaltstackResult connectNExecute(String cmd, long execTimeout) throws IOException { + return connectNExecute(cmd, -1, -1, execTimeout); } /** @@ -98,9 +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); @@ -112,12 +119,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); - result = sshConnection.execCommand(cmd, out, errs); + out = new FileOutputStream(outFilePath); + errs = new FileOutputStream(errFilePath); + result = sshConnection.execCommand(cmd, out, errs, result); sshConnection.disconnect(); - out.close(); - errs.close(); if (result.getSshExitStatus() != 0) { return sortExitStatus(result.getSshExitStatus(), errFilePath, cmd); } @@ -130,6 +135,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..e4bceb5b 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, -1); + 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,19 @@ 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); + long execTimeout = messageProcessor.reqExecTimeout(params); + String commandToExecute = putToCommands(slsName, applyTo); + testResult = execCommand(ctx, params, commandToExecute, execTimeout); + 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 +368,19 @@ 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); + long execTimeout = messageProcessor.reqExecTimeout(params); + String commandToExecute = putToCommands(ctx, slsFile, applyTo); + testResult = execCommand(ctx, params, commandToExecute, execTimeout); + 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 +396,30 @@ 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, + long execTimeout) + 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, execTimeout); + } else { + testResult = testServer.mockReqExec(params); + } } else { - testResult = testServer.MockReqExec(params); + if (!testMode) { + testResult = sshClient.connectNExecute(commandToExecute, execTimeout); + } 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/impl/SshConnection.java b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/impl/SshConnection.java index 96ed7d2d..fd66eb10 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 f282a338..16ab8dca 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; @@ -71,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"; @@ -239,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 { @@ -289,15 +306,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,23 +342,28 @@ 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) 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; } - 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()); @@ -348,13 +371,16 @@ 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) { 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"); diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/impl/TestConnectionBuilder.java b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestConnectionBuilder.java index d9a38414..933f3fcf 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/impl/TestConnectionBuilder.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestConnectionBuilder.java @@ -22,7 +22,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.appc.adapter.impl; +package org.onap.ccsdk.adapter.impl; import org.junit.After; import org.junit.Before; @@ -41,21 +41,12 @@ import static org.junit.Assert.fail; public class TestConnectionBuilder { - private final String PENDING = "100"; - private final String SUCCESS = "400"; - private String message = "{\"Results\":{\"192.168.1.10\":{\"Id\":\"101\",\"StatusCode\":200,\"StatusMessage\":\"SUCCESS\"}},\"StatusCode\":200,\"StatusMessage\":\"FINISHED\"}"; - private ConnectionBuilder connBuilder; - private String TestId; - private boolean testMode = true; private Map<String, String> params; - private SvcLogicContext svcContext; @Before public void setup() throws IllegalArgumentException { - testMode = true; - svcContext = new SvcLogicContext(); String HostName = "test"; String Port = "10"; String User = "test"; @@ -70,10 +61,8 @@ public class TestConnectionBuilder { @After public void tearDown() { - testMode = false; connBuilder = null; params = null; - svcContext = null; } @Test diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/impl/TestSaltstackAdapterImpl.java b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterImpl.java index d60059e4..48f5c20b 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/impl/TestSaltstackAdapterImpl.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterImpl.java @@ -22,7 +22,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.appc.adapter.impl; +package org.onap.ccsdk.adapter.impl; import org.junit.After; import org.junit.Before; @@ -40,10 +40,6 @@ import static org.junit.Assert.fail; public class TestSaltstackAdapterImpl { - private final String PENDING = "100"; - private final String SUCCESS = "400"; - private String message = "{\"Results\":{\"192.168.1.10\":{\"Id\":\"101\",\"StatusCode\":200,\"StatusMessage\":\"SUCCESS\"}},\"StatusCode\":200,\"StatusMessage\":\"FINISHED\"}"; - private SaltstackAdapterImpl adapter; private String TestId; private boolean testMode = true; @@ -457,7 +453,6 @@ public class TestSaltstackAdapterImpl { params.put("Id", "test1"); 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(TestId, "test1"); } @@ -476,7 +471,6 @@ public class TestSaltstackAdapterImpl { params.put("Id", "test1"); 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(TestId, "test1"); } @@ -495,7 +489,6 @@ public class TestSaltstackAdapterImpl { params.put("Id", "test1"); 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(TestId, "test1"); } @@ -559,7 +552,6 @@ public class TestSaltstackAdapterImpl { params.put("applyTo", "minion1"); 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(TestId, "test1"); } @@ -579,7 +571,6 @@ public class TestSaltstackAdapterImpl { params.put("applyTo", "minion1"); 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(TestId, "test1"); } @@ -621,7 +612,6 @@ public class TestSaltstackAdapterImpl { params.put("applyTo", "*"); 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(TestId, "test1"); } @@ -641,7 +631,6 @@ public class TestSaltstackAdapterImpl { params.put("applyTo", "*"); 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(TestId, "test1"); } @@ -703,7 +692,6 @@ public class TestSaltstackAdapterImpl { params.put("Id", "test1"); 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(TestId, "test1"); } @@ -747,7 +735,6 @@ public class TestSaltstackAdapterImpl { params.put("applyTo", "minion1"); 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(TestId, "test1"); } @@ -790,7 +777,6 @@ public class TestSaltstackAdapterImpl { params.put("applyTo", "*"); 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(TestId, "test1"); } @@ -813,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/java/org/onap/appc/adapter/impl/TestSaltstackAdapterPropertiesProviderImpl.java b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterPropertiesProviderImpl.java index 92759183..094e78c7 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/impl/TestSaltstackAdapterPropertiesProviderImpl.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/impl/TestSaltstackAdapterPropertiesProviderImpl.java @@ -22,7 +22,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.appc.adapter.impl; +package org.onap.ccsdk.adapter.impl; import org.junit.After; import org.junit.Before; @@ -37,13 +37,9 @@ import java.util.Properties; import static org.junit.Assert.assertEquals; public class TestSaltstackAdapterPropertiesProviderImpl { - private final String PENDING = "100"; - private final String SUCCESS = "400"; - private String message = "{\"Results\":{\"192.168.1.10\":{\"Id\":\"101\",\"StatusCode\":200,\"StatusMessage\":\"SUCCESS\"}},\"StatusCode\":200,\"StatusMessage\":\"FINISHED\"}"; private SaltstackAdapterImpl adapter; private Properties params; - private SvcLogicContext svcContext; @Before @@ -55,7 +51,6 @@ public class TestSaltstackAdapterPropertiesProviderImpl { public void tearDown() { adapter = null; params = null; - svcContext = null; } @Test(expected = SvcLogicException.class) diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/model/TestJsonParser.java b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/model/TestJsonParser.java index ae13da6c..74e7ed0c 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/model/TestJsonParser.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/model/TestJsonParser.java @@ -19,7 +19,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.appc.adapter.model; +package org.onap.ccsdk.adapter.model; import org.codehaus.jettison.json.JSONException; import org.junit.Test; diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/model/TestSaltstackAdapter.java b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/model/TestSaltstackAdapter.java index 37b6502c..0caaf332 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/appc/adapter/model/TestSaltstackAdapter.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/java/org/onap/ccsdk/adapter/model/TestSaltstackAdapter.java @@ -21,7 +21,7 @@ * ECOMP is a trademark and service mark of AT&T Intellectual Property. * ============LICENSE_END========================================================= */ -package org.onap.appc.adapter.model; +package org.onap.ccsdk.adapter.model; import org.junit.Test; import org.onap.ccsdk.sli.adaptors.saltstack.model.SaltstackMessageParser; @@ -39,7 +39,6 @@ public class TestSaltstackAdapter { private Class[] parameterTypes; private SaltstackMessageParser saltstackMessageParser; private Method m; - private String name; @Test public void callPrivateConstructorsMethodsForCodeCoverage() throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { @@ -48,21 +47,18 @@ public class TestSaltstackAdapter { Class<?>[] classesOne = {SaltstackMessageParser.class}; for(Class<?> clazz : classesOne) { Constructor<?> constructor = clazz.getDeclaredConstructor(); - name = constructor.getName(); constructor.setAccessible(true); assertNotNull(constructor.newInstance()); } Class<?>[] classesTwo = {SaltstackServerEmulator.class}; for(Class<?> clazz : classesTwo) { Constructor<?> constructor = clazz.getDeclaredConstructor(); - name = constructor.getName(); constructor.setAccessible(true); assertNotNull(constructor.newInstance()); } Class<?>[] classesThree = {SaltstackResult.class}; for(Class<?> clazz : classesThree) { Constructor<?> constructor = clazz.getDeclaredConstructor(); - name = constructor.getName(); constructor.setAccessible(true); assertNotNull(constructor.newInstance()); } 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 00000000..aff05939 --- /dev/null +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/resources/config.sls @@ -0,0 +1,2 @@ +vim: + pkg.installed diff --git a/saltstack-adapter/saltstack-adapter-provider/src/test/resources/org/onap/appc/default.properties b/saltstack-adapter/saltstack-adapter-provider/src/test/resources/org/onap/ccsdk/default.properties index 2f8fb458..2f8fb458 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/test/resources/org/onap/appc/default.properties +++ b/saltstack-adapter/saltstack-adapter-provider/src/test/resources/org/onap/ccsdk/default.properties diff --git a/saltstack-adapter/staltstack-example-server/README b/saltstack-adapter/staltstack-example-server/README deleted file mode 100644 index 687f52d3..00000000 --- a/saltstack-adapter/staltstack-example-server/README +++ /dev/null @@ -1,30 +0,0 @@ -''' -/*- -* ============LICENSE_START======================================================= -* ONAP : APPC -* ================================================================================ -* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. -* ================================================================================ -* Copyright (C) 2017 Amdocs -* ============================================================================= -* 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. -* See the License for the specific language governing permissions and -* limitations under the License. -* -* ECOMP is a trademark and service mark of AT&T Intellectual Property. -* ============LICENSE_END========================================================= -*/ -''' - -============ -INSTALLATION: -============ -TODO: Instruction to build saltstack and enable SSH for adaptor communication.
\ No newline at end of file diff --git a/saltstack-adapter/staltstack-example-server/README.md b/saltstack-adapter/staltstack-example-server/README.md new file mode 100644 index 00000000..beaf32f4 --- /dev/null +++ b/saltstack-adapter/staltstack-example-server/README.md @@ -0,0 +1,189 @@ +''' +/*- +* ============LICENSE_START======================================================= +* ONAP : APPC +* ================================================================================ +* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. +* ================================================================================ +* Copyright (C) 2017 Amdocs +* ============================================================================= +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +* +* ECOMP is a trademark and service mark of AT&T Intellectual Property. +* ============LICENSE_END========================================================= +*/ +''' + +<CREATING saltstack environment is outside the scope of this adaptor, however the requirement is as follows> +a. The SaltStack server should have it’s SSH enabled. +b. Via ssh user account we should have the access to run saltstack command (here we will see how to enable root access via ssh and connect to server via root user). +============ +INSTALLATION: Saltstack DEMO Environment creation: +============ + +1, Install VirtualBox. +2, Install Vagrant. +3, Download https://github.com/UtahDave/salt-vagrant-demo. You can use git or download a zip of the project directly from GitHub (sample Vagrant attached). +4, Extract the zip file you downloaded, and then open a command prompt to the extracted directory. +5, Run vagrant up to start the demo environment: vagrant up + After Vagrant ups (~10 minutes) and you are back at the command prompt, you are ready to continue. + More info: https://docs.saltstack.com/en/getstarted/fundamentals/ + +============ +Configuration: Sample Saltstack server execution configuration requirement. +============ +1, login to Master Saltstack server node: +"sudo vi /etc/ssh/sshd_config" and SET the following +PermitEmptyPasswords yes +PermitRootLogin yes + +SAVE and close. + +2, Run: "sudo passwd root" +and set the root password. +Then run: "sudo reboot" + +3, On the host machine, open the virtual box set a port forwarding to the master server for 2222 -> 22 +This will redirect messages to host machine to the Vagarant Master server. + +============ +TESTING: Sample Saltstack server command execution. +============ + + @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 address of SS server>"); + 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_shouldSetSuccessRealSSLNoApplyTo() 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"); + + 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; + } + }
\ No newline at end of file diff --git a/saltstack-adapter/staltstack-example-server/Vagrantfile-sample b/saltstack-adapter/staltstack-example-server/Vagrantfile-sample new file mode 100644 index 00000000..5fbcfbb7 --- /dev/null +++ b/saltstack-adapter/staltstack-example-server/Vagrantfile-sample @@ -0,0 +1,69 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +# Vagrantfile API/syntax version. Don't touch unless you know what you're doing! +VAGRANTFILE_API_VERSION = "2" + +Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| + os = "bento/ubuntu-16.04" + net_ip = "192.168.50" + + config.vm.define :master, primary: true do |master_config| + master_config.vm.provider "virtualbox" do |vb| + vb.memory = "2048" + vb.cpus = 1 + vb.name = "master" + end + master_config.vm.box = "#{os}" + master_config.vm.host_name = 'saltmaster.local' + master_config.vm.network "private_network", ip: "#{net_ip}.10" + master_config.vm.synced_folder "saltstack/salt/", "/srv/salt" + master_config.vm.synced_folder "saltstack/pillar/", "/srv/pillar" + + master_config.vm.provision :salt do |salt| + salt.master_config = "saltstack/etc/master" + salt.master_key = "saltstack/keys/master_minion.pem" + salt.master_pub = "saltstack/keys/master_minion.pub" + salt.minion_key = "saltstack/keys/master_minion.pem" + salt.minion_pub = "saltstack/keys/master_minion.pub" + salt.seed_master = { + "minion1" => "saltstack/keys/minion1.pub", + "minion2" => "saltstack/keys/minion2.pub" + } + + salt.install_type = "stable" + salt.install_master = true + salt.no_minion = true + salt.verbose = true + salt.colorize = true + salt.bootstrap_options = "-P -c /tmp" + end + end + + + [ + ["minion1", "#{net_ip}.11", "1024", os ], + ["minion2", "#{net_ip}.12", "1024", os ], + ].each do |vmname,ip,mem,os| + config.vm.define "#{vmname}" do |minion_config| + minion_config.vm.provider "virtualbox" do |vb| + vb.memory = "#{mem}" + vb.cpus = 1 + vb.name = "#{vmname}" + end + minion_config.vm.box = "#{os}" + minion_config.vm.hostname = "#{vmname}" + minion_config.vm.network "private_network", ip: "#{ip}" + + minion_config.vm.provision :salt do |salt| + salt.minion_config = "saltstack/etc/#{vmname}" + salt.minion_key = "saltstack/keys/#{vmname}.pem" + salt.minion_pub = "saltstack/keys/#{vmname}.pub" + salt.install_type = "stable" + salt.verbose = true + salt.colorize = true + salt.bootstrap_options = "-P -c /tmp" + end + end + end + end
\ No newline at end of file diff --git a/saltstack-adapter/staltstack-example-server/saltstack_sample_sls-2.yml b/saltstack-adapter/staltstack-example-server/saltstack_sample_sls-2.yml new file mode 100644 index 00000000..468d2a24 --- /dev/null +++ b/saltstack-adapter/staltstack-example-server/saltstack_sample_sls-2.yml @@ -0,0 +1,34 @@ +# /*- +# * ============LICENSE_START======================================================= +# * ONAP : APPC +# * ================================================================================ +# * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. +# * ================================================================================ +# * Copyright (C) 2017 Amdocs +# * ============================================================================= +# * 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. +# * See the License for the specific language governing permissions and +# * limitations under the License. +# * +# * ECOMP is a trademark and service mark of AT&T Intellectual Property. +# * ============LICENSE_END========================================================= +# */ + +my-vim: + git.latest: + - name: https://github.com/nbari/my-vim + - target: /usr/local/share/my-vim + - rev: master + - submodules: True + cmd.wait: + - name: 'cd /usr/local/share/my-vim; git submodule init; git submodule foreach git pull origin master; git submodule update' + - watch: + - git: my-vim
\ No newline at end of file diff --git a/saltstack-adapter/staltstack-example-server/saltstact_sample_sls.yml b/saltstack-adapter/staltstack-example-server/saltstact_sample_sls.yml new file mode 100644 index 00000000..bcc7fda9 --- /dev/null +++ b/saltstack-adapter/staltstack-example-server/saltstact_sample_sls.yml @@ -0,0 +1,26 @@ +# /*- +# * ============LICENSE_START======================================================= +# * ONAP : APPC +# * ================================================================================ +# * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. +# * ================================================================================ +# * Copyright (C) 2017 Amdocs +# * ============================================================================= +# * 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. +# * See the License for the specific language governing permissions and +# * limitations under the License. +# * +# * ECOMP is a trademark and service mark of AT&T Intellectual Property. +# * ============LICENSE_END========================================================= +# */ + +vim: + pkg.installed |