2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.crud.dao.champ;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
29 import org.onap.crud.entity.Edge;
30 import org.onap.crud.entity.Vertex;
31 import org.onap.crud.exception.CrudException;
32 import org.onap.schema.OxmModelValidator;
33 import org.onap.schema.RelationshipSchemaValidator;
35 import com.google.gson.Gson;
36 import com.google.gson.GsonBuilder;
37 import com.google.gson.JsonElement;
38 import com.google.gson.JsonObject;
39 import com.google.gson.annotations.Expose;
40 import com.google.gson.annotations.SerializedName;
42 public class ChampBulkPayloadResponse {
44 private HashMap<String, Vertex> vertices = new HashMap<>();
45 private HashMap<String, Edge> edges = new HashMap<>();
49 @SerializedName(value = "objects")
50 private List<JsonElement> objects = new ArrayList<JsonElement>();
53 @SerializedName(value = "relationships")
54 private List<JsonElement> relationships = new ArrayList<JsonElement>();
58 private static final Gson gson = new GsonBuilder().disableHtmlEscaping().excludeFieldsWithoutExposeAnnotation()
62 public String toJson() {
63 return gson.toJson(this);
66 public static ChampBulkPayloadResponse fromJson(String payload) {
67 ChampBulkPayloadResponse response = gson.fromJson(payload, ChampBulkPayloadResponse.class);
73 public void populateChampData(String version) throws CrudException {
75 for (JsonElement object : this.getObjects()) {
77 JsonObject champObject = object.getAsJsonObject();
78 String itemKey = champObject.get("label").getAsString();
79 JsonObject vertexObject = champObject.get("vertex").getAsJsonObject();
80 Vertex vertex = OxmModelValidator.validateOutgoingPayload(version, buildVertex(vertexObject));
81 this.getVertices().put(itemKey, vertex);
84 for (JsonElement rel : this.getRelationships()) {
86 JsonObject champRelationship = rel.getAsJsonObject();
87 String itemKey = champRelationship.get("label").getAsString();
88 JsonObject relObject = champRelationship.get("edge").getAsJsonObject();
89 Edge edge = RelationshipSchemaValidator.validateOutgoingPayload(version, buildEdge(relObject));
90 this.getEdges().put(itemKey, edge);
98 private Edge buildEdge(JsonObject obj) {
99 JsonObject relKeyObject = obj.get("key").getAsJsonObject();
101 String relType = obj.get("type").getAsString();
102 String relKey = relKeyObject.get("value").getAsString();
104 Vertex source = buildVertex(obj.get("source").getAsJsonObject());
105 Vertex target = buildVertex(obj.get("target").getAsJsonObject());
107 Edge.Builder edgeBuilder = new Edge.Builder(relType).id(relKey).source(source)
110 if (obj.has("properties")) {
111 JsonObject propsObject = obj.get("properties").getAsJsonObject();
112 List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
113 propsObject.getAsJsonObject().entrySet());
115 for (Map.Entry<String, JsonElement> entry :entries) {
116 edgeBuilder.property(entry.getKey(), entry.getValue().getAsString());
121 return edgeBuilder.build();
124 private Vertex buildVertex(JsonObject obj) {
125 JsonObject vertexKeyObject = obj.get("key").getAsJsonObject();
127 String vertexType = obj.get("type").getAsString();
128 String vertexKey = vertexKeyObject.get("value").getAsString();
129 Vertex.Builder vertexBuilder = new Vertex.Builder(vertexType).id(vertexKey);
131 if (obj.has("properties")) {
132 JsonObject propsObject = obj.get("properties").getAsJsonObject();
133 List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
134 propsObject.getAsJsonObject().entrySet());
136 for (Map.Entry<String, JsonElement> entry :entries) {
137 vertexBuilder.property(entry.getKey(), entry.getValue().getAsString());
142 return vertexBuilder.build();
145 public HashMap<String, Edge> getEdges() {
149 public void setEdges(HashMap<String, Edge> edges) {
153 public List<JsonElement> getObjects() {
157 public void setObjects(List<JsonElement> objects) {
158 this.objects = objects;
161 public HashMap<String, Vertex> getVertices() {
165 public List<JsonElement> getRelationships() {
166 return relationships;