2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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============================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.champ.event;
26 import javax.ws.rs.core.Response.Status;
28 import org.onap.aai.champcore.model.ChampObject;
29 import org.onap.champ.exception.ChampServiceException;
31 import com.google.gson.Gson;
32 import com.google.gson.GsonBuilder;
33 import com.google.gson.JsonElement;
34 import com.google.gson.JsonObject;
35 import com.google.gson.annotations.SerializedName;
36 import com.google.gson.reflect.TypeToken;
39 * This class provides a generic representation of a Vertex as provided by the
42 public class GraphEventVertex {
45 * The unique identifier used to identify this vertex in the graph data
48 @SerializedName("key")
51 @SerializedName("schema-version")
52 private String modelVersion;
55 * Type label assigned to this vertex.
60 * Map of all of the properties assigned to this vertex.
62 private JsonElement properties;
65 * Marshaller/unmarshaller for converting to/from JSON.
67 private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
69 public GraphEventVertex(String id, String modelVersion, String type, JsonElement properties) {
71 this.modelVersion = modelVersion;
73 this.properties = properties;
76 public GraphEventVertex() {
80 public String getId() {
84 public void setId(String id) {
88 public String getType() {
92 public void setType(String type) {
97 public JsonElement getProperties() {
101 public void setProperties(JsonElement properties) {
102 this.properties = properties;
105 public String getModelVersion() {
109 public void setModelVersion(String modelVersion) {
110 this.modelVersion = modelVersion;
114 * Unmarshalls this Vertex object into a JSON string.
116 * @return - A JSON format string representation of this Vertex.
118 public String toJson() {
119 return gson.toJson(this);
123 * Marshalls the provided JSON string into a Vertex object.
125 * @param json - The JSON string to produce the Vertex from.
126 * @return - A Vertex object.
127 * @throws SpikeException
129 public static GraphEventVertex fromJson(String json) throws ChampServiceException {
133 // Make sure that we were actually provided a non-empty string
136 if (json == null || json.isEmpty()) {
137 throw new ChampServiceException("Empty or null JSON string.", Status.BAD_REQUEST);
140 // Marshall the string into a Vertex object.
141 return gson.fromJson(json, GraphEventVertex.class);
143 } catch (Exception ex) {
144 throw new ChampServiceException("Unable to parse JSON string: ", Status.BAD_REQUEST);
149 public String toString() {
155 public static GraphEventVertex fromChampObject(ChampObject champObject, String modelVersion) {
157 java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
158 JsonObject props = gson.toJsonTree(champObject.getProperties(), mapType).getAsJsonObject();
159 GraphEventVertex graphEventVertex = new GraphEventVertex(champObject.getKey().orElse("").toString(),
160 modelVersion, champObject.getType(), props);
161 return graphEventVertex;
166 public ChampObject toChampObject() {
167 ChampObject.Builder builder = new ChampObject.Builder(this.getType());
168 if(this.getId()!=null && !this.getId().isEmpty()){
169 builder.key(this.getId());
172 if (this.getProperties() != null) {
173 java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
174 Map<String, Object> propertiesMap = gson.fromJson(this.getProperties(), mapType);
175 for (String key : propertiesMap.keySet()) {
176 builder.property(key, propertiesMap.get(key));
180 return builder.build();