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 com.thinkaurelius.titan.core.TitanGraph;
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 ExportImportTitanServlet {
45 private static Logger log = Logger.getLogger(ExportImportTitanServlet.class.getName());
49 @Consumes(MediaType.MULTIPART_FORM_DATA)
50 @Produces(MediaType.APPLICATION_OCTET_STREAM)
51 public Response export(@FormDataParam("titanProperties") File titanPropertiesFile,
52 @FormDataParam("metadata") String exportGraphMetadata) {
54 printTitanConfigFile(titanPropertiesFile);
55 printMetadata(exportGraphMetadata);
57 Properties titanProperties = convertFileToProperties(titanPropertiesFile);
59 if (titanProperties == null) {
60 Response response = Utils.buildOkResponse(400, "cannot parse titan properties file", null);
64 Configuration conf = new BaseConfiguration();
65 for (Entry<Object, Object> entry : titanProperties.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 TitanGraph openGraph = Utils.openGraph(conf);
74 if (openGraph == null) {
75 Response buildErrorResponse = Utils.buildOkResponse(500, "failed to open graph", null);
76 return buildErrorResponse;
81 Response buildOkResponse = Utils.buildOkResponse(200, "ok man", null);
83 return buildOkResponse;
86 private Properties convertFileToProperties(File titanPropertiesFile) {
88 Properties properties = new Properties();
90 FileReader fileReader = null;
92 fileReader = new FileReader(titanPropertiesFile);
93 properties.load(fileReader);
95 } catch (Exception e) {
96 log.error("Failed to convert file to properties", e);
99 if (fileReader != null) {
102 } catch (IOException e) {
103 log.error("Failed to close file", e);
111 private void printTitanConfigFile(File titanPropertiesFile) {
113 if (log.isDebugEnabled()) {
114 StringBuilder builder = new StringBuilder();
115 try (BufferedReader br = new BufferedReader(new FileReader(titanPropertiesFile))) {
117 while ((line = br.readLine()) != null) {
118 builder.append(line + Utils.NEW_LINE);
121 log.debug(builder.toString());
123 } catch (IOException e) {
124 log.error("Cannot print titan properties file", e);
129 private void printMetadata(String exportGraphMetadata) {
131 log.debug(exportGraphMetadata);
135 public String exportGraph(TitanGraph graph, String outputDirectory) {
137 String result = null;
139 // GraphMLWriter graphMLWriter = new GraphMLWriter(graph);
140 GraphMLWriter graphMLWriter = GraphMLWriter.build().create();
142 String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".ml";
144 OutputStream out = null;
146 out = new BufferedOutputStream(new ByteArrayOutputStream());
148 // graphMLWriter.outputGraph(out);
150 graphMLWriter.writeGraph(out, graph);
157 } catch (Exception e) {
158 log.info("export Graph failed - {}" , e);
160 graph.tx().rollback();
166 } catch (IOException e) {
167 log.info("close FileOutputStream failed - {}" , e);