AAI-1523 Batch reformat aai-core
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / SimpleFormat.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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.onap.aai.serialization.queryformats;
22
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonObject;
25
26 import java.io.UnsupportedEncodingException;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Optional;
30
31 import org.apache.tinkerpop.gremlin.structure.Edge;
32 import org.apache.tinkerpop.gremlin.structure.Property;
33 import org.apache.tinkerpop.gremlin.structure.Vertex;
34 import org.onap.aai.db.props.AAIProperties;
35 import org.onap.aai.exceptions.AAIException;
36 import org.onap.aai.introspection.Introspector;
37 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
38 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
39
40 public class SimpleFormat extends RawFormat {
41
42     protected SimpleFormat(Builder builder) {
43         super(builder);
44
45     }
46
47     @Override
48     public int parallelThreshold() {
49         return 20;
50     }
51
52     @Override
53     public Optional<JsonObject> createPropertiesObject(Vertex v) throws AAIFormatVertexException {
54         try {
55             final Introspector obj =
56                     loader.introspectorFromName(v.<String>property(AAIProperties.NODE_TYPE).orElse(null));
57
58             final List<Vertex> wrapper = new ArrayList<>();
59
60             wrapper.add(v);
61
62             try {
63                 serializer.dbToObject(wrapper, obj, this.depth, true, "false");
64             } catch (AAIException | UnsupportedEncodingException e) {
65                 throw new AAIFormatVertexException(
66                         "Failed to format vertex - error while serializing: " + e.getMessage(), e);
67             }
68
69             final String json = obj.marshal(false);
70             return Optional.of(parser.parse(json).getAsJsonObject());
71         } catch (AAIUnknownObjectException e) {
72             return Optional.empty();
73         }
74
75     }
76
77     @Override
78     protected void addEdge(Edge e, Vertex v, JsonArray array) throws AAIFormatVertexException {
79
80         Property property = e.property("private");
81
82         if (property.isPresent()) {
83             if ("true".equals(e.property("private").value().toString())) {
84                 return;
85             }
86         }
87
88         super.addEdge(e, v, array);
89     }
90 }