Renaming openecomp to onap
[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<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   public AaiEventEntity() {
132     SimpleDateFormat dateFormat = new SimpleDateFormat(TIMESTAMP_FORMAT);
133     Timestamp timestamp = new Timestamp(System.currentTimeMillis());
134     String currentFormattedTimeStamp = dateFormat.format(timestamp);
135     this.lastmodTimestamp = currentFormattedTimeStamp;
136   }
137
138   public void deriveFields() throws NoSuchAlgorithmException {
139     this.id = NodeUtils.generateUniqueShaDigest(link);
140     this.searchTags = concatArray(searchTagCollection, ';');
141     this.searchTagIds = concatArray(searchTagIdCollection, ';');
142     this.crossReferenceEntityValues = concatArray(crossEntityReferenceCollection, ';');
143   }
144
145
146   /*
147    * (non-Javadoc)
148    * 
149    * @see org.onap.aai.datarouter.entity.AAIEventEntity#getAsJson()
150    */
151   @Override
152   public String getAsJson() throws IOException {
153
154     JsonObject obj = Json.createObjectBuilder().add("entityType", entityType)
155         .add("entityPrimaryKeyValue", entityPrimaryKeyValue).add("searchTagIDs", searchTagIds)
156         .add("searchTags", searchTags).add("crossEntityReferenceValues", crossReferenceEntityValues)
157         .add("lastmodTimestamp", lastmodTimestamp).add("link", link).build();
158
159     return obj.toString();
160   }
161
162
163   public void addSearchTagWithKey(String searchTag, String key) {
164     searchTagIdCollection.add(key);
165     searchTagCollection.add(searchTag);
166   }
167
168   public void addCrossEntityReferenceValue(String crossEntityReferenceValue) {
169     if (!crossEntityReferenceCollection.contains(crossEntityReferenceValue)) {
170       crossEntityReferenceCollection.add(crossEntityReferenceValue);
171     }
172   }
173
174   public String getEntityType() {
175     return entityType;
176   }
177
178   public String getEntityPrimaryKeyName() {
179     return entityPrimaryKeyName;
180   }
181
182   public String getEntityPrimaryKeyValue() {
183     return entityPrimaryKeyValue;
184   }
185
186
187   /*
188    * (non-Javadoc)
189    * 
190    * @see org.onap.aai.datarouter.entity.AAIEventEntity#getId()
191    */
192   @Override
193   public String getId() {
194     return id;
195   }
196
197   public ArrayList<String> getSearchTagCollection() {
198     return searchTagCollection;
199   }
200
201   public String getSearchTags() {
202     return searchTags;
203   }
204
205   public String getSearchTagIDs() {
206     return searchTagIds;
207   }
208
209   public void setSearchTagIDs(String searchTagIDs) {
210     this.searchTagIds = searchTagIDs;
211   }
212
213   public void setEntityType(String entityType) {
214     this.entityType = entityType;
215   }
216
217   public void setId(String id) {
218     this.id = id;
219   }
220
221   public void setSearchTagCollection(ArrayList<String> searchTagCollection) {
222     this.searchTagCollection = searchTagCollection;
223   }
224
225   public void setSearchTags(String searchTags) {
226     this.searchTags = searchTags;
227   }
228
229   public ArrayList<String> getSearchTagIdCollection() {
230     return searchTagIdCollection;
231   }
232
233   public void setSearchTagIdCollection(ArrayList<String> searchTagIdCollection) {
234     this.searchTagIdCollection = searchTagIdCollection;
235   }
236
237   public String getLastmodTimestamp() {
238     return lastmodTimestamp;
239   }
240
241   public void setLastmodTimestamp(String lastmodTimestamp) {
242     this.lastmodTimestamp = lastmodTimestamp;
243   }
244
245   public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
246     this.entityPrimaryKeyName = entityPrimaryKeyName;
247   }
248
249   public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
250     this.entityPrimaryKeyValue = entityPrimaryKeyValue;
251   }
252   
253   public String getLink() {
254     return link;
255   }
256   
257   public void setLink(String link) {
258     this.link = link;
259   }
260
261   /*
262    * public void mergeEntity(AAIEventEntity entityToMergeIn) {
263    * 
264    * if ( entityToMergeIn == null ) { return; }
265    * 
266    * if ( !entityToMergeIn.getEntityType().equals( entityType )) { entityType =
267    * entityToMergeIn.getEntityType(); }
268    * 
269    * if ( !entityToMergeIn.getEntityType().equals( entityType )) { entityType =
270    * entityToMergeIn.getEntityType(); }
271    * 
272    * }
273    */
274
275   public String getCrossReferenceEntityValues() {
276     return crossReferenceEntityValues;
277   }
278
279   public void setCrossReferenceEntityValues(String crossReferenceEntityValues) {
280     this.crossReferenceEntityValues = crossReferenceEntityValues;
281   }
282
283   @Override
284   public String toString() {
285     return "AAIEventEntity [" + (entityType != null ? "entityType=" + entityType + ", " : "")
286         + (entityPrimaryKeyName != null ? "entityPrimaryKeyName=" + entityPrimaryKeyName + ", "
287             : "")
288         + (entityPrimaryKeyValue != null ? "entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", "
289             : "")
290         + (searchTagCollection != null ? "searchTagCollection=" + searchTagCollection + ", " : "")
291         + (searchTagIdCollection != null ? "searchTagIDCollection=" + searchTagIdCollection + ", "
292             : "")
293         + (crossEntityReferenceCollection != null
294             ? "crossEntityReferenceCollection=" + crossEntityReferenceCollection + ", " : "")
295         + "lastmodTimestamp=" + lastmodTimestamp + ", " + (id != null ? "id=" + id + ", " : "")
296         + (searchTags != null ? "searchTags=" + searchTags + ", " : "")
297         + (searchTagIds != null ? "searchTagIDs=" + searchTagIds + ", " : "")
298         + (crossReferenceEntityValues != null
299             ? "crossReferenceEntityValues=" + crossReferenceEntityValues : "")
300         + "]";
301   }
302
303 }