2 * ============LICENSE_START=======================================================
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
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.inventory.entity;
25 import java.io.Serializable;
26 import java.security.MessageDigest;
27 import java.security.NoSuchAlgorithmException;
28 import java.util.List;
30 import org.onap.aai.sparky.config.oxm.OxmEntityDescriptor;
31 import org.onap.aai.sparky.config.oxm.OxmEntityLookup;
32 import org.onap.aai.sparky.sync.entity.IndexDocument;
33 import org.onap.aai.sparky.util.NodeUtils;
35 import com.fasterxml.jackson.annotation.JsonIgnore;
36 import com.fasterxml.jackson.annotation.JsonProperty;
37 import com.fasterxml.jackson.core.JsonProcessingException;
38 import com.fasterxml.jackson.databind.ObjectMapper;
41 * The Class GeoIndexDocument.
43 public class GeoIndexDocument implements Serializable, IndexDocument {
46 private static final long serialVersionUID = -5188479658230319058L;
48 protected String entityType;
49 protected String entityPrimaryKeyValue;
50 protected String entityPrimaryKeyName;
51 protected String latitude;
52 protected String longitude;
53 protected String selfLink;
56 protected OxmEntityLookup oxmEntityLookup;
59 protected ObjectMapper mapper = new ObjectMapper();
60 // generated, SHA-256 digest
65 * Convert bytes to hex string.
67 * @param bytesToConvert the bytes to convert
70 private static String convertBytesToHexString(byte[] bytesToConvert) {
71 StringBuffer hexString = new StringBuffer();
72 for (int i = 0; i < bytesToConvert.length; i++) {
73 hexString.append(Integer.toHexString(0xFF & bytesToConvert[i]));
75 return hexString.toString();
80 public boolean isValidGeoDocument() {
82 boolean isValid = true;
84 isValid &= (this.getEntityType() != null);
85 isValid &= (this.getLatitude() != null);
86 isValid &= (this.getLongitude() != null);
87 isValid &= (this.getId() != null);
88 isValid &= (this.getSelfLink() != null);
90 isValid &= NodeUtils.isNumeric(this.getLatitude());
91 isValid &= NodeUtils.isNumeric(this.getLongitude());
99 * @param list the list
100 * @param delimiter the delimiter
103 private static String concatArray(List<String> list, char delimiter) {
105 if (list == null || list.size() == 0) {
109 StringBuilder result = new StringBuilder(64);
111 int listSize = list.size();
112 boolean firstValue = true;
114 for (String item : list) {
120 result.append(delimiter).append(item);
125 return result.toString();
130 * We'll try and create a unique identity key that we can use for differencing the previously
131 * imported record sets as we won't have granular control of what is created/removed and when. The
132 * best we can hope for is identification of resources by generated Id until the Identity-Service
133 * UUID is tagged against all resources, then we can use that instead.
137 * Generate unique sha digest.
139 * @param entityType the entity type
140 * @param fieldName the field name
141 * @param fieldValue the field value
143 * @throws NoSuchAlgorithmException the no such algorithm exception
145 public static String generateUniqueShaDigest(String entityType, String fieldName,
146 String fieldValue) throws NoSuchAlgorithmException {
149 * Basically SHA-256 will result in an identity with a guaranteed uniqueness compared to just a
150 * java hashcode value.
152 MessageDigest digest = MessageDigest.getInstance("SHA-256");
153 digest.update(String.format("%s.%s.%s", entityType, fieldName, fieldValue).getBytes());
154 return convertBytesToHexString(digest.digest());
158 * Instantiates a new geo index document.
160 public GeoIndexDocument() {}
165 * @see com.att.queryrouter.dao.DocumentStoreDataEntity#getAsJson()
170 public String getAsJson() throws JsonProcessingException {
172 if (latitude != null && longitude != null) {
175 * A valid entry from this class is one that has both lat and long. If one or both is missing
176 * we shouldn't be indexing anything.
179 return NodeUtils.convertObjectToJson(this, true);
188 * @see org.openecomp.sparky.synchronizer.entity.IndexDocument#deriveFields()
191 public void deriveFields() {
194 * We'll try and create a unique identity key that we can use for differencing the previously
195 * imported record sets as we won't have granular control of what is created/removed and when.
196 * The best we can hope for is identification of resources by generated Id until the
197 * Identity-Service UUID is tagged against all resources, then we can use that instead.
200 OxmEntityDescriptor descriptor = oxmEntityLookup.getEntityDescriptors().get(entityType);
201 String entityPrimaryKeyName = NodeUtils.concatArray(
202 descriptor.getPrimaryKeyAttributeNames(), "/");
205 NodeUtils.generateUniqueShaDigest(entityType, entityPrimaryKeyName, entityPrimaryKeyValue);
209 * @see java.lang.Object#toString()
212 public String toString() {
213 return "TopographicalEntity [" + ("entityType=" + entityType + ", ")
214 + ("entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", ")
215 + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ") + ("ID=" + id + ", ")
216 + ("selfLink=" + selfLink) + "]";
221 public String getId() {
225 @JsonProperty("entityType")
226 public String getEntityType() {
230 public void setEntityType(String entityType) {
231 this.entityType = entityType;
234 @JsonProperty("entityPrimaryKeyValue")
235 public String getEntityPrimaryKeyValue() {
236 return entityPrimaryKeyValue;
239 public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
240 this.entityPrimaryKeyValue = entityPrimaryKeyValue;
243 @JsonProperty("entityPrimaryKeyName")
244 public String getEntityPrimaryKeyName() {
245 return entityPrimaryKeyName;
248 public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
249 this.entityPrimaryKeyName = entityPrimaryKeyName;
253 public String getLatitude() {
257 public void setLatitude(String latitude) {
258 this.latitude = latitude;
261 @JsonProperty("long")
262 public String getLongitude() {
266 public void setLongitude(String longitude) {
267 this.longitude = longitude;
270 @JsonProperty("link")
271 public String getSelfLink() {
275 public void setSelfLink(String selfLink) {
276 this.selfLink = selfLink;
280 public static long getSerialversionuid() {
281 return serialVersionUID;
284 public void setId(String id) {