Integrate aai-schema-ingest library into 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 package org.onap.aai.serialization.queryformats;
21
22 import com.google.gson.JsonArray;
23 import com.google.gson.JsonObject;
24 import org.apache.tinkerpop.gremlin.structure.Edge;
25 import org.apache.tinkerpop.gremlin.structure.Property;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.onap.aai.db.props.AAIProperties;
28 import org.onap.aai.exceptions.AAIException;
29 import org.onap.aai.introspection.Introspector;
30 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
31 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
32
33 import java.io.UnsupportedEncodingException;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Optional;
37
38 public class SimpleFormat extends RawFormat {
39
40
41         protected SimpleFormat(Builder builder) {
42                 super(builder);
43         
44         }
45         
46         @Override
47         public int parallelThreshold() {
48                 return 20;
49         }
50         
51         @Override
52         public Optional<JsonObject> createPropertiesObject(Vertex v) throws AAIFormatVertexException {
53                 try {
54                         final Introspector obj = loader.introspectorFromName(
55                                                                                 v.<String>property(AAIProperties.NODE_TYPE)
56                                                                                         .orElse(null)
57                                                                          );
58
59                         final List<Vertex> wrapper = new ArrayList<>();
60
61                         wrapper.add(v);
62
63                         try {
64                                 serializer.dbToObject(wrapper, obj, this.depth, true, "false");
65                         } catch (AAIException | UnsupportedEncodingException  e) {
66                                 throw new AAIFormatVertexException("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
78         @Override
79         protected void addEdge(Edge e, Vertex v,  JsonArray array) throws AAIFormatVertexException {
80
81                 Property property = e.property("private");
82
83                 if(property.isPresent()){
84                     if("true".equals(e.property("private").value().toString())){
85                         return;
86                         }
87                 }
88
89                 super.addEdge(e, v, array);
90         }
91 }