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() {}
169 public String getAsJson() throws JsonProcessingException {
171 if (latitude != null && longitude != null) {
174 * A valid entry from this class is one that has both lat and long. If one or both is missing
175 * we shouldn't be indexing anything.
178 return NodeUtils.convertObjectToJson(this, true);
187 * @see org.openecomp.sparky.synchronizer.entity.IndexDocument#deriveFields()
190 public void deriveFields() {
193 * We'll try and create a unique identity key that we can use for differencing the previously
194 * imported record sets as we won't have granular control of what is created/removed and when.
195 * The best we can hope for is identification of resources by generated Id until the
196 * Identity-Service UUID is tagged against all resources, then we can use that instead.
199 OxmEntityDescriptor descriptor = oxmEntityLookup.getEntityDescriptors().get(entityType);
200 String entityPrimaryKeyName = NodeUtils.concatArray(
201 descriptor.getPrimaryKeyAttributeNames(), "/");
204 NodeUtils.generateUniqueShaDigest(entityType, entityPrimaryKeyName, entityPrimaryKeyValue);
208 * @see java.lang.Object#toString()
211 public String toString() {
212 return "TopographicalEntity [" + ("entityType=" + entityType + ", ")
213 + ("entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", ")
214 + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ") + ("ID=" + id + ", ")
215 + ("selfLink=" + selfLink) + "]";
220 public String getId() {
224 @JsonProperty("entityType")
225 public String getEntityType() {
229 public void setEntityType(String entityType) {
230 this.entityType = entityType;
233 @JsonProperty("entityPrimaryKeyValue")
234 public String getEntityPrimaryKeyValue() {
235 return entityPrimaryKeyValue;
238 public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
239 this.entityPrimaryKeyValue = entityPrimaryKeyValue;
242 @JsonProperty("entityPrimaryKeyName")
243 public String getEntityPrimaryKeyName() {
244 return entityPrimaryKeyName;
247 public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
248 this.entityPrimaryKeyName = entityPrimaryKeyName;
252 public String getLatitude() {
256 public void setLatitude(String latitude) {
257 this.latitude = latitude;
260 @JsonProperty("long")
261 public String getLongitude() {
265 public void setLongitude(String longitude) {
266 this.longitude = longitude;
269 @JsonProperty("link")
270 public String getSelfLink() {
274 public void setSelfLink(String selfLink) {
275 this.selfLink = selfLink;
279 public static long getSerialversionuid() {
280 return serialVersionUID;
283 public void setId(String id) {