General Sonar fixes 19/94319/6
authorArindam Mondal <arind.mondal@samsung.com>
Tue, 27 Aug 2019 04:47:44 +0000 (13:47 +0900)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Tue, 27 Aug 2019 12:48:28 +0000 (12:48 +0000)
+) Replace System.out with logger
++) Use try-with-resources to close the resource
+++) Format to delete tab charachter
++++) flip equals to avoid potential NPE

Issue-ID: SDC-2542
Change-Id: If57f36072ec67e9622d4fe51bce8336094dc10c8
Signed-off-by: arindamm <arind.mondal@samsung.com>
asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLConverter.java
asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/GraphMLDataAnalyzer.java

index ee33c82..a7c6d38 100644 (file)
@@ -7,9 +7,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 package org.openecomp.sdc.asdctool.impl;
 
 import com.google.gson.Gson;
-import org.janusgraph.core.JanusGraphEdge;
-import org.janusgraph.core.JanusGraphFactory;
-import org.janusgraph.core.JanusGraph;
-import org.janusgraph.core.JanusGraphQuery;
-import org.janusgraph.core.JanusGraphVertex;
-import org.apache.commons.configuration.BaseConfiguration;
-import org.apache.commons.lang3.tuple.ImmutablePair;
-import org.apache.tinkerpop.gremlin.structure.*;
-import org.apache.tinkerpop.gremlin.structure.io.IoCore;
-import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper;
-import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONReader;
-import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter;
-import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
-import org.openecomp.sdc.asdctool.Utils;
-import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
-import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
-import org.openecomp.sdc.common.log.wrappers.Logger;
-
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.FileWriter;
-import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.ArrayList;
@@ -54,17 +35,31 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import org.apache.commons.configuration.BaseConfiguration;
+import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.tinkerpop.gremlin.structure.Element;
+import org.apache.tinkerpop.gremlin.structure.Graph;
+import org.apache.tinkerpop.gremlin.structure.Property;
+import org.apache.tinkerpop.gremlin.structure.Vertex;
+import org.apache.tinkerpop.gremlin.structure.io.IoCore;
+import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper;
+import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONReader;
+import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter;
+import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
+import org.janusgraph.core.JanusGraph;
+import org.janusgraph.core.JanusGraphFactory;
+import org.janusgraph.core.JanusGraphQuery;
+import org.janusgraph.core.JanusGraphVertex;
+import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
+import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
+import org.openecomp.sdc.common.log.wrappers.Logger;
 
 public class GraphMLConverter {
 
-       private static final String FROM_VERTEX = "fromVertex={}";
-
     private static final String STORAGE_BACKEND = "storage.backend";
 
     private static final String INMEMORY = "inmemory";
 
-    private static final String CLOSE_FILE_OUTPUT_STREAM_FAILED = "close FileOutputStream failed - {}";
-
     private static final String EXPORT_GRAPH = "exportGraph.";
 
     private static final String DOT_JSON = ".json";
@@ -75,491 +70,340 @@ public class GraphMLConverter {
 
     private static Logger log = Logger.getLogger(GraphMLConverter.class.getName());
 
-       private Gson gson = new Gson();
-
-       public boolean importGraph(String[] args) {
-
-               JanusGraph graph = null;
-               try {
-                       String janusGraphFileLocation = args[1];
-                       String inputFile = args[2];
-                       graph = openGraph(janusGraphFileLocation);
-
-                       List<ImmutablePair<String, String>> propertiesCriteriaToDelete = new ArrayList<>();
-                       ImmutablePair<String, String> immutablePair1 = new ImmutablePair<>("healthcheckis", "GOOD");
-                       ImmutablePair<String, String> immutablePair2 = new ImmutablePair<>(NODE_LABEL, "user");
-                       ImmutablePair<String, String> immutablePair3 = new ImmutablePair<>(NODE_LABEL,
-                                       "resourceCategory");
-                       ImmutablePair<String, String> immutablePair4 = new ImmutablePair<>(NODE_LABEL,
-                                       "serviceCategory");
-
-                       propertiesCriteriaToDelete.add(immutablePair1);
-                       propertiesCriteriaToDelete.add(immutablePair2);
-                       propertiesCriteriaToDelete.add(immutablePair3);
-                       propertiesCriteriaToDelete.add(immutablePair4);
-
-                       return importJsonGraph(graph, inputFile, propertiesCriteriaToDelete);
-
-               } catch (Exception e) {
-                       log.info("import graph failed - {} " , e);
-                       return false;
-               } finally {
-                       if (graph != null) {
-                               graph.close();
-                       }
-               }
-
-       }
-
-       public boolean exportGraph(String[] args) {
-
-               JanusGraph graph = null;
-               try {
-                       String janusGraphFileLocation = args[1];
-                       String outputDirectory = args[2];
-                       graph = openGraph(janusGraphFileLocation);
-
-                       String result = exportJsonGraph(graph, outputDirectory);
-
-                       if (result == null) {
-                               return false;
-                       }
-
-                       System.out.println(EXPORTED_FILE + result);
-               } catch (Exception e) {
-                       log.info("export graph failed -{}" , e);
-                       return false;
-               } finally {
-                       if (graph != null) {
-                               graph.close();
-                       }
-               }
-
-               return true;
-       }
-
-       public String exportGraphMl(String[] args) {
-
-               JanusGraph graph = null;
-               String result = null;
-               try {
-                       String janusGraphFileLocation = args[1];
-                       String outputDirectory = args[2];
-                       graph = openGraph(janusGraphFileLocation);
-
-                       result = exportGraphMl(graph, outputDirectory);
-
-                       System.out.println(EXPORTED_FILE + result);
-               } catch (Exception e) {
-                       log.info("export exportGraphMl failed - {}" , e);
-                       return null;
-               } finally {
-                       if (graph != null) {
-                               graph.close();
-                       }
-               }
-
-               return result;
-       }
-
-       public boolean findErrorInJsonGraph(String[] args) {
-
-               JanusGraph graph = null;
-               try {
-                       String janusGraphFileLocation = args[1];
-                       String outputDirectory = args[2];
-                       graph = openGraph(janusGraphFileLocation);
-
-                       String result = findErrorInJsonGraph(graph, outputDirectory);
-
-                       if (result == null) {
-                               return false;
-                       }
-
-                       System.out.println(EXPORTED_FILE + result);
-               } catch (Exception e) {
-                       log.info("find Error In Json Graph failed - {}" , e);
-                       return false;
-               } finally {
-                       if (graph != null) {
-                               graph.close();
-                       }
-               }
-
-               return true;
-       }
-
-       public JanusGraph openGraph(String janusGraphFileLocation) {
-
-               return JanusGraphFactory.open(janusGraphFileLocation);
-
-       }
-
-       public String exportJsonGraph(JanusGraph graph, String outputDirectory) {
-
-               String result = null;
-
-               String outputFile = outputDirectory + File.separator + EXPORT_GRAPH + System.currentTimeMillis() + DOT_JSON;
-
-               OutputStream out = null;
-               try {
-                       out = new BufferedOutputStream(new FileOutputStream(outputFile));
-
-                       final GraphSONWriter.Builder builder = GraphSONWriter.build();
-                       final GraphSONMapper mapper = newGraphSONMapper(graph);
-                       builder.mapper(mapper);
-                       final GraphSONWriter writer = builder.create();
-                       writer.writeGraph(out, graph);
-
-                       graph.tx().commit();
-
-                       result = outputFile;
-
-               } catch (Exception e) {
-                       log.info("export Json Graph failed - {}" , e);
-                       graph.tx().rollback();
-               } finally {
-                       try {
-                               if (out != null) {
-                                       out.close();
-                               }
-                       } catch (IOException e) {
-                               log.info(CLOSE_FILE_OUTPUT_STREAM_FAILED , e);
-                       }
-               }
-               return result;
-
-       }
-
-       public String exportGraphMl(JanusGraph graph, String outputDirectory) {
-               String result = null;
-               String outputFile = outputDirectory + File.separator + EXPORT_GRAPH + System.currentTimeMillis() + ".graphml";
-               try {
-                       try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile))) {
-                               graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(os, graph);
-                       }
-                       result = outputFile;
-                       graph.tx().commit();
-               } catch (Exception e) {
-                       graph.tx().rollback();
-                       log.info("export Graph Ml failed - {}" , e);
-               }
-               return result;
-
-       }
-
-       private static GraphSONMapper newGraphSONMapper(final Graph graph) {
-               final GraphSONMapper.Builder builder = graph.io(IoCore.graphson()).mapper();
-               return builder.create();
-       }
-
-       public boolean importJsonGraph(JanusGraph graph, String graphJsonFile,
-                       List<ImmutablePair<String, String>> propertiesCriteriaToDelete) {
-
-               boolean result = false;
-
-               InputStream is = null;
-
-               try {
+    private Gson gson = new Gson();
+
+    public boolean importGraph(String[] args) {
+
+        JanusGraph graph = null;
+        try {
+            String janusGraphFileLocation = args[1];
+            String inputFile = args[2];
+            graph = openGraph(janusGraphFileLocation);
+
+            List<ImmutablePair<String, String>> propertiesCriteriaToDelete = new ArrayList<>();
+            ImmutablePair<String, String> immutablePair1 = new ImmutablePair<>("healthcheckis", "GOOD");
+            ImmutablePair<String, String> immutablePair2 = new ImmutablePair<>(NODE_LABEL, "user");
+            ImmutablePair<String, String> immutablePair3 = new ImmutablePair<>(NODE_LABEL, "resourceCategory");
+            ImmutablePair<String, String> immutablePair4 = new ImmutablePair<>(NODE_LABEL, "serviceCategory");
+
+            propertiesCriteriaToDelete.add(immutablePair1);
+            propertiesCriteriaToDelete.add(immutablePair2);
+            propertiesCriteriaToDelete.add(immutablePair3);
+            propertiesCriteriaToDelete.add(immutablePair4);
 
-                       if (propertiesCriteriaToDelete != null) {
-                               for (Entry<String, String> entry : propertiesCriteriaToDelete
+            return importJsonGraph(graph, inputFile, propertiesCriteriaToDelete);
 
-                               ) {
+        } catch (Exception e) {
+            log.info("import graph failed ", e);
+            return false;
+        } finally {
+            if (graph != null) {
+                graph.close();
+            }
+        }
 
-                                       String key = entry.getKey();
-                                       String value = entry.getValue();
-                                       Iterator iterator = graph.query().has(key, value).vertices().iterator();
-                                       while (iterator.hasNext()) {
-                                               Vertex vertex = (Vertex) iterator.next();
-                                               vertex.remove();
-                                               System.out.println("Remove vertex of type " + key + " and value " + value);
-                                       }
+    }
 
-                               }
-                       }
-                       File file = new File(graphJsonFile);
-                       if (!file.isFile()) {
-                               System.out.println("File " + graphJsonFile + " cannot be found.");
-                               return result;
-                       }
+    public boolean exportGraph(String[] args) {
 
-                       is = new BufferedInputStream(new FileInputStream(graphJsonFile));
-                       System.out.println("Before importing file " + graphJsonFile);
+        JanusGraph graph = null;
+        try {
+            String janusGraphFileLocation = args[1];
+            String outputDirectory = args[2];
+            graph = openGraph(janusGraphFileLocation);
 
-                       GraphSONReader create = GraphSONReader.build().create();
-                       create.readGraph(is, graph);
+            String result = exportJsonGraph(graph, outputDirectory);
 
-                       graph.tx().commit();
+            if (result == null) {
+                return false;
+            }
 
-                       result = true;
+            log.info("{} {}", EXPORTED_FILE, result);
+        } catch (Exception e) {
+            log.info("export graph failed ", e);
+            return false;
+        } finally {
+            if (graph != null) {
+                graph.close();
+            }
+        }
+
+        return true;
+    }
+
+    public String exportGraphMl(String[] args) {
+
+        JanusGraph graph = null;
+        String result = null;
+        try {
+            String janusGraphFileLocation = args[1];
+            String outputDirectory = args[2];
+            graph = openGraph(janusGraphFileLocation);
 
-               } catch (Exception e) {
-                       System.out.println("Failed to import graph " + e.getMessage());
-                       log.info("Failed to import graph - {}" , e);
-                       graph.tx().rollback();
-               } finally {
-                       try {
-                               if (is != null) {
-                                       is.close();
-                               }
-                       } catch (IOException e) {
-                               log.info(CLOSE_FILE_OUTPUT_STREAM_FAILED , e);
-                       }
-               }
+            result = exportGraphMl(graph, outputDirectory);
 
-               return result;
+            log.info("{} {}", EXPORTED_FILE, result);
+        } catch (Exception e) {
+            log.info("export exportGraphMl failed ", e);
+            return null;
+        } finally {
+            if (graph != null) {
+                graph.close();
+            }
+        }
 
-       }
+        return result;
+    }
+
+    public boolean findErrorInJsonGraph(String[] args) {
+
+        JanusGraph graph = null;
+        try {
+            String janusGraphFileLocation = args[1];
+            String outputDirectory = args[2];
+            graph = openGraph(janusGraphFileLocation);
+
+            String result = findErrorInJsonGraph(graph, outputDirectory);
+
+            if (result == null) {
+                return false;
+            }
+
+            log.info("{} {}", EXPORTED_FILE, result);
+        } catch (Exception e) {
+            log.info("find Error In Json Graph failed ", e);
+            return false;
+        } finally {
+            if (graph != null) {
+                graph.close();
+            }
+        }
+
+        return true;
+    }
+
+    public JanusGraph openGraph(String janusGraphFileLocation) {
 
-       public String findErrorInJsonGraph(JanusGraph graph, String outputDirectory) {
+        return JanusGraphFactory.open(janusGraphFileLocation);
 
-               boolean runVertexScan = false;
-               boolean runEdgeScan = false;
+    }
 
-               String result = null;
+    public String exportJsonGraph(JanusGraph graph, String outputDirectory) {
 
-               String outputFile = outputDirectory + File.separator + EXPORT_GRAPH + System.currentTimeMillis() + DOT_JSON;
+        String result = null;
 
-               OutputStream out = null;
-               try {
-                       out = new BufferedOutputStream(new FileOutputStream(outputFile));
+        String outputFile = outputDirectory + File.separator + EXPORT_GRAPH + System.currentTimeMillis() + DOT_JSON;
 
-                       if (runEdgeScan) {
+        try (final OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile))) {
 
-                               Vertex vertexFrom = null;
-                               Vertex vertexTo = null;
-                               Edge edge = null;
+            final GraphSONWriter.Builder builder = GraphSONWriter.build();
+            final GraphSONMapper mapper = newGraphSONMapper(graph);
+            builder.mapper(mapper);
+            final GraphSONWriter writer = builder.create();
+            writer.writeGraph(out, graph);
 
-                               Iterable<JanusGraphEdge> edges = graph.query().edges();
-                               Iterator<JanusGraphEdge> iterator = edges.iterator();
-                               while (iterator.hasNext()) {
+            graph.tx().commit();
 
-                                       try {
+            result = outputFile;
 
-                                               edge = iterator.next();
+        } catch (Exception e) {
+            log.info("export Json Graph failed ", e);
+            graph.tx().rollback();
+        }
+        return result;
 
-                                               vertexFrom = edge.outVertex();
-                                               vertexTo = edge.inVertex();
+    }
 
-                                               BaseConfiguration conf = new BaseConfiguration();
-                                               conf.setProperty(STORAGE_BACKEND, INMEMORY);
-                                               JanusGraph openGraph = Utils.openGraph(conf);
+    public String exportGraphMl(JanusGraph graph, String outputDirectory) {
+        String result = null;
+        String outputFile =
+            outputDirectory + File.separator + EXPORT_GRAPH + System.currentTimeMillis() + ".graphml";
+        try {
+            try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile))) {
+                graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(os, graph);
+            }
+            result = outputFile;
+            graph.tx().commit();
+        } catch (Exception e) {
+            graph.tx().rollback();
+            log.info("export Graph Ml failed ", e);
+        }
+        return result;
 
-                                               JanusGraphVertex addVertexFrom = openGraph.addVertex();
-                                               Utils.setProperties(addVertexFrom, Utils.getProperties(vertexFrom));
+    }
 
