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