Fix Sonar bugs 15/9415/1
authorDeterme, Sebastien (sd378r) <sd378r@intl.att.com>
Wed, 30 Aug 2017 12:49:27 +0000 (05:49 -0700)
committerDeterme, Sebastien (sd378r) <sd378r@intl.att.com>
Wed, 30 Aug 2017 12:49:27 +0000 (05:49 -0700)
In client java packages Blocker issues fixed

Change-Id: I7ea1fb546ad32422da7eef596a989e4696238651
Issue-Id: CLAMP-43
Signed-off-by: Determe, Sebastien (sd378r) <sd378r@intl.att.com>
src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java
src/main/java/org/onap/clamp/clds/client/PolicyClient.java
src/main/java/org/onap/clamp/clds/client/SdcCatalogServices.java

index 3d8d5d5..718a2e9 100644 (file)
@@ -96,18 +96,16 @@ public class DcaeDispatcherServices {
             if (inStream != null) {\r
                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inStream));\r
                 String inputLine = null;\r
-                StringBuffer response = new StringBuffer();\r
+                StringBuilder response = new StringBuilder();\r
                 while ((inputLine = bufferedReader.readLine()) != null) {\r
                     response.append(inputLine);\r
                 }\r
                 responseStr = response.toString();\r
             }\r
 \r
