From dae4b37c7f067becbae331ef8bff60235c53a61c Mon Sep 17 00:00:00 2001 From: kurczews Date: Tue, 16 Jan 2018 11:49:07 +0100 Subject: Fix some sonar issues Fix sonar issues assigned to me Change-Id: I926c011fd9d2c4f5628c4151e8c50a8fb5f3ea86 Issue-ID: APPC-412 Signed-off-by: kurczews --- .../ansible/model/AnsibleMessageParser.java | 30 ++++++++++++++-------- .../appc/adapter/ansible/model/AnsibleResult.java | 11 +++++--- .../provider/operation/impl/EvacuateServer.java | 4 +-- 3 files changed, 29 insertions(+), 16 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 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 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) { diff --git a/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/main/java/org/onap/appc/adapter/iaas/provider/operation/impl/EvacuateServer.java b/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/main/java/org/onap/appc/adapter/iaas/provider/operation/impl/EvacuateServer.java index 3778dded8..73e468846 100644 --- a/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/main/java/org/onap/appc/adapter/iaas/provider/operation/impl/EvacuateServer.java +++ b/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/main/java/org/onap/appc/adapter/iaas/provider/operation/impl/EvacuateServer.java @@ -225,7 +225,7 @@ public class EvacuateServer extends ProviderServerOperation { // If a snapshot exists, do a rebuild to apply the latest snapshot to the evacuated server. // This is the default behavior unless the optional parameter is set to FALSE. - if (rebuildVm == null || !rebuildVm.equalsIgnoreCase("false")) { + if (rebuildVm == null || !"false".equalsIgnoreCase(rebuildVm)) { List snapshots = server.getSnapshots(); if (snapshots == null || snapshots.isEmpty()) { logger.debug("No snapshots available - skipping rebuild after evacuate"); @@ -352,4 +352,4 @@ public class EvacuateServer extends ProviderServerOperation { public void setProvideAdapterRef(ProviderAdapterImpl pai) { paImpl = pai; } -} +} \ No newline at end of file -- cgit 1.2.3-korg