Catalog alignment
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / servlets / ExportImportJanusGraphServlet.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 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;
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.BufferedOutputStream;
38 import java.io.BufferedReader;
39 import java.io.ByteArrayOutputStream;
40 import java.io.File;
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;
47
48 @Path("/janusgraph")
49 public class ExportImportJanusGraphServlet {
50
51         private static Logger log = Logger.getLogger(ExportImportJanusGraphServlet.class.getName());
52
53         @GET
54         @Path("export")
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) {
59
60                 printJanusGraphConfigFile(janusGraphPropertiesFile);
61                 printMetadata(exportGraphMetadata);
62
63                 Properties janusGraphProperties = convertFileToProperties(janusGraphPropertiesFile);
64
65                 if (janusGraphProperties == null) {
66                         Response response = Utils.buildOkResponse(400, "cannot parse janusgraph properties file", null);
67                         return response;
68                 }
69
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);
75                 }
76
77                 conf.setProperty("storage.machine-id-appendix", System.currentTimeMillis() % 1000);
78
79                 try(JanusGraph openGraph = Utils.openGraph(conf)){
80                         
81                         if (openGraph == null) {
82                                 Response buildErrorResponse = Utils.buildOkResponse(500, "failed to open graph", null);
83                                 return buildErrorResponse;
84                         }
85         
86                         // Open JanusGraph Graph
87         
88                         Response buildOkResponse = Utils.buildOkResponse(200, "ok man", null);
89         
90                         return buildOkResponse;
91                 }
92         }
93
94         private Properties convertFileToProperties(File janusGraphPropertiesFile) {
95
96                 Properties properties = new Properties();
97
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);
102                         return null;
103                 }
104
105                 return properties;
106         }
107
108         private void printJanusGraphConfigFile(File janusGraphPropertiesFile) {
109
110                 if (log.isDebugEnabled()) {
111                         StringBuilder builder = new StringBuilder();
112                         try (BufferedReader br = new BufferedReader(new FileReader(janusGraphPropertiesFile))) {
113                                 String line;
114                                 while ((line = br.readLine()) != null) {
115                                         builder.append(line + Utils.NEW_LINE);
116                                 }
117
118                                 log.debug(builder.toString());
119
120                         } catch (IOException e) {
121                                 log.error("Cannot print janusgraph properties file", e);
122                         }
123                 }
124         }
125
126         private void printMetadata(String exportGraphMetadata) {
127
128                 log.debug(exportGraphMetadata);
129
130         }
131
132         public String exportGraph(JanusGraph graph, String outputDirectory) {
133
134                 String result = null;
135
136                 // GraphMLWriter graphMLWriter = new GraphMLWriter(graph);
137                 GraphMLWriter graphMLWriter = GraphMLWriter.build().create();
138
139                 String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".ml";
140
141                 OutputStream out = null;
142                 try {
143                         out = new BufferedOutputStream(new ByteArrayOutputStream());
144
145                         // graphMLWriter.outputGraph(out);
146
147                         graphMLWriter.writeGraph(out, graph);
148
149                         // graph.commit();
150                         graph.tx().commit();
151
152                         result = outputFile;
153
154                 } catch (Exception e) {
155                         e.printStackTrace();
156                         // graph.rollback();
157                         graph.tx().rollback();
158                 } finally {
159                         try {
160                                 if (out != null) {
161                                         out.close();
162                                 }
163                         } catch (IOException e) {
164                                 e.printStackTrace();
165                         }
166                 }
167                 return result;
168
169         }
170
171 }