2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.aai.datarouter.entity;
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;
33 import javax.json.Json;
34 import javax.json.JsonObject;
36 import org.onap.aai.datarouter.util.NodeUtils;
39 * Note: AAIEventEntity is a port forward of IndexDocument Has been renamed here to move forward
40 * with abstraction of document store technology.
42 public class AaiEventEntity implements DocumentStoreDataEntity, Serializable {
44 private static final long serialVersionUID = -5188479658230319058L;
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;
55 private static final String TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
57 * Generated fields, leave the settings for junit overrides
60 // generated, SHA-256 digest
64 * generated based on searchTagCollection values
66 protected String searchTags;
67 protected String searchTagIds;
68 protected String crossReferenceEntityValues;
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]));
76 return hexString.toString();
79 private static String concatArray(List<String> list, char delimiter) {
81 if (list == null || list.isEmpty()) {
85 StringBuilder result = new StringBuilder(64);
87 boolean firstValue = true;
89 for (String item : list) {
95 result.append(delimiter).append(item);
100 return result.toString();
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;
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, ';');
122 * @see org.onap.aai.datarouter.entity.AAIEventEntity#getAsJson()
125 public String getAsJson() throws IOException {
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();
132 return obj.toString();
136 public void addSearchTagWithKey(String searchTag, String key) {
137 searchTagIdCollection.add(key);
138 searchTagCollection.add(searchTag);
141 public void addCrossEntityReferenceValue(String crossEntityReferenceValue) {
142 if (!crossEntityReferenceCollection.contains(crossEntityReferenceValue)) {
143 crossEntityReferenceCollection.add(crossEntityReferenceValue);
147 public String getEntityType() {
151 public String getEntityPrimaryKeyName() {
152 return entityPrimaryKeyName;
155 public String getEntityPrimaryKeyValue() {
156 return entityPrimaryKeyValue;
163 * @see org.onap.aai.datarouter.entity.AAIEventEntity#getId()
166 public String getId() {
170 public ArrayList<String> getSearchTagCollection() {
171 return searchTagCollection;
174 public String getSearchTags() {
178 public String getSearchTagIDs() {
182 public void setSearchTagIDs(String searchTagIDs) {
183 this.searchTagIds = searchTagIDs;
186 public void setEntityType(String entityType) {
187 this.entityType = entityType;
190 public void setId(String id) {
194 public void setSearchTagCollection(ArrayList<String> searchTagCollection) {
195 this.searchTagCollection = searchTagCollection;
198 public void setSearchTags(String searchTags) {
199 this.searchTags = searchTags;
202 public ArrayList<String> getSearchTagIdCollection() {
203 return searchTagIdCollection;
206 public void setSearchTagIdCollection(ArrayList<String> searchTagIdCollection) {
207 this.searchTagIdCollection = searchTagIdCollection;
210 public String getLastmodTimestamp() {
211 return lastmodTimestamp;
214 public void setLastmodTimestamp(String lastmodTimestamp) {
215 this.lastmodTimestamp = lastmodTimestamp;
218 public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
219 this.entityPrimaryKeyName = entityPrimaryKeyName;
222 public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
223 this.entityPrimaryKeyValue = entityPrimaryKeyValue;
226 public String getLink() {
230 public void setLink(String link) {
235 * public void mergeEntity(AAIEventEntity entityToMergeIn) {
237 * if ( entityToMergeIn == null ) { return; }
239 * if ( !entityToMergeIn.getEntityType().equals( entityType )) { entityType =
240 * entityToMergeIn.getEntityType(); }
242 * if ( !entityToMergeIn.getEntityType().equals( entityType )) { entityType =
243 * entityToMergeIn.getEntityType(); }
248 public String getCrossReferenceEntityValues() {
249 return crossReferenceEntityValues;
252 public void setCrossReferenceEntityValues(String crossReferenceEntityValues) {
253 this.crossReferenceEntityValues = crossReferenceEntityValues;
257 public String toString() {
258 return "AAIEventEntity [" + (entityType != null ? "entityType=" + entityType + ", " : "")
259 + (entityPrimaryKeyName != null ? "entityPrimaryKeyName=" + entityPrimaryKeyName + ", "
261 + (entityPrimaryKeyValue != null ? "entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", "
263 + (searchTagCollection != null ? "searchTagCollection=" + searchTagCollection + ", " : "")
264 + (searchTagIdCollection != null ? "searchTagIDCollection=" + searchTagIdCollection + ", "
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 : "")