58ea4a7a57dcad3d709c4ce1b35b251097761d0b
[aai/gizmo.git] / src / main / java / org / onap / crud / event / GraphEventEdge.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Gizmo
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  *
22  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23  */
24 package org.onap.crud.event;
25
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;
32
33 import org.onap.crud.entity.Edge;
34 import org.onap.crud.entity.Vertex;
35 import org.onap.crud.exception.CrudException;
36
37 import java.util.Map;
38 import javax.ws.rs.core.Response.Status;
39
40 /**
41  * This class provides a generic representation of an Edge as provided by the
42  * graph data store.
43  */
44 public class GraphEventEdge {
45
46   /**
47    * The unique identifier used to identify this edge in the graph data store.
48    */
49   @SerializedName("key")
50   private String id;
51
52   @SerializedName("schema-version")
53   private String modelVersion;
54
55   /**
56    * Type label assigned to this vertex.
57    */
58   private String type;
59
60   /**
61    * Source vertex for our edge.
62    */
63   private GraphEventVertex source;
64
65   /**
66    * Target vertex for our edge.
67    */
68   private GraphEventVertex target;
69
70   /**
71    * Map of all of the properties assigned to this vertex.
72    */
73   private JsonElement properties;
74
75   /**
76    * Marshaller/unmarshaller for converting to/from JSON.
77    */
78   private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
79
80   public GraphEventEdge(String id, String modelVersion, String type, GraphEventVertex source,
81                         GraphEventVertex target, JsonElement properties) {
82     this.id = id;
83     this.modelVersion = modelVersion;
84     this.type = type;
85     this.source = source;
86     this.target = target;
87     this.properties = properties;
88   }
89
90   public GraphEventEdge() {
91
92   }
93
94   public String getId() {
95     return id;
96   }
97
98   public void setId(String id) {
99     this.id = id;
100   }
101
102   public String getType() {
103     return type;
104   }
105
106   public void setType(String type) {
107     this.type = type;
108   }
109
110   public GraphEventVertex getSource() {
111     return source;
112   }
113
114   public void setSource(GraphEventVertex source) {
115     this.source = source;
116   }
117
118   public GraphEventVertex getTarget() {
119     return target;
120   }
121
122   public void setTarget(GraphEventVertex target) {
123     this.target = target;
124   }
125
126   public JsonElement getProperties() {
127     return properties;
128   }
129
130   public void setProperties(JsonElement properties) {
131     this.properties = properties;
132   }
133
134   public String getModelVersion() {
135     return modelVersion;
136   }
137
138   public void setModelVersion(String modelVersion) {
139     this.modelVersion = modelVersion;
140   }
141
142   /**
143    * Unmarshalls this Edge object into a JSON string.
144    *
145    * @return - A JSON format string representation of this Edge.
146    */
147   public String toJson() {
148     return gson.toJson(this);
149   }
150
151   /**
152    * Marshalls the provided JSON string into a Edge object.
153    *
154    * @param json - The JSON string to produce the Edge from.
155    * @return - A Edge object.
156    * @throws SpikeException
157    */
158   public static GraphEventEdge fromJson(String json) throws CrudException {
159
160     try {
161
162       // Make sure that we were actually provided a non-empty string
163       // before we
164       // go any further.
165       if (json == null || json.isEmpty()) {
166         throw new CrudException("Unable to parse JSON string: ", Status.BAD_REQUEST);
167       }
168
169       // Marshall the string into an Edge object.
170       return gson.fromJson(json, GraphEventEdge.class);
171
172     } catch (Exception ex) {
173       throw new CrudException("Unable to parse JSON string: ", Status.BAD_REQUEST);
174     }
175   }
176
177   public static GraphEventEdge fromEdge(Edge edge, String modelVersion) {
178
179     java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
180     JsonObject props = gson.toJsonTree(edge.getProperties(), mapType).getAsJsonObject();
181
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);
186
187     return graphEventEdge;
188
189   }
190
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())
195           .build());
196     }
197     if (this.getTarget() != null) {
198       builder.target(new Vertex.Builder(this.getTarget().getType()).id(this.getTarget().getId())
199           .build());
200     }
201
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));
207       }
208     }
209     return builder.build();
210
211   }
212 }