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