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