-                                               JanusGraphVertex addVertexTo = openGraph.addVertex();
-                                               Utils.setProperties(addVertexTo, Utils.getProperties(vertexTo));
+    private static GraphSONMapper newGraphSONMapper(final Graph graph) {
+        final GraphSONMapper.Builder builder = graph.io(IoCore.graphson()).mapper();
+        return builder.create();
+    }
 
-                                               Edge addEdge = addVertexFrom.addEdge(edge.label(), addVertexTo);
-                                               Utils.setProperties(addEdge, Utils.getProperties(edge));
+    public boolean importJsonGraph(JanusGraph graph, String graphJsonFile,
+                                   List<ImmutablePair<String, String>> propertiesCriteriaToDelete) {
 
-                                               log.info(FROM_VERTEX, Utils.getProperties(vertexFrom));
-                                               log.info("toVertex={}", Utils.getProperties(vertexTo));
-                                               log.info("edge={} {} ",edge.label(),Utils.getProperties(edge));
+        boolean result = false;
 
-                                               GraphSONWriter create = GraphSONWriter.build().create();
-                                               create.writeGraph(out, openGraph);
+        if (propertiesCriteriaToDelete != null) {
+            for (Entry<String, String> entry : propertiesCriteriaToDelete
 
-                                               openGraph.tx().rollback();
+            ) {
 
-                                       } catch (Exception e) {
-                                               log.info("run Edge Scan failed - {}" , e);
+                String key = entry.getKey();
+                String value = entry.getValue();
+                Iterator iterator = graph.query().has(key, value).vertices().iterator();
+                while (iterator.hasNext()) {
+                    Vertex vertex = (Vertex) iterator.next();
+                    vertex.remove();
+                    log.info("Remove vertex of type{} ", key, " and value {}", value);
+                }
 
-                                               log.error(FROM_VERTEX, Utils.getProperties(vertexFrom));
-                                               log.error("toVertex={}", Utils.getProperties(vertexTo));
-                                               log.error("edge={} {} ",edge.label(),Utils.getProperties(edge));
+            }
+        }
+        File file = new File(graphJsonFile);
+        if (!file.isFile()) {
+            log.info("File ", graphJsonFile, " cannot be found.");
+            return result;
+        }
 
-                                               break;
+        try (final InputStream is = new BufferedInputStream(new FileInputStream(graphJsonFile))) {
 
-                                       }
-                               }
+            log.info("Before importing file ", graphJsonFile);
 
