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