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============================================
21 package org.onap.champ.event;
25 import javax.ws.rs.core.Response.Status;
27 import org.onap.aai.champcore.model.ChampObject;
28 import org.onap.champ.exception.ChampServiceException;
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
32 import com.google.gson.JsonElement;
33 import com.google.gson.JsonObject;
34 import com.google.gson.annotations.SerializedName;
35 import com.google.gson.reflect.TypeToken;
38 * This class provides a generic representation of a Vertex as provided by the
41 public class GraphEventVertex {
44 * The unique identifier used to identify this vertex in the graph data
47 @SerializedName("key")
50 @SerializedName("schema-version")
51 private String modelVersion;
54 * Type label assigned to this vertex.
59 * Map of all of the properties assigned to this vertex.
61 private JsonElement properties;
64 * Marshaller/unmarshaller for converting to/from JSON.
66 private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
68 public GraphEventVertex(String id, String modelVersion, String type, JsonElement properties) {
70 this.modelVersion = modelVersion;
72 this.properties = properties;
75 public GraphEventVertex() {
79 public String getId() {
83 public void setId(String id) {
87 public String getType() {
91 public void setType(String type) {
96 public JsonElement getProperties() {
100 public void setProperties(JsonElement properties) {
101 this.properties = properties;
104 public String getModelVersion() {
108 public void setModelVersion(String modelVersion) {
109 this.modelVersion = modelVersion;
113 * Unmarshalls this Vertex object into a JSON string.
115 * @return - A JSON format string representation of this Vertex.
117 public String toJson() {
118 return gson.toJson(this);
122 * Marshalls the provided JSON string into a Vertex object.
124 * @param json - The JSON string to produce the Vertex from.
125 * @return - A Vertex object.
126 * @throws SpikeException
128 public static GraphEventVertex fromJson(String json) throws ChampServiceException {
132 // Make sure that we were actually provided a non-empty string
135 if (json == null || json.isEmpty()) {
136 throw new ChampServiceException("Empty or null JSON string.", Status.BAD_REQUEST);
139 // Marshall the string into a Vertex object.
140 return gson.fromJson(json, GraphEventVertex.class);
142 } catch (Exception ex) {
143 throw new ChampServiceException("Unable to parse JSON string: ", Status.BAD_REQUEST);
148 public String toString() {
154 public static GraphEventVertex fromChampObject(ChampObject champObject, String modelVersion) {
156 java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
157 JsonObject props = gson.toJsonTree(champObject.getProperties(), mapType).getAsJsonObject();
158 GraphEventVertex graphEventVertex = new GraphEventVertex(champObject.getKey().orElse("").toString(),
159 modelVersion, champObject.getType(), props);
160 return graphEventVertex;
165 public ChampObject toChampObject() {
166 ChampObject.Builder builder = new ChampObject.Builder(this.getType());
167 if(this.getId()!=null && !this.getId().isEmpty()){
168 builder.key(this.getId());
171 if (this.getProperties() != null) {
172 java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
173 Map<String, Object> propertiesMap = gson.fromJson(this.getProperties(), mapType);
174 for (String key : propertiesMap.keySet()) {
175 builder.property(key, propertiesMap.get(key));
179 return builder.build();