0391ae3a722b1b25dc0ee58a94e5bfa959d6abca
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / entity / AaiEventEntity.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  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 /* 
24 * ============LICENSE_START=======================================================
25 * DataRouter
26 * ================================================================================
27 * Copyright © 2017 AT&T Intellectual Property.
28 * Copyright © 2017 Amdocs
29 * All rights reserved.
30 * ================================================================================
31 * Licensed under the Apache License, Version 2.0 (the "License");
32 * you may not use this file except in compliance with the License.
33 * You may obtain a copy of the License at
34
35 *      http://www.apache.org/licenses/LICENSE-2.0
36
37 * Unless required by applicable law or agreed to in writing, software
38 * distributed under the License is distributed on an "AS IS" BASIS,
39 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40 * See the License for the specific language governing permissions and
41 * limitations under the License.
42 * ============LICENSE_END=========================================================
43
44 * ECOMP and OpenECOMP are trademarks
45 * and service marks of AT&T Intellectual Property.
46 */
47
48 package org.onap.aai.datarouter.entity;
49
50 import java.io.IOException;
51 import java.io.Serializable;
52 import java.security.MessageDigest;
53 import java.security.NoSuchAlgorithmException;
54 import java.sql.Timestamp;
55 import java.text.SimpleDateFormat;
56 import java.util.ArrayList;
57 import java.util.List;
58
59 import javax.json.Json;
60 import javax.json.JsonObject;
61
62 import org.openecomp.datarouter.util.NodeUtils;
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<>();
76   protected ArrayList<String> searchTagIdCollection = new ArrayList<>();
77   protected ArrayList<String> crossEntityReferenceCollection = new ArrayList<>();
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     StringBuilder hexString = new StringBuilder();
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.isEmpty()) {
108       return "";
109     }
110
111     StringBuilder result = new StringBuilder(64);
112
113     boolean firstValue = true;
114
115     for (String item : list) {
116
117       if (firstValue) {
118         result.append(item);
119         firstValue = false;
120       } else {
121         result.append(delimiter).append(item);
122       }
123
124     }
125
126     return result.toString();
127
128   }
129
130   public AaiEventEntity() {
131     SimpleDateFormat dateFormat = new SimpleDateFormat(TIMESTAMP_FORMAT);
132     Timestamp timestamp = new Timestamp(System.currentTimeMillis());
133     String currentFormattedTimeStamp = dateFormat.format(timestamp);
134     this.lastmodTimestamp = currentFormattedTimeStamp;
135   }
136
137   public void deriveFields() throws NoSuchAlgorithmException {
138     this.id = NodeUtils.generateUniqueShaDigest(link);
139     this.searchTags = concatArray(searchTagCollection, ';');
140     this.searchTagIds = concatArray(searchTagIdCollection, ';');
141     this.crossReferenceEntityValues = concatArray(crossEntityReferenceCollection, ';');
142   }
143
144
145   /*
146    * (non-Javadoc)
147    * 
148    * @see org.onap.aai.datarouter.entity.AAIEventEntity#getAsJson()
149    */
150   @Override
151   public String getAsJson() throws IOException {
152
153     JsonObject obj = Json.createObjectBuilder().add("entityType", entityType)
154         .add("entityPrimaryKeyValue", entityPrimaryKeyValue).add("searchTagIDs", searchTagIds)
155         .add("searchTags", searchTags).add("crossEntityReferenceValues", crossReferenceEntityValues)
156         .add("lastmodTimestamp", lastmodTimestamp).add("link", link).build();
157
158     return obj.toString();
159   }
160
161
162   public void addSearchTagWithKey(String searchTag, String key) {
163     searchTagIdCollection.add(key);
164     searchTagCollection.add(searchTag);
165   }
166
167   public void addCrossEntityReferenceValue(String crossEntityReferenceValue) {
168     if (!crossEntityReferenceCollection.contains(crossEntityReferenceValue)) {
169       crossEntityReferenceCollection.add(crossEntityReferenceValue);
170     }
171   }
172
173   public String getEntityType() {
174     return entityType;
175   }
176
177   public String getEntityPrimaryKeyName() {
178     return entityPrimaryKeyName;
179   }
180
181   public String getEntityPrimaryKeyValue() {
182     return entityPrimaryKeyValue;
183   }
184
185
186   /*
187    * (non-Javadoc)
188    * 
189    * @see org.onap.aai.datarouter.entity.AAIEventEntity#getId()
190    */
191   @Override
192   public String getId() {
193     return id;
194   }
195
196   public ArrayList<String> getSearchTagCollection() {
197     return searchTagCollection;
198   }
199
200   public String getSearchTags() {
201     return searchTags;
202   }
203
204   public String getSearchTagIDs() {
205     return searchTagIds;
206   }
207
208   public void setSearchTagIDs(String searchTagIDs) {
209     this.searchTagIds = searchTagIDs;
210   }
211
212   public void setEntityType(String entityType) {
213     this.entityType = entityType;
214   }
215
216   public void setId(String id) {
217     this.id = id;
218   }
219
220   public void setSearchTagCollection(ArrayList<String> searchTagCollection) {
221     this.searchTagCollection = searchTagCollection;
222   }
223
224   public void setSearchTags(String searchTags) {
225     this.searchTags = searchTags;
226   }
227
228   public ArrayList<String> getSearchTagIdCollection() {
229     return searchTagIdCollection;
230   }
231
232   public void setSearchTagIdCollection(ArrayList<String> searchTagIdCollection) {
233     this.searchTagIdCollection = searchTagIdCollection;
234   }
235
236   public String getLastmodTimestamp() {
237     return lastmodTimestamp;
238   }
239
240   public void setLastmodTimestamp(String lastmodTimestamp) {
241     this.lastmodTimestamp = lastmodTimestamp;
242   }
243
244   public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
245     this.entityPrimaryKeyName = entityPrimaryKeyName;
246   }
247
248   public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
249     this.entityPrimaryKeyValue = entityPrimaryKeyValue;
250   }
251   
252   public String getLink() {
253     return link;
254   }
255   
256   public void setLink(String link) {
257     this.link = link;
258   }
259
260   /*
261    * public void mergeEntity(AAIEventEntity entityToMergeIn) {
262    * 
263    * if ( entityToMergeIn == null ) { return; }
264    * 
265    * if ( !entityToMergeIn.getEntityType().equals( entityType )) { entityType =
266    * entityToMergeIn.getEntityType(); }
267    * 
268    * if ( !entityToMergeIn.getEntityType().equals( entityType )) { entityType =
269    * entityToMergeIn.getEntityType(); }
270    * 
271    * }
272    */
273
274   public String getCrossReferenceEntityValues() {
275     return crossReferenceEntityValues;
276   }
277
278   public void setCrossReferenceEntityValues(String crossReferenceEntityValues) {
279     this.crossReferenceEntityValues = crossReferenceEntityValues;
280   }
281
282   @Override
283   public String toString() {
284     return "AAIEventEntity [" + (entityType != null ? "entityType=" + entityType + ", " : "")
285         + (entityPrimaryKeyName != null ? "entityPrimaryKeyName=" + entityPrimaryKeyName + ", "
286             : "")
287         + (entityPrimaryKeyValue != null ? "entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", "
288             : "")
289         + (searchTagCollection != null ? "searchTagCollection=" + searchTagCollection + ", " : "")
290         + (searchTagIdCollection != null ? "searchTagIDCollection=" + searchTagIdCollection + ", "
291             : "")
292         + (crossEntityReferenceCollection != null
293             ? "crossEntityReferenceCollection=" + crossEntityReferenceCollection + ", " : "")
294         + "lastmodTimestamp=" + lastmodTimestamp + ", " + (id != null ? "id=" + id + ", " : "")
295         + (searchTags != null ? "searchTags=" + searchTags + ", " : "")
296         + (searchTagIds != null ? "searchTagIDs=" + searchTagIds + ", " : "")
297         + (crossReferenceEntityValues != null
298             ? "crossReferenceEntityValues=" + crossReferenceEntityValues : "")
299         + "]";
300   }
301
302 }