Fixing sonar issues. 93/12893/2
authorBharat saraswal <bharat.saraswal@huawei.com>
Sun, 17 Sep 2017 07:46:44 +0000 (13:16 +0530)
committerBharat saraswal <bharat.saraswal@huawei.com>
Sun, 17 Sep 2017 09:40:34 +0000 (09:40 +0000)
removed redundant code.

Issue-Id:DCAEGEN2-92

Change-Id: I7c006e51332713440e637dbac39f47075e803ea9
Signed-off-by: Bharat saraswal <bharat.saraswal@huawei.com>
src/main/java/org/onap/dcae/commonFunction/CustomExceptionLoader.java
src/main/java/org/onap/dcae/commonFunction/DmaapPropertyReader.java

index 175ad44..0ed3971 100644 (file)
 
 package org.onap.dcae.commonFunction;
 
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.IOException;
-import java.util.HashMap;
-
-import java.util.Map.Entry;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonIOException;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
 import com.google.gson.JsonSyntaxException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map.Entry;
 
 
 public class CustomExceptionLoader {
 
-    public static HashMap<String, JsonArray> map = null;
-    private static final Logger log = LoggerFactory.getLogger ( CustomExceptionLoader.class );
-    
+    public static HashMap<String, JsonArray> map;
+    private static final Logger log = LoggerFactory.getLogger(CustomExceptionLoader.class);
+
     //For standalone test
     //LoadMap Invoked from servletSetup
     /*
-       public static void main(String[] args) {
+    public static void main(String[] args) {
 
                System.out.println("CustomExceptionLoader.main --> Arguments -- ExceptionConfig file: " + args[0] + "StatusCode:" + args[1]+ " Error Msg:" + args[2]);
                CommonStartup.exceptionConfig = args[0];
@@ -62,71 +60,73 @@ public class CustomExceptionLoader {
            
        }
        */
-       
-       public static void LoadMap () {
-               
-                map = new HashMap<String, JsonArray>();
-                FileReader fr = null;
-                try {
-                               JsonElement root = null;
-                               fr = new FileReader(CommonStartup.exceptionConfig);
-                               root = new JsonParser().parse(fr);
-                               JsonObject jsonObject = root.getAsJsonObject().get("code").getAsJsonObject();
-
-                               for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
-                                    map.put(entry.getKey(), (JsonArray) entry.getValue());
-                               }
-                               
-                               log.debug("CustomExceptionLoader.LoadMap --> Map loaded - " + map);
-                       } catch (JsonIOException e) {
-                               e.printStackTrace();
-                       } catch (JsonSyntaxException e) {
-                               e.printStackTrace();
-                       } catch (FileNotFoundException e) {
-                               e.printStackTrace();
-                       } catch (Exception e) {
-                               e.printStackTrace();
-                       }
-                       finally {
-                       if (fr != null) {
-                               try {
-                                               fr.close();
-                                       } catch (IOException e) {
-                                               log.error("Error closing file reader stream : " +e.toString());
-                                       }
-                       }
-                   }
-       }
 