-            if (responseStr != null) {\r
-                if (requestFailed) {\r
-                    logger.error("requestFailed - responseStr=" + responseStr);\r
-                    throw new BadRequestException(responseStr);\r
-                }\r
+            if (responseStr != null && requestFailed) {\r
+                logger.error("requestFailed - responseStr=" + responseStr);\r
+                throw new BadRequestException(responseStr);\r
             }\r
 \r
             logger.debug("response code " + responseCode);\r
@@ -270,7 +268,7 @@ public class DcaeDispatcherServices {
 \r
                 String inputLine = null;\r
 \r
-                StringBuffer response = new StringBuffer();\r
+                StringBuilder response = new StringBuilder();\r
 \r
                 while ((inputLine = in.readLine()) != null) {\r
                     response.append(inputLine);\r
@@ -279,11 +277,9 @@ public class DcaeDispatcherServices {
                 responseStr = response.toString();\r
             }\r
 \r
-            if (responseStr != null) {\r
-                if (requestFailed) {\r
-                    logger.error("requestFailed - responseStr=" + responseStr);\r
-                    throw new BadRequestException(responseStr);\r
-                }\r
+            if (responseStr != null && requestFailed) {\r
+                logger.error("requestFailed - responseStr=" + responseStr);\r
+                throw new BadRequestException(responseStr);\r
             }\r
 \r
             logger.debug("response code " + responseCode);\r
index e16f056..6095af1 100644 (file)
@@ -61,8 +61,11 @@ import org.springframework.context.ApplicationContext;
  * Policy utility methods - specifically, send the policy.
  */
 public class PolicyClient {
-    protected static final EELFLogger logger        = EELFManager.getInstance().getLogger(PolicyClient.class);
-    protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
+
+    protected static final String     LOG_POLICY_PREFIX = "Response is ";
+
+    protected static final EELFLogger logger            = EELFManager.getInstance().getLogger(PolicyClient.class);
+    protected static final EELFLogger metricsLogger     = EELFManager.getInstance().getMetricsLogger();
 
     @Value("${org.onap.clamp.config.files.cldsPolicyConfig:'classpath:/clds/clds-policy-config.properties'}")
     protected String                  cldsPolicyConfigFile;
@@ -73,10 +76,6 @@ public class PolicyClient {
     @Autowired
     protected RefProp                 refProp;
 
-    public PolicyClient() {
-
-    }
-
     /**
      * Perform send of microservice policy.
      *
@@ -189,11 +188,11 @@ public class PolicyClient {
 
         // API method to create or update Policy.
         PolicyChangeResponse response = null;
-        String responseMessage;
+        String responseMessage = "";
         Date startTime = new Date();
         try {
             List<Integer> versions = getVersions(policyNamePrefix, prop);
-            if (versions.size() <= 0) {
+            if (versions.isEmpty()) {
                 LoggingUtils.setTargetContext("Policy", "createPolicy");
                 logger.info("Attempting to create policy for action=" + prop.getActionCd());
                 response = policyEngine.createPolicy(policyParameters);
@@ -205,9 +204,9 @@ public class PolicyClient {
                 responseMessage = response.getResponseMessage();
             }
         } catch (Exception e) {
-            responseMessage = e.toString();
+            logger.error("Exception occurred during policy communnication", e);
         }
-        logger.info("response is " + responseMessage);
+        logger.info(LOG_POLICY_PREFIX + responseMessage);
 
         LoggingUtils.setTimeContext(startTime, new Date());
 
@@ -261,15 +260,15 @@ public class PolicyClient {
 
         // API method to create or update Policy.
         PolicyChangeResponse response = null;
-        String responseMessage;
+        String responseMessage = "";
         try {
             logger.info("Attempting to push policy...");
             response = policyEngine.pushPolicy(pushPolicyParameters);
             responseMessage = response.getResponseMessage();
         } catch (Exception e) {
-            responseMessage = e.toString();
+            logger.error("Exception occurred during policy communnication", e);
         }
-        logger.info("response is " + responseMessage);
+        logger.info(LOG_POLICY_PREFIX + responseMessage);
 
         if (response != null && (response.getResponseCode() == 200 || response.getResponseCode() == 204)) {
             logger.info("Policy push successful");
@@ -333,7 +332,7 @@ public class PolicyClient {
             logger.info("Policy versions.size()=" + versions.size());
         } catch (Exception e) {
             // just print warning - if no policy version found
-            logger.warn("warning: policy not found...policy name - " + policyName);
+            logger.warn("warning: policy not found...policy name - " + policyName, e);
         }
 
         return versions;
@@ -401,8 +400,7 @@ public class PolicyClient {
         deletePolicyParameters.setPdpGroup(refProp.getStringValue("policy.pdp.group"));
         deletePolicyParameters.setPolicyType(policyType);
         // send delete request
-        String responseMessage = null;
-        responseMessage = sendDeletePolicy(deletePolicyParameters, prop);
+        String responseMessage = sendDeletePolicy(deletePolicyParameters, prop);
 
         logger.info("Deleting policy from PAP...");
         deletePolicyParameters.setPolicyComponent("PAP");
@@ -438,15 +436,15 @@ public class PolicyClient {
 
         // API method to create or update Policy.
         PolicyChangeResponse response = null;
-        String responseMessage;
+        String responseMessage = "";
         try {
             logger.info("Attempting to delete policy...");
             response = policyEngine.deletePolicy(deletePolicyParameters);
             responseMessage = response.getResponseMessage();
         } catch (Exception e) {
-            responseMessage = e.toString();
+            logger.error("Exception occurred during policy communnication", e);
         }
-        logger.info("response is " + responseMessage);
+        logger.info(LOG_POLICY_PREFIX + responseMessage);
 
         if (response != null && response.getResponseCode() == 200) {
             logger.info("Policy delete successful");
index a4e332a..24a3d3c 100644 (file)
@@ -423,14 +423,14 @@ public class SdcCatalogServices {
 \r
             // To remove duplicate resources from serviceDetail and add valid\r
             // vfs to service\r
-            if (cldsSdcServiceDetail != null && cldsSdcServiceDetail.getResources() != null) {\r
+            if (cldsSdcServiceDetail.getResources() != null) {\r
                 List<CldsSdcResource> cldsSdcResourceList = removeDuplicateSdcResourceInstances(\r
                         cldsSdcServiceDetail.getResources());\r
-                if (cldsSdcResourceList != null && cldsSdcResourceList.size() > 0) {\r
+                if (cldsSdcResourceList != null && !cldsSdcResourceList.isEmpty()) {\r
                     List<CldsVfData> cldsVfDataList = new ArrayList<>();\r
                     for (CldsSdcResource currCldsSdcResource : cldsSdcResourceList) {\r
                         if (currCldsSdcResource != null && currCldsSdcResource.getResoucreType() != null\r
-                                && currCldsSdcResource.getResoucreType().equalsIgnoreCase("VF")) {\r
+                                && "VF".equalsIgnoreCase(currCldsSdcResource.getResoucreType())) {\r
                             CldsVfData currCldsVfData = new CldsVfData();\r
                             currCldsVfData.setVfName(currCldsSdcResource.getResourceInstanceName());\r
                             currCldsVfData.setVfInvariantResourceUUID(currCldsSdcResource.getResourceInvariantUUID());\r
@@ -454,7 +454,7 @@ public class SdcCatalogServices {
      */\r
     private void getAllVfcForVfList(List<CldsVfData> cldsVfDataList, String catalogUrl) throws IOException {\r
         // todo : refact this..\r
-        if (cldsVfDataList != null && cldsVfDataList.size() > 0) {\r
+        if (cldsVfDataList != null && !cldsVfDataList.isEmpty()) {\r
             List<CldsSdcResourceBasicInfo> allVfResources = getAllSdcVForVfcResourcesBasedOnResourceType(\r
                     RESOURCE_VF_TYPE);\r
             List<CldsSdcResourceBasicInfo> allVfcResources = getAllSdcVForVfcResourcesBasedOnResourceType(\r
@@ -475,7 +475,7 @@ public class SdcCatalogServices {
                             List<CldsVfcData> vfcDataListFromVfResponse = getVfcDataListFromVfResponse(vfResponse);\r
                             if (vfcDataListFromVfResponse != null) {\r
                                 currCldsVfData.setCldsVfcs(vfcDataListFromVfResponse);\r
-                                if (vfcDataListFromVfResponse.size() > 0) {\r
+                                if (!vfcDataListFromVfResponse.isEmpty()) {\r
                                     // To get artifacts for every VFC and get\r
                                     // alarm conditions from artifact\r
                                     for (CldsVfcData currCldsVfcData : vfcDataListFromVfResponse) {\r
@@ -537,7 +537,7 @@ public class SdcCatalogServices {
 \r
     private List<CldsVfcData> getVFCfromCVFC(String resourceUUID) {\r
         String catalogUrl = refProp.getStringValue("sdc.catalog.url");\r
-        List<CldsVfcData> cldsVfcDataList = new ArrayList<CldsVfcData>();\r
+        List<CldsVfcData> cldsVfcDataList = new ArrayList<>();\r
 \r
         if (resourceUUID != null) {\r
             String vfcResourceUUIDUrl = catalogUrl + "resources" + "/" + resourceUUID + "/metadata";\r
@@ -569,10 +569,11 @@ public class SdcCatalogServices {
     }\r
 \r
     private String removeUnwantedBracesFromString(String id) {\r
+        String idReworked = "";\r
         if (id != null && id.contains("\"")) {\r
-            id = id.replaceAll("\"", "");\r
+            idReworked = id.replaceAll("\"", "");\r
         }\r
-        return id;\r
+        return idReworked;\r
     }\r
 \r
     private List<CldsAlarmCondition> getAlarmCondtionsFromVfc(String vfcResponse) throws IOException {\r
@@ -611,7 +612,7 @@ public class SdcCatalogServices {
 \r
     // Method to get the artifact for any particular VF\r
     private List<CldsVfKPIData> getFieldPathFromVF(String vfResponse) throws JsonProcessingException, IOException {\r
-        List<CldsVfKPIData> cldsVfKPIDataList = new ArrayList<CldsVfKPIData>();\r
+        List<CldsVfKPIData> cldsVfKPIDataList = new ArrayList<>();\r
         ObjectMapper mapper = new ObjectMapper();\r
         ObjectNode vfResponseNode = (ObjectNode) mapper.readTree(vfResponse);\r
         ArrayNode artifactsArrayNode = (ArrayNode) vfResponseNode.get("artifacts");\r
@@ -664,7 +665,7 @@ public class SdcCatalogServices {
 \r
     // Method to get the artifactURL Data and set the CldsVfKPIData node\r
     private List<CldsVfKPIData> parseCsvToGetFieldPath(String allFieldPathValues) throws IOException {\r
-        List<CldsVfKPIData> cldsVfKPIDataList = new ArrayList<CldsVfKPIData>();\r
+        List<CldsVfKPIData> cldsVfKPIDataList = new ArrayList<>();\r
         Reader alarmReader = new StringReader(allFieldPathValues);\r
         Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(alarmReader);\r
         if (records != null) {\r
@@ -701,8 +702,8 @@ public class SdcCatalogServices {
 \r
     public String getResponsesFromArtifactUrl(String artifactsUrl) {\r
         String hostUrl = refProp.getStringValue("sdc.hostUrl");\r
-        artifactsUrl = artifactsUrl.replaceAll("\"", "");\r
-        String artifactUrl = hostUrl + artifactsUrl;\r
+        String artifactsUrlReworked = artifactsUrl.replaceAll("\"", "");\r
+        String artifactUrl = hostUrl + artifactsUrlReworked;\r
         logger.info("value of artifactURl:" + artifactUrl);\r
         String currArtifactResponse = getCldsServicesOrResourcesBasedOnURL(artifactUrl, true);\r
         logger.info("value of artifactResponse:" + currArtifactResponse);\r
@@ -720,8 +721,8 @@ public class SdcCatalogServices {
      */\r
     public String getCldsServicesOrResourcesBasedOnURL(String url, boolean alarmConditions) {\r
         try {\r
-            url = removeUnwantedBracesFromString(url);\r
-            URL urlObj = new URL(url);\r
+            String urlReworked = removeUnwantedBracesFromString(url);\r
+            URL urlObj = new URL(urlReworked);\r
 \r
             HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();\r
             String basicAuth = SdcReq.getSdcBasicAuth(refProp);\r
@@ -738,7 +739,9 @@ public class SdcCatalogServices {
                 response = new StringBuilder();\r
                 String inputLine;\r
                 while ((inputLine = in.readLine()) != null) {\r
-                    response.append(inputLine);\r
+                    if (!inputLine.isEmpty()) {\r
+                        response.append(inputLine);\r
+                    }\r
                     if (alarmConditions) {\r
                         response.append("\n");\r
                     }\r
@@ -780,7 +783,7 @@ public class SdcCatalogServices {
 \r
             // To create byKpi\r
             ObjectNode kpiObjectNode = mapper.createObjectNode();\r
-            if (cldsServiceData.getCldsVfs() != null && cldsServiceData.getCldsVfs().size() > 0) {\r
+            if (cldsServiceData.getCldsVfs() != null && !cldsServiceData.getCldsVfs().isEmpty()) {\r
                 for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) {\r
                     if (currCldsVfData != null) {\r
                         createKpiObjectNodeByVfUuid(mapper, kpiObjectNode, currCldsVfData.getCldsKPIList());\r
@@ -791,7 +794,7 @@ public class SdcCatalogServices {
 \r
             // To create byVfc and alarmCondition with vfcResourceUUID\r
             ObjectNode vfcResourceUuidObjectNode = mapper.createObjectNode();\r
-            if (cldsServiceData.getCldsVfs() != null && cldsServiceData.getCldsVfs().size() > 0) {\r
+            if (cldsServiceData.getCldsVfs() != null && !cldsServiceData.getCldsVfs().isEmpty()) {\r
                 for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) {\r
                     if (currCldsVfData != null) {\r
                         createAlarmCondObjectNodeByVfcUuid(mapper, vfcResourceUuidObjectNode,\r
@@ -902,7 +905,7 @@ public class SdcCatalogServices {
             List<CldsAlarmCondition> cldsAlarmCondList) {\r
         ObjectNode alarmCondKeyNode = mapper.createObjectNode();\r
 \r
-        if (cldsAlarmCondList != null && cldsAlarmCondList.size() > 0) {\r
+        if (cldsAlarmCondList != null && !cldsAlarmCondList.isEmpty()) {\r
             for (CldsAlarmCondition currCldsAlarmCondition : cldsAlarmCondList) {\r
                 if (currCldsAlarmCondition != null) {\r
                     ObjectNode alarmCondNode = mapper.createObjectNode();\r
@@ -925,7 +928,7 @@ public class SdcCatalogServices {
         ObjectNode vfObjectNode = mapper.createObjectNode();\r
         ObjectNode vfUuidNode = mapper.createObjectNode();\r
         List<CldsVfData> cldsVfsList = cldsServiceData.getCldsVfs();\r
-        if (cldsVfsList != null && cldsVfsList.size() > 0) {\r
+        if (cldsVfsList != null && !cldsVfsList.isEmpty()) {\r
             for (CldsVfData currCldsVfData : cldsVfsList) {\r
                 if (currCldsVfData != null) {\r
                     vfUuidNode.put(currCldsVfData.getVfInvariantResourceUUID(), currCldsVfData.getVfName());\r
@@ -941,7 +944,7 @@ public class SdcCatalogServices {
 \r
     private void createKpiObjectNodeByVfUuid(ObjectMapper mapper, ObjectNode vfResourceUuidObjectNode,\r
             List<CldsVfKPIData> cldsVfKpiDataList) {\r
-        if (cldsVfKpiDataList != null && cldsVfKpiDataList.size() > 0) {\r
+        if (cldsVfKpiDataList != null && !cldsVfKpiDataList.isEmpty()) {\r
             for (CldsVfKPIData currCldsVfKpiData : cldsVfKpiDataList) {\r
                 if (currCldsVfKpiData != null) {\r
                     ObjectNode thresholdNameObjectNode = mapper.createObjectNode();\r
@@ -967,11 +970,11 @@ public class SdcCatalogServices {
         ObjectNode vfcObjectNode = mapper.createObjectNode();\r
         ObjectNode alarmCondNode = mapper.createObjectNode();\r
         ObjectNode alertDescNode = mapper.createObjectNode();\r
-        if (cldsVfcDataList != null && cldsVfcDataList.size() > 0) {\r
+        if (cldsVfcDataList != null && !cldsVfcDataList.isEmpty()) {\r
             for (CldsVfcData currCldsVfcData : cldsVfcDataList) {\r
                 if (currCldsVfcData != null) {\r
                     if (currCldsVfcData.getCldsAlarmConditions() != null\r
-                            && currCldsVfcData.getCldsAlarmConditions().size() > 0) {\r
+                            && !currCldsVfcData.getCldsAlarmConditions().isEmpty()) {\r
                         for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) {\r
                             alarmCondNode.put(currCldsAlarmCondition.getAlarmConditionKey(),\r
                                     currCldsAlarmCondition.getAlarmConditionKey());\r
@@ -1001,12 +1004,12 @@ public class SdcCatalogServices {
     private ObjectNode createVfcObjectNodeByVfUuid(ObjectMapper mapper, List<CldsVfData> cldsVfDataList) {\r
         ObjectNode vfUuidObjectNode = mapper.createObjectNode();\r
 \r
-        if (cldsVfDataList != null && cldsVfDataList.size() > 0) {\r
+        if (cldsVfDataList != null && !cldsVfDataList.isEmpty()) {\r
             for (CldsVfData currCldsVfData : cldsVfDataList) {\r
                 if (currCldsVfData != null) {\r
                     ObjectNode vfcObjectNode = mapper.createObjectNode();\r
                     ObjectNode vfcUuidNode = mapper.createObjectNode();\r
-                    if (currCldsVfData.getCldsVfcs() != null && currCldsVfData.getCldsVfcs().size() > 0) {\r
+                    if (currCldsVfData.getCldsVfcs() != null && !currCldsVfData.getCldsVfcs().isEmpty()) {\r
                         for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) {\r
                             vfcUuidNode.put(currCldsVfcData.getVfcInvariantResourceUUID(),\r
                                     currCldsVfcData.getVfcName());\r
@@ -1126,7 +1129,7 @@ public class SdcCatalogServices {
     private String getResourceUuidFromResourceInvariantUuid(String resourceInvariantUuid,\r
             List<CldsSdcResourceBasicInfo> resourceInfoList) throws IOException {\r
         String resourceUuid = null;\r
-        if (resourceInfoList != null && resourceInfoList.size() > 0) {\r
+        if (resourceInfoList != null && !resourceInfoList.isEmpty()) {\r
             for (CldsSdcResourceBasicInfo currResource : resourceInfoList) {\r
                 if (currResource != null && currResource.getInvariantUUID() != null && currResource.getUuid() != null\r
                         && currResource.getInvariantUUID().equalsIgnoreCase(resourceInvariantUuid)) {\r