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.servlets;
23 import org.apache.commons.configuration.BaseConfiguration;
24 import org.apache.commons.configuration.Configuration;
25 import org.apache.tinkerpop.gremlin.structure.io.graphml.GraphMLWriter;
26 import org.glassfish.jersey.media.multipart.FormDataParam;
27 import org.janusgraph.core.JanusGraph;
28 import org.openecomp.sdc.asdctool.Utils;
29 import org.openecomp.sdc.common.log.wrappers.Logger;
31 import javax.ws.rs.Consumes;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.Response;
37 import java.io.BufferedOutputStream;
38 import java.io.BufferedReader;
39 import java.io.ByteArrayOutputStream;
41 import java.io.FileReader;
42 import java.io.IOException;
43 import java.io.OutputStream;
44 import java.util.Map.Entry;
45 import java.util.Properties;
46 //import com.tinkerpop.blueprints.util.io.graphml.GraphMLWriter;
49 public class ExportImportJanusGraphServlet {
51 private static Logger log = Logger.getLogger(ExportImportJanusGraphServlet.class.getName());
55 @Consumes(MediaType.MULTIPART_FORM_DATA)
56 @Produces(MediaType.APPLICATION_OCTET_STREAM)
57 public Response export(@FormDataParam("janusGraphProperties") File janusGraphPropertiesFile,
58 @FormDataParam("metadata") String exportGraphMetadata) {
60 printJanusGraphConfigFile(janusGraphPropertiesFile);
61 printMetadata(exportGraphMetadata);
63 Properties janusGraphProperties = convertFileToProperties(janusGraphPropertiesFile);
65 if (janusGraphProperties == null) {
66 Response response = Utils.buildOkResponse(400, "cannot parse janusgraph properties file", null);
70 Configuration conf = new BaseConfiguration();
71 for (Entry<Object, Object> entry : janusGraphProperties.entrySet()) {
72 String key = entry.getKey().toString();
73 Object value = entry.getValue();
74 conf.setProperty(key, value);
77 conf.setProperty("storage.machine-id-appendix", System.currentTimeMillis() % 1000);
79 try(JanusGraph openGraph = Utils.openGraph(conf)){
81 if (openGraph == null) {
82 Response buildErrorResponse = Utils.buildOkResponse(500, "failed to open graph", null);
83 return buildErrorResponse;
86 // Open JanusGraph Graph
88 Response buildOkResponse = Utils.buildOkResponse(200, "ok man", null);
90 return buildOkResponse;
94 private Properties convertFileToProperties(File janusGraphPropertiesFile) {
96 Properties properties = new Properties();
98 try (FileReader fileReader = new FileReader(janusGraphPropertiesFile)){
99 properties.load(fileReader);
100 } catch (Exception e) {
101 log.error("Failed to convert file to properties", e);
108 private void printJanusGraphConfigFile(File janusGraphPropertiesFile) {
110 if (log.isDebugEnabled()) {
111 StringBuilder builder = new StringBuilder();
112 try (BufferedReader br = new BufferedReader(new FileReader(janusGraphPropertiesFile))) {
114 while ((line = br.readLine()) != null) {
115 builder.append(line + Utils.NEW_LINE);
118 log.debug(builder.toString());
120 } catch (IOException e) {
121 log.error("Cannot print janusgraph properties file", e);
126 private void printMetadata(String exportGraphMetadata) {
128 log.debug(exportGraphMetadata);
132 public String exportGraph(JanusGraph graph, String outputDirectory) {
134 String result = null;
136 // GraphMLWriter graphMLWriter = new GraphMLWriter(graph);
137 GraphMLWriter graphMLWriter = GraphMLWriter.build().create();
139 String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".ml";
141 OutputStream out = null;
143 out = new BufferedOutputStream(new ByteArrayOutputStream());
145 // graphMLWriter.outputGraph(out);
147 graphMLWriter.writeGraph(out, graph);
154 } catch (Exception e) {
157 graph.tx().rollback();
163 } catch (IOException e) {