Removed below sonar issues:
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / serialization / queryformats / SimpleFormat.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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
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.openecomp.aai.serialization.queryformats;
22
23 import java.io.UnsupportedEncodingException;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28 import org.openecomp.aai.db.props.AAIProperties;
29 import org.openecomp.aai.exceptions.AAIException;
30 import org.openecomp.aai.introspection.Introspector;
31 import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
32 import org.openecomp.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
33
34 import com.google.gson.JsonObject;
35
36 public class SimpleFormat extends RawFormat {
37
38
39         protected SimpleFormat(Builder builder) {
40                 super(builder);
41         
42         }
43         
44         @Override
45         public int parallelThreshold() {
46                 return 20;
47         }
48         
49         @Override
50         public JsonObject createPropertiesObject(Vertex v) throws AAIFormatVertexException {
51                 try {
52                         final Introspector obj = loader.introspectorFromName(
53                                                                                 v.<String>property(AAIProperties.NODE_TYPE)
54                                                                                         .orElse(null)
55                                                                          );
56
57                         final List<Vertex> wrapper = new ArrayList<>();
58
59                         wrapper.add(v);
60
61                         try {
62                                 serializer.dbToObject(wrapper, obj, this.depth, true, "false");
63                         } catch (AAIException | UnsupportedEncodingException  e) {
64                                 throw new AAIFormatVertexException("Failed to format vertex - error while serializing: " + e.getMessage(), e);
65                         }
66
67                         final String json = obj.marshal(false);
68                         return parser.parse(json).getAsJsonObject();
69                 } catch (AAIUnknownObjectException e) {
70                         throw new AAIFormatVertexException("Failed to format vertex - unknown object", e);
71                 }
72                 
73
74         }
75 }