Fix Blocker and Critical Sonar issues/bugs. 85/9585/1
authorDileep Ranganathan <dileep.ranganathan@intel.com>
Thu, 31 Aug 2017 02:36:12 +0000 (19:36 -0700)
committerDileep Ranganathan <dileep.ranganathan@intel.com>
Thu, 31 Aug 2017 03:22:02 +0000 (20:22 -0700)
Includes ad-hoc fixes for sonar issues within the files modified.

Change-Id: Ide181bbe3e05cbc969706a3d798cd99c5609a0b2
Issue-id: AAI-246
Signed-off-by: Dileep Ranganathan <dileep.ranganathan@intel.com>
aai-core/src/main/java/org/openecomp/aai/db/schema/ScriptDriver.java
aai-core/src/main/java/org/openecomp/aai/dbgen/DataGrooming.java
aai-core/src/main/java/org/openecomp/aai/dbmap/AAIGraph.java
aai-core/src/main/java/org/openecomp/aai/dmaap/AAIDmaapEventJMSConsumer.java
aai-core/src/main/java/org/openecomp/aai/parsers/uri/URIParser.java
aai-core/src/main/java/org/openecomp/aai/query/builder/GraphTraversalBuilder.java
aai-core/src/main/java/org/openecomp/aai/serialization/db/DBSerializer.java
aai-core/src/main/java/org/openecomp/aai/util/GenerateXsd.java
aai-core/src/main/java/org/openecomp/aai/util/swagger/GenerateSwagger.java

