[AAI-2175] Change aai champ container processes to run as non-root on the host
[aai/champ.git] / champ-lib / champ-janus / src / main / java / org / onap / aai / champjanus / graph / impl / GraphSON.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * Modifications Copyright (C) 2019 IBM
8  * ===================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *        http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END============================================
21  */
22 package org.onap.aai.champjanus.graph.impl;
23
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.OutputStream;
27
28 import org.apache.tinkerpop.gremlin.structure.Direction;
29 import org.apache.tinkerpop.gremlin.structure.Vertex;
30 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper;
31 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter;
32 import org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry;
33 import org.onap.aai.champcore.FormatMapper;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.google.gson.JsonObject;
38 import com.google.gson.JsonParser;
39
40 public class GraphSON implements FormatMapper {
41     private static final Logger LOGGER = LoggerFactory.getLogger(GraphSON.class);
42     private final GraphSONMapper mapper;
43     private final GraphSONWriter writer;
44     protected JsonParser parser;
45
46     public GraphSON() {
47         this.mapper = GraphSONMapper.build().addRegistry(JanusGraphIoRegistry.getInstance()).create();
48         this.writer = GraphSONWriter.build().mapper(this.mapper).create();
49         this.parser = new JsonParser();
50     }
51
52     public JsonObject formatObject(Object v) {
53         OutputStream os = new ByteArrayOutputStream();
54         String result = "";
55
56         try {
57             this.writer.writeVertex(os, (Vertex) v, Direction.BOTH);
58             result = os.toString();
59         } catch (IOException var5) {
60             LOGGER.debug("Exception occured while formatting object : " + var5.getMessage());
61         }
62
63         return this.parser.parse(result).getAsJsonObject();
64     }
65
66     public int parallelThreshold() {
67         return 50;
68     }
69 }