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