-                               graph.tx().rollback();
+            GraphSONReader create = GraphSONReader.build().create();
+            create.readGraph(is, graph);
 
-                       }
+            graph.tx().commit();
 
-                       if (runVertexScan) {
+            result = true;
 
-                               Vertex vertex = null;
-                               Iterator<Vertex> iteratorVertex = graph.vertices();
-                               while (iteratorVertex.hasNext()) {
+        } catch (Exception e) {
+            log.info("Failed to import graph ", e);
+            graph.tx().rollback();
+        }
+        return result;
 
-                                       try {
+    }
 
-                                               vertex = iteratorVertex.next();
-                                               Iterator<Edge> iterator2 = vertex.edges(Direction.BOTH);
-                                               if (!iterator2.hasNext()) {
+    public String findErrorInJsonGraph(JanusGraph graph, String outputDirectory) {
 
-                                                       BaseConfiguration conf = new BaseConfiguration();
-                                                       conf.setProperty(STORAGE_BACKEND, INMEMORY);
-                                                       JanusGraph openGraph = Utils.openGraph(conf);
+        String result = null;
 
-                                                       JanusGraphVertex addVertexFrom = openGraph.addVertex();
-                                                       Utils.setProperties(addVertexFrom, Utils.getProperties(vertex));
+        String outputFile = outputDirectory + File.separator + EXPORT_GRAPH + System.currentTimeMillis() + DOT_JSON;
 
-                                                       log.info(FROM_VERTEX, Utils.getProperties(addVertexFrom));
+        try (final OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile))) {
 
-                                                       GraphSONWriter create = GraphSONWriter.build().create();
-                                                       create.writeGraph(out, openGraph);
+            graph.query().has(GraphPropertiesDictionary.HEALTH_CHECK.getProperty(), "GOOD").vertices();
 
-                                                       openGraph.tx().rollback();
+            BaseConfiguration conf = new BaseConfiguration();
+            conf.setProperty(STORAGE_BACKEND, INMEMORY);
+            for (NodeTypeEnum nodeTypeEnum : NodeTypeEnum.values()) {
+                removeNodesByLabel(graph, nodeTypeEnum.getName());
+            }
 
-                                               }
+            GraphSONWriter create = GraphSONWriter.build().create();
+            create.writeGraph(out, graph);
 
-                                       } catch (Exception e) {
-                                               log.info("run Vertex Scan failed - {}" , e);
+            graph.tx().rollback();
 
-                                               Object property1 = vertex.value(GraphPropertiesDictionary.HEALTH_CHECK.getProperty());
-                                               System.out.println(property1);
+        } catch (Exception e) {
+            log.info("find Error In Json Graph failed ", e);
+            graph.tx().rollback();
+        }
+        return result;
 
