Update license date and text
[aai/gizmo.git] / src / main / java / org / onap / crud / event / GraphEventVertex.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.Vertex;
31 import org.onap.crud.exception.CrudException;
32
33 import java.util.Map;
34 import javax.ws.rs.core.Response.Status;
35
36 /**
37  * This class provides a generic representation of a Vertex as provided by the
38  * graph data store.
39  */
40 public class GraphEventVertex {
41
42   /**
43    * The unique identifier used to identify this vertex in the graph data
44    * 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    * Map of all of the properties assigned to this vertex.
59    */
60   private JsonElement properties;
61
62   /**
63    * Marshaller/unmarshaller for converting to/from JSON.
64    */
65   private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
66
67   public GraphEventVertex(String id, String modelVersion, String type, JsonElement properties) {
68     this.id = id;
69     this.modelVersion = modelVersion;
70     this.type = type;
71     this.properties = properties;
72   }
73
74   public GraphEventVertex() {
75
76   }
77
78   public String getId() {
79     return id;
80   }
81
82   public void setId(String id) {
83     this.id = id;
84   }
85
86   public String getType() {
87     return type;
88   }
89
90   public void setType(String type) {
91     this.type = type;
92   }
93
94
95   public JsonElement getProperties() {
96     return properties;
97   }
98
99   public void setProperties(JsonElement properties) {
100     this.properties = properties;
101   }
102
103   public String getModelVersion() {
104     return modelVersion;
105   }
106
107   public void setModelVersion(String modelVersion) {
108     this.modelVersion = modelVersion;
109   }
110
111   /**
112    * Unmarshalls this Vertex object into a JSON string.
113    *
114    * @return - A JSON format string representation of this Vertex.
115    */
116   public String toJson() {
117     return gson.toJson(this);
118   }
119
120   /**
121    * Marshalls the provided JSON string into a Vertex object.
122    *
123    * @param json - The JSON string to produce the Vertex from.
124    * @return - A Vertex object.
125    * @throws SpikeException
126    */
127   public static GraphEventVertex fromJson(String json) throws CrudException {
128
129     try {
130
131       // Make sure that we were actually provided a non-empty string
132       // before we
133       // go any further.
134       if (json == null || json.isEmpty()) {
135         throw new CrudException("Empty or null JSON string.", Status.BAD_REQUEST);
136       }
137
138       // Marshall the string into a Vertex object.
139       return gson.fromJson(json, GraphEventVertex.class);
140
141     } catch (Exception ex) {
142       throw new CrudException("Unable to parse JSON string: ", Status.BAD_REQUEST);
143     }
144   }
145
146   @Override
147   public String toString() {
148
149     return toJson();
150   }
151
152   public static GraphEventVertex fromVertex(Vertex vertex, String modelVersion) {
153
154     java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
155     JsonObject props = gson.toJsonTree(vertex.getProperties(), mapType).getAsJsonObject();
156     GraphEventVertex graphEventVertex = new GraphEventVertex(vertex.getId().orElse(""),
157         modelVersion, vertex.getType(), props);
158     return graphEventVertex;
159
160   }
161
162   public Vertex toVertex() {
163     Vertex.Builder builder = new Vertex.Builder(this.getType()).id(this.getId());
164
165     if (this.getProperties() != null) {
166       java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
167       Map<String, Object> propertiesMap = gson.fromJson(this.getProperties(), mapType);
168       for (String key : propertiesMap.keySet()) {
169         builder.property(key, propertiesMap.get(key));
170       }
171     }
172
173     return builder.build();
174
175   }
176
177
178 }