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.Edge;
31 import org.onap.crud.entity.Vertex;
32 import org.onap.crud.exception.CrudException;
35 import javax.ws.rs.core.Response.Status;
38 * This class provides a generic representation of an Edge as provided by the
41 public class GraphEventEdge {
44 * The unique identifier used to identify this edge in the graph data store.
46 @SerializedName("key")
49 @SerializedName("schema-version")
50 private String modelVersion;
53 * Type label assigned to this vertex.
58 * Source vertex for our edge.
60 private GraphEventVertex source;
63 * Target vertex for our edge.
65 private GraphEventVertex target;
68 * Map of all of the properties assigned to this vertex.
70 private JsonElement properties;
73 * Marshaller/unmarshaller for converting to/from JSON.
75 private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
77 public GraphEventEdge(String id, String modelVersion, String type, GraphEventVertex source,
78 GraphEventVertex target, JsonElement properties) {
80 this.modelVersion = modelVersion;
84 this.properties = properties;
87 public GraphEventEdge() {
91 public String getId() {
95 public void setId(String id) {
99 public String getType() {
103 public void setType(String type) {
107 public GraphEventVertex getSource() {
111 public void setSource(GraphEventVertex source) {
112 this.source = source;
115 public GraphEventVertex getTarget() {
119 public void setTarget(GraphEventVertex target) {
120 this.target = target;
123 public JsonElement getProperties() {
127 public void setProperties(JsonElement properties) {
128 this.properties = properties;
131 public String getModelVersion() {
135 public void setModelVersion(String modelVersion) {
136 this.modelVersion = modelVersion;
140 * Unmarshalls this Edge object into a JSON string.
142 * @return - A JSON format string representation of this Edge.
144 public String toJson() {
145 return gson.toJson(this);
149 * Marshalls the provided JSON string into a Edge object.
151 * @param json - The JSON string to produce the Edge from.
152 * @return - A Edge object.
153 * @throws SpikeException
155 public static GraphEventEdge fromJson(String json) throws CrudException {
159 // Make sure that we were actually provided a non-empty string
162 if (json == null || json.isEmpty()) {
163 throw new CrudException("Unable to parse JSON string: ", Status.BAD_REQUEST);
166 // Marshall the string into an Edge object.
167 return gson.fromJson(json, GraphEventEdge.class);
169 } catch (Exception ex) {
170 throw new CrudException("Unable to parse JSON string: ", Status.BAD_REQUEST);
174 public static GraphEventEdge fromEdge(Edge edge, String modelVersion) {
176 java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
177 JsonObject props = gson.toJsonTree(edge.getProperties(), mapType).getAsJsonObject();
179 GraphEventEdge graphEventEdge = new GraphEventEdge(edge.getId().orElse(""), modelVersion,
180 edge.getType(), new GraphEventVertex(edge.getSource().getId().orElse(""), null,
181 edge.getSource().getType(), null), new GraphEventVertex(edge.getTarget().getId().orElse(""),
182 null, edge.getTarget().getType(), null), props);
184 return graphEventEdge;
188 public Edge toEdge() {
189 Edge.Builder builder = new Edge.Builder(this.getType()).id(this.getId());
190 if (this.getSource() != null) {
191 builder.source(new Vertex.Builder(this.getSource().getType()).id(this.getSource().getId())
194 if (this.getTarget() != null) {
195 builder.target(new Vertex.Builder(this.getTarget().getType()).id(this.getTarget().getId())
199 if (this.getProperties() != null) {
200 java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
201 Map<String, Object> propertiesMap = gson.fromJson(this.getProperties(), mapType);
202 for (String key : propertiesMap.keySet()) {
203 builder.property(key, propertiesMap.get(key));
206 return builder.build();