Migrate Spike code to ONAP
[aai/spike.git] / src / main / java / org / onap / aai / spike / event / outgoing / SpikeVertex.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.aai.spike.event.outgoing;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonElement;
26 import com.google.gson.annotations.SerializedName;
27 import org.onap.aai.spike.exception.SpikeException;
28
29 /**
30  * This class provides a generic representation of a Vertex as provided by the graph data store.
31  *
32  */
33 public class SpikeVertex {
34
35     /**
36      * The unique identifier used to identify this vertex in the graph data store.
37      */
38     @SerializedName("key")
39     private String id;
40
41     @SerializedName("schema-version")
42     private String modelVersion;
43
44     /** Type label assigned to this vertex. */
45     private String type;
46
47     /** Map of all of the properties assigned to this vertex. */
48     private JsonElement properties;
49
50     /** Marshaller/unmarshaller for converting to/from JSON. */
51     private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
52
53     public String getId() {
54         return id;
55     }
56
57     public void setId(String id) {
58         this.id = id;
59     }
60
61     public String getType() {
62         return type;
63     }
64
65     public void setType(String type) {
66         this.type = type;
67     }
68
69     public JsonElement getProperties() {
70         return properties;
71     }
72
73     public void setProperties(JsonElement properties) {
74         this.properties = properties;
75     }
76
77     public String getModelVersion() {
78         return modelVersion;
79     }
80
81     public void setModelVersion(String modelVersion) {
82         this.modelVersion = modelVersion;
83     }
84
85     /**
86      * Unmarshalls this Vertex object into a JSON string.
87      * 
88      * @return - A JSON format string representation of this Vertex.
89      */
90     public String toJson() {
91         return gson.toJson(this);
92     }
93
94     /**
95      * Marshalls the provided JSON string into a Vertex object.
96      * 
97      * @param json - The JSON string to produce the Vertex from.
98      * 
99      * @return - A Vertex object.
100      * 
101      * @throws SpikeException
102      */
103     public static SpikeVertex fromJson(String json) throws SpikeException {
104
105         try {
106
107             // Make sure that we were actually provided a non-empty string
108             // before we
109             // go any further.
110             if (json == null || json.isEmpty()) {
111                 throw new SpikeException("Empty or null JSON string.");
112             }
113
114             // Marshall the string into a Vertex object.
115             return gson.fromJson(json, SpikeVertex.class);
116
117         } catch (Exception ex) {
118             throw new SpikeException("Unable to parse JSON string: " + ex.getMessage());
119         }
120     }
121
122     @Override
123     public String toString() {
124
125         return toJson();
126     }
127 }