index 458c4c9..9a90ea2 100644 (file)
@@ -36,7 +36,7 @@ import com.thinkaurelius.titan.core.TitanGraph;
 
 public class ScriptDriver {
 
-       
+
        /**
         * The main method.
         *
@@ -46,7 +46,7 @@ public class ScriptDriver {
         * @throws JsonMappingException the json mapping exception
         * @throws IOException Signals that an I/O exception has occurred.
         */
-       public static void main (String[] args) throws AAIException, JsonGenerationException, JsonMappingException, IOException {
+       public static void main (String[] args) throws AAIException, IOException {
                CommandLineArgs cArgs = new CommandLineArgs();
                
                new JCommander(cArgs, args);
@@ -56,26 +56,24 @@ public class ScriptDriver {
                }
                String config = cArgs.config;
                AAIConfig.init();
-               TitanGraph graph = TitanFactory.open(config);
-               if (!(cArgs.type.equals("oxm") || cArgs.type.equals("graph"))) {
-                       System.out.println("type: " + cArgs.type + " not recognized.");
-                       System.exit(1);
-               }
-               
-               Auditor a = null;
-               if (cArgs.type.equals("oxm")) {
-                       a = AuditorFactory.getOXMAuditor(Version.v8);
-               } else if (cArgs.type.equals("graph")) {
-                       a = AuditorFactory.getGraphAuditor(graph);
+               try (TitanGraph graph = TitanFactory.open(config)) {
+                       if (!("oxm".equals(cArgs.type) || "graph".equals(cArgs.type))) {
+                               System.out.println("type: " + cArgs.type + " not recognized.");
+                               System.exit(1);
+                       }
+
+                       AuditDoc doc = null;
+                       if ("oxm".equals(cArgs.type)) {
+                               doc = AuditorFactory.getOXMAuditor(Version.v8).getAuditDoc();
+                       } else if ("graph".equals(cArgs.type)) {
+                               doc = AuditorFactory.getGraphAuditor(graph).getAuditDoc();
+                       }
+
+                       ObjectMapper mapper = new ObjectMapper();
+
+                       String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(doc);
+                       System.out.println(json);
                }
-               
-               AuditDoc doc = a.getAuditDoc();
-               
-               ObjectMapper mapper = new ObjectMapper();
-               
-               String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(doc);
-               System.out.println(json);
-               
        }
        
 }
index f0aaf60..8dca75c 100644 (file)
@@ -395,8 +395,7 @@ public class DataGrooming {
                        }
 
                        LOGGER.info(" Will write to " + fullOutputFileName );
-                       FileWriter fw = new FileWriter(groomOutFile.getAbsoluteFile());
-                       bw = new BufferedWriter(fw);
+                       bw = new BufferedWriter(new FileWriter(groomOutFile.getAbsoluteFile()));
                        ErrorLogHelper.loadProperties();
                        
                        LOGGER.info("    ---- NOTE --- about to open graph (takes a little while)--------\n");
@@ -1273,7 +1272,7 @@ public class DataGrooming {
                                        LOGGER.warn("Got an IOException trying to close bufferedWriter() \n", iox);
                                }
                        }
-                       
+
                        if (g != null && g.tx().isOpen()) {
                                // Any changes that worked correctly should have already done
                                // their commits.
@@ -1424,13 +1423,11 @@ public class DataGrooming {
                // Vertex Id's to delete on this run
                Set<String> delList = new LinkedHashSet<>();
                String fullFileName = targetDir + AAIConstants.AAI_FILESEP + fileName;
-               BufferedReader br = null;
-               
-               try {
-                       br = new BufferedReader(new FileReader(fullFileName));
+
+               try(BufferedReader br = new BufferedReader(new FileReader(fullFileName))) {
                        String line = br.readLine();
                        while (line != null) {
-                               if (!line.equals("") && line.startsWith("DeleteCandidate")) {
+                               if (!"".equals(line) && line.startsWith("DeleteCandidate")) {
                                        if (edgesOnlyFlag && (!line.contains("Bad Edge"))) {
                                                // We're not going to process edge guys
                                        } else if (dontFixOrphans && line.contains("Orphan")) {
@@ -1554,7 +1551,8 @@ public class DataGrooming {
                try {
                        keyProps = loader.introspectorFromName(vtxANodeType).getKeys();
                } catch (AAIUnknownObjectException e) {
-                       throw new AAIException("AAI_6105", "Required Property name(s) not found for nodeType = " + vtxANodeType + ")"); 
+                       LOGGER.warn("Required property not found", e);
+                       throw new AAIException("AAI_6105", "Required Property name(s) not found for nodeType = " + vtxANodeType + ")");
                }
                
                Iterator<String> keyPropI = keyProps.iterator();
@@ -2310,7 +2308,7 @@ public class DataGrooming {
                        return 0;
                }
                long unixTimeNow = System.currentTimeMillis();
-               long windowInMillis = timeWindowMinutes * 60 * 1000;
+               long windowInMillis = timeWindowMinutes * 60L * 1000;
                
                long startTimeStamp = unixTimeNow - windowInMillis;
                
index b3cc899..2ee8baf 100644 (file)
@@ -54,11 +54,12 @@ import com.thinkaurelius.titan.core.schema.TitanManagement;
  */
 public class AAIGraph {
 
-       protected Map<String, TitanGraph> graphs = new HashMap<>();
+       private static final EELFLogger logger = EELFManager.getInstance().getLogger(AAIGraph.class);
        protected static final String COMPONENT = "aaidbmap";
+       protected Map<String, TitanGraph> graphs = new HashMap<>();
        private final String REALTIME_DB = "realtime";
        private final String CACHED_DB = "cached";
-       private final EELFLogger logger = EELFManager.getInstance().getLogger(this.getClass().getSimpleName());
+
 
 
        /**
@@ -95,38 +96,22 @@ public class AAIGraph {
        }
        
        private void loadGraph(String name, String configPath) throws AAIException {
-               try {
-                       final TitanGraph graph = TitanFactory.open(configPath);
-                       InputStream is = new FileInputStream(configPath);
-               Properties graphProps = new Properties();
-               graphProps.load(is);
-               
-                       if (graphProps.get("storage.backend").equals("inmemory")) { 
+               try (TitanGraph graph = TitanFactory.open(configPath);
+                       InputStream is = new FileInputStream(configPath)) {
+
+                       Properties graphProps = new Properties();
+                       graphProps.load(is);
+
+                       if ("inmemory".equals(graphProps.get("storage.backend"))) {
                                // Load the propertyKeys, indexes and edge-Labels into the DB
                                loadSchema(graph);
-                               if (graphProps.containsKey("load.snapshot.file")) {
-                                       String value = graphProps.getProperty("load.snapshot.file");
-                                       if ("true".equals(value)) {
-                                               try {
-                                                       String location = System.getProperty("snapshot.location");
-                                                       logAndPrint(logger, "Loading snapshot to inmemory graph.");
-                                                       Graph transaction = graph.newTransaction();
-                                                       transaction.io(IoCore.graphson()).readGraph(location);
-                                                       transaction.tx().commit();
-                                                       logAndPrint(logger, "Snapshot loaded to inmemory graph.");
-                                               } catch (IOException e) {
-                                                       graph.close();
-                                                       logAndPrint(logger, "ERROR: Could not load datasnapshot to in memory graph. \n" + ExceptionUtils.getFullStackTrace(e));
-                                                       System.exit(0);
-                                               }
-                                       }
-                               }
+                               loadSnapShotToInMemoryGraph(graph, graphProps);
                        }
-                       
+
                        if (graph == null) {
                                throw new AAIException("AAI_5102");
                        }
-                       
+
                        graphs.put(name, graph);
                } catch (FileNotFoundException fnfe) {
                        throw new AAIException("AAI_4001");
@@ -135,7 +120,30 @@ public class AAIGraph {
 
            }
        }
-       
+
+       private void loadSnapShotToInMemoryGraph(TitanGraph graph, Properties graphProps) {
+               if (logger.isDebugEnabled()) {
+                       logger.debug("Load Snapshot to InMemory Graph");
+               }
+               if (graphProps.containsKey("load.snapshot.file")) {
+                       String value = graphProps.getProperty("load.snapshot.file");
+                       if ("true".equals(value)) {
+                               try (Graph transaction = graph.newTransaction()) {
+                                       String location = System.getProperty("snapshot.location");
+                                       logAndPrint(logger, "Loading snapshot to inmemory graph.");
+                                       transaction.io(IoCore.graphson()).readGraph(location);
+                                       transaction.tx().commit();
+                                       logAndPrint(logger, "Snapshot loaded to inmemory graph.");
+                               } catch (Exception e) {
+                                       logAndPrint(logger,
+                                               "ERROR: Could not load datasnapshot to in memory graph. \n"
+                                                       + ExceptionUtils.getFullStackTrace(e));
+                                       System.exit(0);
+                               }
+                       }
+               }
+       }
+
        private void loadSchema(TitanGraph graph) {
                // Load the propertyKeys, indexes and edge-Labels into the DB
                TitanManagement graphMgt = graph.openManagement();
index a30ebc0..8acb954 100644 (file)
@@ -52,13 +52,11 @@ public class AAIDmaapEventJMSConsumer implements MessageListener {
 
        public AAIDmaapEventJMSConsumer() throws org.apache.commons.configuration.ConfigurationException {
                super();
-               try {
+               try(FileReader reader = new FileReader(new File(AAIConstants.AAI_EVENT_DMAAP_PROPS))) {
 
                        if (this.httpClient == null) {
-                               FileReader reader = new FileReader(new File(AAIConstants.AAI_EVENT_DMAAP_PROPS));
                                aaiEventProps = new Properties();
                                aaiEventProps.load(reader);
-                               reader.close();
 
                                String host = aaiEventProps.getProperty("host");
                                String topic = aaiEventProps.getProperty("topic");
@@ -70,6 +68,7 @@ public class AAIDmaapEventJMSConsumer implements MessageListener {
 
                } catch (IOException e) {
                        ErrorLogHelper.logError("AAI_4000", "Error updating dmaap config file for aai event.");
+                       LOGGER.error(e.getMessage(), e);
                }
 
        }
index b4d5ee8..27a5145 100644 (file)
@@ -141,6 +141,9 @@ public class URIParser {
                                                throw new AAIException("AAI_3000", uri + " not a valid path. Cannot end in " + RestTokens.COUSIN);
                                        }
                                        introspector = loader.introspectorFromName(parts[i+1]);
+                                       if(null == previousObj) {
+                                               throw new AAIException("AAI_3001");
+                                       }
                                        if (previousObj.isContainer() && introspector.isContainer()) {
                                                throw new AAIException("AAI_3000", uri + " not a valid path. Cannot chain plurals together");
                                        }
index 73b57d0..d0a7098 100644 (file)
@@ -221,7 +221,7 @@ public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> {
                                                prop = metadata.get();
                                        }
                                        //this is because the index is registered as an Integer
-                                       if (val != null && val.getClass().equals(Long.class)) {
+                                       if (val.getClass().equals(Long.class)) {
                                                traversal.has(prop,new Integer(val.toString()));
                                        } else {
                                                traversal.has(prop, val);
index c04c460..bf70b69 100644 (file)
@@ -744,7 +744,7 @@ public class DBSerializer {
                                                                        Introspector argumentObject = obj.newIntrospectorInstanceOfNestedProperty(property);
                                                                        
                                                                        Object result = dbToObject(argumentObject, childVertex, seen, depth, nodeOnly, cleanUp);
-                                                                       if (result != null) { 
+                                                                       if (result != null && getList != null) {
                                                                                getList.add(argumentObject.getUnderlyingObject());
                                                                        }
                                                                        
index 76c954e..99a06f4 100644 (file)
@@ -1763,7 +1763,7 @@ public class GenerateXsd {
                                Object nodeset = expr.evaluate(element, XPathConstants.NODESET);
                                if (nodeset != null) {
                                        NodeList nodes = (NodeList) nodeset;
-                                       if (nodes != null && nodes.getLength() > 0) {
+                                       if (nodes.getLength() > 0) {
                                                Element xmlProperty = (Element)nodes.item(0);
                                                result = xmlProperty.getElementsByTagName("xml-property");
                                        }
index 07f201e..5731485 100644 (file)
@@ -76,6 +76,10 @@ public class GenerateSwagger {
             ex.printStackTrace();
         }
 
+        if(null == swaggerMap) {
+            throw new IOException();
+        }
+
         Map<String, Object> map = (Map<String, Object>) swaggerMap.get("paths");
         Map<String, Object> schemaDefinitionmap = (Map<String, Object>) swaggerMap.get("definitions");
         Map<String, Object> infoMap = (Map<String, Object>) swaggerMap.get("info");