aboutsummaryrefslogtreecommitdiffstats
path: root/asdctool/src/main/java/org/openecomp/sdc/asdctool/servlets/ExportImportTitanServlet.java
blob: c1f9335d59c0c416156d5e8d5795d382573e3e45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.asdctool.servlets;

import com.thinkaurelius.titan.core.TitanGraph;
import org.apache.commons.configuration.BaseConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.tinkerpop.gremlin.structure.io.graphml.GraphMLWriter;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.openecomp.sdc.asdctool.Utils;
import org.openecomp.sdc.common.log.wrappers.Logger;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.*;
import java.util.Map.Entry;
import java.util.Properties;
//import com.tinkerpop.blueprints.util.io.graphml.GraphMLWriter;

@Path("/titan")
public class ExportImportTitanServlet {

	private static Logger log = Logger.getLogger(ExportImportTitanServlet.class.getName());

	@GET
	@Path("export")
	@Consumes(MediaType.MULTIPART_FORM_DATA)
	@Produces(MediaType.APPLICATION_OCTET_STREAM)
	public Response export(@FormDataParam("titanProperties") File titanPropertiesFile,
			@FormDataParam("metadata") String exportGraphMetadata) {

		printTitanConfigFile(titanPropertiesFile);
		printMetadata(exportGraphMetadata);

		Properties titanProperties = convertFileToProperties(titanPropertiesFile);

		if (titanProperties == null) {
			Response response = Utils.buildOkResponse(400, "cannot parse titan properties file", null);
			return response;
		}

		Configuration conf = new BaseConfiguration();
		for (Entry<Object, Object> entry : titanProperties.entrySet()) {
			String key = entry.getKey().toString();
			Object value = entry.getValue();
			conf.setProperty(key, value);
		}

		conf.setProperty("storage.machine-id-appendix", System.currentTimeMillis() % 1000);

		TitanGraph openGraph = Utils.openGraph(conf);
		if (openGraph == null) {
			Response buildErrorResponse = Utils.buildOkResponse(500, "failed to open graph", null);
			return buildErrorResponse;
		}

		// Open Titan Graph

		Response buildOkResponse = Utils.buildOkResponse(200, "ok man", null);

		return buildOkResponse;
	}

	private Properties convertFileToProperties(File titanPropertiesFile) {

		Properties properties = new Properties();

		FileReader fileReader = null;
		try {
			fileReader = new FileReader(titanPropertiesFile);
			properties.load(fileReader);

		} catch (Exception e) {
			log.error("Failed to convert file to properties", e);
			return null;
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (IOException e) {
					log.error("Failed to close file", e);
				}
			}
		}

		return properties;
	}

	private void printTitanConfigFile(File titanPropertiesFile) {

		if (log.isDebugEnabled()) {
			StringBuilder builder = new StringBuilder();
			try (BufferedReader br = new BufferedReader(new FileReader(titanPropertiesFile))) {
				String line;
				while ((line = br.readLine()) != null) {
					builder.append(line + Utils.NEW_LINE);
				}

				log.debug(builder.toString());

			} catch (IOException e) {
				log.error("Cannot print titan properties file", e);
			}
		}
	}

	private void printMetadata(String exportGraphMetadata) {

		log.debug(exportGraphMetadata);

	}

	public String exportGraph(TitanGraph graph, String outputDirectory) {

		String result = null;

		// GraphMLWriter graphMLWriter = new GraphMLWriter(graph);
		GraphMLWriter graphMLWriter = GraphMLWriter.build().create();

		String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".ml";

		OutputStream out = null;
		try {
			out = new BufferedOutputStream(new ByteArrayOutputStream());

			// graphMLWriter.outputGraph(out);

			graphMLWriter.writeGraph(out, graph);

			// graph.commit();
			graph.tx().commit();

			result = outputFile;

		} catch (Exception e) {
			log.info("export Graph failed - {}" , e);
			// graph.rollback();
			graph.tx().rollback();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				log.info("close FileOutputStream failed - {}" , e);
			}
		}
		return result;

	}

}