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 is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.crud.event;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonElement;
29 import com.google.gson.JsonObject;
30 import com.google.gson.annotations.SerializedName;
31 import com.google.gson.reflect.TypeToken;
33 import org.onap.crud.entity.Edge;
34 import org.onap.crud.entity.Vertex;
35 import org.onap.crud.exception.CrudException;
38 import javax.ws.rs.core.Response.Status;
41 * This class provides a generic representation of an Edge as provided by the
44 public class GraphEventEdge {
47 * The unique identifier used to identify this edge in the graph data store.
49 @SerializedName("key")
52 @SerializedName("schema-version")
53 private String modelVersion;
56 * Type label assigned to this vertex.
61 * Source vertex for our edge.
63 private GraphEventVertex source;
66 * Target vertex for our edge.
68 private GraphEventVertex target;
71 * Map of all of the properties assigned to this vertex.
73 private JsonElement properties;
76 * Marshaller/unmarshaller for converting to/from JSON.
78 private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
80 public GraphEventEdge(String id, String modelVersion, String type, GraphEventVertex source,
81 GraphEventVertex target, JsonElement properties) {
83 this.modelVersion = modelVersion;
87 this.properties = properties;
90 public GraphEventEdge() {
94 public String getId() {
98 public void setId(String id) {
102 public String getType() {
106 public void setType(String type) {
110 public GraphEventVertex getSource() {
114 public void setSource(GraphEventVertex source) {
115 this.source = source;
118 public GraphEventVertex getTarget() {
122 public void setTarget(GraphEventVertex target) {
123 this.target = target;
126 public JsonElement getProperties() {
130 public void setProperties(JsonElement properties) {
131 this.properties = properties;
134 public String getModelVersion() {
138 public void setModelVersion(String modelVersion) {
139 this.modelVersion = modelVersion;
143 * Unmarshalls this Edge object into a JSON string.
145 * @return - A JSON format string representation of this Edge.
147 public String toJson() {
148 return gson.toJson(this);
152 * Marshalls the provided JSON string into a Edge object.
154 * @param json - The JSON string to produce the Edge from.
155 * @return - A Edge object.
156 * @throws SpikeException
158 public static GraphEventEdge fromJson(String json) throws CrudException {
162 // Make sure that we were actually provided a non-empty string
165 if (json == null || json.isEmpty()) {
166 throw new CrudException("Unable to parse JSON string: ", Status.BAD_REQUEST);
169 // Marshall the string into an Edge object.
170 return gson.fromJson(json, GraphEventEdge.class);
172 } catch (Exception ex) {
173 throw new CrudException("Unable to parse JSON string: ", Status.BAD_REQUEST);
177 public static GraphEventEdge fromEdge(Edge edge, String modelVersion) {
179 java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
180 JsonObject props = gson.toJsonTree(edge.getProperties(), mapType).getAsJsonObject();
182 GraphEventEdge graphEventEdge = new GraphEventEdge(edge.getId().orElse(""), modelVersion,
183 edge.getType(), new GraphEventVertex(edge.getSource().getId().orElse(""), null,
184 edge.getSource().getType(), null), new GraphEventVertex(edge.getTarget().getId().orElse(""),
185 null, edge.getTarget().getType(), null), props);
187 return graphEventEdge;
191 public Edge toEdge() {
192 Edge.Builder builder = new Edge.Builder(this.getType()).id(this.getId());
193 if (this.getSource() != null) {
194 builder.source(new Vertex.Builder(this.getSource().getType()).id(this.getSource().getId())
197 if (this.getTarget() != null) {
198 builder.target(new Vertex.Builder(this.getTarget().getType()).id(this.getTarget().getId())
202 if (this.getProperties() != null) {
203 java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
204 Map<String, Object> propertiesMap = gson.fromJson(this.getProperties(), mapType);
205 for (String key : propertiesMap.keySet()) {
206 builder.property(key, propertiesMap.get(key));
209 return builder.build();