-                                               Object property2 = vertex.value("healthcheck");
-                                               System.out.println(property2);
+    }
 
-                                               break;
+    private void removeNodesByLabel(JanusGraph graph, String label) {
+        Iterable<JanusGraphVertex> vertices =
+            graph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), label).vertices();
+        Iterator<JanusGraphVertex> iterator = vertices.iterator();
+        while (iterator.hasNext()) {
+            Vertex next2 = iterator.next();
+            next2.remove();
+        }
+    }
 
-                                       }
-                               }
+    public String exportUsers(JanusGraph graph, String outputDirectory) {
 
-                               graph.tx().rollback();
+        List<Map<String, Object>> users = new ArrayList<>();
+        String result = null;
 
-                       }
+        String outputFile = outputDirectory + File.separator + "users." + System.currentTimeMillis() + DOT_JSON;
 
-                       Iterable<JanusGraphVertex> vertices2 = graph.query()
-                                       .has(GraphPropertiesDictionary.HEALTH_CHECK.getProperty(), "GOOD").vertices();
-                       ;
+        JanusGraphQuery graphQuery =
+            graph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.User.getName());
 
-                       BaseConfiguration conf = new BaseConfiguration();
-                       conf.setProperty(STORAGE_BACKEND, INMEMORY);
-                       for (NodeTypeEnum nodeTypeEnum : NodeTypeEnum.values()) {
-                               removeNodesByLabel(graph, nodeTypeEnum.getName());
-                       }
+        @SuppressWarnings("unchecked")
+        Iterable<JanusGraphVertex> vertices = graphQuery.vertices();
 
