2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.openecomp.crud.event;
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;
33 import org.openecomp.crud.entity.Vertex;
34 import org.openecomp.crud.exception.CrudException;
37 import javax.ws.rs.core.Response.Status;
40 * This class provides a generic representation of a Vertex as provided by the
43 public class GraphEventVertex {
46 * The unique identifier used to identify this vertex in the graph data
49 @SerializedName("key")
52 @SerializedName("schema-version")
53 private String modelVersion;
56 * Type label assigned to this vertex.
61 * Map of all of the properties assigned to this vertex.
63 private JsonElement properties;
66 * Marshaller/unmarshaller for converting to/from JSON.
68 private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
70 public GraphEventVertex(String id, String modelVersion, String type, JsonElement properties) {
72 this.modelVersion = modelVersion;
74 this.properties = properties;
77 public GraphEventVertex() {
81 public String getId() {
85 public void setId(String id) {
89 public String getType() {
93 public void setType(String type) {
98 public JsonElement getProperties() {
102 public void setProperties(JsonElement properties) {
103 this.properties = properties;
106 public String getModelVersion() {
110 public void setModelVersion(String modelVersion) {
111 this.modelVersion = modelVersion;
115 * Unmarshalls this Vertex object into a JSON string.
117 * @return - A JSON format string representation of this Vertex.
119 public String toJson() {
120 return gson.toJson(this);
124 * Marshalls the provided JSON string into a Vertex object.
126 * @param json - The JSON string to produce the Vertex from.
127 * @return - A Vertex object.
128 * @throws SpikeException
130 public static GraphEventVertex fromJson(String json) throws CrudException {
134 // Make sure that we were actually provided a non-empty string
137 if (json == null || json.isEmpty()) {
138 throw new CrudException("Empty or null JSON string.", Status.BAD_REQUEST);
141 // Marshall the string into a Vertex object.
142 return gson.fromJson(json, GraphEventVertex.class);
144 } catch (Exception ex) {
145 throw new CrudException("Unable to parse JSON string: ", Status.BAD_REQUEST);
150 public String toString() {
155 public static GraphEventVertex fromVertex(Vertex vertex, String modelVersion) {
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;
165 public Vertex toVertex() {
166 Vertex.Builder builder = new Vertex.Builder(this.getType()).id(this.getId());
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));
176 return builder.build();