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=========================================================
21 package org.onap.aai.sparky.inventory.entity;
23 import java.io.Serializable;
24 import java.security.MessageDigest;
25 import java.security.NoSuchAlgorithmException;
26 import java.util.List;
28 import org.onap.aai.sparky.config.oxm.OxmEntityDescriptor;
29 import org.onap.aai.sparky.config.oxm.OxmEntityLookup;
30 import org.onap.aai.sparky.sync.entity.IndexDocument;
31 import org.onap.aai.sparky.util.NodeUtils;
33 import com.fasterxml.jackson.annotation.JsonIgnore;
34 import com.fasterxml.jackson.annotation.JsonProperty;
35 import com.fasterxml.jackson.core.JsonProcessingException;
36 import com.fasterxml.jackson.databind.ObjectMapper;
39 * The Class GeoIndexDocument.
41 public class GeoIndexDocument implements Serializable, IndexDocument {
44 private static final long serialVersionUID = -5188479658230319058L;
46 protected String entityType;
47 protected String entityPrimaryKeyValue;
48 protected String entityPrimaryKeyName;
49 protected String latitude;
50 protected String longitude;
51 protected String selfLink;
54 protected OxmEntityLookup oxmEntityLookup;
57 protected ObjectMapper mapper = new ObjectMapper();
58 // generated, SHA-256 digest
63 * Convert bytes to hex string.
65 * @param bytesToConvert the bytes to convert
68 private static String convertBytesToHexString(byte[] bytesToConvert) {
69 StringBuffer hexString = new StringBuffer();
70 for (int i = 0; i < bytesToConvert.length; i++) {
71 hexString.append(Integer.toHexString(0xFF & bytesToConvert[i]));
73 return hexString.toString();
78 public boolean isValidGeoDocument() {
80 boolean isValid = true;
82 isValid &= (this.getEntityType() != null);
83 isValid &= (this.getLatitude() != null);
84 isValid &= (this.getLongitude() != null);
85 isValid &= (this.getId() != null);
86 isValid &= (this.getSelfLink() != null);
88 isValid &= NodeUtils.isNumeric(this.getLatitude());
89 isValid &= NodeUtils.isNumeric(this.getLongitude());
97 * @param list the list
98 * @param delimiter the delimiter
101 private static String concatArray(List<String> list, char delimiter) {
103 if (list == null || list.size() == 0) {
107 StringBuilder result = new StringBuilder(64);
109 int listSize = list.size();
110 boolean firstValue = true;
112 for (String item : list) {
118 result.append(delimiter).append(item);
123 return result.toString();
128 * We'll try and create a unique identity key that we can use for differencing the previously
129 * imported record sets as we won't have granular control of what is created/removed and when. The
130 * best we can hope for is identification of resources by generated Id until the Identity-Service
131 * UUID is tagged against all resources, then we can use that instead.
135 * Generate unique sha digest.
137 * @param entityType the entity type
138 * @param fieldName the field name
139 * @param fieldValue the field value
141 * @throws NoSuchAlgorithmException the no such algorithm exception
143 public static String generateUniqueShaDigest(String entityType, String fieldName,
144 String fieldValue) throws NoSuchAlgorithmException {
147 * Basically SHA-256 will result in an identity with a guaranteed uniqueness compared to just a
148 * java hashcode value.
150 MessageDigest digest = MessageDigest.getInstance("SHA-256");
151 digest.update(String.format("%s.%s.%s", entityType, fieldName, fieldValue).getBytes());
152 return convertBytesToHexString(digest.digest());
156 * Instantiates a new geo index document.
158 public GeoIndexDocument() {}
167 public String getAsJson() throws JsonProcessingException {
169 if (latitude != null && longitude != null) {
172 * A valid entry from this class is one that has both lat and long. If one or both is missing
173 * we shouldn't be indexing anything.
176 return NodeUtils.convertObjectToJson(this, true);
185 * @see org.openecomp.sparky.synchronizer.entity.IndexDocument#deriveFields()
188 public void deriveFields() {
191 * We'll try and create a unique identity key that we can use for differencing the previously
192 * imported record sets as we won't have granular control of what is created/removed and when.
193 * The best we can hope for is identification of resources by generated Id until the
194 * Identity-Service UUID is tagged against all resources, then we can use that instead.
197 OxmEntityDescriptor descriptor = oxmEntityLookup.getEntityDescriptors().get(entityType);
198 String entityPrimaryKeyName = NodeUtils.concatArray(
199 descriptor.getPrimaryKeyAttributeNames(), "/");
202 NodeUtils.generateUniqueShaDigest(entityType, entityPrimaryKeyName, entityPrimaryKeyValue);
206 * @see java.lang.Object#toString()
209 public String toString() {
210 return "TopographicalEntity [" + ("entityType=" + entityType + ", ")
211 + ("entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", ")
212 + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ") + ("ID=" + id + ", ")
213 + ("selfLink=" + selfLink) + "]";
218 public String getId() {
222 @JsonProperty("entityType")
223 public String getEntityType() {
227 public void setEntityType(String entityType) {
228 this.entityType = entityType;
231 @JsonProperty("entityPrimaryKeyValue")
232 public String getEntityPrimaryKeyValue() {
233 return entityPrimaryKeyValue;
236 public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
237 this.entityPrimaryKeyValue = entityPrimaryKeyValue;
240 @JsonProperty("entityPrimaryKeyName")
241 public String getEntityPrimaryKeyName() {
242 return entityPrimaryKeyName;
245 public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
246 this.entityPrimaryKeyName = entityPrimaryKeyName;
250 public String getLatitude() {
254 public void setLatitude(String latitude) {
255 this.latitude = latitude;
258 @JsonProperty("long")
259 public String getLongitude() {
263 public void setLongitude(String longitude) {
264 this.longitude = longitude;
267 @JsonProperty("link")
268 public String getSelfLink() {
272 public void setSelfLink(String selfLink) {
273 this.selfLink = selfLink;
277 public static long getSerialversionuid() {
278 return serialVersionUID;
281 public void setId(String id) {