+        if (vertices != null) {
+            for (Vertex v : vertices) {
+                Map<String, Object> properties = getProperties(v);
+                properties.remove(GraphPropertiesDictionary.LABEL.getProperty());
+                users.add(properties);
+            }
+        }
 
-                       GraphSONWriter create = GraphSONWriter.build().create();
-                       create.writeGraph(out, graph);
+        graph.tx().commit();
 
-                       graph.tx().rollback();
+        String jsonUsers = gson.toJson(users);
 
-               } catch (Exception e) {
-                       log.info("find Error In Json Graph failed - {}" , e);
-                       graph.tx().rollback();
-               } finally {
-                       try {
-                               if (out != null) {
-                                       out.close();
-                               }
-                       } catch (IOException e) {
-                               log.info(CLOSE_FILE_OUTPUT_STREAM_FAILED , e);
-                       }
-               }
-               return result;
+        try (final FileWriter fileWriter = new FileWriter(outputFile)) {
 
-       }
+            fileWriter.write(jsonUsers);
 
-       private void removeNodesByLabel(JanusGraph graph, String label) {
-               Iterable<JanusGraphVertex> vertices = graph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), label)
-                               .vertices();
-               Iterator<JanusGraphVertex> iterator = vertices.iterator();
-               while (iterator.hasNext()) {
-                       Vertex next2 = iterator.next();
-                       next2.remove();
-               }
-       }
+            result = outputFile;
 
-       public String exportUsers(JanusGraph graph, String outputDirectory) {
+        } catch (Exception e) {
+            log.info("export Users failed ", e);
+            graph.tx().rollback();
+        }
+        return result;
 
-               List<Map<String, Object>> users = new ArrayList<>();
-               String result = null;
+    }
 
-               String outputFile = outputDirectory + File.separator + "users." + System.currentTimeMillis() + DOT_JSON;
+    public Map<String, Object> getProperties(Element element) {
 
-               FileWriter fileWriter = null;
-               try {
+        Map<String, Object> result = new HashMap<>();
+        ;
 
-                       JanusGraphQuery graphQuery = graph.query().has(GraphPropertiesDictionary.LABEL.getProperty(),
-                                       NodeTypeEnum.User.getName());
+        if (element.keys() != null && !element.keys().isEmpty()) {
+            Map<String, Property> propertyMap =
+                ElementHelper.propertyMap(element, element.keys().toArray(new String[element.keys().size()]));
 
-                       @SuppressWarnings("unchecked")
-                       Iterable<JanusGraphVertex> vertices = graphQuery.vertices();
+            for (Entry<String, Property> entry : propertyMap.entrySet()) {
+                String key = entry.getKey();
+                Object value = entry.getValue().value();
 
-                       if (vertices != null) {
-                               for (Vertex v : vertices) {
-                                       Map<String, Object> properties = getProperties(v);
-                                       properties.remove(GraphPropertiesDictionary.LABEL.getProperty());
-                                       users.add(properties);
-                               }
-                       }
+                result.put(key, value);
+            }
+        }
+        return result;
+    }
 
-                       graph.tx().commit();
+    public boolean exportUsers(String[] args) {
 
-                       String jsonUsers = gson.toJson(users);
+        JanusGraph graph = null;
+        try {
+            String janusGraphFileLocation = args[1];
+            String outputDirectory = args[2];
+            graph = openGraph(janusGraphFileLocation);
 
-                       fileWriter = new FileWriter(outputFile);
-                       fileWriter.write(jsonUsers);
+            String result = exportUsers(graph, outputDirectory);
 
-                       result = outputFile;
+            if (result == null) {
+                return false;
+            }
 
-               } catch (Exception e) {
-                       log.info("export Users failed - {}" , e);
-                       graph.tx().rollback();
-               } finally {
-                       try {
-                               if (fileWriter != null) {
-                                       fileWriter.close();
-                               }
-                       } catch (IOException e) {
-                               log.info(CLOSE_FILE_OUTPUT_STREAM_FAILED , e);
-                       }
-               }
-               return result;
-
-       }
+            log.info(EXPORTED_FILE, result);
+        } catch (Exception e) {
+            log.info("export Users failed ", e);
+            return false;
+        } finally {
+            if (graph != null) {
+                graph.close();
+            }
+        }
 
-       public Map<String, Object> getProperties(Element element) {
-
-               Map<String, Object> result = new HashMap<>();
-               ;
-
-               if (element.keys() != null && !element.keys().isEmpty()) {
-                       Map<String, Property> propertyMap = ElementHelper.propertyMap(element,
-                                       element.keys().toArray(new String[element.keys().size()]));
-
-                       for (Entry<String, Property> entry : propertyMap.entrySet()) {
-                               String key = entry.getKey();
-                               Object value = entry.getValue().value();
-
-                               result.put(key, value);
-                       }
-               }
-               return result;
-       }
-
-       public boolean exportUsers(String[] args) {
-
-               JanusGraph graph = null;
-               try {
-                       String janusGraphFileLocation = args[1];
-                       String outputDirectory = args[2];
-                       graph = openGraph(janusGraphFileLocation);
-
-                       String result = exportUsers(graph, outputDirectory);
-
-                       if (result == null) {
-                               return false;
-                       }
-
-                       System.out.println(EXPORTED_FILE + result);
-               } catch (Exception e) {
-                       log.info("export Users failed - {}" , e);
-                       return false;
-               } finally {
-                       if (graph != null) {
-                               graph.close();
-                       }
-               }
-
-               return true;
-       }
+        return true;
+    }
 }
