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.champ.event;
25 import javax.ws.rs.core.Response.Status;
27 import org.onap.aai.champcore.model.ChampObject;
28 import org.onap.aai.champcore.model.ChampRelationship;
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 an Edge as provided by the
42 public class GraphEventEdge {
45 * The unique identifier used to identify this edge in the graph data store.
47 @SerializedName("key")
50 @SerializedName("schema-version")
51 private String modelVersion;
54 * Type label assigned to this vertex.
59 * Source vertex for our edge.
61 private GraphEventVertex source;
64 * Target vertex for our edge.
66 private GraphEventVertex target;
69 * Map of all of the properties assigned to this vertex.
71 private JsonElement properties;
74 * Marshaller/unmarshaller for converting to/from JSON.
76 private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
78 public GraphEventEdge(String id, String modelVersion, String type, GraphEventVertex source,
79 GraphEventVertex target, JsonElement properties) {
81 this.modelVersion = modelVersion;
85 this.properties = properties;
88 public GraphEventEdge() {
92 public String getId() {
96 public void setId(String id) {
100 public String getType() {
104 public void setType(String type) {
108 public GraphEventVertex getSource() {
112 public void setSource(GraphEventVertex source) {
113 this.source = source;
116 public GraphEventVertex getTarget() {
120 public void setTarget(GraphEventVertex target) {
121 this.target = target;
124 public JsonElement getProperties() {
128 public void setProperties(JsonElement properties) {
129 this.properties = properties;
132 public String getModelVersion() {
136 public void setModelVersion(String modelVersion) {
137 this.modelVersion = modelVersion;
141 * Unmarshalls this Edge object into a JSON string.
143 * @return - A JSON format string representation of this Edge.
145 public String toJson() {
146 return gson.toJson(this);
150 * Marshalls the provided JSON string into a Edge object.
152 * @param json - The JSON string to produce the Edge from.
153 * @return - A Edge object.
154 * @throws SpikeException
156 public static GraphEventEdge fromJson(String json) throws ChampServiceException {
160 // Make sure that we were actually provided a non-empty string
163 if (json == null || json.isEmpty()) {
164 throw new ChampServiceException("Unable to parse JSON string: ", Status.BAD_REQUEST);
167 // Marshall the string into an Edge object.
168 return gson.fromJson(json, GraphEventEdge.class);
170 } catch (Exception ex) {
171 throw new ChampServiceException("Unable to parse JSON string: ", Status.BAD_REQUEST);
175 public static GraphEventEdge fromChampRelationship(ChampRelationship edge, String modelVersion) {
177 java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
178 JsonObject props = gson.toJsonTree(edge.getProperties(), mapType).getAsJsonObject();
180 GraphEventEdge graphEventEdge = new GraphEventEdge(edge.getKey().orElse("").toString(), modelVersion,
181 edge.getType(), new GraphEventVertex(edge.getSource().getKey().orElse("").toString(), null,
182 edge.getSource().getType(), null), new GraphEventVertex(edge.getTarget().getKey().orElse("").toString(),
183 null, edge.getTarget().getType(), null), props);
185 return graphEventEdge;
189 public ChampRelationship toChampRelationship() {
190 ChampObject sourceChampObject=null;
191 ChampObject targetChampObject=null;
192 if (this.getSource() != null) {
193 sourceChampObject = new ChampObject.Builder(this.getSource().getType()).key(this.getSource().getId())
196 if (this.getTarget() != null) {
197 targetChampObject = new ChampObject.Builder(this.getTarget().getType()).key(this.getTarget().getId())
201 ChampRelationship.Builder builder = new ChampRelationship.Builder(sourceChampObject, targetChampObject, type);
202 if(this.getId()!=null && !this.getId().isEmpty()){
203 builder.key(this.getId());
207 if (this.getProperties() != null) {
208 java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
209 Map<String, Object> propertiesMap = gson.fromJson(this.getProperties(), mapType);
210 for (String key : propertiesMap.keySet()) {
211 builder.property(key, propertiesMap.get(key));
214 return builder.build();