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.crud.event;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import com.google.gson.annotations.SerializedName;
28 import com.google.gson.reflect.TypeToken;
30 import org.onap.crud.entity.Vertex;
31 import org.onap.crud.exception.CrudException;
34 import javax.ws.rs.core.Response.Status;
37 * This class provides a generic representation of a Vertex as provided by the
40 public class GraphEventVertex {
43 * The unique identifier used to identify this vertex in the graph data
46 @SerializedName("key")
49 @SerializedName("schema-version")
50 private String modelVersion;
53 * Type label assigned to this vertex.
58 * Map of all of the properties assigned to this vertex.
60 private JsonElement properties;
63 * Marshaller/unmarshaller for converting to/from JSON.
65 private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
67 public GraphEventVertex(String id, String modelVersion, String type, JsonElement properties) {
69 this.modelVersion = modelVersion;
71 this.properties = properties;
74 public GraphEventVertex() {
78 public String getId() {
82 public void setId(String id) {
86 public String getType() {
90 public void setType(String type) {
95 public JsonElement getProperties() {
99 public void setProperties(JsonElement properties) {
100 this.properties = properties;
103 public String getModelVersion() {
107 public void setModelVersion(String modelVersion) {
108 this.modelVersion = modelVersion;
112 * Unmarshalls this Vertex object into a JSON string.
114 * @return - A JSON format string representation of this Vertex.
116 public String toJson() {
117 return gson.toJson(this);
121 * Marshalls the provided JSON string into a Vertex object.
123 * @param json - The JSON string to produce the Vertex from.
124 * @return - A Vertex object.
125 * @throws SpikeException
127 public static GraphEventVertex fromJson(String json) throws CrudException {
131 // Make sure that we were actually provided a non-empty string
134 if (json == null || json.isEmpty()) {
135 throw new CrudException("Empty or null JSON string.", Status.BAD_REQUEST);
138 // Marshall the string into a Vertex object.
139 return gson.fromJson(json, GraphEventVertex.class);
141 } catch (Exception ex) {
142 throw new CrudException("Unable to parse JSON string: ", Status.BAD_REQUEST);
147 public String toString() {
152 public static GraphEventVertex fromVertex(Vertex vertex, String modelVersion) {
154 java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
155 JsonObject props = gson.toJsonTree(vertex.getProperties(), mapType).getAsJsonObject();
156 GraphEventVertex graphEventVertex = new GraphEventVertex(vertex.getId().orElse(""),
157 modelVersion, vertex.getType(), props);
158 return graphEventVertex;
162 public Vertex toVertex() {
163 Vertex.Builder builder = new Vertex.Builder(this.getType()).id(this.getId());
165 if (this.getProperties() != null) {
166 java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
167 Map<String, Object> propertiesMap = gson.fromJson(this.getProperties(), mapType);
168 for (String key : propertiesMap.keySet()) {
169 builder.property(key, propertiesMap.get(key));
173 return builder.build();