5528d9aacd498d8422f2cd0e5e72dbff6c9df21c
[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-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
22 package org.onap.aai.datarouter.entity;
23
24 import java.io.IOException;
25 import java.io.Serializable;
26 import java.security.MessageDigest;
27 import java.security.NoSuchAlgorithmException;
28 import java.sql.Timestamp;
29 import java.text.SimpleDateFormat;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import javax.json.Json;
34 import javax.json.JsonObject;
35
36 import org.onap.aai.datarouter.util.NodeUtils;
37
38 /**
39  * Note: AAIEventEntity is a port forward of IndexDocument Has been renamed here to move forward
40  * with abstraction of document store technology.
41  */
42 public class AaiEventEntity implements DocumentStoreDataEntity, Serializable {
43
44   private static final long serialVersionUID = -5188479658230319058L;
45
46   protected String entityType;
47   protected String entityPrimaryKeyName;
48   protected String entityPrimaryKeyValue;
49   protected ArrayList<String> searchTagCollection = new ArrayList<>();
50   protected ArrayList<String> searchTagIdCollection = new ArrayList<>();
51   protected ArrayList<String> crossEntityReferenceCollection = new ArrayList<>();
52   protected String lastmodTimestamp;
53   protected String link;
54
55   private static final String TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
56   /*
57    * Generated fields, leave the settings for junit overrides
58    */
59
60   // generated, SHA-256 digest
61   protected String id;
62
63   /*
64    * generated based on searchTagCollection values
65    */
66   protected String searchTags;
67   protected String searchTagIds;
68   protected String crossReferenceEntityValues;
69
70
71   private static String convertBytesToHexString(byte[] bytesToConvert) {
72     StringBuilder hexString = new StringBuilder();
73     for (int i = 0; i < bytesToConvert.length; i++) {
74       hexString.append(Integer.toHexString(0xFF & bytesToConvert[i]));
75     }
76     return hexString.toString();
77   }
78
79   private static String concatArray(List<String> list, char delimiter) {
80
81     if (list == null || list.isEmpty()) {
82       return "";
83     }
84
85     StringBuilder result = new StringBuilder(64);
86
87     boolean firstValue = true;
88
89     for (String item : list) {
90
91       if (firstValue) {
92         result.append(item);
93         firstValue = false;
94       } else {
95         result.append(delimiter).append(item);
96       }
97
98     }
99
100     return result.toString();
101
102   }
103
104   public AaiEventEntity() {
105     SimpleDateFormat dateFormat = new SimpleDateFormat(TIMESTAMP_FORMAT);
106     Timestamp timestamp = new Timestamp(System.currentTimeMillis());
107     String currentFormattedTimeStamp = dateFormat.format(timestamp);
108     this.lastmodTimestamp = currentFormattedTimeStamp;
109   }
110
111   public void deriveFields() throws NoSuchAlgorithmException {
112     this.id = NodeUtils.generateUniqueShaDigest(link);
113     this.searchTags = concatArray(searchTagCollection, ';');
114     this.searchTagIds = concatArray(searchTagIdCollection, ';');
115     this.crossReferenceEntityValues = concatArray(crossEntityReferenceCollection, ';');
116   }
117
118
119   /*
120    * (non-Javadoc)
121    * 
122    * @see org.onap.aai.datarouter.entity.AAIEventEntity#getAsJson()
123    */
124   @Override
125   public String getAsJson() throws IOException {
126
127     JsonObject obj = Json.createObjectBuilder().add("entityType", entityType)
128         .add("entityPrimaryKeyValue", entityPrimaryKeyValue).add("searchTagIDs", searchTagIds)
129         .add("searchTags", searchTags).add("crossEntityReferenceValues", crossReferenceEntityValues)
130         .add("lastmodTimestamp", lastmodTimestamp).add("link", link).build();
131
132     return obj.toString();
133   }
134
135
136   public void addSearchTagWithKey(String searchTag, String key) {
137     searchTagIdCollection.add(key);
138     searchTagCollection.add(searchTag);
139   }
140
141   public void addCrossEntityReferenceValue(String crossEntityReferenceValue) {
142     if (!crossEntityReferenceCollection.contains(crossEntityReferenceValue)) {
143       crossEntityReferenceCollection.add(crossEntityReferenceValue);
144     }
145   }
146
147   public String getEntityType() {
148     return entityType;
149   }
150
151   public String getEntityPrimaryKeyName() {
152     return entityPrimaryKeyName;
153   }
154
155   public String getEntityPrimaryKeyValue() {
156     return entityPrimaryKeyValue;
157   }
158
159
160   /*
161    * (non-Javadoc)
162    * 
163    * @see org.onap.aai.datarouter.entity.AAIEventEntity#getId()
164    */
165   @Override
166   public String getId() {
167     return id;
168   }
169
170   public ArrayList<String> getSearchTagCollection() {
171     return searchTagCollection;
172   }
173
174   public String getSearchTags() {
175     return searchTags;
176   }
177
178   public String getSearchTagIDs() {
179     return searchTagIds;
180   }
181
182   public void setSearchTagIDs(String searchTagIDs) {
183     this.searchTagIds = searchTagIDs;
184   }
185
186   public void setEntityType(String entityType) {
187     this.entityType = entityType;
188   }
189
190   public void setId(String id) {
191     this.id = id;
192   }
193
194   public void setSearchTagCollection(ArrayList<String> searchTagCollection) {
195     this.searchTagCollection = searchTagCollection;
196   }
197
198   public void setSearchTags(String searchTags) {
199     this.searchTags = searchTags;
200   }
201
202   public ArrayList<String> getSearchTagIdCollection() {
203     return searchTagIdCollection;
204   }
205
206   public void setSearchTagIdCollection(ArrayList<String> searchTagIdCollection) {
207     this.searchTagIdCollection = searchTagIdCollection;
208   }
209
210   public String getLastmodTimestamp() {
211     return lastmodTimestamp;
212   }
213
214   public void setLastmodTimestamp(String lastmodTimestamp) {
215     this.lastmodTimestamp = lastmodTimestamp;
216   }
217
218   public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
219     this.entityPrimaryKeyName = entityPrimaryKeyName;
220   }
221
222   public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
223     this.entityPrimaryKeyValue = entityPrimaryKeyValue;
224   }
225   
226   public String getLink() {
227     return link;
228   }
229   
230   public void setLink(String link) {
231     this.link = link;
232   }
233
234   /*
235    * public void mergeEntity(AAIEventEntity entityToMergeIn) {
236    * 
237    * if ( entityToMergeIn == null ) { return; }
238    * 
239    * if ( !entityToMergeIn.getEntityType().equals( entityType )) { entityType =
240    * entityToMergeIn.getEntityType(); }
241    * 
242    * if ( !entityToMergeIn.getEntityType().equals( entityType )) { entityType =
243    * entityToMergeIn.getEntityType(); }
244    * 
245    * }
246    */
247
248   public String getCrossReferenceEntityValues() {
249     return crossReferenceEntityValues;
250   }
251
252   public void setCrossReferenceEntityValues(String crossReferenceEntityValues) {
253     this.crossReferenceEntityValues = crossReferenceEntityValues;
254   }
255
256   @Override
257   public String toString() {
258     return "AAIEventEntity [" + (entityType != null ? "entityType=" + entityType + ", " : "")
259         + (entityPrimaryKeyName != null ? "entityPrimaryKeyName=" + entityPrimaryKeyName + ", "
260             : "")
261         + (entityPrimaryKeyValue != null ? "entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", "
262             : "")
263         + (searchTagCollection != null ? "searchTagCollection=" + searchTagCollection + ", " : "")
264         + (searchTagIdCollection != null ? "searchTagIDCollection=" + searchTagIdCollection + ", "
265             : "")
266         + (crossEntityReferenceCollection != null
267             ? "crossEntityReferenceCollection=" + crossEntityReferenceCollection + ", " : "")
268         + "lastmodTimestamp=" + lastmodTimestamp + ", " + (id != null ? "id=" + id + ", " : "")
269         + (searchTags != null ? "searchTags=" + searchTags + ", " : "")
270         + (searchTagIds != null ? "searchTagIDs=" + searchTagIds + ", " : "")
271         + (crossReferenceEntityValues != null
272             ? "crossReferenceEntityValues=" + crossReferenceEntityValues : "")
273         + "]";
274   }
275
276 }