2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.champ.event;
29 import javax.ws.rs.core.Response.Status;
31 import org.onap.aai.champcore.model.ChampObject;
32 import org.onap.champ.exception.ChampServiceException;
34 import com.google.gson.Gson;
35 import com.google.gson.GsonBuilder;
36 import com.google.gson.JsonElement;
37 import com.google.gson.JsonObject;
38 import com.google.gson.annotations.SerializedName;
39 import com.google.gson.reflect.TypeToken;
42 * This class provides a generic representation of a Vertex as provided by the
45 public class GraphEventVertex {
48 * The unique identifier used to identify this vertex in the graph data
51 @SerializedName("key")
54 @SerializedName("schema-version")
55 private String modelVersion;
58 * Type label assigned to this vertex.
63 * Map of all of the properties assigned to this vertex.
65 private JsonElement properties;
68 * Marshaller/unmarshaller for converting to/from JSON.
70 private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
72 public GraphEventVertex(String id, String modelVersion, String type, JsonElement properties) {
74 this.modelVersion = modelVersion;
76 this.properties = properties;
79 public GraphEventVertex() {
83 public String getId() {
87 public void setId(String id) {
91 public String getType() {
95 public void setType(String type) {
100 public JsonElement getProperties() {
104 public void setProperties(JsonElement properties) {
105 this.properties = properties;
108 public String getModelVersion() {
112 public void setModelVersion(String modelVersion) {
113 this.modelVersion = modelVersion;
117 * Unmarshalls this Vertex object into a JSON string.
119 * @return - A JSON format string representation of this Vertex.
121 public String toJson() {
122 return gson.toJson(this);
126 * Marshalls the provided JSON string into a Vertex object.
128 * @param json - The JSON string to produce the Vertex from.
129 * @return - A Vertex object.
130 * @throws SpikeException
132 public static GraphEventVertex fromJson(String json) throws ChampServiceException {
136 // Make sure that we were actually provided a non-empty string
139 if (json == null || json.isEmpty()) {
140 throw new ChampServiceException("Empty or null JSON string.", Status.BAD_REQUEST);
143 // Marshall the string into a Vertex object.
144 return gson.fromJson(json, GraphEventVertex.class);
146 } catch (Exception ex) {
147 throw new ChampServiceException("Unable to parse JSON string: ", Status.BAD_REQUEST);
152 public String toString() {
158 public static GraphEventVertex fromChampObject(ChampObject champObject, String modelVersion) {
160 java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
161 JsonObject props = gson.toJsonTree(champObject.getProperties(), mapType).getAsJsonObject();
162 GraphEventVertex graphEventVertex = new GraphEventVertex(champObject.getKey().orElse("").toString(),
163 modelVersion, champObject.getType(), props);
164 return graphEventVertex;
169 public ChampObject toChampObject() {
170 ChampObject.Builder builder = new ChampObject.Builder(this.getType());
171 if(this.getId()!=null && !this.getId().isEmpty()){
172 builder.key(this.getId());
175 if (this.getProperties() != null) {
176 java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
177 Map<String, Object> propertiesMap = gson.fromJson(this.getProperties(), mapType);
178 for (String key : propertiesMap.keySet()) {
179 builder.property(key, propertiesMap.get(key));
183 return builder.build();