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