ba49e3a37c183d7b42488492f9fbf513e0e74ebf
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.serialization.queryformats;
23
24 import java.io.UnsupportedEncodingException;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.apache.tinkerpop.gremlin.structure.Vertex;
29 import org.onap.aai.db.props.AAIProperties;
30 import org.onap.aai.exceptions.AAIException;
31 import org.onap.aai.introspection.Introspector;
32 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
33 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
34
35 import com.google.gson.JsonObject;
36
37 public class SimpleFormat extends RawFormat {
38
39
40         protected SimpleFormat(Builder builder) {
41                 super(builder);
42         
43         }
44         
45         @Override
46         public int parallelThreshold() {
47                 return 20;
48         }
49         
50         @Override
51         public JsonObject createPropertiesObject(Vertex v) throws AAIFormatVertexException {
52                 try {
53                         final Introspector obj = loader.introspectorFromName(
54                                                                                 v.<String>property(AAIProperties.NODE_TYPE)
55                                                                                         .orElse(null)
56                                                                          );
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("Failed to format vertex - error while serializing: " + e.getMessage(), e);
66                         }
67
68                         final String json = obj.marshal(false);
69                         return parser.parse(json).getAsJsonObject();
70                 } catch (AAIUnknownObjectException e) {
71                         throw new AAIFormatVertexException("Failed to format vertex - unknown object", e);
72                 }
73                 
74
75         }
76 }