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