Fix some sonar issues 05/28305/4
authorkurczews <krzysztof.kurczewski@nokia.com>
Tue, 16 Jan 2018 10:49:07 +0000 (11:49 +0100)
committerPatrick Brady <pb071s@att.com>
Wed, 17 Jan 2018 20:16:49 +0000 (20:16 +0000)
Fix sonar issues assigned to me

Change-Id: I926c011fd9d2c4f5628c4151e8c50a8fb5f3ea86
Issue-ID: APPC-412
Signed-off-by: kurczews <krzysztof.kurczewski@nokia.com>
appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleMessageParser.java
appc-adapters/appc-ansible-adapter/appc-ansible-adapter-bundle/src/main/java/org/onap/appc/adapter/ansible/model/AnsibleResult.java
appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/main/java/org/onap/appc/adapter/iaas/provider/operation/impl/EvacuateServer.java

index 6ed5732..c119668 100644 (file)
@@ -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(
index 066a3da..87fe981 100644 (file)
 
 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) {
index 3778dde..73e4688 100644 (file)
@@ -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<Image> 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