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.janusgraph.core.JanusGraph;
24 import org.apache.commons.configuration.BaseConfiguration;
25 import org.apache.commons.configuration.Configuration;
26 import org.apache.tinkerpop.gremlin.structure.io.graphml.GraphMLWriter;
27 import org.glassfish.jersey.media.multipart.FormDataParam;
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;
38 import java.util.Map.Entry;
39 import java.util.Properties;
40 //import com.tinkerpop.blueprints.util.io.graphml.GraphMLWriter;
43 public class ExportImportJanusGraphServlet {
45 private static Logger log = Logger.getLogger(ExportImportJanusGraphServlet.class.getName());
49 @Consumes(MediaType.MULTIPART_FORM_DATA)
50 @Produces(MediaType.APPLICATION_OCTET_STREAM)
51 public Response export(@FormDataParam("janusGraphProperties") File janusGraphPropertiesFile,
52 @FormDataParam("metadata") String exportGraphMetadata) {
54 printJanusGraphConfigFile(janusGraphPropertiesFile);
55 printMetadata(exportGraphMetadata);
57 Properties janusGraphProperties = convertFileToProperties(janusGraphPropertiesFile);
59 if (janusGraphProperties == null) {
60 Response response = Utils.buildOkResponse(400, "cannot parse janusgraph properties file", null);
64 Configuration conf = new BaseConfiguration();
65 for (Entry<Object, Object> entry : janusGraphProperties.entrySet()) {
66 String key = entry.getKey().toString();
67 Object value = entry.getValue();
68 conf.setProperty(key, value);
71 conf.setProperty("storage.machine-id-appendix", System.currentTimeMillis() % 1000);
73 try(JanusGraph openGraph = Utils.openGraph(conf)){
75 if (openGraph == null) {
76 Response buildErrorResponse = Utils.buildOkResponse(500, "failed to open graph", null);
77 return buildErrorResponse;
80 // Open JanusGraph Graph
82 Response buildOkResponse = Utils.buildOkResponse(200, "ok man", null);
84 return buildOkResponse;
88 private Properties convertFileToProperties(File janusGraphPropertiesFile) {
90 Properties properties = new Properties();
92 try (FileReader fileReader = new FileReader(janusGraphPropertiesFile)){
93 properties.load(fileReader);
94 } catch (Exception e) {
95 log.error("Failed to convert file to properties", e);
102 private void printJanusGraphConfigFile(File janusGraphPropertiesFile) {
104 if (log.isDebugEnabled()) {
105 StringBuilder builder = new StringBuilder();
106 try (BufferedReader br = new BufferedReader(new FileReader(janusGraphPropertiesFile))) {
108 while ((line = br.readLine()) != null) {
109 builder.append(line + Utils.NEW_LINE);
112 log.debug(builder.toString());
114 } catch (IOException e) {
115 log.error("Cannot print janusgraph properties file", e);
120 private void printMetadata(String exportGraphMetadata) {
122 log.debug(exportGraphMetadata);
126 public String exportGraph(JanusGraph graph, String outputDirectory) {
128 String result = null;
130 // GraphMLWriter graphMLWriter = new GraphMLWriter(graph);
131 GraphMLWriter graphMLWriter = GraphMLWriter.build().create();
133 String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".ml";
135 OutputStream out = null;
137 out = new BufferedOutputStream(new ByteArrayOutputStream());
139 // graphMLWriter.outputGraph(out);
141 graphMLWriter.writeGraph(out, graph);
148 } catch (Exception e) {
149 log.info("export Graph failed - {}" , e);
151 graph.tx().rollback();
157 } catch (IOException e) {
158 log.info("close FileOutputStream failed - {}" , e);