diff options
Diffstat (limited to 'appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap')
2 files changed, 27 insertions, 14 deletions
diff --git a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleMessageParser.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleMessageParser.java index 6ed57320c..c11966860 100644 --- a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleMessageParser.java +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleMessageParser.java @@ -39,6 +39,8 @@ import org.json.JSONException; import org.json.JSONObject; import org.onap.appc.exceptions.APPCException; import com.google.common.base.Strings; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Class that validates and constructs requests sent/received from @@ -63,6 +65,8 @@ public class AnsibleMessageParser { private static final String VERSION_OPT_KEY = "Version"; private static final String ACTION_OPT_KEY = "Action"; + private static final Logger LOGGER = LoggerFactory.getLogger(AnsibleMessageParser.class); + /** * Accepts a map of strings and * a) validates if all parameters are appropriate (else, throws an exception) and @@ -192,30 +196,29 @@ public class AnsibleMessageParser { ansibleResult.setStatusCode(codeStatus); ansibleResult.setStatusMessage(messageStatus); - System.out.println( - "Received response with code = " + Integer.toString(codeStatus) + " Message = " + messageStatus); + LOGGER.info("Received response with code = {}, Message = {}", codeStatus, messageStatus); if (!postResponse.isNull("Results")) { // Results are available. process them // Results is a dictionary of the form // {host :{status:s, group:g, message:m, hostname:h}, ...} - System.out.println("Processing results in response"); + LOGGER.info("Processing results in response"); JSONObject results = postResponse.getJSONObject("Results"); - System.out.println("Get JSON dictionary from Results .."); + LOGGER.info("Get JSON dictionary from Results .."); Iterator<String> hosts = results.keys(); - System.out.println("Iterating through hosts"); + LOGGER.info("Iterating through hosts"); while (hosts.hasNext()) { String host = hosts.next(); - System.out.println("Processing host = " + host); + 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); - System.out.println("Code = " + Integer.toString(subCode) + " Message = " + message); + LOGGER.info("Code = {}, Message = {}", subCode, message); if (subCode != 200 || !message.equals("SUCCESS")) { finalCode = AnsibleResultCodes.REQ_FAILURE.getValue(); @@ -291,10 +294,7 @@ public class AnsibleMessageParser { break; case FILE_PARAMETERS_OPT_KEY: - // Files may have strings with newlines. Escape them as appropriate - String formattedPayload = payload.replace("\n", "\\n").replace("\r", "\\r"); - JSONObject fileParams = new JSONObject(formattedPayload); - jsonPayload.put(key, fileParams); + jsonPayload.put(key, getFilePayload(payload)); break; default: @@ -302,6 +302,14 @@ public class AnsibleMessageParser { } } + /** + * 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 APPCException { if (!params.containsKey(key)) { throw new APPCException(String.format( diff --git a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleResult.java b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleResult.java index 066a3da27..87fe98159 100644 --- a/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleResult.java +++ b/appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleResult.java @@ -24,18 +24,23 @@ package org.onap.appc.adapter.ansible.model; -/* Simple class to store code and message returned by POST/GET to an Ansible Server */ +/** + * Simple class to store code and message returned by POST/GET to an Ansible Server + */ public class AnsibleResult { + + private static final String EMPTY_VALUE = "UNKNOWN"; + private int statusCode; private String statusMessage; private String results; public AnsibleResult() { - this(-1, "UNKNOWN", "UNKNOWN"); + this(-1, EMPTY_VALUE, EMPTY_VALUE); } public AnsibleResult(int code, String message) { - this(code, message, "UNKNOWN"); + this(code, message, EMPTY_VALUE); } public AnsibleResult(int code, String message, String result) { |