2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.asdctool.impl;
23 import com.google.gson.Gson;
24 import org.apache.commons.configuration.BaseConfiguration;
25 import org.apache.commons.lang3.tuple.ImmutablePair;
26 import org.apache.tinkerpop.gremlin.structure.Element;
27 import org.apache.tinkerpop.gremlin.structure.Graph;
28 import org.apache.tinkerpop.gremlin.structure.Property;
29 import org.apache.tinkerpop.gremlin.structure.Vertex;
30 import org.apache.tinkerpop.gremlin.structure.io.IoCore;
31 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper;
32 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONReader;
33 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter;
34 import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
35 import org.janusgraph.core.JanusGraph;
36 import org.janusgraph.core.JanusGraphFactory;
37 import org.janusgraph.core.JanusGraphQuery;
38 import org.janusgraph.core.JanusGraphVertex;
39 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
40 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
41 import org.openecomp.sdc.common.log.wrappers.Logger;
43 import java.io.BufferedInputStream;
44 import java.io.BufferedOutputStream;
46 import java.io.FileInputStream;
47 import java.io.FileOutputStream;
48 import java.io.FileWriter;
49 import java.io.InputStream;
50 import java.io.OutputStream;
51 import java.util.ArrayList;
52 import java.util.HashMap;
53 import java.util.Iterator;
54 import java.util.List;
56 import java.util.Map.Entry;
58 public class GraphMLConverter {
60 private static final String STORAGE_BACKEND = "storage.backend";
62 private static final String INMEMORY = "inmemory";
64 private static final String EXPORT_GRAPH = "exportGraph.";
66 private static final String DOT_JSON = ".json";
68 private static final String EXPORTED_FILE = "Exported file=";
70 private static final String NODE_LABEL = "nodeLabel";
72 private static Logger log = Logger.getLogger(GraphMLConverter.class.getName());
74 private Gson gson = new Gson();
75 private static final String LOG_FORMATTER = "{} {}";
77 public boolean importGraph(String[] args) {
79 JanusGraph graph = null;
81 String janusGraphFileLocation = args[1];
82 String inputFile = args[2];
83 graph = openGraph(janusGraphFileLocation);
85 List<ImmutablePair<String, String>> propertiesCriteriaToDelete = new ArrayList<>();
86 ImmutablePair<String, String> immutablePair1 = new ImmutablePair<>("healthcheckis", "GOOD");
87 ImmutablePair<String, String> immutablePair2 = new ImmutablePair<>(NODE_LABEL, "user");
88 ImmutablePair<String, String> immutablePair3 = new ImmutablePair<>(NODE_LABEL, "resourceCategory");
89 ImmutablePair<String, String> immutablePair4 = new ImmutablePair<>(NODE_LABEL, "serviceCategory");
91 propertiesCriteriaToDelete.add(immutablePair1);
92 propertiesCriteriaToDelete.add(immutablePair2);
93 propertiesCriteriaToDelete.add(immutablePair3);
94 propertiesCriteriaToDelete.add(immutablePair4);
96 return importJsonGraph(graph, inputFile, propertiesCriteriaToDelete);
98 } catch (Exception e) {
100 log.info("import graph failed ", e);
110 public boolean exportGraph(String[] args) {
112 JanusGraph graph = null;
114 String janusGraphFileLocation = args[1];
115 String outputDirectory = args[2];
116 graph = openGraph(janusGraphFileLocation);
118 String result = exportJsonGraph(graph, outputDirectory);
120 if (result == null) {
124 log.info(LOG_FORMATTER, EXPORTED_FILE, result);
125 } catch (Exception e) {
127 log.info("export graph failed ", e);
138 public String exportGraphMl(String[] args) {
140 JanusGraph graph = null;
141 String result = null;
143 String janusGraphFileLocation = args[1];
144 String outputDirectory = args[2];
145 graph = openGraph(janusGraphFileLocation);
147 result = exportGraphMl(graph, outputDirectory);
149 log.info(LOG_FORMATTER, EXPORTED_FILE, result);
150 } catch (Exception e) {
152 log.info("export exportGraphMl failed ", e);
163 public boolean findErrorInJsonGraph(String[] args) {
165 JanusGraph graph = null;
167 String janusGraphFileLocation = args[1];
168 String outputDirectory = args[2];
169 graph = openGraph(janusGraphFileLocation);
171 String result = findErrorInJsonGraph(graph, outputDirectory);
173 if (result == null) {
177 log.info(LOG_FORMATTER, EXPORTED_FILE, result);
178 } catch (Exception e) {
180 log.info("find Error In Json Graph failed ", e);
191 public JanusGraph openGraph(String janusGraphFileLocation) {
193 return JanusGraphFactory.open(janusGraphFileLocation);
197 public String exportJsonGraph(JanusGraph graph, String outputDirectory) {
199 String result = null;
201 String outputFile = outputDirectory + File.separator + EXPORT_GRAPH + System.currentTimeMillis() + DOT_JSON;
203 try (final OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile))) {
205 final GraphSONWriter.Builder builder = GraphSONWriter.build();
206 final GraphSONMapper mapper = newGraphSONMapper(graph);
207 builder.mapper(mapper);
208 final GraphSONWriter writer = builder.create();
209 writer.writeGraph(out, graph);
215 } catch (Exception e) {
217 log.info("export Json Graph failed ", e);
218 graph.tx().rollback();
225 public String exportGraphMl(JanusGraph graph, String outputDirectory) {
226 String result = null;
228 outputDirectory + File.separator + EXPORT_GRAPH + System.currentTimeMillis() + ".graphml";
230 try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile))) {
231 graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(os, graph);
235 } catch (Exception e) {
236 graph.tx().rollback();
238 log.info("export Graph Ml failed ", e);
244 private static GraphSONMapper newGraphSONMapper(final Graph graph) {
245 final GraphSONMapper.Builder builder = graph.io(IoCore.graphson()).mapper();
246 return builder.create();
249 public boolean importJsonGraph(JanusGraph graph, String graphJsonFile,
250 List<ImmutablePair<String, String>> propertiesCriteriaToDelete) {
252 boolean result = false;
254 if (propertiesCriteriaToDelete != null) {
255 for (Entry<String, String> entry : propertiesCriteriaToDelete
259 String key = entry.getKey();
260 String value = entry.getValue();
261 Iterator iterator = graph.query().has(key, value).vertices().iterator();
262 while (iterator.hasNext()) {
263 Vertex vertex = (Vertex) iterator.next();
265 log.info("Remove vertex of type{} ", key, " and value {}", value);
270 File file = new File(graphJsonFile);
271 if (!file.isFile()) {
272 log.info("File ", graphJsonFile, " cannot be found.");
276 try (final InputStream is = new BufferedInputStream(new FileInputStream(graphJsonFile))) {
278 log.info("Before importing file ", graphJsonFile);
280 GraphSONReader create = GraphSONReader.build().create();
281 create.readGraph(is, graph);
287 } catch (Exception e) {
288 log.info("Failed to import graph ", e);
290 graph.tx().rollback();
297 public String findErrorInJsonGraph(JanusGraph graph, String outputDirectory) {
299 String result = null;
301 String outputFile = outputDirectory + File.separator + EXPORT_GRAPH + System.currentTimeMillis() + DOT_JSON;
303 try (final OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile))) {
305 graph.query().has(GraphPropertiesDictionary.HEALTH_CHECK.getProperty(), "GOOD").vertices();
307 BaseConfiguration conf = new BaseConfiguration();
308 conf.setProperty(STORAGE_BACKEND, INMEMORY);
309 for (NodeTypeEnum nodeTypeEnum : NodeTypeEnum.values()) {
310 removeNodesByLabel(graph, nodeTypeEnum.getName());
313 GraphSONWriter create = GraphSONWriter.build().create();
314 create.writeGraph(out, graph);
316 graph.tx().rollback();
318 } catch (Exception e) {
320 log.info("find Error In Json Graph failed ", e);
321 graph.tx().rollback();
328 private void removeNodesByLabel(JanusGraph graph, String label) {
329 Iterable<JanusGraphVertex> vertices =
330 graph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), label).vertices();
331 Iterator<JanusGraphVertex> iterator = vertices.iterator();
332 while (iterator.hasNext()) {
333 Vertex next2 = iterator.next();
338 public String exportUsers(JanusGraph graph, String outputDirectory) {
340 List<Map<String, Object>> users = new ArrayList<>();
341 String result = null;
343 String outputFile = outputDirectory + File.separator + "users." + System.currentTimeMillis() + DOT_JSON;
345 JanusGraphQuery graphQuery =
346 graph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.User.getName());
348 @SuppressWarnings("unchecked")
349 Iterable<JanusGraphVertex> vertices = graphQuery.vertices();
351 if (vertices != null) {
352 for (Vertex v : vertices) {
353 Map<String, Object> properties = getProperties(v);
354 properties.remove(GraphPropertiesDictionary.LABEL.getProperty());
355 users.add(properties);
361 String jsonUsers = gson.toJson(users);
363 try (final FileWriter fileWriter = new FileWriter(outputFile)) {
365 fileWriter.write(jsonUsers);
369 } catch (Exception e) {
371 log.info("export Users failed ", e);
372 graph.tx().rollback();
379 public Map<String, Object> getProperties(Element element) {
381 Map<String, Object> result = new HashMap<>();
384 if (element.keys() != null && !element.keys().isEmpty()) {
385 Map<String, Property> propertyMap =
386 ElementHelper.propertyMap(element, element.keys().toArray(new String[element.keys().size()]));
388 for (Entry<String, Property> entry : propertyMap.entrySet()) {
389 String key = entry.getKey();
390 Object value = entry.getValue().value();
392 result.put(key, value);
398 public boolean exportUsers(String[] args) {
400 JanusGraph graph = null;
402 String janusGraphFileLocation = args[1];
403 String outputDirectory = args[2];
404 graph = openGraph(janusGraphFileLocation);
406 String result = exportUsers(graph, outputDirectory);
408 if (result == null) {
412 log.info(EXPORTED_FILE, result);
413 } catch (Exception e) {
415 log.info("export Users failed ", e);