2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.aai.serialization.queryformats;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.List;
30 import org.apache.tinkerpop.gremlin.structure.Direction;
31 import org.apache.tinkerpop.gremlin.structure.Vertex;
32 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
34 import org.openecomp.aai.db.props.AAIProperties;
35 import org.openecomp.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
36 import org.openecomp.aai.serialization.queryformats.utils.UrlBuilder;
37 import com.google.gson.Gson;
38 import com.google.gson.JsonArray;
39 import com.google.gson.JsonObject;
41 public class SimpleFormat implements FormatMapper {
43 private final UrlBuilder urlBuilder;
44 private final Set<String> blacklist;
45 private final Collection<String> props = Arrays.asList(AAIProperties.AAI_URI, AAIProperties.NODE_TYPE);
46 public SimpleFormat(UrlBuilder urlBuilder) {
47 this.urlBuilder = urlBuilder;
48 this.blacklist = new HashSet<>();
49 blacklist.addAll(props);
53 public JsonObject formatObject(Object input) throws AAIFormatVertexException {
54 Vertex v = (Vertex)input;
55 JsonObject json = new JsonObject();
56 json.addProperty("id", v.id().toString());
57 json.addProperty("node-type", v.<String>value(AAIProperties.NODE_TYPE));
58 json.addProperty("url", this.urlBuilder.pathed(v));
59 json.add("properties", this.createPropertiesObject(v));
60 json.add("related-to", this.createRelationshipObject(v));
66 public int parallelThreshold() {
70 private JsonObject createPropertiesObject(Vertex v) {
71 JsonObject json = new JsonObject();
72 Iterator<VertexProperty<Object>> iter = v.properties();
74 while (iter.hasNext()) {
75 VertexProperty<Object> prop = iter.next();
76 if (!blacklist.contains(prop.key())) {
77 if (prop.value() instanceof String) {
78 json.addProperty(prop.key(), (String)prop.value());
79 } else if (prop.value() instanceof Boolean) {
80 json.addProperty(prop.key(), (Boolean)prop.value());
81 } else if (prop.value() instanceof Number) {
82 json.addProperty(prop.key(), (Number)prop.value());
83 } else if (prop.value() instanceof List) {
84 Gson gson = new Gson();
85 String list = gson.toJson(prop.value());
87 json.addProperty(prop.key(), list);
98 private JsonArray createRelationshipObject(Vertex v) throws AAIFormatVertexException {
99 JsonArray jarray = new JsonArray();
100 Iterator<Vertex> iter = v.vertices(Direction.BOTH);
102 while (iter.hasNext()) {
103 Vertex related = iter.next();
105 JsonObject json = new JsonObject();
106 json.addProperty("id", related.id().toString());
107 json.addProperty("node-type", related.<String>value(AAIProperties.NODE_TYPE));
108 json.addProperty("url", this.urlBuilder.pathed(related));