Performance Improvements for Gizmo bulk API
[aai/gizmo.git] / src / main / java / org / onap / crud / dao / champ / ChampBulkPayloadResponse.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.crud.dao.champ;
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
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;
34
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;
41
42 public class ChampBulkPayloadResponse {
43
44   private HashMap<String, Vertex> vertices = new HashMap<>();
45   private HashMap<String, Edge> edges = new HashMap<>();
46
47
48   @Expose
49   @SerializedName(value = "objects")
50   private List<JsonElement> objects = new ArrayList<JsonElement>();
51
52   @Expose
53   @SerializedName(value = "relationships")
54   private List<JsonElement> relationships = new ArrayList<JsonElement>();
55
56
57
58   private static final Gson gson = new GsonBuilder().disableHtmlEscaping().excludeFieldsWithoutExposeAnnotation()
59           .create();
60
61
62   public String toJson() {
63     return  gson.toJson(this);
64   }
65
66   public static ChampBulkPayloadResponse fromJson(String payload) {
67     ChampBulkPayloadResponse response =  gson.fromJson(payload, ChampBulkPayloadResponse.class);
68     return response;
69   }
70
71
72
73   public void populateChampData(String version) throws CrudException {
74
75     for (JsonElement object : this.getObjects()) {
76
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);
82     }
83
84     for (JsonElement rel : this.getRelationships()) {
85
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);
91
92     }
93
94
95   }
96
97
98   private Edge buildEdge(JsonObject obj) {
99     JsonObject relKeyObject = obj.get("key").getAsJsonObject();
100
101     String relType = obj.get("type").getAsString();
102     String relKey = relKeyObject.get("value").getAsString();
103
104     Vertex source = buildVertex(obj.get("source").getAsJsonObject());
105     Vertex target = buildVertex(obj.get("target").getAsJsonObject());
106
107     Edge.Builder edgeBuilder = new Edge.Builder(relType).id(relKey).source(source)
108             .target(target);
109
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());
114
115       for (Map.Entry<String, JsonElement> entry :entries) {
116         edgeBuilder.property(entry.getKey(), entry.getValue().getAsString());
117       }
118
119     }
120
121     return edgeBuilder.build();
122   }
123
124   private Vertex buildVertex(JsonObject obj) {
125     JsonObject vertexKeyObject = obj.get("key").getAsJsonObject();
126
127     String vertexType = obj.get("type").getAsString();
128     String vertexKey = vertexKeyObject.get("value").getAsString();
129     Vertex.Builder vertexBuilder = new Vertex.Builder(vertexType).id(vertexKey);
130
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());
135
136       for (Map.Entry<String, JsonElement> entry :entries) {
137         vertexBuilder.property(entry.getKey(), entry.getValue().getAsString());
138       }
139
140     }
141
142     return vertexBuilder.build();
143   }
144
145   public HashMap<String, Edge> getEdges() {
146     return edges;
147   }
148
149   public void setEdges(HashMap<String, Edge> edges) {
150     this.edges = edges;
151   }
152
153   public List<JsonElement> getObjects() {
154     return objects;
155   }
156
157   public void setObjects(List<JsonElement> objects) {
158     this.objects = objects;
159   }
160
161   public HashMap<String, Vertex> getVertices() {
162     return vertices;
163   }
164
165   public List<JsonElement> getRelationships() {
166     return relationships;
167   }
168
169
170
171
172 }