Add lombok support to simple classes
[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.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;
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("/janusgraph")
43 public class ExportImportJanusGraphServlet {
44
45         private static Logger log = Logger.getLogger(ExportImportJanusGraphServlet.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("janusGraphProperties") File janusGraphPropertiesFile,
52                         @FormDataParam("metadata") String exportGraphMetadata) {
53
54                 printJanusGraphConfigFile(janusGraphPropertiesFile);
55                 printMetadata(exportGraphMetadata);
56
57                 Properties janusGraphProperties = convertFileToProperties(janusGraphPropertiesFile);
58
59                 if (janusGraphProperties == null) {
60                         Response response = Utils.buildOkResponse(400, "cannot parse janusgraph properties file", null);
61                         return response;
62                 }
63
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);
69                 }
70
71                 conf.setProperty("storage.machine-id-appendix", System.currentTimeMillis() % 1000);
72
73                 try(JanusGraph openGraph = Utils.openGraph(conf)){
74                         
75                         if (openGraph == null) {
76                                 Response buildErrorResponse = Utils.buildOkResponse(500, "failed to open graph", null);
77                                 return buildErrorResponse;
78                         }
79         
80                         // Open JanusGraph Graph
81         
82                         Response buildOkResponse = Utils.buildOkResponse(200, "ok man", null);
83         
84                         return buildOkResponse;
85                 }
86         }
87
88         private Properties convertFileToProperties(File janusGraphPropertiesFile) {
89
90                 Properties properties = new Properties();
91
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);
96                         return null;
97                 }
98
99                 return properties;
100         }
101
102         private void printJanusGraphConfigFile(File janusGraphPropertiesFile) {
103
104                 if (log.isDebugEnabled()) {
105                         StringBuilder builder = new StringBuilder();
106                         try (BufferedReader br = new BufferedReader(new FileReader(janusGraphPropertiesFile))) {
107                                 String line;
108                                 while ((line = br.readLine()) != null) {
109                                         builder.append(line + Utils.NEW_LINE);
110                                 }
111
112                                 log.debug(builder.toString());
113
114                         } catch (IOException e) {
115                                 log.error("Cannot print janusgraph properties file", e);
116                         }
117                 }
118         }
119
120         private void printMetadata(String exportGraphMetadata) {
121
122                 log.debug(exportGraphMetadata);
123
124         }
125
126         public String exportGraph(JanusGraph graph, String outputDirectory) {
127
128                 String result = null;
129
130                 // GraphMLWriter graphMLWriter = new GraphMLWriter(graph);
131                 GraphMLWriter graphMLWriter = GraphMLWriter.build().create();
132
133                 String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".ml";
134
135                 OutputStream out = null;
136                 try {
137                         out = new BufferedOutputStream(new ByteArrayOutputStream());
138
139                         // graphMLWriter.outputGraph(out);
140
141                         graphMLWriter.writeGraph(out, graph);
142
143                         // graph.commit();
144                         graph.tx().commit();
145
146                         result = outputFile;
147
148                 } catch (Exception e) {
149                         log.info("export Graph failed - {}" , e);
150                         // graph.rollback();
151                         graph.tx().rollback();
152                 } finally {
153                         try {
154                                 if (out != null) {
155                                         out.close();
156                                 }
157                         } catch (IOException e) {
158                                 log.info("close FileOutputStream failed - {}" , e);
159                         }
160                 }
161                 return result;
162
163         }
164
165 }