re base code
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / servlets / ExportImportTitanServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.asdctool.servlets;
22
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;
30
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.*;
38 import java.util.Map.Entry;
39 import java.util.Properties;
40 //import com.tinkerpop.blueprints.util.io.graphml.GraphMLWriter;
41
42 @Path("/titan")
43 public class ExportImportTitanServlet {
44
45         private static Logger log = Logger.getLogger(ExportImportTitanServlet.class.getName());
46
47         @GET
48         @Path("export")
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) {
53
54                 printTitanConfigFile(titanPropertiesFile);
55                 printMetadata(exportGraphMetadata);
56
57                 Properties titanProperties = convertFileToProperties(titanPropertiesFile);
58
59                 if (titanProperties == null) {
60                         Response response = Utils.buildOkResponse(400, "cannot parse titan properties file", null);
61                         return response;
62                 }
63
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);
69                 }
70
71                 conf.setProperty("storage.machine-id-appendix", System.currentTimeMillis() % 1000);
72
73                 TitanGraph openGraph = Utils.openGraph(conf);
74                 if (openGraph == null) {
75                         Response buildErrorResponse = Utils.buildOkResponse(500, "failed to open graph", null);
76                         return buildErrorResponse;
77                 }
78
79                 // Open Titan Graph
80
81                 Response buildOkResponse = Utils.buildOkResponse(200, "ok man", null);
82
83                 return buildOkResponse;
84         }
85
86         private Properties convertFileToProperties(File titanPropertiesFile) {
87
88                 Properties properties = new Properties();
89
90                 FileReader fileReader = null;
91                 try {
92                         fileReader = new FileReader(titanPropertiesFile);
93                         properties.load(fileReader);
94
95                 } catch (Exception e) {
96                         log.error("Failed to convert file to properties", e);
97                         return null;
98                 } finally {
99                         if (fileReader != null) {
100                                 try {
101                                         fileReader.close();
102                                 } catch (IOException e) {
103                                         log.error("Failed to close file", e);
104                                 }
105                         }
106                 }
107
108                 return properties;
109         }
110
111         private void printTitanConfigFile(File titanPropertiesFile) {
112
113                 if (log.isDebugEnabled()) {
114                         StringBuilder builder = new StringBuilder();
115                         try (BufferedReader br = new BufferedReader(new FileReader(titanPropertiesFile))) {
116                                 String line;
117                                 while ((line = br.readLine()) != null) {
118                                         builder.append(line + Utils.NEW_LINE);
119                                 }
120
121                                 log.debug(builder.toString());
122
123                         } catch (IOException e) {
124                                 log.error("Cannot print titan properties file", e);
125                         }
126                 }
127         }
128
129         private void printMetadata(String exportGraphMetadata) {
130
131                 log.debug(exportGraphMetadata);
132
133         }
134
135         public String exportGraph(TitanGraph graph, String outputDirectory) {
136
137                 String result = null;
138
139                 // GraphMLWriter graphMLWriter = new GraphMLWriter(graph);
140                 GraphMLWriter graphMLWriter = GraphMLWriter.build().create();
141
142                 String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".ml";
143
144                 OutputStream out = null;
145                 try {
146                         out = new BufferedOutputStream(new ByteArrayOutputStream());
147
148                         // graphMLWriter.outputGraph(out);
149
150                         graphMLWriter.writeGraph(out, graph);
151
152                         // graph.commit();
153                         graph.tx().commit();
154
155                         result = outputFile;
156
157                 } catch (Exception e) {
158                         log.info("export Graph failed - {}" , e);
159                         // graph.rollback();
160                         graph.tx().rollback();
161                 } finally {
162                         try {
163                                 if (out != null) {
164                                         out.close();
165                                 }
166                         } catch (IOException e) {
167                                 log.info("close FileOutputStream failed - {}" , e);
168                         }
169                 }
170                 return result;
171
172         }
173
174 }