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