Fix sonar issues 00/109600/3
authorRodrigo Lima <rodrigo.lima@yoppworks.com>
Tue, 23 Jun 2020 18:34:07 +0000 (14:34 -0400)
committerRodrigo Lima <rodrigo.lima@yoppworks.com>
Wed, 8 Jul 2020 14:54:28 +0000 (10:54 -0400)
- Fix Sonar issues in the classes : UpdatePropertyToolInternal, MigratePserverAndPnfEquipType,
UniquePropertyCheck, AuditOXM, PrintVertexDetails, DataGroomingTasks,
DeletePInterface, MigrateDataFromASDCToConfiguration, MigrateSAREvcInventory,
MigrateModelVer, PserverDedupWithDifferentSourcesOfTruth, GenericQueryProcessor,
DataExportTasks

Issue-ID: AAI-2963
Signed-off-by: Rodrigo Lima <rodrigo.lima@yoppworks.com>
Change-Id: I76ff744153805a009d8378b9839f83456d9ebe5c

13 files changed:
src/main/java/org/onap/aai/dataexport/DataExportTasks.java
src/main/java/org/onap/aai/datagrooming/DataGroomingTasks.java
src/main/java/org/onap/aai/datasnapshot/PrintVertexDetails.java
src/main/java/org/onap/aai/db/schema/AuditOXM.java
src/main/java/org/onap/aai/dbgen/UpdatePropertyToolInternal.java
src/main/java/org/onap/aai/migration/v12/DeletePInterface.java
src/main/java/org/onap/aai/migration/v12/MigrateDataFromASDCToConfiguration.java
src/main/java/org/onap/aai/migration/v12/MigrateSAREvcInventory.java
src/main/java/org/onap/aai/migration/v13/MigrateModelVer.java
src/main/java/org/onap/aai/migration/v13/MigratePserverAndPnfEquipType.java
src/main/java/org/onap/aai/migration/v14/PserverDedupWithDifferentSourcesOfTruth.java
src/main/java/org/onap/aai/rest/search/GenericQueryProcessor.java
src/main/java/org/onap/aai/util/UniquePropertyCheck.java

