2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
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.
25 package org.onap.aai.sparky.inventory.entity;
27 import java.io.Serializable;
28 import java.security.MessageDigest;
29 import java.security.NoSuchAlgorithmException;
30 import java.util.List;
32 import org.onap.aai.sparky.config.oxm.OxmEntityDescriptor;
33 import org.onap.aai.sparky.config.oxm.OxmEntityLookup;
34 import org.onap.aai.sparky.sync.entity.IndexDocument;
35 import org.onap.aai.sparky.util.NodeUtils;
37 import com.fasterxml.jackson.annotation.JsonIgnore;
38 import com.fasterxml.jackson.annotation.JsonProperty;
39 import com.fasterxml.jackson.core.JsonProcessingException;
40 import com.fasterxml.jackson.databind.ObjectMapper;
43 * The Class GeoIndexDocument.
45 public class GeoIndexDocument implements Serializable, IndexDocument {
48 private static final long serialVersionUID = -5188479658230319058L;
50 protected String entityType;
51 protected String entityPrimaryKeyValue;
52 protected String entityPrimaryKeyName;
53 protected String latitude;
54 protected String longitude;
55 protected String selfLink;
58 protected OxmEntityLookup oxmEntityLookup;
61 protected ObjectMapper mapper = new ObjectMapper();
62 // generated, SHA-256 digest
67 * Convert bytes to hex string.
69 * @param bytesToConvert the bytes to convert
72 private static String convertBytesToHexString(byte[] bytesToConvert) {
73 StringBuffer hexString = new StringBuffer();
74 for (int i = 0; i < bytesToConvert.length; i++) {
75 hexString.append(Integer.toHexString(0xFF & bytesToConvert[i]));
77 return hexString.toString();
82 public boolean isValidGeoDocument() {
84 boolean isValid = true;
86 isValid &= (this.getEntityType() != null);
87 isValid &= (this.getLatitude() != null);
88 isValid &= (this.getLongitude() != null);
89 isValid &= (this.getId() != null);
90 isValid &= (this.getSelfLink() != null);
92 isValid &= NodeUtils.isNumeric(this.getLatitude());
93 isValid &= NodeUtils.isNumeric(this.getLongitude());
101 * @param list the list
102 * @param delimiter the delimiter
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.
139 * Generate unique sha digest.
141 * @param entityType the entity type
142 * @param fieldName the field name
143 * @param fieldValue the field value
145 * @throws NoSuchAlgorithmException the no such algorithm exception
147 public static String generateUniqueShaDigest(String entityType, String fieldName,
148 String fieldValue) throws NoSuchAlgorithmException {
151 * Basically SHA-256 will result in an identity with a guaranteed uniqueness compared to just a
152 * java hashcode value.
154 MessageDigest digest = MessageDigest.getInstance("SHA-256");
155 digest.update(String.format("%s.%s.%s", entityType, fieldName, fieldValue).getBytes());
156 return convertBytesToHexString(digest.digest());
160 * Instantiates a new geo index document.
162 public GeoIndexDocument() {}
171 public String getAsJson() throws JsonProcessingException {
173 if (latitude != null && longitude != null) {
176 * A valid entry from this class is one that has both lat and long. If one or both is missing
177 * we shouldn't be indexing anything.
180 return NodeUtils.convertObjectToJson(this, true);
189 * @see org.openecomp.sparky.synchronizer.entity.IndexDocument#deriveFields()
192 public void deriveFields() {
195 * We'll try and create a unique identity key that we can use for differencing the previously
196 * imported record sets as we won't have granular control of what is created/removed and when.
197 * The best we can hope for is identification of resources by generated Id until the
198 * Identity-Service UUID is tagged against all resources, then we can use that instead.
201 OxmEntityDescriptor descriptor = oxmEntityLookup.getEntityDescriptors().get(entityType);
202 String entityPrimaryKeyName = NodeUtils.concatArray(
203 descriptor.getPrimaryKeyAttributeNames(), "/");
206 NodeUtils.generateUniqueShaDigest(entityType, entityPrimaryKeyName, entityPrimaryKeyValue);
210 * @see java.lang.Object#toString()
213 public String toString() {
214 return "TopographicalEntity [" + ("entityType=" + entityType + ", ")
215 + ("entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", ")
216 + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ") + ("ID=" + id + ", ")
217 + ("selfLink=" + selfLink) + "]";
222 public String getId() {
226 @JsonProperty("entityType")
227 public String getEntityType() {
231 public void setEntityType(String entityType) {
232 this.entityType = entityType;
235 @JsonProperty("entityPrimaryKeyValue")
236 public String getEntityPrimaryKeyValue() {
237 return entityPrimaryKeyValue;
240 public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
241 this.entityPrimaryKeyValue = entityPrimaryKeyValue;
244 @JsonProperty("entityPrimaryKeyName")
245 public String getEntityPrimaryKeyName() {
246 return entityPrimaryKeyName;
249 public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
250 this.entityPrimaryKeyName = entityPrimaryKeyName;
254 public String getLatitude() {
258 public void setLatitude(String latitude) {
259 this.latitude = latitude;
262 @JsonProperty("long")
263 public String getLongitude() {
267 public void setLongitude(String longitude) {
268 this.longitude = longitude;
271 @JsonProperty("link")
272 public String getSelfLink() {
276 public void setSelfLink(String selfLink) {
277 this.selfLink = selfLink;
281 public static long getSerialversionuid() {
282 return serialVersionUID;
285 public void setId(String id) {