-       public static String[] LookupMap (String error, String errormsg) {
-                
-                String[] retarray = null;
-                
-                log.debug("CustomExceptionLoader.LookupMap -->" + " HTTP StatusCode:" + error + " Msg:" + errormsg);
-                try{
-                        
-                        JsonArray jarray = map.get(error);
-                         for (int i = 0; i < jarray.size(); i++) {
-                                 
-                            JsonElement val = jarray.get(i).getAsJsonObject().get("Reason");
-                            JsonArray ec = (JsonArray) jarray.get(i).getAsJsonObject().get("ErrorCode");
-                                log.trace("CustomExceptionLoader.LookupMap Parameter -> Error msg : " + errormsg + " Reason text being matched:" + val);                       
-                                if (errormsg.contains(val.toString().replace("\"", ""))){
-                                        log.trace("CustomExceptionLoader.LookupMap Successful! Exception matched to error message StatusCode:" + ec.get(0).toString() + "ErrorMessage:" + ec.get(1).toString());
-                                        retarray = new String[2];
-                                        retarray[0]=ec.get(0).toString();
-                                        retarray[1]=ec.get(1).toString();
-                                        return retarray;
-                                }
-                           }
-  
-                }
-                catch (Exception e)
-                {
-                        System.out.println(e.getMessage());
-                }
-               
-                return retarray;
-       }
+    public static void LoadMap() {
+
+        map = new HashMap<String, JsonArray>();
+        FileReader fr = null;
+        try {
+            JsonElement root = null;
+            fr = new FileReader(CommonStartup.exceptionConfig);
+            root = new JsonParser().parse(fr);
+            JsonObject jsonObject = root.getAsJsonObject().get("code").getAsJsonObject();
+
+            for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
+                map.put(entry.getKey(), (JsonArray) entry.getValue());
+            }
+
+            log.debug("CustomExceptionLoader.LoadMap --> Map loaded - " + map);
+            fr.close();
+        } catch (JsonIOException e) {
+            log.error("json IO exception occured " + e.getLocalizedMessage(), e);
+        } catch (JsonSyntaxException e) {
+            log.error("JsonSyntaxException occured " + e.getLocalizedMessage(), e);
+        } catch (FileNotFoundException e) {
+            log.error("FileNotFoundException occured " + e.getLocalizedMessage(), e);
+        } catch (Exception e) {
+            log.error("exception occured " + e.getLocalizedMessage(), e);
+        } finally {
+            if (fr != null) {
+                try {
+                    fr.close();
+                } catch (IOException e) {
+                    log.error("Error closing file reader stream : " + e.getLocalizedMessage(), e);
+                }
+            }
+        }
+    }
+
+    public static String[] LookupMap(String error, String errorMsg) {
+
+        String[] retarray = null;
+
+        log.debug("CustomExceptionLoader.LookupMap -->" + " HTTP StatusCode:" + error + " Msg:"
+            + errorMsg);
+        try {
+
+            JsonArray jarray = map.get(error);
+            for (int i = 0; i < jarray.size(); i++) {
+
+                JsonElement val = jarray.get(i).getAsJsonObject().get("Reason");
+                JsonArray ec = (JsonArray) jarray.get(i).getAsJsonObject().get("ErrorCode");
+                log.trace("CustomExceptionLoader.LookupMap Parameter -> Error msg : " + errorMsg
+                    + " Reason text being matched:" + val);
+                if (errorMsg.contains(val.toString().replace("\"", ""))) {
+                    log.trace(
+                        "CustomExceptionLoader.LookupMap Successful! Exception matched to error message StatusCode:"
+                            + ec.get(0) + "ErrorMessage:" + ec.get(1));
+                    retarray = new String[2];
+                    retarray[0] = ec.get(0).toString();
+                    retarray[1] = ec.get(1).toString();
+                    return retarray;
+                }
+            }
+
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+        }
+
+        return retarray;
+    }
 
 }
index 62383f5..ff2a3ba 100644 (file)
 
 package org.onap.dcae.commonFunction;
 
-import java.io.FileNotFoundException;
-
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonIOException;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import com.google.gson.JsonSyntaxException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+
 import java.io.FileReader;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
 
