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.openecomp.sparky.inventory.entity;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.fasterxml.jackson.databind.node.ObjectNode;
28 import java.io.Serializable;
29 import java.security.MessageDigest;
30 import java.security.NoSuchAlgorithmException;
31 import java.util.List;
33 import javax.json.Json;
34 import javax.json.JsonObject;
36 import org.openecomp.sparky.config.oxm.OxmEntityDescriptor;
37 import org.openecomp.sparky.config.oxm.OxmModelLoader;
38 import org.openecomp.sparky.synchronizer.entity.IndexDocument;
39 import org.openecomp.sparky.util.NodeUtils;
42 * The Class GeoIndexDocument.
44 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;
54 protected OxmModelLoader loader;
55 protected ObjectMapper mapper = new ObjectMapper();
56 // generated, SHA-256 digest
60 * Instantiates a new geo index document.
62 * @param loader the loader
64 public GeoIndexDocument(OxmModelLoader loader) {
70 * Convert bytes to hex string.
72 * @param bytesToConvert the bytes to convert
75 private static String convertBytesToHexString(byte[] bytesToConvert) {
76 StringBuffer hexString = new StringBuffer();
77 for (int i = 0; i < bytesToConvert.length; i++) {
78 hexString.append(Integer.toHexString(0xFF & bytesToConvert[i]));
80 return hexString.toString();
85 public boolean isValidGeoDocument() {
87 boolean isValid = true;
89 isValid &= (this.getEntityType() != null);
90 isValid &= (this.getLatitude() != null);
91 isValid &= (this.getLongitude() != null);
92 isValid &= (this.getId() != null);
93 isValid &= (this.getSelfLink() != null);
95 isValid &= NodeUtils.isNumeric(this.getLatitude());
96 isValid &= NodeUtils.isNumeric(this.getLongitude());
104 * @param list the list
105 * @param delimiter the delimiter
108 private static String concatArray(List<String> list, char delimiter) {
110 if (list == null || list.size() == 0) {
114 StringBuilder result = new StringBuilder(64);
116 int listSize = list.size();
117 boolean firstValue = true;
119 for (String item : list) {
125 result.append(delimiter).append(item);
130 return result.toString();
135 * We'll try and create a unique identity key that we can use for differencing the previously
136 * imported record sets as we won't have granular control of what is created/removed and when. The
137 * best we can hope for is identification of resources by generated Id until the Identity-Service
138 * UUID is tagged against all resources, then we can use that instead.
142 * Generate unique sha digest.
144 * @param entityType the entity type
145 * @param fieldName the field name
146 * @param fieldValue the field value
148 * @throws NoSuchAlgorithmException the no such algorithm exception
150 public static String generateUniqueShaDigest(String entityType, String fieldName,
151 String fieldValue) throws NoSuchAlgorithmException {
154 * Basically SHA-256 will result in an identity with a guaranteed uniqueness compared to just a
155 * java hashcode value.
157 MessageDigest digest = MessageDigest.getInstance("SHA-256");
158 digest.update(String.format("%s.%s.%s", entityType, fieldName, fieldValue).getBytes());
159 return convertBytesToHexString(digest.digest());
163 * Instantiates a new geo index document.
165 public GeoIndexDocument() {}
170 * @see com.att.queryrouter.dao.DocumentStoreDataEntity#getAsJson()
173 public String getIndexDocumentJson() {
175 JsonObject obj = null;
177 if (latitude != null && longitude != null) {
179 Json.createObjectBuilder().add("entityType", entityType)
180 .add("pkey", entityPrimaryKeyValue)
182 Json.createObjectBuilder().add("lat", latitude).add("lon", longitude))
183 .add("selfLink", selfLink).build();
186 obj = Json.createObjectBuilder().add("entityType", entityType)
187 .add("pkey", entityPrimaryKeyValue).add("selfLink", selfLink).build();
190 return obj.toString();
194 * @see org.openecomp.sparky.synchronizer.entity.IndexDocument#deriveFields()
197 public void deriveFields() {
200 * We'll try and create a unique identity key that we can use for differencing the previously
201 * imported record sets as we won't have granular control of what is created/removed and when.
202 * The best we can hope for is identification of resources by generated Id until the
203 * Identity-Service UUID is tagged against all resources, then we can use that instead.
206 OxmEntityDescriptor descriptor = loader.getEntityDescriptor(entityType);
207 String entityPrimaryKeyName = NodeUtils.concatArray(
208 descriptor.getPrimaryKeyAttributeName(), "/");
211 NodeUtils.generateUniqueShaDigest(entityType, entityPrimaryKeyName, entityPrimaryKeyValue);
215 * @see java.lang.Object#toString()
218 public String toString() {
219 return "TopographicalEntity [" + ("entityType=" + entityType + ", ")
220 + ("entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", ")
221 + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ") + ("ID=" + id + ", ")
222 + ("selfLink=" + selfLink) + "]";
226 public String getId() {
230 public String getEntityType() {
234 public void setEntityType(String entityType) {
235 this.entityType = entityType;
238 public String getEntityPrimaryKeyValue() {
239 return entityPrimaryKeyValue;
242 public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
243 this.entityPrimaryKeyValue = entityPrimaryKeyValue;
246 public String getEntityPrimaryKeyName() {
247 return entityPrimaryKeyName;
250 public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
251 this.entityPrimaryKeyName = entityPrimaryKeyName;
254 public String getLatitude() {
258 public void setLatitude(String latitude) {
259 this.latitude = latitude;
262 public String getLongitude() {
266 public void setLongitude(String longitude) {
267 this.longitude = longitude;
270 public String getSelfLink() {
274 public void setSelfLink(String selfLink) {
275 this.selfLink = selfLink;
278 public static long getSerialversionuid() {
279 return serialVersionUID;
282 public void setId(String id) {
287 public ObjectNode getBulkImportEntity() {
288 // TODO Auto-generated method stub
289 return buildImportDataObject(this.entityType, this.entityPrimaryKeyValue, this.selfLink,
290 this.latitude, this.longitude);
294 * Builds the import data object.
296 * @param entityType the entity type
297 * @param entityPrimaryKeyValue the entity primary key value
298 * @param selfLink the self link
299 * @param latitude the latitude
300 * @param longitude the longitude
301 * @return the object node
303 @SuppressWarnings("deprecation")
304 protected ObjectNode buildImportDataObject(String entityType, String entityPrimaryKeyValue,
305 String selfLink, String latitude, String longitude) {
306 ObjectNode childNode = mapper.createObjectNode();
307 childNode.put("lat", latitude);
308 childNode.put("lon", longitude);
309 ObjectNode parentNode = mapper.createObjectNode();
311 parentNode.put("entityType", entityType);
312 parentNode.put("pkey", entityPrimaryKeyValue);
313 parentNode.put("selfLink", selfLink);
314 parentNode.put("location", childNode);