diff options
author | Ganesh Chandrasekaran <ganesh.c@samsung.com> | 2018-07-17 17:27:24 +0900 |
---|---|---|
committer | Ganesh Chandrasekaran <ganesh.c@samsung.com> | 2018-07-17 17:27:36 +0900 |
commit | 43a3f54d5ac9ab15025910a575df9fd4be6237d8 (patch) | |
tree | 7cad10c6b8f2910ec3f18696ef4272dd0e9b1c64 /saltstack-adapter/saltstack-adapter-provider/src/main | |
parent | b08a1b8cf2440fac7fc3d7eb7580a58d977524a2 (diff) |
saltstack adaptor reqExecLog API cleanup
Issue-ID: CCSDK-331
Change-Id: I951efe3df1ad838e065ba6fd62dda53d533e6422
Signed-off-by: Ganesh Chandrasekaran <ganesh.c@samsung.com>
Diffstat (limited to 'saltstack-adapter/saltstack-adapter-provider/src/main')
6 files changed, 20 insertions, 307 deletions
diff --git a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/SaltstackAdapter.java b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/SaltstackAdapter.java index 1cbb495cf..cdfe4ff7b 100644 --- a/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/SaltstackAdapter.java +++ b/saltstack-adapter/saltstack-adapter-provider/src/main/java/org/onap/ccsdk/sli/adaptors/saltstack/SaltstackAdapter.java @@ -56,16 +56,4 @@ public interface SaltstackAdapter extends SvcLogicJavaPlugin { * The response from Saltstack comes in json format and it is automatically put * to context for DGs access, with a certain prefix*/ void reqExecSLSFile(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException; - - /* Method to get log of a saltState execution request - * The response from Saltstack comes in json format and it is automatically put - * to context for DGs access, with a certain prefix*/ - void reqExecLog(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException; - - /** - * Set the command execution timeout - * - * @param timeout time in milliseconds - */ - void setExecTimeout(long timeout); } 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 3469103b5..bd811fffb 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 @@ -47,7 +47,6 @@ import java.io.StringWriter; * - return default sshclient (which only trusts known CAs from default cacerts file for process) this is the default * option **/ -//TODO: This class is to be altered completely based on the SALTSTACK server communication. public class ConnectionBuilder { private static final EELFLogger logger = EELFManager.getInstance().getLogger(ConnectionBuilder.class); @@ -186,65 +185,4 @@ public class ConnectionBuilder { } return result; } - - /** - * 1. Connect to SSH server. - * 2. Exec remote command over SSH. Return command execution status. - * Command output is written to out or err stream. - * - * @param commands list of commands to execute - * @param payloadSLS has the SLS file location that is to be sent to server - * @param retryDelay delay between retry to make a SSH connection. - * @param retryCount number of count retry to make a SSH connection. - * @return command execution status - */ - public SaltstackResult connectNExecuteSLS(String commands, String payloadSLS, int retryDelay, int retryCount) { - - SaltstackResult result = new SaltstackResult(); - try { - //TODO: to implement SSH connected client to Saltstack Server - } catch (Exception io) { - logger.error("Caught Exception", io); - result.setStatusCode(SaltstackResultCodes.IO_EXCEPTION.getValue()); - result.setStatusMessage(io.getMessage()); - } - return result; - } - - /** - * Disconnect from SSH server. - */ - public SaltstackResult disConnect() { - - SaltstackResult result = new SaltstackResult(); - try { - //TODO: to implement SSH connected client to Saltstack Server - } catch (Exception io) { - logger.error("Caught Exception", io); - result.setStatusCode(SaltstackResultCodes.IO_EXCEPTION.getValue()); - result.setStatusMessage(io.getMessage()); - } - return result; - } - - /** - * Exec remote command over SSH. Return command execution status. - * Command output is written to out or err stream. - * - * @param cmd command to execute - * @return command execution status - */ - public SaltstackResult connectNExecuteLog(String cmd) { - - SaltstackResult result = new SaltstackResult(); - - try { - //TODO: to implement SSH command execute - } catch (Exception io) { - result.setStatusCode(SaltstackResultCodes.IO_EXCEPTION.getValue()); - result.setStatusMessage(io.getMessage()); - logger.error("Caught IOException", io); - } - 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 77874b0e5..acd3db20d 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 @@ -50,11 +50,6 @@ import java.util.Properties; public class SaltstackAdapterImpl implements SaltstackAdapter { /** - * The constant used to define the service name in the mapped diagnostic context - */ - @SuppressWarnings("nls") - public static final String MDC_SERVICE = "service"; - /** * The constant for the status code for a failed outcome */ @SuppressWarnings("nls") @@ -66,17 +61,13 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { public static final String OUTCOME_SUCCESS = "success"; public static final String CONNECTION_RETRY_DELAY = "retryDelay"; public static final String CONNECTION_RETRY_COUNT = "retryCount"; - private static final long EXEC_TIMEOUT = 120000; /** * Adapter Name */ private static final String ADAPTER_NAME = "Saltstack Adapter"; - private static final String APPC_EXCEPTION_CAUGHT = "APPCException caught"; private static final String RESULT_CODE_ATTRIBUTE_NAME = "org.onap.appc.adapter.saltstack.result.code"; private static final String MESSAGE_ATTRIBUTE_NAME = "org.onap.appc.adapter.saltstack.message"; - private static final String RESULTS_ATTRIBUTE_NAME = "org.onap.appc.adapter.saltstack.results"; private static final String ID_ATTRIBUTE_NAME = "org.onap.appc.adapter.saltstack.Id"; - private static final String LOG_ATTRIBUTE_NAME = "org.onap.appc.adapter.saltstack.log"; private static final String CLIENT_TYPE_PROPERTY_NAME = "org.onap.appc.adapter.saltstack.clientType"; 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"; @@ -87,7 +78,6 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { * The logger to be used */ private static final EELFLogger logger = EELFManager.getInstance().getLogger(SaltstackAdapterImpl.class); - private long timeout = EXEC_TIMEOUT; /** * Connection object **/ @@ -139,11 +129,6 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { return ADAPTER_NAME; } - @Override - public void setExecTimeout(long timeout) { - this.timeout = timeout; - } - /** * Method posts info to Context memory in case of an error and throws a * SvcLogicException causing SLI to register this as a failure @@ -293,7 +278,7 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { } else { logger.info(String.format("Execution of request : successful.")); ctx.setAttribute(RESULT_CODE_ATTRIBUTE_NAME, Integer.toString(testResult.getStatusCode())); - ctx.setAttribute(MESSAGE_ATTRIBUTE_NAME, "success"); + ctx.setAttribute(MESSAGE_ATTRIBUTE_NAME, OUTCOME_SUCCESS); ctx.setAttribute(ID_ATTRIBUTE_NAME, reqID); } } @@ -379,19 +364,6 @@ public class SaltstackAdapterImpl implements SaltstackAdapter { } } - /** - * Public method to get logs from saltState execution for a specific request Posts the following back - * to Svc context memory - * <p> - * It blocks till the Saltstack Server responds or the session times out very similar to - * reqExecResult logs are returned in the DG context variable org.onap.appc.adapter.saltstack.log - */ - @Override - public void reqExecLog(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException { - //TODO: to implement - - } - public SaltstackResult execCommand(SvcLogicContext ctx, Map<String, String> params, String commandToExecute, long execTimeout) throws SvcLogicException{ 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 fd66eb100..d8616920b 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,7 +50,6 @@ 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; 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 16ab8dca4..f7c513c92 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 @@ -30,9 +30,7 @@ package org.onap.ccsdk.sli.adaptors.saltstack.model; */ import com.google.common.base.Strings; -import org.json.JSONArray; import org.codehaus.jettison.json.JSONException; -import org.json.JSONObject; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.slf4j.Logger; @@ -43,25 +41,16 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; -import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; import java.util.Map; import java.util.Properties; -import java.util.Set; import java.util.UUID; /** * Class that validates and constructs requests sent/received from * Saltstack Server */ -//TODO: This class is to be altered completely based on the SALTSTACK server communicaiton. public class SaltstackMessageParser { - private static final String STATUS_MESSAGE_KEY = "StatusMessage"; - private static final String STATUS_CODE_KEY = "StatusCode"; - - private static final String SALTSTATE_NAME_KEY = "SaltStateName"; private static final String SS_AGENT_HOSTNAME_KEY = "HostName"; private static final String SS_AGENT_PORT_KEY = "Port"; private static final String PASS_KEY = "Password"; @@ -74,48 +63,9 @@ public class SaltstackMessageParser { 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"; - private static final String ENV_PARAMETERS_OPT_KEY = "EnvParameters"; - private static final String NODE_LIST_OPT_KEY = "NodeList"; - private static final String TIMEOUT_OPT_KEY = "Timeout"; - private static final String VERSION_OPT_KEY = "Version"; - private static final String ACTION_OPT_KEY = "Action"; - private static final Logger LOGGER = LoggerFactory.getLogger(SaltstackMessageParser.class); /** - * Accepts a map of strings and - * a) validates if all parameters are appropriate (else, throws an exception) and - * b) if correct returns a JSON object with appropriate key-value pairs to send to the server. - * <p> - * Mandatory parameters, that must be in the supplied information to the Saltstack Adapter - * 1. URL to connect to - * 2. credentials for URL (assume username password for now) - * 3. SaltState name - */ - public JSONObject reqMessage(Map<String, String> params) throws SvcLogicException { - final String[] mandatoryTestParams = {SS_AGENT_HOSTNAME_KEY, SALTSTATE_NAME_KEY, USER_KEY, PASS_KEY}; - final String[] optionalTestParams = {ENV_PARAMETERS_OPT_KEY, NODE_LIST_OPT_KEY, LOCAL_PARAMETERS_OPT_KEY, - TIMEOUT_OPT_KEY, VERSION_OPT_KEY, FILE_PARAMETERS_OPT_KEY, ACTION_OPT_KEY}; - - JSONObject jsonPayload = new JSONObject(); - - for (String key : mandatoryTestParams) { - throwIfMissingMandatoryParam(params, key); - jsonPayload.put(key, params.get(key)); - } - - parseOptionalParams(params, optionalTestParams, jsonPayload); - - // Generate a unique uuid for the test - String reqId = UUID.randomUUID().toString(); - jsonPayload.put(SS_AGENT_HOSTNAME_KEY, reqId); - - return jsonPayload; - } - - /** * Method that validates that the Map has enough information * to query Saltstack server for a result. If so, it returns * the appropriate PORT number. @@ -208,18 +158,23 @@ public class SaltstackMessageParser { } String slsName = params.get(SaltstackMessageParser.SLS_NAME); try { - if(slsName.substring(slsName.lastIndexOf("."), slsName.length()).equalsIgnoreCase(".sls")) + if (slsName.substring(slsName.lastIndexOf("."), slsName.length()).equalsIgnoreCase(".sls")) { return stripExtension(slsName); + } } catch (StringIndexOutOfBoundsException e) { return slsName; } return slsName; } - private String stripExtension (String str) { - if (str == null) return null; + private String stripExtension(String str) { + if (str == null) { + return null; + } int pos = str.lastIndexOf("."); - if (pos == -1) return str; + if (pos == -1) { + return str; + } return str.substring(0, pos); } @@ -306,7 +261,7 @@ public class SaltstackMessageParser { * and returns an SaltstackResult object. */ public SaltstackResult parseResponse(SvcLogicContext ctx, String pfx, - SaltstackResult saltstackResult, boolean slsExec) throws IOException{ + SaltstackResult saltstackResult, boolean slsExec) throws IOException { int code = saltstackResult.getStatusCode(); InputStream in = null; boolean executionStatus = true, retCodeFound = false; @@ -343,23 +298,26 @@ public class SaltstackMessageParser { return new SaltstackResult(SaltstackResultCodes.INVALID_RESPONSE_FILE.getValue(), "error parsing response file " + saltstackResult.getOutputFileName() + " : " + e.getMessage()); } finally { - if( in != null ) + if (in != null) { in.close(); + } } if (slsExec) { - if (!retCodeFound) + if (!retCodeFound) { return new SaltstackResult(SaltstackResultCodes.COMMAND_EXEC_FAILED_STATUS.getValue(), "error in executing configuration at the server"); - if (!executionStatus) + } + if (!executionStatus) { return new SaltstackResult(SaltstackResultCodes.COMMAND_EXEC_FAILED_STATUS.getValue(), "error in executing configuration at the server"); + } } saltstackResult.setStatusCode(SaltstackResultCodes.FINAL_SUCCESS.getValue()); return saltstackResult; } public SaltstackResult putToProperties(SvcLogicContext ctx, String pfx, - SaltstackResult saltstackResult) throws IOException{ + SaltstackResult saltstackResult) throws IOException { InputStream in = null; try { File file = new File(saltstackResult.getOutputFileName()); @@ -379,153 +337,14 @@ public class SaltstackMessageParser { saltstackResult = new SaltstackResult(SaltstackResultCodes.INVALID_RESPONSE_FILE.getValue(), "Error parsing response file = " + saltstackResult.getOutputFileName() + ". Error = " + e.getMessage()); } finally { - if( in != null ) + if (in != null) { in.close(); - } - saltstackResult.setStatusCode(SaltstackResultCodes.FINAL_SUCCESS.getValue()); - return saltstackResult; - } - - /** - * This method parses response from an Saltstack server when we do a GET for a result - * and returns an SaltstackResult object. - **/ - public SaltstackResult parseGetResponse(String input) throws SvcLogicException { - - SaltstackResult saltstackResult = new SaltstackResult(); - - try { - JSONObject postResponse = new JSONObject(input); - saltstackResult = parseGetResponseNested(saltstackResult, postResponse); - } catch (Exception e) { - saltstackResult = new SaltstackResult(SaltstackResultCodes.INVALID_COMMAND.getValue(), - "Error parsing response = " + input + ". Error = " + e.getMessage(), "", -1); - } - return saltstackResult; - } - - private SaltstackResult parseGetResponseNested(SaltstackResult saltstackResult, JSONObject postRsp) throws SvcLogicException { - - int codeStatus = postRsp.getInt(STATUS_CODE_KEY); - String messageStatus = postRsp.getString(STATUS_MESSAGE_KEY); - int finalCode = SaltstackResultCodes.FINAL_SUCCESS.getValue(); - - boolean valCode = - SaltstackResultCodes.CODE.checkValidCode(SaltstackResultCodes.FINALRESPONSE.getValue(), codeStatus); - - if (!valCode) { - throw new SvcLogicException("Invalid FinalResponse code = " + codeStatus + " received. MUST be one of " - + SaltstackResultCodes.CODE.getValidCodes(SaltstackResultCodes.FINALRESPONSE.getValue())); - } - - saltstackResult.setStatusCode(codeStatus); - saltstackResult.setStatusMessage(messageStatus); - LOGGER.info("Received response with code = {}, Message = {}", codeStatus, messageStatus); - - if (!postRsp.isNull("Results")) { - - // Results are available. process them - // Results is a dictionary of the form - // {host :{status:s, group:g, message:m, hostname:h}, ...} - LOGGER.info("Processing results in response"); - JSONObject results = postRsp.getJSONObject("Results"); - LOGGER.info("Get JSON dictionary from Results .."); - Iterator<String> hosts = results.keys(); - LOGGER.info("Iterating through hosts"); - - while (hosts.hasNext()) { - String host = hosts.next(); - LOGGER.info("Processing host = {}", host); - - try { - JSONObject hostResponse = results.getJSONObject(host); - int subCode = hostResponse.getInt(STATUS_CODE_KEY); - String message = hostResponse.getString(STATUS_MESSAGE_KEY); - - LOGGER.info("Code = {}, Message = {}", subCode, message); - - if (subCode != 200 || !message.equals("SUCCESS")) { - finalCode = SaltstackResultCodes.REQ_FAILURE.getValue(); - } - } catch (Exception e) { - saltstackResult.setStatusCode(SaltstackResultCodes.INVALID_RESPONSE.getValue()); - saltstackResult.setStatusMessage(String.format( - "Error processing response message = %s from host %s", results.getString(host), host)); - break; - } } - - saltstackResult.setStatusCode(finalCode); - - // We return entire Results object as message - saltstackResult.setResults(results.toString()); - - } else { - saltstackResult.setStatusCode(SaltstackResultCodes.INVALID_RESPONSE.getValue()); - saltstackResult.setStatusMessage("Results not found in GET for response"); } + saltstackResult.setStatusCode(SaltstackResultCodes.FINAL_SUCCESS.getValue()); return saltstackResult; } - private void parseOptionalParams(Map<String, String> params, String[] optionalTestParams, JSONObject jsonPayload) { - - Set<String> optionalParamsSet = new HashSet<>(); - Collections.addAll(optionalParamsSet, optionalTestParams); - - //@formatter:off - params.entrySet() - .stream() - .filter(entry -> optionalParamsSet.contains(entry.getKey())) - .filter(entry -> !Strings.isNullOrEmpty(entry.getValue())) - .forEach(entry -> parseOptionalParam(entry, jsonPayload)); - //@formatter:on - } - - private void parseOptionalParam(Map.Entry<String, String> params, JSONObject jsonPayload) { - String key = params.getKey(); - String payload = params.getValue(); - - switch (key) { - case TIMEOUT_OPT_KEY: - int timeout = Integer.parseInt(payload); - if (timeout < 0) { - throw new NumberFormatException(" : specified negative integer for timeout = " + payload); - } - jsonPayload.put(key, payload); - break; - - case VERSION_OPT_KEY: - jsonPayload.put(key, payload); - break; - - case LOCAL_PARAMETERS_OPT_KEY: - case ENV_PARAMETERS_OPT_KEY: - JSONObject paramsJson = new JSONObject(payload); - jsonPayload.put(key, paramsJson); - break; - - case NODE_LIST_OPT_KEY: - JSONArray paramsArray = new JSONArray(payload); - jsonPayload.put(key, paramsArray); - break; - - case FILE_PARAMETERS_OPT_KEY: - jsonPayload.put(key, getFilePayload(payload)); - break; - - default: - break; - } - } - - /** - * Return payload with escaped newlines - */ - private JSONObject getFilePayload(String payload) { - String formattedPayload = payload.replace("\n", "\\n").replace("\r", "\\r"); - return new JSONObject(formattedPayload); - } - private void throwIfMissingMandatoryParam(Map<String, String> params, String key) throws SvcLogicException { if (!params.containsKey(key)) { throw new SvcLogicException(String.format( 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 adbf9bd9a..1b62e4bdb 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 @@ -44,10 +44,7 @@ import java.util.regex.Pattern; public class SaltstackServerEmulator { private static final String SALTSTATE_FILE_NAME = "fileName"; - private static final String STATUS_CODE = "StatusCode"; - private static final String STATUS_MESSAGE = "StatusMessage"; private final EELFLogger logger = EELFManager.getInstance().getLogger(SaltstackServerEmulator.class); - private String saltStateName = "test_saltState.yaml"; /** * Method that emulates the response from an Saltstack Server |