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.
26 package org.openecomp.sparky.inventory.entity;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.fasterxml.jackson.databind.node.ObjectNode;
31 import java.io.Serializable;
32 import java.security.MessageDigest;
33 import java.security.NoSuchAlgorithmException;
34 import java.util.List;
36 import javax.json.Json;
37 import javax.json.JsonObject;
39 import org.openecomp.sparky.config.oxm.OxmEntityDescriptor;
40 import org.openecomp.sparky.config.oxm.OxmModelLoader;
41 import org.openecomp.sparky.synchronizer.entity.IndexDocument;
42 import org.openecomp.sparky.util.NodeUtils;
45 * The Class GeoIndexDocument.
47 public class GeoIndexDocument implements Serializable, IndexDocument {
49 private static final long serialVersionUID = -5188479658230319058L;
51 protected String entityType;
52 protected String entityPrimaryKeyValue;
53 protected String entityPrimaryKeyName;
54 protected String latitude;
55 protected String longitude;
56 protected String selfLink;
57 protected OxmModelLoader loader;
58 protected ObjectMapper mapper = new ObjectMapper();
59 // generated, SHA-256 digest
63 * Instantiates a new geo index document.
65 * @param loader the loader
67 public GeoIndexDocument(OxmModelLoader loader) {
73 * Convert bytes to hex string.
75 * @param bytesToConvert the bytes to convert
78 private static String convertBytesToHexString(byte[] bytesToConvert) {
79 StringBuffer hexString = new StringBuffer();
80 for (int i = 0; i < bytesToConvert.length; i++) {
81 hexString.append(Integer.toHexString(0xFF & bytesToConvert[i]));
83 return hexString.toString();
88 public boolean isValidGeoDocument() {
90 boolean isValid = true;
92 isValid &= (this.getEntityType() != null);
93 isValid &= (this.getLatitude() != null);
94 isValid &= (this.getLongitude() != null);
95 isValid &= (this.getId() != null);
96 isValid &= (this.getSelfLink() != null);
98 isValid &= NodeUtils.isNumeric(this.getLatitude());
99 isValid &= NodeUtils.isNumeric(this.getLongitude());
107 * @param list the list
108 * @param delimiter the delimiter
111 private static String concatArray(List<String> list, char delimiter) {
113 if (list == null || list.size() == 0) {
117 StringBuilder result = new StringBuilder(64);
119 int listSize = list.size();
120 boolean firstValue = true;
122 for (String item : list) {
128 result.append(delimiter).append(item);
133 return result.toString();
138 * We'll try and create a unique identity key that we can use for differencing the previously
139 * imported record sets as we won't have granular control of what is created/removed and when. The
140 * best we can hope for is identification of resources by generated Id until the Identity-Service
141 * UUID is tagged against all resources, then we can use that instead.
145 * Generate unique sha digest.
147 * @param entityType the entity type
148 * @param fieldName the field name
149 * @param fieldValue the field value
151 * @throws NoSuchAlgorithmException the no such algorithm exception
153 public static String generateUniqueShaDigest(String entityType, String fieldName,
154 String fieldValue) throws NoSuchAlgorithmException {
157 * Basically SHA-256 will result in an identity with a guaranteed uniqueness compared to just a
158 * java hashcode value.
160 MessageDigest digest = MessageDigest.getInstance("SHA-256");
161 digest.update(String.format("%s.%s.%s", entityType, fieldName, fieldValue).getBytes());
162 return convertBytesToHexString(digest.digest());
166 * Instantiates a new geo index document.
168 public GeoIndexDocument() {}
173 * @see com.att.queryrouter.dao.DocumentStoreDataEntity#getAsJson()
176 public String getIndexDocumentJson() {
178 JsonObject obj = null;
180 if (latitude != null && longitude != null) {
182 Json.createObjectBuilder().add("entityType", entityType)
183 .add("pkey", entityPrimaryKeyValue)
185 Json.createObjectBuilder().add("lat", latitude).add("lon", longitude))
186 .add("selfLink", selfLink).build();
189 obj = Json.createObjectBuilder().add("entityType", entityType)
190 .add("pkey", entityPrimaryKeyValue).add("selfLink", selfLink).build();
193 return obj.toString();
197 * @see org.openecomp.sparky.synchronizer.entity.IndexDocument#deriveFields()
200 public void deriveFields() {
203 * We'll try and create a unique identity key that we can use for differencing the previously
204 * imported record sets as we won't have granular control of what is created/removed and when.
205 * The best we can hope for is identification of resources by generated Id until the
206 * Identity-Service UUID is tagged against all resources, then we can use that instead.
209 OxmEntityDescriptor descriptor = loader.getEntityDescriptor(entityType);
210 String entityPrimaryKeyName = NodeUtils.concatArray(
211 descriptor.getPrimaryKeyAttributeName(), "/");
214 NodeUtils.generateUniqueShaDigest(entityType, entityPrimaryKeyName, entityPrimaryKeyValue);
218 * @see java.lang.Object#toString()
221 public String toString() {
222 return "TopographicalEntity [" + ("entityType=" + entityType + ", ")
223 + ("entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", ")
224 + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ") + ("ID=" + id + ", ")
225 + ("selfLink=" + selfLink) + "]";
229 public String getId() {
233 public String getEntityType() {
237 public void setEntityType(String entityType) {
238 this.entityType = entityType;
241 public String getEntityPrimaryKeyValue() {
242 return entityPrimaryKeyValue;
245 public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
246 this.entityPrimaryKeyValue = entityPrimaryKeyValue;
249 public String getEntityPrimaryKeyName() {
250 return entityPrimaryKeyName;
253 public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
254 this.entityPrimaryKeyName = entityPrimaryKeyName;
257 public String getLatitude() {
261 public void setLatitude(String latitude) {
262 this.latitude = latitude;
265 public String getLongitude() {
269 public void setLongitude(String longitude) {
270 this.longitude = longitude;
273 public String getSelfLink() {
277 public void setSelfLink(String selfLink) {
278 this.selfLink = selfLink;
281 public static long getSerialversionuid() {
282 return serialVersionUID;
285 public void setId(String id) {
290 public ObjectNode getBulkImportEntity() {
291 // TODO Auto-generated method stub
292 return buildImportDataObject(this.entityType, this.entityPrimaryKeyValue, this.selfLink,
293 this.latitude, this.longitude);
297 * Builds the import data object.
299 * @param entityType the entity type
300 * @param entityPrimaryKeyValue the entity primary key value
301 * @param selfLink the self link
302 * @param latitude the latitude
303 * @param longitude the longitude
304 * @return the object node
306 @SuppressWarnings("deprecation")
307 protected ObjectNode buildImportDataObject(String entityType, String entityPrimaryKeyValue,
308 String selfLink, String latitude, String longitude) {
309 ObjectNode childNode = mapper.createObjectNode();
310 childNode.put("lat", latitude);
311 childNode.put("lon", longitude);
312 ObjectNode parentNode = mapper.createObjectNode();
314 parentNode.put("entityType", entityType);
315 parentNode.put("pkey", entityPrimaryKeyValue);
316 parentNode.put("selfLink", selfLink);
317 parentNode.put("location", childNode);