index 028c729..143312b 100644 (file)
@@ -76,7 +76,8 @@ import org.apache.commons.io.filefilter.RegexFileFilter;
 public class DataExportTasks {
 
        private static final Logger LOGGER;
-       private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
+
+       private final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        
        static {
                System.setProperty("aai.service.name", DataExportTasks.class.getSimpleName());
@@ -160,7 +161,7 @@ public class DataExportTasks {
                if ( "false".equalsIgnoreCase(enableMultipleSnapshots)){
                        // find the second to latest data snapshot
                        snapshot = findSnapshot();
-                       snapshotFilePath = snapshot.getAbsolutePath();
+                       snapshotFilePath = snapshot != null ?snapshot.getAbsolutePath() : null;
                        if ( "true".equalsIgnoreCase (enablePartialGraph) ) {
                                        String[] command = new String[2];
                                        command[0] = AAIConstants.AAI_HOME + AAIConstants.AAI_FILESEP + "bin" + AAIConstants.AAI_FILESEP + "dynamicPayloadPartial.sh";
index 4309ece..21f3172 100644 (file)
@@ -51,7 +51,7 @@ public class DataGroomingTasks {
        private AaiScheduledTaskAuditLog auditLog;
        
        private static final Logger LOGGER = LoggerFactory.getLogger(DataGroomingTasks.class);
-       private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
+       private final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
 
        @Autowired
        private LoaderFactory loaderFactory;
index 12b3099..ff6835b 100644 (file)
@@ -74,11 +74,10 @@ public class PrintVertexDetails implements Runnable{
                        if (debugOn) {
                                // This is much slower, but sometimes we need to find out which single line is
                                // causing a failure
-                               try {
+                               try(FileOutputStream subFileStr = new FileOutputStream(fname)) {
                                        int okCount = 0;
                                        int failCount = 0;
                                        Long debugDelayMsL = new Long(debugDelayMs);
-                                       FileOutputStream subFileStr = new FileOutputStream(fname);
                                        
                                        GraphWriter graphWriter = null;
                                        if ("gryo".equalsIgnoreCase(snapshotType)) {
@@ -117,12 +116,10 @@ public class PrintVertexDetails implements Runnable{
                                                                        + "], aai-uri = [" + aaiUri + "]. ";
                                                        System.out.println(fmsg);
                                                        LOGGER.debug(" PrintVertexDetails " + fmsg);
-                                                       // e.printStackTrace();
                                                }
                                        }
                                        System.out.println(" -- Printed " + okCount + " vertexes out to " + fname + ", with " + failCount
                                                        + " failed.");
-                                       subFileStr.close();
                                } catch (Exception e) {
                                        AAIException ae = new AAIException("AAI_6128", e , "Error running PrintVertexDetails in debugon");
                                        ErrorLogHelper.logException(ae);
@@ -136,16 +133,14 @@ public class PrintVertexDetails implements Runnable{
                                        vtxList.add(gt.next());
                                }
                                
-                               try {
+                               try(FileOutputStream subFileStr = new FileOutputStream(fname)) {
                                        int count = vtxList.size();
                                        Iterator<Vertex> vSubItr = vtxList.iterator();
-                                       FileOutputStream subFileStr = new FileOutputStream(fname);
                                        if ("gryo".equalsIgnoreCase(snapshotType)) {
                                                jg.io(IoCore.gryo()).writer().create().writeVertices(subFileStr, vSubItr, Direction.BOTH);
                                        } else {
                                                jg.io(IoCore.graphson()).writer().create().writeVertices(subFileStr, vSubItr, Direction.BOTH);
                                        }
-                                       subFileStr.close();
                                        String pmsg = " -- Printed " + count + " vertexes out to " + fname;
                                        System.out.println(pmsg);
                                        LOGGER.debug(" PrintVertexDetails " + pmsg);
index e976bed..9c5ef4d 100644 (file)
@@ -153,7 +153,7 @@ public class AuditOXM extends Auditor {
                if (namespace == null) {
                        namespace = "";
                }
-               boolean isTopLevel = namespace != "";
+               boolean isTopLevel = !namespace.isEmpty();
                List<String> unique = Arrays.asList(uniqueProps.split(","));
                Set<String> indexed = temp.getIndexedProperties();
                Set<String> keys = temp.getKeys();
index 79b988d..91f62f9 100644 (file)
@@ -243,9 +243,9 @@ public class UpdatePropertyToolInternal {
 
         if (filePath != null && !filePath.isEmpty()) {
             // Add vertex Ids listed from the given file name
-            try {
+            try(BufferedReader br = new BufferedReader(new FileReader(filePath))) {
                 logAndPrint("Loading file at: " + filePath);
-                BufferedReader br = new BufferedReader(new FileReader(filePath));
+
                 StringBuilder sb = new StringBuilder();
                 String nextLine = br.readLine();
 
@@ -261,7 +261,6 @@ public class UpdatePropertyToolInternal {
                 }
                 String allVertexIdsString = sb.toString();
                 logAndPrint("All vertex IDs from file " + filePath + ":\n" + allVertexIdsString);
-                br.close();
             } catch (IOException ioe) {
                 logErrorAndPrint("ERROR reading in text file failed.", ioe);
             }
index 1089b2f..8d306c4 100644 (file)
@@ -70,7 +70,7 @@ public class DeletePInterface extends Migrator {
                                                if (cousins == null || cousins.isEmpty()) {
                                                        if (children == null || children.isEmpty()) {
                                                                logger.info("Delete p-interface: " + getVertexURI(pInterfV));
-                                                               pInterfV.remove();
+                                                               if(pInterfV != null) pInterfV.remove();
                                                                count++;
                                                        } else {
                                                                skipCount++;
index 819c7d4..39224ad 100644 (file)
@@ -92,7 +92,7 @@ public class MigrateDataFromASDCToConfiguration extends Migrator {
             logger.error("Found Exception" , a);
         } finally {
             try {
-                br.close();
+                if(br !=null) br.close();
             } catch (IOException e) {
                 success = false;
                 logger.error("Found Exception" , e);
index e05999d..a24d21a 100644 (file)
@@ -430,7 +430,7 @@ public class MigrateSAREvcInventory extends Migrator {
     private Vertex createNewConfigurationFromSARData(Map<String, String> sarColValues, Vertex forwardingPathVtx, int lineNumber) {
        
        Vertex configurationVtx = null;
-       String forwardingPathId = forwardingPathVtx.value(this.FOWARDING_PATH_ID);
+      String forwardingPathId = forwardingPathVtx !=null ? forwardingPathVtx.value(this.FOWARDING_PATH_ID) : "";
        try {
                
                List<Vertex> configList = g.V(forwardingPathVtx).out("org.onap.relationships.inventory.Uses").has("aai-node-type","configuration")
@@ -470,7 +470,7 @@ public class MigrateSAREvcInventory extends Migrator {
        try {
                Introspector evc = loader.introspectorFromName(EVC_NODE_TYPE);
                        evcVtx = serializer.createNewVertex(evc);
-                       evcId = configurationVtx.value(this.PROPERTY_CONFIGURATION_ID);
+                       evcId = configurationVtx != null ? configurationVtx.value(this.PROPERTY_CONFIGURATION_ID) : "";
                        
                        String cir = sarColValues.get("evcAccessCIR");
                        int length = cir.length();
index 7bc9a7d..1e0ef5e 100644 (file)
@@ -142,8 +142,8 @@ public class MigrateModelVer extends Migrator{
                 
                 String uri = String.format("/service-design-and-creation/models/model/%s/model-vers/model-ver/%s", currrentValueModelInvariantID, currentValueModelVersionID);
                 String propertyKey = NODETYPEKEYMAP.get(nodeTypeString);
-                String propertyValue = vertex.value(propertyKey).toString();
-                logger.info("Processing "+nodeTypeString+ " vertex with key "+vertex.value(propertyKey).toString());
+                String propertyValue = vertex != null ? vertex.value(propertyKey).toString() : "";
+                logger.info("Processing "+nodeTypeString+ " vertex with key "+ propertyValue);
                 Vertex modelVerVertex = null;
                 
                 if (modelVerUriVtxIdMap.containsKey(uri)){
index 61c488c..94b85b1 100644 (file)
@@ -70,7 +70,7 @@ public class MigratePserverAndPnfEquipType extends Migrator{
                        currentValueOfEquipType = getEquipTypeNodeValue(vertex);
                        hostName = getHostNameNodeValue(vertex);
                        if("Server".equals(currentValueOfEquipType) ||"server".equals(currentValueOfEquipType) ){
-                               vertex.property(EQUIP_TYPE_PROPERTY, "SERVER");
+                               if(vertex != null) vertex.property(EQUIP_TYPE_PROPERTY, "SERVER");
                                this.touchVertexProperties(vertex, false);
                                logger.info("changed Pserver equip-type from " + currentValueOfEquipType + " to SERVER having hostname : " + hostName);
                                pserverCount++;
@@ -89,7 +89,7 @@ public class MigratePserverAndPnfEquipType extends Migrator{
                        currentValueOfEquipType = getEquipTypeNodeValue(vertex);
                        pnfName = getPnfNameNodeValue(vertex);
                        if("Switch".equals(currentValueOfEquipType)||"switch".equals(currentValueOfEquipType)){
-                               vertex.property(EQUIP_TYPE_PROPERTY, "SWITCH");
+                               if(vertex != null) vertex.property(EQUIP_TYPE_PROPERTY, "SWITCH");
                                this.touchVertexProperties(vertex, false);
                                logger.info("changed Pnf equip-type from "+ currentValueOfEquipType +" to SWITCH having pnf-name :" + pnfName);
                                pnfCount++;
index 54ed12c..11f7bd5 100644 (file)
@@ -119,7 +119,7 @@ public class PserverDedupWithDifferentSourcesOfTruth extends EdgeSwingMigrator {
 
     @Override
     public Optional<String[]> getAffectedNodeTypes() {
-        return null;
+        return Optional.empty();
     }
 
     @Override
index 9bcd843..85573fc 100644 (file)
@@ -115,10 +115,10 @@ public abstract class GenericQueryProcessor {
                Map<String, Object> params = new HashMap<>();
                String query = "";
                 if (this.isGremlin) {
-                       query = gremlin.get();
+                       query = gremlin.isPresent() ? gremlin.get() : "";
                        
                }else if (this.isDsl) {
-                       String dslUserQuery = dsl.get();
+                       String dslUserQuery = dsl.isPresent() ? dsl.get() : "";
                        if(dslQueryProcessorOptional.isPresent()){
                                String dslQuery = dslQueryProcessorOptional.get().parseAaiQuery(dslUserQuery);
                                query = groovyQueryBuilder.executeTraversal(dbEngine, dslQuery, params);
index 8ef5139..20432e6 100644 (file)
@@ -73,11 +73,10 @@ public class UniquePropertyCheck {
                String propertyName = args[0];
                Graph graph = null;
                
-               try {   
+               try(JanusGraph tGraph = JanusGraphFactory.open(new AAIGraphConfig.Builder(AAIConstants.REALTIME_DB_CONFIG).forService(UniquePropertyCheck.class.getSimpleName()).withGraphType("realtime").buildConfiguration())) {
                AAIConfig.init();
                System.out.println("    ---- NOTE --- about to open graph (takes a little while)--------\n");
-               JanusGraph tGraph = JanusGraphFactory.open(new AAIGraphConfig.Builder(AAIConstants.REALTIME_DB_CONFIG).forService(UniquePropertyCheck.class.getSimpleName()).withGraphType("realtime").buildConfiguration());
-               
+
                if( tGraph == null ) {
                        logAndPrint(logger, " Error:  Could not get JanusGraph ");
                        System.exit(1);