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.aai.champcore.model.ChampRelationship;
33 import org.onap.champ.exception.ChampServiceException;
35 import com.google.gson.Gson;
36 import com.google.gson.GsonBuilder;
37 import com.google.gson.JsonElement;
38 import com.google.gson.JsonObject;
39 import com.google.gson.annotations.SerializedName;
40 import com.google.gson.reflect.TypeToken;
43 * This class provides a generic representation of an Edge as provided by the
46 public class GraphEventEdge {
49 * The unique identifier used to identify this edge in the graph data store.
51 @SerializedName("key")
54 @SerializedName("schema-version")
55 private String modelVersion;
58 * Type label assigned to this vertex.
63 * Source vertex for our edge.
65 private GraphEventVertex source;
68 * Target vertex for our edge.
70 private GraphEventVertex target;
73 * Map of all of the properties assigned to this vertex.
75 private JsonElement properties;
78 * Marshaller/unmarshaller for converting to/from JSON.
80 private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
82 public GraphEventEdge(String id, String modelVersion, String type, GraphEventVertex source,
83 GraphEventVertex target, JsonElement properties) {
85 this.modelVersion = modelVersion;
89 this.properties = properties;
92 public GraphEventEdge() {
96 public String getId() {
100 public void setId(String id) {
104 public String getType() {
108 public void setType(String type) {
112 public GraphEventVertex getSource() {
116 public void setSource(GraphEventVertex source) {
117 this.source = source;
120 public GraphEventVertex getTarget() {
124 public void setTarget(GraphEventVertex target) {
125 this.target = target;
128 public JsonElement getProperties() {
132 public void setProperties(JsonElement properties) {
133 this.properties = properties;
136 public String getModelVersion() {
140 public void setModelVersion(String modelVersion) {
141 this.modelVersion = modelVersion;
145 * Unmarshalls this Edge object into a JSON string.
147 * @return - A JSON format string representation of this Edge.
149 public String toJson() {
150 return gson.toJson(this);
154 * Marshalls the provided JSON string into a Edge object.
156 * @param json - The JSON string to produce the Edge from.
157 * @return - A Edge object.
158 * @throws SpikeException
160 public static GraphEventEdge fromJson(String json) throws ChampServiceException {
164 // Make sure that we were actually provided a non-empty string
167 if (json == null || json.isEmpty()) {
168 throw new ChampServiceException("Unable to parse JSON string: ", Status.BAD_REQUEST);
171 // Marshall the string into an Edge object.
172 return gson.fromJson(json, GraphEventEdge.class);
174 } catch (Exception ex) {
175 throw new ChampServiceException("Unable to parse JSON string: ", Status.BAD_REQUEST);
179 public static GraphEventEdge fromChampRelationship(ChampRelationship edge, String modelVersion) {
181 java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
182 JsonObject props = gson.toJsonTree(edge.getProperties(), mapType).getAsJsonObject();
184 GraphEventEdge graphEventEdge = new GraphEventEdge(edge.getKey().orElse("").toString(), modelVersion,
185 edge.getType(), new GraphEventVertex(edge.getSource().getKey().orElse("").toString(), null,
186 edge.getSource().getType(), null), new GraphEventVertex(edge.getTarget().getKey().orElse("").toString(),
187 null, edge.getTarget().getType(), null), props);
189 return graphEventEdge;
193 public ChampRelationship toChampRelationship() {
194 ChampObject sourceChampObject=null;
195 ChampObject targetChampObject=null;
196 if (this.getSource() != null) {
197 sourceChampObject = new ChampObject.Builder(this.getSource().getType()).key(this.getSource().getId())
200 if (this.getTarget() != null) {
201 targetChampObject = new ChampObject.Builder(this.getTarget().getType()).key(this.getTarget().getId())
205 ChampRelationship.Builder builder = new ChampRelationship.Builder(sourceChampObject, targetChampObject, type);
206 if(this.getId()!=null && !this.getId().isEmpty()){
207 builder.key(this.getId());
211 if (this.getProperties() != null) {
212 java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
213 Map<String, Object> propertiesMap = gson.fromJson(this.getProperties(), mapType);
214 for (String key : propertiesMap.keySet()) {
215 builder.property(key, propertiesMap.get(key));
218 return builder.build();