index 1b065c2..8537092 100644 (file)
@@ -7,9 +7,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
 package org.openecomp.sdc.asdctool.impl;
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
@@ -32,213 +38,207 @@ import org.jdom2.util.IteratorIterable;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.io.File;
-import java.io.FileOutputStream;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
 public class GraphMLDataAnalyzer {
 
-       private static Logger log = LoggerFactory.getLogger(GraphMLDataAnalyzer.class);
-
-       private static final String[] COMPONENT_SHEET_HEADER = { "uniqueId", "type", "name", "toscaResourceName",
-                       "resourceType", "version", "deleted", "hasNonCalculatedReqCap" };
-       private static final String[] COMPONENT_INSTANCES_SHEET_HEADER = { "uniqueId", "name", "originUid", "originType",
-                       "containerUid" };
-
-       public String analyzeGraphMLData(String[] args) {
-               String result = null;
-               try {
-                       String mlFileLocation = args[0];
-                       result = _analyzeGraphMLData(mlFileLocation);
-                       log.info("Analyzed ML file=" + mlFileLocation + ", XLS result=" + result);
-               } catch (Exception e) {
-                       log.error("analyze GraphML Data failed - {}" , e);
-                       return null;
-               }
-               return result;
-       }
-
-       private String _analyzeGraphMLData(String mlFileLocation) throws Exception {
-               // Parse ML file
-               SAXBuilder builder = new SAXBuilder();
-               File xmlFile = new File(mlFileLocation);
-               Document document = builder.build(xmlFile);
-               
-               // XLS data file name
-               String outputFile = mlFileLocation.replace(".graphml", ".xls");
-               Workbook wb = new HSSFWorkbook();
-               try(FileOutputStream fileOut = new FileOutputStream(outputFile)){
-                       writeComponents(wb, document);
-                       writeComponentInstances(wb, document);
-                       wb.write(fileOut);
-               }catch(Exception e){
-                       log.error("analyze GraphML Data failed - {}" , e);
-               }
-               return outputFile;      
-       }
-
-       private void writeComponents(Workbook wb, Document document) {
-               Sheet componentsSheet = wb.createSheet("Components");
-               Row currentRow = componentsSheet.createRow(0);
-               for (int i = 0; i < COMPONENT_SHEET_HEADER.length; i++) {
-                       currentRow.createCell(i).setCellValue(COMPONENT_SHEET_HEADER[i]);
-               }
-
-               List<ComponentRow> components = getComponents(document);
-               int rowNum = 1;
-               for (ComponentRow row : components) {
-                       currentRow = componentsSheet.createRow(rowNum++);
-                       currentRow.createCell(0).setCellValue(row.getUniqueId());
-                       currentRow.createCell(1).setCellValue(row.getType());
-                       currentRow.createCell(2).setCellValue(row.getName());
-                       currentRow.createCell(3).setCellValue(row.getToscaResourceName());
-                       currentRow.createCell(4).setCellValue(row.getResourceType());
-                       currentRow.createCell(5).setCellValue(row.getVersion());
-                       currentRow.createCell(6).setCellValue(row.getIsDeleted() != null ? row.getIsDeleted().toString() : "false");
-                       currentRow.createCell(7).setCellValue(row.getHasNonCalculatedReqCap());
-               }
-       }
-
-       private void writeComponentInstances(Workbook wb, Document document) {
-               Sheet componentsSheet = wb.createSheet("ComponentInstances");
-               Row currentRow = componentsSheet.createRow(0);
-               for (int i = 0; i < COMPONENT_INSTANCES_SHEET_HEADER.length; i++) {
-                       currentRow.createCell(i).setCellValue(COMPONENT_INSTANCES_SHEET_HEADER[i]);
-               }
-               List<ComponentInstanceRow> components = getComponentInstances(document);
-               int rowNum = 1;
-               for (ComponentInstanceRow row : components) {
-                       currentRow = componentsSheet.createRow(rowNum++);
-                       currentRow.createCell(0).setCellValue(row.getUniqueId());
-                       currentRow.createCell(1).setCellValue(row.getName());
-                       currentRow.createCell(2).setCellValue(row.getOriginUid());
-                       currentRow.createCell(3).setCellValue(row.getOriginType());
-                       currentRow.createCell(4).setCellValue(row.getContainerUid());
-               }
-       }
-
-       private List<ComponentRow> getComponents(Document document) {
-               List<ComponentRow> res = new ArrayList<>();
-               Element root = document.getRootElement();
-               ElementFilter filter = new ElementFilter("graph");
-               Element graph = root.getDescendants(filter).next();
-               filter = new ElementFilter("edge");
-               IteratorIterable<Element> edges = graph.getDescendants(filter);
-               Set<String> componentsHavingReqOrCap = new HashSet<>();
-               filter = new ElementFilter("data");
-               for (Element edge : edges) {
-                       IteratorIterable<Element> dataNodes = edge.getDescendants(filter);
-                       for (Element data : dataNodes) {
-                               String attributeValue = data.getAttributeValue("key");
-                               if( attributeValue.equals("labelE")) {
-                                       String edgeLabel = data.getText();
-                                       if (edgeLabel.equals("REQUIREMENT") || edgeLabel.equals("CAPABILITY")) {
-                                               componentsHavingReqOrCap.add(edge.getAttributeValue("source"));
-                                       }
-                               }
-                       }
-               }
-
-               filter = new ElementFilter("node");
-               IteratorIterable<Element> nodes = graph.getDescendants(filter);
-               filter = new ElementFilter("data");
-               for (Element element : nodes) {
-                       IteratorIterable<Element> dataNodes = element.getDescendants(filter);
-                       ComponentRow componentRow = new ComponentRow();
-                       boolean isComponent = false;
-                       for (Element data : dataNodes) {
-                               String attributeValue = data.getAttributeValue("key");
-                               switch (attributeValue) {
-                               case "nodeLabel":
-                                       String nodeLabel = data.getText();
-                                       if (nodeLabel.equals("resource") || nodeLabel.equals("service")) {
-                                               isComponent = true;
-                                               componentRow.setType(nodeLabel);
-                                               String componentId = element.getAttributeValue("id");
-                                               componentRow.setHasNonCalculatedReqCap(componentsHavingReqOrCap.contains(componentId));
-                                       }
-                                       break;
-                               case "uid":
-                                       componentRow.setUniqueId(data.getText());
-                                       break;
-                               case "name":
-                                       componentRow.setName(data.getText());
-                                       break;
-                               case "toscaResourceName":
-                                       componentRow.setToscaResourceName(data.getText());
-                                       break;
-                               case "resourceType":
-                                       componentRow.setResourceType(data.getText());
-                                       break;
-                               case "version":
-                                       componentRow.setVersion(data.getText());
-                                       break;
-                               case "deleted":
-                                       componentRow.setIsDeleted(Boolean.parseBoolean(data.getText()));
-                                       break;
-                               default:
-                                       break;
-                               }
-                       }
-                       if (isComponent) {
-                               res.add(componentRow);
-                       }
-               }
-               return res;
-       }
-
-       private List<ComponentInstanceRow> getComponentInstances(Document document) {
-               List<ComponentInstanceRow> res = new ArrayList<>();
-               Element root = document.getRootElement();
-               ElementFilter filter = new ElementFilter("graph");
-               Element graph = root.getDescendants(filter).next();
-               filter = new ElementFilter("node");
-               IteratorIterable<Element> nodes = graph.getDescendants(filter);
-               filter = new ElementFilter("data");
-               for (Element element : nodes) {
-                       IteratorIterable<Element> dataNodes = element.getDescendants(filter);
-                       ComponentInstanceRow componentInstRow = new ComponentInstanceRow();
-                       boolean isComponentInst = false;
-                       for (Element data : dataNodes) {
-                               String attributeValue = data.getAttributeValue("key");
-                               switch (attributeValue) {
-                               case "nodeLabel":
-                                       String nodeLabel = data.getText();
-                                       if (nodeLabel.equals("resourceInstance")) {
-                                               isComponentInst = true;
-                                       }
-                                       break;
-                               case "uid":
-                                       componentInstRow.setUniqueId(data.getText());
-                                       break;
-                               case "name":
-                                       componentInstRow.setName(data.getText());
-                                       break;
-                               case "originType":
-                                       componentInstRow.setOriginType(data.getText());
-                                       break;
-                               default:
-                                       break;
-                               }
-                       }
-                       if (isComponentInst) {
-                               // Assuming the uid is in standard form of
-                               // <container>.<origin>.<name>
-                               String uniqueId = componentInstRow.getUniqueId();
-                               if (uniqueId != null) {
-                                       String[] split = uniqueId.split("\\.");
-                                       if (split.length == 3) {
-                                               componentInstRow.setContainerUid(split[0]);
-                                               componentInstRow.setOriginUid(split[1]);
-                                       }
-                               }
-                               res.add(componentInstRow);
-                       }
-               }
-               return res;
-       }
+    private static Logger log = LoggerFactory.getLogger(GraphMLDataAnalyzer.class);
+
+    private static final String[] COMPONENT_SHEET_HEADER = {"uniqueId", "type", "name", "toscaResourceName",
+        "resourceType", "version", "deleted", "hasNonCalculatedReqCap"};
+    private static final String[] COMPONENT_INSTANCES_SHEET_HEADER =
+        {"uniqueId", "name", "originUid", "originType", "containerUid"};
+
+    public String analyzeGraphMLData(String[] args) {
+        String result = null;
+        try {
+            String mlFileLocation = args[0];
+            result = analyzeGraphMLData(mlFileLocation);
+            log.info("Analyzed ML file=" + mlFileLocation + ", XLS result=" + result);
+        } catch (Exception e) {
+            log.error("analyze GraphML Data failed - {}", e);
+            return null;
+        }
+        return result;
+    }
+
+    private String analyzeGraphMLData(String mlFileLocation) throws Exception {
+        // Parse ML file
+        SAXBuilder builder = new SAXBuilder();
+        File xmlFile = new File(mlFileLocation);
+        Document document = builder.build(xmlFile);
+
+        // XLS data file name
+        String outputFile = mlFileLocation.replace(".graphml", ".xls");
+
+        try (Workbook wb = new HSSFWorkbook(); FileOutputStream fileOut = new FileOutputStream(outputFile)) {
+            writeComponents(wb, document);
+            writeComponentInstances(wb, document);
+            wb.write(fileOut);
+        } catch (Exception e) {
+            log.error("analyze GraphML Data failed - {}", e);
+        }
+        return outputFile;
+    }
+
+    private void writeComponents(Workbook wb, Document document) {
+        Sheet componentsSheet = wb.createSheet("Components");
+        Row currentRow = componentsSheet.createRow(0);
+        for (int i = 0; i < COMPONENT_SHEET_HEADER.length; i++) {
+            currentRow.createCell(i).setCellValue(COMPONENT_SHEET_HEADER[i]);
+        }
+
+        List<ComponentRow> components = getComponents(document);
+        int rowNum = 1;
+        for (ComponentRow row : components) {
+            currentRow = componentsSheet.createRow(rowNum++);
+            currentRow.createCell(0).setCellValue(row.getUniqueId());
+            currentRow.createCell(1).setCellValue(row.getType());
+            currentRow.createCell(2).setCellValue(row.getName());
+            currentRow.createCell(3).setCellValue(row.getToscaResourceName());
+            currentRow.createCell(4).setCellValue(row.getResourceType());
+            currentRow.createCell(5).setCellValue(row.getVersion());
+            currentRow.createCell(6)
+                .setCellValue(row.getIsDeleted() != null ? row.getIsDeleted().toString() : "false");
+            currentRow.createCell(7).setCellValue(row.getHasNonCalculatedReqCap());
+        }
+    }
+
+    private void writeComponentInstances(Workbook wb, Document document) {
+        Sheet componentsSheet = wb.createSheet("ComponentInstances");
+        Row currentRow = componentsSheet.createRow(0);
+        for (int i = 0; i < COMPONENT_INSTANCES_SHEET_HEADER.length; i++) {
+            currentRow.createCell(i).setCellValue(COMPONENT_INSTANCES_SHEET_HEADER[i]);
+        }
+        List<ComponentInstanceRow> components = getComponentInstances(document);
+        int rowNum = 1;
+        for (ComponentInstanceRow row : components) {
+            currentRow = componentsSheet.createRow(rowNum++);
+            currentRow.createCell(0).setCellValue(row.getUniqueId());
+            currentRow.createCell(1).setCellValue(row.getName());
+            currentRow.createCell(2).setCellValue(row.getOriginUid());
+            currentRow.createCell(3).setCellValue(row.getOriginType());
+            currentRow.createCell(4).setCellValue(row.getContainerUid());
+        }
+    }
+
+    private List<ComponentRow> getComponents(Document document) {
+        List<ComponentRow> res = new ArrayList<>();
+        Element root = document.getRootElement();
+        ElementFilter filter = new ElementFilter("graph");
+        Element graph = root.getDescendants(filter).next();
+        filter = new ElementFilter("edge");
+        IteratorIterable<Element> edges = graph.getDescendants(filter);
+        Set<String> componentsHavingReqOrCap = new HashSet<>();
+        filter = new ElementFilter("data");
+        for (Element edge : edges) {
+            IteratorIterable<Element> dataNodes = edge.getDescendants(filter);
+            for (Element data : dataNodes) {
+                String attributeValue = data.getAttributeValue("key");
+                if ("labelE".equals(attributeValue)) {
+                    String edgeLabel = data.getText();
+                    if ("REQUIREMENT".equals(edgeLabel) || "CAPABILITY".equals(edgeLabel)) {
+                        componentsHavingReqOrCap.add(edge.getAttributeValue("source"));
+                    }
+                }
+            }
+        }
+
+        filter = new ElementFilter("node");
+        IteratorIterable<Element> nodes = graph.getDescendants(filter);
+        filter = new ElementFilter("data");
+        for (Element element : nodes) {
+            IteratorIterable<Element> dataNodes = element.getDescendants(filter);
+            ComponentRow componentRow = new ComponentRow();
+            boolean isComponent = false;
+            for (Element data : dataNodes) {
+                String attributeValue = data.getAttributeValue("key");
+                switch (attributeValue) {
+                    case "nodeLabel":
+                        String nodeLabel = data.getText();
+                        if ("resource".equals(nodeLabel) || "service".equals(nodeLabel)) {
+                            isComponent = true;
+                            componentRow.setType(nodeLabel);
+                            String componentId = element.getAttributeValue("id");
+                            componentRow.setHasNonCalculatedReqCap(componentsHavingReqOrCap.contains(componentId));
+                        }
+                        break;
+                    case "uid":
+                        componentRow.setUniqueId(data.getText());
+                        break;
+                    case "name":
+                        componentRow.setName(data.getText());
+                        break;
+                    case "toscaResourceName":
+                        componentRow.setToscaResourceName(data.getText());
+                        break;
+                    case "resourceType":
+                        componentRow.setResourceType(data.getText());
+                        break;
+                    case "version":
+                        componentRow.setVersion(data.getText());
+                        break;
+                    case "deleted":
+                        componentRow.setIsDeleted(Boolean.parseBoolean(data.getText()));
+                        break;
+                    default:
+                        break;
+                }
+            }
+            if (isComponent) {
+                res.add(componentRow);
+            }
+        }
+        return res;
+    }
+
+    private List<ComponentInstanceRow> getComponentInstances(Document document) {
+        List<ComponentInstanceRow> res = new ArrayList<>();
+        Element root = document.getRootElement();
+        ElementFilter filter = new ElementFilter("graph");
+        Element graph = root.getDescendants(filter).next();
+        filter = new ElementFilter("node");
+        IteratorIterable<Element> nodes = graph.getDescendants(filter);
+        filter = new ElementFilter("data");
+        for (Element element : nodes) {
+            IteratorIterable<Element> dataNodes = element.getDescendants(filter);
+            ComponentInstanceRow componentInstRow = new ComponentInstanceRow();
+            boolean isComponentInst = false;
+            for (Element data : dataNodes) {
+                String attributeValue = data.getAttributeValue("key");
+                switch (attributeValue) {
+                    case "nodeLabel":
+                        String nodeLabel = data.getText();
+                        if ("resourceInstance".equals(nodeLabel)) {
+                            isComponentInst = true;
+                        }
+                        break;
+                    case "uid":
+                        componentInstRow.setUniqueId(data.getText());
+                        break;
+                    case "name":
+                        componentInstRow.setName(data.getText());
+                        break;
+                    case "originType":
+                        componentInstRow.setOriginType(data.getText());
+                        break;
+                    default:
+                        break;
+                }
+            }
+            if (isComponentInst) {
+                // Assuming the uid is in standard form of
+                // <container>.<origin>.<name>
+                String uniqueId = componentInstRow.getUniqueId();
+                if (uniqueId != null) {
+                    String[] split = uniqueId.split("\\.");
+                    if (split.length == 3) {
+                        componentInstRow.setContainerUid(split[0]);
+                        componentInstRow.setOriginUid(split[1]);
+                    }
+                }
+                res.add(componentInstRow);
+            }
+        }
+        return res;
+    }
 
 }