-import com.google.gson.JsonArray;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonIOException;
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParser;
-import com.google.gson.JsonSyntaxException;
-
 public class DmaapPropertyReader {
 
-       private static DmaapPropertyReader instance = null;
-
-       private static final Logger log = LoggerFactory.getLogger(DmaapPropertyReader.class);
-
-       public HashMap<String, String> dmaap_hash = new HashMap<String, String>();
-
-       private DmaapPropertyReader(String CambriaConfigFile) {
-
-               FileReader fr = null;
-               try {
-                       JsonElement root = null;
-                       fr = new FileReader(CambriaConfigFile);
-                       root = new JsonParser().parse(fr);
-
-                       //Check if dmaap config is handled by legacy controller/service manager
-                       if (root.getAsJsonObject().has("channels")) {
-                               JsonArray jsonObject = (JsonArray) root.getAsJsonObject().get("channels");
-
-                               for (int i = 0; i < jsonObject.size(); i++) {
-                                       log.debug("TOPIC:" + jsonObject.get(i).getAsJsonObject().get("cambria.topic") + " HOST-URL:"
-                                                       + jsonObject.get(i).getAsJsonObject().get("cambria.url") + " HOSTS:"
-                                                       + jsonObject.get(i).getAsJsonObject().get("cambria.hosts") + " PWD:"
-                                                       + jsonObject.get(i).getAsJsonObject().get("basicAuthPassword") + " USER:"
-                                                       + jsonObject.get(i).getAsJsonObject().get("basicAuthUsername") + " NAME:"
-                                                       + jsonObject.get(i).getAsJsonObject().get("name"));
-
-                                       String convertedname = jsonObject.get(i).getAsJsonObject().get("name").toString().replace("\"", "");
-                                       dmaap_hash.put(convertedname + ".cambria.topic",
-                                                       jsonObject.get(i).getAsJsonObject().get("cambria.topic").toString().replace("\"", ""));
-
-                                       if (jsonObject.get(i).getAsJsonObject().get("cambria.hosts") != null) {
-                                               dmaap_hash.put(convertedname + ".cambria.hosts",
-                                                               jsonObject.get(i).getAsJsonObject().get("cambria.hosts").toString().replace("\"", ""));
-                                       }
-                                       if (jsonObject.get(i).getAsJsonObject().get("cambria.url") != null) {
-                                               dmaap_hash.put(convertedname + ".cambria.url",
-                                                               jsonObject.get(i).getAsJsonObject().get("cambria.url").toString().replace("\"", ""));
-                                       }
-                                       if (jsonObject.get(i).getAsJsonObject().get("basicAuthPassword") != null) {
-                                               dmaap_hash.put(convertedname + ".basicAuthPassword", jsonObject.get(i).getAsJsonObject()
-                                                               .get("basicAuthPassword").toString().replace("\"", ""));
-                                       }
-                                       if (jsonObject.get(i).getAsJsonObject().get("basicAuthUsername") != null) {
-                                               dmaap_hash.put(convertedname + ".basicAuthUsername", jsonObject.get(i).getAsJsonObject()
-                                                               .get("basicAuthUsername").toString().replace("\"", ""));
-                                       }
-
-                               }
-                       } else {
-
-                               //Handing new format from  controllergen2/config_binding_service
-                               JsonObject jsonObject = root.getAsJsonObject();
-                               Set<Map.Entry<String, JsonElement>> entries = jsonObject.entrySet();
-
-                               for (Map.Entry<String, JsonElement> entry : entries) {
-                                       
-                                       JsonElement topicurl = entry.getValue().getAsJsonObject().get("dmaap_info").getAsJsonObject().get("topic_url");
-                                       String[] urlParts = dmaapUrlSplit(topicurl.toString().replace("\"", ""));
-
-                                       String mrTopic = null;
-                                       String mrUrl = null;
-                                       String[] hostport = null;
-                                       String username = null;
-                                       String userpwd = null;
-
-                                       try {
-
-                                               if (null != urlParts) {
-                                                       mrUrl = urlParts[2];
-
-                                                       // DCAE internal dmaap topic convention
-                                                       if (urlParts[3].equals("events")) {
-                                                               mrTopic = urlParts[4];
-                                                       } else {
-                                                               // ONAP dmaap topic convention
-                                                               mrTopic = urlParts[3];
-                                                               hostport = mrUrl.split(":");
-                                                       }
-
-                                               }
-                                       } catch (NullPointerException e) {
-                                               System.out.println("NullPointerException");
-                                               e.getMessage();
-                                       }
-
-                                       if (entry.getValue().getAsJsonObject().has("aaf_username")) {
-                                               username = entry.getValue().getAsJsonObject().get("aaf_username").toString().replace("\"", "");
-                                       }
-                                       if (entry.getValue().getAsJsonObject().has("aaf_password")) {
-                                               userpwd = entry.getValue().getAsJsonObject().get("aaf_password").toString().replace("\"", "");
-                                       }
-                                       if (hostport == null) {
-                                               log.debug("TOPIC:" + mrTopic + " HOST-URL:" + mrUrl + " PWD:" + userpwd + " USER:" + username);
-                                       } else {
-                                               log.debug("TOPIC:" + mrTopic + " HOST-URL:" + mrUrl + " HOSTS:" + hostport[0] + " PWD:"
-                                                               + userpwd + " USER:" + username + " NAME:" + entry.getKey());
-                                       }
-
-                                       dmaap_hash.put(entry.getKey() + ".cambria.topic", mrTopic);
-
-                                       if (!(hostport == null)) {
-                                               dmaap_hash.put(entry.getKey() + ".cambria.hosts", hostport[0]);
-                                       }
-
-                                       if (!(mrUrl == null)) {
-                                               dmaap_hash.put(entry.getKey() + ".cambria.url", mrUrl);
-                                       }
-
-                                       if (!(username == null)) {
-                                               dmaap_hash.put(entry.getKey() + ".basicAuthUsername", username);
-                                       }
-
-                                       if (!(userpwd == null)) {
-                                               dmaap_hash.put(entry.getKey() + ".basicAuthPassword", userpwd);
-                                       }
-
-                               }
-
-                       }
-
-               } catch (JsonIOException | JsonSyntaxException |
-
-                               FileNotFoundException e1) {
-                       e1.printStackTrace();
-                       log.error("Problem loading Dmaap Channel configuration file: " + e1.toString());
-               } finally {
-                       if (fr != null) {
-                               try {
-                                       fr.close();
-                               } catch (IOException e) {
-                                       log.error("Error closing file reader stream : " + e.toString());
-                               }
-                       }
-               }
-
-       }
-
-       /***
-        * Dmaap url structure pub - https://<dmaaphostname>:<port>/events/
-        * <namespace>.<dmaapcluster>.<topic>, sub - https://<dmaaphostname>:
-        * <port>/events/<namespace>.<dmaapcluster>.<topic>/G1/u1";
-        *
-        * Onap url structure pub - http://<dmaaphostname>:<port>/<unauthenticated>.
-        * <topic>,
-        */
-
-       private String[] dmaapUrlSplit(String dmUrl) {
-               String[] multUrls = dmUrl.split(",");
-
-               StringBuffer newUrls = new StringBuffer();
-               String urlParts[] = null;
-               for (int i = 0; i < multUrls.length; i++) {
-                       urlParts = multUrls[i].split("/");
-                       if (i == 0) {
-                               newUrls = newUrls.append(urlParts[2]);
-                       } else {
-                               newUrls = newUrls.append(",").append(urlParts[2]);
-                       }
-               }
-               return urlParts;
-       }
-
-       public static synchronized DmaapPropertyReader getInstance(String ChannelConfig) {
-               if (instance == null) {
-                       instance = new DmaapPropertyReader(ChannelConfig);
-               }
-               return instance;
-       }
-
-       public String getKeyValue(String HashKey) {
-               return this.dmaap_hash.get(HashKey);
-       }
+    private static DmaapPropertyReader instance = null;
+
+    private static final Logger log = LoggerFactory.getLogger(DmaapPropertyReader.class);
+    private static final String TOPIC = "TOPIC:";
+    private static final String HOST_URL = " HOST-URL:";
+    private static final String CAMBRIA_URL = "cambria.url";
+    private static final String CAMBRIA_HOSTS = "cambria.hosts";
+    private static final String USER = " USER:";
+    private static final String PWD = " PWD:";
+    private static final String BASIC_AUTH_PASSWORD = "basicAuthPassword";
+    private static final String BASIC_AUTH_USERNAME = "basicAuthUsername";
+
+    public HashMap<String, String> dmaap_hash = new HashMap<String, String>();
+
+    private DmaapPropertyReader(String cambriaConfigFile) {
+
+        FileReader fr = null;
+        try {
+            JsonElement root;
+            fr = new FileReader(cambriaConfigFile);
+            root = new JsonParser().parse(fr);
+
+            //Check if dmaap config is handled by legacy controller/service manager
+            if (root.getAsJsonObject().has("channels")) {
+                JsonArray jsonObject = (JsonArray) root.getAsJsonObject().get("channels");
+
+                for (int i = 0; i < jsonObject.size(); i++) {
+                    log.debug(String.format("%s%s%s%s HOSTS:%s%s%s%s%s NAME:%s", TOPIC,
+                        jsonObject.get(i).getAsJsonObject().get("cambria.topic"), HOST_URL,
+                        jsonObject.get(i).getAsJsonObject().get(CAMBRIA_URL),
+                        jsonObject.get(i).getAsJsonObject().get(CAMBRIA_HOSTS), PWD,
+                        jsonObject.get(i).getAsJsonObject().get(BASIC_AUTH_PASSWORD), USER,
+                        jsonObject.get(i).getAsJsonObject().get(BASIC_AUTH_USERNAME),
+                        jsonObject.get(i).getAsJsonObject().get("name")));
+
+                    String convertedname = jsonObject.get(i).getAsJsonObject().get("name")
+                        .toString().replace("\"", "");
+                    dmaap_hash.put(convertedname + ".cambria.topic",
+                        jsonObject.get(i).getAsJsonObject().get("cambria.topic").toString()
+                            .replace("\"", ""));
+
+                    if (jsonObject.get(i).getAsJsonObject().get(CAMBRIA_HOSTS) != null) {
+                        dmaap_hash.put(convertedname + ".cambria.hosts",
+                            jsonObject.get(i).getAsJsonObject().get(CAMBRIA_HOSTS).toString()
+                                .replace("\"", ""));
+                    }
+                    if (jsonObject.get(i).getAsJsonObject().get(CAMBRIA_URL) != null) {
+                        dmaap_hash.put(convertedname + ".cambria.url",
+                            jsonObject.get(i).getAsJsonObject().get(CAMBRIA_URL).toString()
+                                .replace("\"", ""));
+                    }
+                    if (jsonObject.get(i).getAsJsonObject().get(BASIC_AUTH_PASSWORD) != null) {
+                        dmaap_hash.put(convertedname + ".basicAuthPassword",
+                            jsonObject.get(i).getAsJsonObject()
+                                .get(BASIC_AUTH_PASSWORD).toString().replace("\"", ""));
+                    }
+                    if (jsonObject.get(i).getAsJsonObject().get(BASIC_AUTH_USERNAME) != null) {
+                        dmaap_hash.put(convertedname + ".basicAuthUsername",
+                            jsonObject.get(i).getAsJsonObject()
+                                .get(BASIC_AUTH_USERNAME).toString().replace("\"", ""));
+                    }
+
+                }
+            } else {
+
+                //Handing new format from  controllergen2/config_binding_service
+                JsonObject jsonObject = root.getAsJsonObject();
+                Set<Map.Entry<String, JsonElement>> entries = jsonObject.entrySet();
+
+                for (Map.Entry<String, JsonElement> entry : entries) {
+
+                    JsonElement topicurl = entry.getValue().getAsJsonObject().get("dmaap_info")
+                        .getAsJsonObject().get("topic_url");
+                    String[] urlParts = dmaapUrlSplit(topicurl.toString().replace("\"", ""));
+
+                    String mrTopic = null;
+                    String mrUrl = null;
+                    String[] hostport = null;
+                    String username = null;
+                    String userpwd = null;
+
+                    try {
+
+                        if (null != urlParts) {
+                            mrUrl = urlParts[2];
+
+                            // DCAE internal dmaap topic convention
+                            if (urlParts[3].equals("events")) {
+                                mrTopic = urlParts[4];
+                            } else {
+                                // ONAP dmaap topic convention
+                                mrTopic = urlParts[3];
+                                hostport = mrUrl.split(":");
+                            }
+
+                        }
+                    } catch (NullPointerException e) {
+                        log.error("NullPointerException " + e.getLocalizedMessage(), e);
+                    }
+
+                    if (entry.getValue().getAsJsonObject().has("aaf_username")) {
+                        username = entry.getValue().getAsJsonObject().get("aaf_username").toString()
+                            .replace("\"", "");
+                    }
+                    if (entry.getValue().getAsJsonObject().has("aaf_password")) {
+                        userpwd = entry.getValue().getAsJsonObject().get("aaf_password").toString()
+                            .replace("\"", "");
+                    }
+                    if (hostport == null) {
+                        log.debug(
+                            String.format("%s%s%s%s%s%s%s%s", TOPIC, mrTopic, HOST_URL, mrUrl, PWD,
+                                userpwd, USER, username));
+                    } else {
+                        log.debug(
+                            String.format("%s%s%s%s HOSTS:%s%s%s%s%s NAME:%s", TOPIC, mrTopic,
+                                HOST_URL, mrUrl, hostport[0], PWD, userpwd, USER, username,
+                                entry.getKey()));
+                    }
+
+                    dmaap_hash.put(entry.getKey() + ".cambria.topic", mrTopic);
+
+                    if (hostport != null) {
+                        dmaap_hash.put(entry.getKey() + ".cambria.hosts", hostport[0]);
+                    }
+
+                    if (mrUrl != null) {
+                        dmaap_hash.put(entry.getKey() + ".cambria.url", mrUrl);
+                    }
+
+                    if (username != null) {
+                        dmaap_hash.put(entry.getKey() + ".basicAuthUsername", username);
+                    }
+
+                    if (userpwd != null) {
+                        dmaap_hash.put(entry.getKey() + ".basicAuthPassword", userpwd);
+                    }
+
+                }
+
+            }
+
+            fr.close();
+        } catch (JsonIOException | JsonSyntaxException | IOException e1) {
+            log.error("Problem loading Dmaap Channel configuration file: " +
+                e1.getLocalizedMessage(), e1);
+        } finally {
+            if (fr != null) {
+                try {
+                    fr.close();
+                } catch (IOException e) {
+                    log.error("Error closing file reader stream : " + e);
+                }
+            }
+        }
+
+    }
+
+    /***
+     * Dmaap url structure pub - https://<dmaaphostname>:<port>/events/
+     * <namespace>.<dmaapcluster>.<topic>, sub - https://<dmaaphostname>:
+     * <port>/events/<namespace>.<dmaapcluster>.<topic>/G1/u1";
+     *
+     * Onap url structure pub - http://<dmaaphostname>:<port>/<unauthenticated>.
+     * <topic>,
+     */
+
+    private String[] dmaapUrlSplit(String dmUrl) {
+        String[] multUrls = dmUrl.split(",");
+
+        StringBuilder newUrls = new StringBuilder();
+        String[] urlParts = null;
+        for (int i = 0; i < multUrls.length; i++) {
+            urlParts = multUrls[i].split("/");
+            if (i == 0) {
+                newUrls = newUrls.append(urlParts[2]);
+            } else {
+                newUrls = newUrls.append(",").append(urlParts[2]);
+            }
+        }
+        return urlParts;
+    }
+
+    public static synchronized DmaapPropertyReader getInstance(String channelConfig) {
+        if (instance == null) {
+            instance = new DmaapPropertyReader(channelConfig);
+        }
+        return instance;
+    }
+
+    public String getKeyValue(String hashKey) {
+        return dmaap_hash.get(hashKey);
+    }
 }