2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 * ============LICENSE_START=======================================================
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
37 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
46 * ECOMP and OpenECOMP are trademarks
47 * and service marks of AT&T Intellectual Property.
50 package org.openecomp.datarouter.entity;
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;
61 import javax.json.Json;
62 import javax.json.JsonObject;
65 * Note: AAIEventEntity is a port forward of IndexDocument Has been renamed here to move forward
66 * with abstraction of document store technology.
68 public class AaiEventEntity implements DocumentStoreDataEntity, Serializable {
70 private static final long serialVersionUID = -5188479658230319058L;
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;
81 private static final String TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
83 * Generated fields, leave the settings for junit overrides
86 // generated, SHA-256 digest
90 * generated based on searchTagCollection values
92 protected String searchTags;
93 protected String searchTagIds;
94 protected String crossReferenceEntityValues;
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]));
102 return hexString.toString();
105 private static String concatArray(List<String> list, char delimiter) {
107 if (list == null || list.size() == 0) {
111 StringBuilder result = new StringBuilder(64);
113 int listSize = list.size();
114 boolean firstValue = true;
116 for (String item : list) {
122 result.append(delimiter).append(item);
127 return result.toString();
132 * We'll try and create a unique identity key that we can use for differencing the previously
133 * imported record sets as we won't have granular control of what is created/removed and when. The
134 * best we can hope for is identification of resources by generated Id until the Identity-Service
135 * UUID is tagged against all resources, then we can use that instead.
138 private static String generateUniqueShaDigest(String entityType, String fieldName,
139 String fieldValue) throws NoSuchAlgorithmException {
142 * Basically SHA-256 will result in an identity with a guaranteed uniqueness compared to just a
143 * java hashcode value.
145 MessageDigest digest = MessageDigest.getInstance("SHA-256");
146 digest.update(String.format("%s.%s.%s", entityType, fieldName, fieldValue).getBytes());
147 return convertBytesToHexString(digest.digest());
151 public AaiEventEntity() {
152 SimpleDateFormat dateFormat = new SimpleDateFormat(TIMESTAMP_FORMAT);
153 Timestamp timestamp = new Timestamp(System.currentTimeMillis());
154 String currentFormattedTimeStamp = dateFormat.format(timestamp);
155 this.lastmodTimestamp = currentFormattedTimeStamp;
158 public void deriveFields() throws NoSuchAlgorithmException {
159 this.id = generateUniqueShaDigest(entityType, entityPrimaryKeyName, entityPrimaryKeyValue);
160 this.searchTags = concatArray(searchTagCollection, ';');
161 this.searchTagIds = concatArray(searchTagIdCollection, ';');
162 this.crossReferenceEntityValues = concatArray(crossEntityReferenceCollection, ';');
169 * @see org.openecomp.datarouter.entity.AAIEventEntity#getAsJson()
172 public String getAsJson() throws IOException {
174 JsonObject obj = Json.createObjectBuilder().add("entityType", entityType)
175 .add("entityPrimaryKeyValue", entityPrimaryKeyValue).add("searchTagIDs", searchTagIds)
176 .add("searchTags", searchTags).add("crossEntityReferenceValues", crossReferenceEntityValues)
177 .add("lastmodTimestamp", lastmodTimestamp).add("link", link).build();
179 return obj.toString();
183 public void addSearchTagWithKey(String searchTag, String key) {
184 searchTagIdCollection.add(key);
185 searchTagCollection.add(searchTag);
188 public void addCrossEntityReferenceValue(String crossEntityReferenceValue) {
189 if (!crossEntityReferenceCollection.contains(crossEntityReferenceValue)) {
190 crossEntityReferenceCollection.add(crossEntityReferenceValue);
194 public String getEntityType() {
198 public String getEntityPrimaryKeyName() {
199 return entityPrimaryKeyName;
202 public String getEntityPrimaryKeyValue() {
203 return entityPrimaryKeyValue;
210 * @see org.openecomp.datarouter.entity.AAIEventEntity#getId()
213 public String getId() {
217 public ArrayList<String> getSearchTagCollection() {
218 return searchTagCollection;
221 public String getSearchTags() {
225 public String getSearchTagIDs() {
229 public void setSearchTagIDs(String searchTagIDs) {
230 this.searchTagIds = searchTagIDs;
233 public void setEntityType(String entityType) {
234 this.entityType = entityType;
237 public void setId(String id) {
241 public void setSearchTagCollection(ArrayList<String> searchTagCollection) {
242 this.searchTagCollection = searchTagCollection;
245 public void setSearchTags(String searchTags) {
246 this.searchTags = searchTags;
249 public ArrayList<String> getSearchTagIdCollection() {
250 return searchTagIdCollection;
253 public void setSearchTagIdCollection(ArrayList<String> searchTagIdCollection) {
254 this.searchTagIdCollection = searchTagIdCollection;
257 public String getLastmodTimestamp() {
258 return lastmodTimestamp;
261 public void setLastmodTimestamp(String lastmodTimestamp) {
262 this.lastmodTimestamp = lastmodTimestamp;
265 public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
266 this.entityPrimaryKeyName = entityPrimaryKeyName;
269 public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
270 this.entityPrimaryKeyValue = entityPrimaryKeyValue;
273 public String getLink() {
277 public void setLink(String link) {
282 * public void mergeEntity(AAIEventEntity entityToMergeIn) {
284 * if ( entityToMergeIn == null ) { return; }
286 * if ( !entityToMergeIn.getEntityType().equals( entityType )) { entityType =
287 * entityToMergeIn.getEntityType(); }
289 * if ( !entityToMergeIn.getEntityType().equals( entityType )) { entityType =
290 * entityToMergeIn.getEntityType(); }
296 public String toString() {
297 return "AAIEventEntity [" + (entityType != null ? "entityType=" + entityType + ", " : "")
298 + (entityPrimaryKeyName != null ? "entityPrimaryKeyName=" + entityPrimaryKeyName + ", "
300 + (entityPrimaryKeyValue != null ? "entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", "
302 + (searchTagCollection != null ? "searchTagCollection=" + searchTagCollection + ", " : "")
303 + (searchTagIdCollection != null ? "searchTagIDCollection=" + searchTagIdCollection + ", "
305 + (crossEntityReferenceCollection != null
306 ? "crossEntityReferenceCollection=" + crossEntityReferenceCollection + ", " : "")
307 + "lastmodTimestamp=" + lastmodTimestamp + ", " + (id != null ? "id=" + id + ", " : "")
308 + (searchTags != null ? "searchTags=" + searchTags + ", " : "")
309 + (searchTagIds != null ? "searchTagIDs=" + searchTagIds + ", " : "")
310 + (crossReferenceEntityValues != null
311 ? "crossReferenceEntityValues=" + crossReferenceEntityValues : "")