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=========================================================
20 package org.openecomp.sdc.asdctool.servlets;
22 import java.io.BufferedReader;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.util.Map.Entry;
27 import java.util.Optional;
28 import java.util.Properties;
29 import javax.ws.rs.Consumes;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35 import org.apache.commons.configuration.BaseConfiguration;
36 import org.apache.commons.configuration.Configuration;
37 import org.glassfish.jersey.media.multipart.FormDataParam;
38 import org.janusgraph.core.JanusGraph;
39 import org.openecomp.sdc.asdctool.Utils;
40 import org.openecomp.sdc.logging.api.Logger;
41 import org.openecomp.sdc.logging.api.LoggerFactory;
44 public class ExportImportJanusGraphServlet {
46 private static final Logger log = LoggerFactory.getLogger(ExportImportJanusGraphServlet.class);
50 @Consumes(MediaType.MULTIPART_FORM_DATA)
51 @Produces(MediaType.APPLICATION_OCTET_STREAM)
52 public Response export(@FormDataParam("janusGraphProperties") final File janusGraphPropertiesFile,
53 @FormDataParam("metadata") final String exportGraphMetadata) {
54 printJanusGraphConfigFile(janusGraphPropertiesFile);
55 printMetadata(exportGraphMetadata);
56 final Properties janusGraphProperties = convertFileToProperties(janusGraphPropertiesFile);
57 if (janusGraphProperties == null) {
58 return Utils.buildOkResponse(400, "cannot parse janusgraph properties file", null);
60 final Configuration conf = new BaseConfiguration();
61 for (final Entry<Object, Object> entry : janusGraphProperties.entrySet()) {
62 final String key = entry.getKey().toString();
63 final Object value = entry.getValue();
64 conf.setProperty(key, value);
66 conf.setProperty("storage.machine-id-appendix", System.currentTimeMillis() % 1000);
67 final Optional<JanusGraph> openGraph = Utils.openGraph(conf);
68 if (openGraph.isPresent()) {
70 return Utils.buildOkResponse(200, "ok man", null);
72 openGraph.get().close();
75 return Utils.buildOkResponse(500, "failed to open graph", null);
79 private Properties convertFileToProperties(final File janusGraphPropertiesFile) {
80 final var properties = new Properties();
81 try (final var fileReader = new FileReader(janusGraphPropertiesFile)) {
82 properties.load(fileReader);
83 } catch (final Exception e) {
84 log.error("Failed to convert file to properties", e);
90 private void printJanusGraphConfigFile(final File janusGraphPropertiesFile) {
91 if (log.isDebugEnabled()) {
92 final var builder = new StringBuilder();
93 try (final var br = new BufferedReader(new FileReader(janusGraphPropertiesFile))) {
95 while ((line = br.readLine()) != null) {
96 builder.append(line + Utils.NEW_LINE);
98 log.debug(builder.toString());
99 } catch (IOException e) {
100 log.error("Cannot print janusgraph properties file", e);
105 private void printMetadata(final String exportGraphMetadata) {
106 log.debug(exportGraphMetadata);