Fix sonar issues 89/57289/2
authorParshad Patel <pars.patel@samsung.com>
Tue, 24 Jul 2018 10:58:14 +0000 (19:58 +0900)
committerParshad Patel <pars.patel@samsung.com>
Wed, 25 Jul 2018 01:19:01 +0000 (10:19 +0900)
Fix use try-with-resources issues in ccsdk/sli/northbound

Issue-ID: CCSDK-332
Change-Id: Ibc61e6b3ec81e774556172c63c0ca062b6bd6a26
Signed-off-by: Parshad Patel <pars.patel@samsung.com>
asdcApi/provider/src/main/java/org/onap/ccsdk/sli/northbound/asdcapi/AsdcApiProvider.java
ueb-listener/src/main/java/org/onap/ccsdk/sli/northbound/uebclient/SdncUebCallback.java

index 0c2ce2f..27280d2 100644 (file)
@@ -179,14 +179,12 @@ public class AsdcApiProvider implements AutoCloseable, ASDCAPIService {
         InstanceIdentifier artifactInstanceId =
                 InstanceIdentifier.<Artifacts>builder(Artifacts.class)
                 .child(Artifact.class, new ArtifactKey(aName, aVersion)).build();
-        ReadOnlyTransaction readTx = dataBroker.newReadOnlyTransaction();
         Optional<Artifact> data = null;
-        try {
+        try(ReadOnlyTransaction readTx = dataBroker.newReadOnlyTransaction()) {
             data = (Optional<Artifact>) readTx.read(LogicalDatastoreType.CONFIGURATION, artifactInstanceId).get();
         } catch (InterruptedException | ExecutionException e) {
             LOG.error("Caught Exception reading MD-SAL for ["+aName+","+ aVersion+"] " ,e);
             return false;
-
         }
 
         return data.isPresent();
index 866fd14..224a519 100644 (file)
@@ -482,24 +482,19 @@ public class SdncUebCallback implements INotificationCallback {
         // Save zip if TOSCA_CSAR
         if (artifact.getArtifactType().contains("TOSCA_CSAR") || artifact.getArtifactName().contains(".csar")) {
 
-               try {
-                       
-                               FileOutputStream outFile = new FileOutputStream(incomingDir.getAbsolutePath() + "/" + artifact.getArtifactName());
-                               outFile.write(payloadBytes, 0, payloadBytes.length);
-                               outFile.close();
-                   writeSucceeded = true;
-               } catch (Exception e) {
-                   LOG.error("Unable to save downloaded zip file to spool directory ("+ incomingDir.getAbsolutePath() +")", e);
-               }
+                try(FileOutputStream outFile = new FileOutputStream(incomingDir.getAbsolutePath() + "/" + artifact.getArtifactName())) {
+                    outFile.write(payloadBytes, 0, payloadBytes.length);
+                    writeSucceeded = true;
+                } catch (Exception e) {
+                    LOG.error("Unable to save downloaded zip file to spool directory ("+ incomingDir.getAbsolutePath() +")", e);
+                }
 
         } else {
 
                        String payload = new String(payloadBytes);
        
-               try {
-                   FileWriter spoolFileWriter = new FileWriter(spoolFile);
+               try(FileWriter spoolFileWriter = new FileWriter(spoolFile)) {
                    spoolFileWriter.write(payload);
-                   spoolFileWriter.close();
                    writeSucceeded = true;
                } catch (Exception e) {
                    LOG.error("Unable to save downloaded file to spool directory ("+ incomingDir.getAbsolutePath() +")", e);
@@ -943,9 +938,8 @@ public class SdncUebCallback implements INotificationCallback {
             msgBuffer.append("<artifact-version>"+artifact.getArtifactVersion()+"</artifact-version>\n");              
         } 
         
-        try {
-            BufferedReader rdr = new BufferedReader(new FileReader(artifact.getFile()));
-
+        try(BufferedReader rdr = new BufferedReader(new FileReader(artifact.getFile()))) {
+            
             String curLine = rdr.readLine();
 
             while (curLine != null) {
@@ -966,8 +960,6 @@ public class SdncUebCallback implements INotificationCallback {
                 }
                 curLine = rdr.readLine();
             }
-            rdr.close();
-
         } catch (Exception e) {
             LOG.error("Could not process spool file "+artifact.getFile().getName(), e);
                        return(DistributionStatusEnum.DEPLOY_ERROR);