Initial ONAP Synapse commit
[aai/data-router.git] / src / main / java / org / openecomp / datarouter / entity / AaiEventEntity.java
1 /**
2  * ============LICENSE_START=======================================================
3  * DataRouter
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 /* 
26 * ============LICENSE_START=======================================================
27 * DataRouter
28 * ================================================================================
29 * Copyright © 2017 AT&T Intellectual Property.
30 * Copyright © 2017 Amdocs
31 * All rights reserved.
32 * ================================================================================
33 * Licensed under the Apache License, Version 2.0 (the "License");
34 * you may not use this file except in compliance with the License.
35 * You may obtain a copy of the License at
36
37 *      http://www.apache.org/licenses/LICENSE-2.0
38
39 * Unless required by applicable law or agreed to in writing, software
40 * distributed under the License is distributed on an "AS IS" BASIS,
41 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42 * See the License for the specific language governing permissions and
43 * limitations under the License.
44 * ============LICENSE_END=========================================================
45
46 * ECOMP and OpenECOMP are trademarks
47 * and service marks of AT&T Intellectual Property.
48 */
49
50 package org.openecomp.datarouter.entity;
51
52 import java.io.IOException;
53 import java.io.Serializable;
54 import java.security.MessageDigest;
55 import java.security.NoSuchAlgorithmException;
56 import java.sql.Timestamp;
57 import java.text.SimpleDateFormat;
58 import java.util.ArrayList;
59 import java.util.List;
60
61 import javax.json.Json;
62 import javax.json.JsonObject;
63
64 /**
65  * Note: AAIEventEntity is a port forward of IndexDocument Has been renamed here to move forward
66  * with abstraction of document store technology.
67  */
68 public class AaiEventEntity implements DocumentStoreDataEntity, Serializable {
69
70   private static final long serialVersionUID = -5188479658230319058L;
71
72   protected String entityType;
73   protected String entityPrimaryKeyName;
74   protected String entityPrimaryKeyValue;
75   protected ArrayList<String> searchTagCollection = new ArrayList<String>();
76   protected ArrayList<String> searchTagIdCollection = new ArrayList<String>();
77   protected ArrayList<String> crossEntityReferenceCollection = new ArrayList<String>();
78   protected String lastmodTimestamp;
79   protected String link;
80
81   private static final String TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
82   /*
83    * Generated fields, leave the settings for junit overrides
84    */
85
86   // generated, SHA-256 digest
87   protected String id;
88
89   /*
90    * generated based on searchTagCollection values
91    */
92   protected String searchTags;
93   protected String searchTagIds;
94   protected String crossReferenceEntityValues;
95
96
97   private static String convertBytesToHexString(byte[] bytesToConvert) {
98     StringBuffer hexString = new StringBuffer();
99     for (int i = 0; i < bytesToConvert.length; i++) {
100       hexString.append(Integer.toHexString(0xFF & bytesToConvert[i]));
101     }
102     return hexString.toString();
103   }
104
105   private static String concatArray(List<String> list, char delimiter) {
106
107     if (list == null || list.size() == 0) {
108       return "";
109     }
110
111     StringBuilder result = new StringBuilder(64);
112
113     int listSize = list.size();
114     boolean firstValue = true;
115
116     for (String item : list) {
117
118       if (firstValue) {
119         result.append(item);
120         firstValue = false;
121       } else {
122         result.append(delimiter).append(item);
123       }
124
125     }
126
127     return result.toString();
128
129   }
130
131   /*
132    * We'll try and create a unique identity key that we can use for differencing the previously
133    * imported record sets as we won't have granular control of what is created/removed and when. The
134    * best we can hope for is identification of resources by generated Id until the Identity-Service
135    * UUID is tagged against all resources, then we can use that instead.
136    */
137
138   private static String generateUniqueShaDigest(String entityType, String fieldName,
139       String fieldValue) throws NoSuchAlgorithmException {
140
141     /*
142      * Basically SHA-256 will result in an identity with a guaranteed uniqueness compared to just a
143      * java hashcode value.
144      */
145     MessageDigest digest = MessageDigest.getInstance("SHA-256");
146     digest.update(String.format("%s.%s.%s", entityType, fieldName, fieldValue).getBytes());
147     return convertBytesToHexString(digest.digest());
148   }
149
150
151   public AaiEventEntity() {
152     SimpleDateFormat dateFormat = new SimpleDateFormat(TIMESTAMP_FORMAT);
153     Timestamp timestamp = new Timestamp(System.currentTimeMillis());
154     String currentFormattedTimeStamp = dateFormat.format(timestamp);
155     this.lastmodTimestamp = currentFormattedTimeStamp;
156   }
157
158   public void deriveFields() throws NoSuchAlgorithmException {
159     this.id = generateUniqueShaDigest(entityType, entityPrimaryKeyName, entityPrimaryKeyValue);
160     this.searchTags = concatArray(searchTagCollection, ';');
161     this.searchTagIds = concatArray(searchTagIdCollection, ';');
162     this.crossReferenceEntityValues = concatArray(crossEntityReferenceCollection, ';');
163   }
164
165
166   /*
167    * (non-Javadoc)
168    * 
169    * @see org.openecomp.datarouter.entity.AAIEventEntity#getAsJson()
170    */
171   @Override
172   public String getAsJson() throws IOException {
173
174     JsonObject obj = Json.createObjectBuilder().add("entityType", entityType)
175         .add("entityPrimaryKeyValue", entityPrimaryKeyValue).add("searchTagIDs", searchTagIds)
176         .add("searchTags", searchTags).add("crossEntityReferenceValues", crossReferenceEntityValues)
177         .add("lastmodTimestamp", lastmodTimestamp).add("link", link).build();
178
179     return obj.toString();
180   }
181
182
183   public void addSearchTagWithKey(String searchTag, String key) {
184     searchTagIdCollection.add(key);
185     searchTagCollection.add(searchTag);
186   }
187
188   public void addCrossEntityReferenceValue(String crossEntityReferenceValue) {
189     if (!crossEntityReferenceCollection.contains(crossEntityReferenceValue)) {
190       crossEntityReferenceCollection.add(crossEntityReferenceValue);
191     }
192   }
193
194   public String getEntityType() {
195     return entityType;
196   }
197
198   public String getEntityPrimaryKeyName() {
199     return entityPrimaryKeyName;
200   }
201
202   public String getEntityPrimaryKeyValue() {
203     return entityPrimaryKeyValue;
204   }
205
206
207   /*
208    * (non-Javadoc)
209    * 
210    * @see org.openecomp.datarouter.entity.AAIEventEntity#getId()
211    */
212   @Override
213   public String getId() {
214     return id;
215   }
216
217   public ArrayList<String> getSearchTagCollection() {
218     return searchTagCollection;
219   }
220
221   public String getSearchTags() {
222     return searchTags;
223   }
224
225   public String getSearchTagIDs() {
226     return searchTagIds;
227   }
228
229   public void setSearchTagIDs(String searchTagIDs) {
230     this.searchTagIds = searchTagIDs;
231   }
232
233   public void setEntityType(String entityType) {
234     this.entityType = entityType;
235   }
236
237   public void setId(String id) {
238     this.id = id;
239   }
240
241   public void setSearchTagCollection(ArrayList<String> searchTagCollection) {
242     this.searchTagCollection = searchTagCollection;
243   }
244
245   public void setSearchTags(String searchTags) {
246     this.searchTags = searchTags;
247   }
248
249   public ArrayList<String> getSearchTagIdCollection() {
250     return searchTagIdCollection;
251   }
252
253   public void setSearchTagIdCollection(ArrayList<String> searchTagIdCollection) {
254     this.searchTagIdCollection = searchTagIdCollection;
255   }
256
257   public String getLastmodTimestamp() {
258     return lastmodTimestamp;
259   }
260
261   public void setLastmodTimestamp(String lastmodTimestamp) {
262     this.lastmodTimestamp = lastmodTimestamp;
263   }
264
265   public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
266     this.entityPrimaryKeyName = entityPrimaryKeyName;
267   }
268
269   public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
270     this.entityPrimaryKeyValue = entityPrimaryKeyValue;
271   }
272   
273   public String getLink() {
274     return link;
275   }
276   
277   public void setLink(String link) {
278     this.link = link;
279   }
280
281   /*
282    * public void mergeEntity(AAIEventEntity entityToMergeIn) {
283    * 
284    * if ( entityToMergeIn == null ) { return; }
285    * 
286    * if ( !entityToMergeIn.getEntityType().equals( entityType )) { entityType =
287    * entityToMergeIn.getEntityType(); }
288    * 
289    * if ( !entityToMergeIn.getEntityType().equals( entityType )) { entityType =
290    * entityToMergeIn.getEntityType(); }
291    * 
292    * }
293    */
294
295   @Override
296   public String toString() {
297     return "AAIEventEntity [" + (entityType != null ? "entityType=" + entityType + ", " : "")
298         + (entityPrimaryKeyName != null ? "entityPrimaryKeyName=" + entityPrimaryKeyName + ", "
299             : "")
300         + (entityPrimaryKeyValue != null ? "entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", "
301             : "")
302         + (searchTagCollection != null ? "searchTagCollection=" + searchTagCollection + ", " : "")
303         + (searchTagIdCollection != null ? "searchTagIDCollection=" + searchTagIdCollection + ", "
304             : "")
305         + (crossEntityReferenceCollection != null
306             ? "crossEntityReferenceCollection=" + crossEntityReferenceCollection + ", " : "")
307         + "lastmodTimestamp=" + lastmodTimestamp + ", " + (id != null ? "id=" + id + ", " : "")
308         + (searchTags != null ? "searchTags=" + searchTags + ", " : "")
309         + (searchTagIds != null ? "searchTagIDs=" + searchTagIds + ", " : "")
310         + (crossReferenceEntityValues != null
311             ? "crossReferenceEntityValues=" + crossReferenceEntityValues : "")
312         + "]";
313   }
314
315 }