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