13809c18f4d59bd43ce69952d9fb38aeb34fef55
[aai/champ.git] / champ-service / src / main / java / org / onap / champ / event / GraphEventEdge.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.aai.champcore.model.ChampRelationship;
30 import org.onap.champ.exception.ChampServiceException;
31
32 import com.google.gson.Gson;
33 import com.google.gson.GsonBuilder;
34 import com.google.gson.JsonElement;
35 import com.google.gson.JsonObject;
36 import com.google.gson.annotations.SerializedName;
37 import com.google.gson.reflect.TypeToken;
38
39 /**
40  * This class provides a generic representation of an Edge as provided by the
41  * graph data store.
42  */
43 public class GraphEventEdge {
44
45   /**
46    * The unique identifier used to identify this edge in the graph data 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    * Source vertex for our edge.
61    */
62   private GraphEventVertex source;
63
64   /**
65    * Target vertex for our edge.
66    */
67   private GraphEventVertex target;
68
69   /**
70    * Map of all of the properties assigned to this vertex.
71    */
72   private JsonElement properties;
73
74   /**
75    * Marshaller/unmarshaller for converting to/from JSON.
76    */
77   private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
78
79   public GraphEventEdge(String id, String modelVersion, String type, GraphEventVertex source,
80                         GraphEventVertex target, JsonElement properties) {
81     this.id = id;
82     this.modelVersion = modelVersion;
83     this.type = type;
84     this.source = source;
85     this.target = target;
86     this.properties = properties;
87   }
88
89   public GraphEventEdge() {
90
91   }
92
93   public String getId() {
94     return id;
95   }
96
97   public void setId(String id) {
98     this.id = id;
99   }
100
101   public String getType() {
102     return type;
103   }
104
105   public void setType(String type) {
106     this.type = type;
107   }
108
109   public GraphEventVertex getSource() {
110     return source;
111   }
112
113   public void setSource(GraphEventVertex source) {
114     this.source = source;
115   }
116
117   public GraphEventVertex getTarget() {
118     return target;
119   }
120
121   public void setTarget(GraphEventVertex target) {
122     this.target = target;
123   }
124
125   public JsonElement getProperties() {
126     return properties;
127   }
128
129   public void setProperties(JsonElement properties) {
130     this.properties = properties;
131   }
132
133   public String getModelVersion() {
134     return modelVersion;
135   }
136
137   public void setModelVersion(String modelVersion) {
138     this.modelVersion = modelVersion;
139   }
140
141   /**
142    * Unmarshalls this Edge object into a JSON string.
143    *
144    * @return - A JSON format string representation of this Edge.
145    */
146   public String toJson() {
147     return gson.toJson(this);
148   }
149
150   /**
151    * Marshalls the provided JSON string into a Edge object.
152    *
153    * @param json - The JSON string to produce the Edge from.
154    * @return - A Edge object.
155    * @throws SpikeException
156    */
157   public static GraphEventEdge fromJson(String json) throws ChampServiceException {
158
159     try {
160
161       // Make sure that we were actually provided a non-empty string
162       // before we
163       // go any further.
164       if (json == null || json.isEmpty()) {
165         throw new ChampServiceException("Unable to parse JSON string: ", Status.BAD_REQUEST);
166       }
167
168       // Marshall the string into an Edge object.
169       return gson.fromJson(json, GraphEventEdge.class);
170
171     } catch (Exception ex) {
172       throw new ChampServiceException("Unable to parse JSON string: ", Status.BAD_REQUEST);
173     }
174   }
175
176   public static GraphEventEdge fromChampRelationship(ChampRelationship edge, String modelVersion) {
177
178     java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
179     JsonObject props = gson.toJsonTree(edge.getProperties(), mapType).getAsJsonObject();
180
181     GraphEventEdge graphEventEdge = new GraphEventEdge(edge.getKey().orElse("").toString(), modelVersion,
182         edge.getType(), new GraphEventVertex(edge.getSource().getKey().orElse("").toString(), null,
183         edge.getSource().getType(), null), new GraphEventVertex(edge.getTarget().getKey().orElse("").toString(),
184         null, edge.getTarget().getType(), null), props);
185
186     return graphEventEdge;
187
188   }
189
190   public ChampRelationship toChampRelationship() {
191     ChampObject sourceChampObject=null;
192     ChampObject targetChampObject=null;
193     if (this.getSource() != null) {
194       sourceChampObject = new ChampObject.Builder(this.getSource().getType()).key(this.getSource().getId())
195           .build();
196     }
197     if (this.getTarget() != null) {
198       targetChampObject =  new ChampObject.Builder(this.getTarget().getType()).key(this.getTarget().getId())
199           .build();
200     }
201     
202     ChampRelationship.Builder builder = new ChampRelationship.Builder(sourceChampObject, targetChampObject, type);
203     if(this.getId()!=null && !this.getId().isEmpty()){
204       builder.key(this.getId());
205     }
206
207
208     if (this.getProperties() != null) {
209       java.lang.reflect.Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
210       Map<String, Object> propertiesMap = gson.fromJson(this.getProperties(), mapType);
211       for (String key : propertiesMap.keySet()) {
212         builder.property(key, propertiesMap.get(key));
213       }
214     }
215     return builder.build();
216
217   }
218 }