4395c8c9e6126d9814c367d1abff1ce850126f2a
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / IdURL.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 com.google.gson.JsonObject;
25 import com.google.gson.JsonParser;
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.Loader;
31 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
32 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
33 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
34
35 public class IdURL extends MultiFormatMapper {
36
37         private final UrlBuilder urlBuilder;
38         private final JsonParser parser;
39         private final Loader loader;
40
41         public IdURL (Loader loader, UrlBuilder urlBuilder) throws AAIException {
42                 this.urlBuilder = urlBuilder;
43                 this.parser = new JsonParser();
44                 this.loader = loader;
45         }
46         
47         @Override
48         public int parallelThreshold() {
49                 return 2500;
50         }
51
52         @Override
53         protected JsonObject getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
54
55                 try {
56                         final Introspector searchResult = this.loader.introspectorFromName("result-data");
57
58                         searchResult.setValue("resource-type", v.value(AAIProperties.NODE_TYPE));
59                         searchResult.setValue("resource-link", this.urlBuilder.id(v));
60
61                         final String json = searchResult.marshal(false);
62
63                         return parser.parse(json).getAsJsonObject();
64                         
65                 } catch (AAIUnknownObjectException e) {
66                         throw new RuntimeException("Fatal error - result-data object does not exist!");
67                 }
68                         
69                 
70         }
71 }