2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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============================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.champ.event;
26 import javax.ws.rs.core.Response.Status;
28 import org.onap.aai.champcore.model.ChampObject;
29 import org.onap.aai.champcore.model.ChampRelationship;
30 import org.onap.champ.exception.ChampServiceException;
32 import com.google.gson.Gson;
33 import com.google.gson.GsonBuilder;
34 import com.google.gson.JsonElement;
35 import com.google.gson.JsonObject;
36 import com.google.gson.annotations.SerializedName;
37 import com.google.gson.reflect.TypeToken;
40 * This class provides a generic representation of an Edge as provided by the
43 public class GraphEventEdge {
46 * The unique identifier used to identify this edge in the graph data store.
48 @SerializedName("key")
51 @SerializedName("schema-version")
52 private String modelVersion;
55 * Type label assigned to this vertex.
60 * Source vertex for our edge.
62 private GraphEventVertex source;
65 * Target vertex for our edge.
67 private GraphEventVertex target;
70 * Map of all of the properties assigned to this vertex.
72 private JsonElement properties;
75 * Marshaller/unmarshaller for converting to/from JSON.
77 private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
79 public GraphEventEdge(String id, String modelVersion, String type, GraphEventVertex source,
80 GraphEventVertex target, JsonElement properties) {
82 this.modelVersion = modelVersion;
86 this.properties = properties;
89 public GraphEventEdge() {
93 public String getId() {
97 public void setId(String id) {
101 public String getType() {
105 public void setType(String type) {
109 public GraphEventVertex getSource() {
113 public void setSource(GraphEventVertex source) {
114 this.source = source;
117 public GraphEventVertex getTarget() {
121 public void setTarget(GraphEventVertex target) {
122 this.target = target;
125 public JsonElement getProperties() {
129 public void setProperties(JsonElement properties) {
130 this.properties = properties;
133 public String getModelVersion() {
137 public void setModelVersion(String modelVersion) {
138 this.modelVersion = modelVersion;
142 * Unmarshalls this Edge object into a JSON string.
144 * @return - A JSON format string representation of this Edge.
146 public String toJson() {
147 return gson.toJson(this);
151 * Marshalls the provided JSON string into a Edge object.
153 * @param json - The JSON string to produce the Edge from.
154 * @return - A Edge object.
155 * @throws SpikeException
157 public static GraphEventEdge fromJson(String json) throws ChampServiceException {
161 // Make sure that we were actually provided a non-empty string
164 if (json == null || json.isEmpty()) {
165 throw new ChampServiceException("Unable to parse JSON string: ", Status.BAD_REQUEST);
168 // Marshall the string into an Edge object.
169 return gson.fromJson(json, GraphEventEdge.class);
171 } catch (Exception ex) {
172 throw new ChampServiceException("Unable to parse JSON string: ", Status.BAD_REQUEST);
176 public static GraphEventEdge fromChampRelationship(ChampRelationship edge, String modelVersion) {
178 java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
179 JsonObject props = gson.toJsonTree(edge.getProperties(), mapType).getAsJsonObject();
181 GraphEventEdge graphEventEdge = new GraphEventEdge(edge.getKey().orElse("").toString(), modelVersion,
182 edge.getType(), new GraphEventVertex(edge.getSource().getKey().orElse("").toString(), null,
183 edge.getSource().getType(), null), new GraphEventVertex(edge.getTarget().getKey().orElse("").toString(),
184 null, edge.getTarget().getType(), null), props);
186 return graphEventEdge;
190 public ChampRelationship toChampRelationship() {
191 ChampObject sourceChampObject=null;
192 ChampObject targetChampObject=null;
193 if (this.getSource() != null) {
194 sourceChampObject = new ChampObject.Builder(this.getSource().getType()).key(this.getSource().getId())
197 if (this.getTarget() != null) {
198 targetChampObject = new ChampObject.Builder(this.getTarget().getType()).key(this.getTarget().getId())
202 ChampRelationship.Builder builder = new ChampRelationship.Builder(sourceChampObject, targetChampObject, type);
203 if(this.getId()!=null && !this.getId().isEmpty()){
204 builder.key(this.getId());
208 if (this.getProperties() != null) {
209 java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
210 Map<String, Object> propertiesMap = gson.fromJson(this.getProperties(), mapType);
211 for (String key : propertiesMap.keySet()) {
212 builder.property(key, propertiesMap.get(key));
215 return builder.build();