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.IOException;
26 import java.io.Serializable;
27 import java.security.MessageDigest;
28 import java.security.NoSuchAlgorithmException;
29 import java.util.List;
31 import javax.json.Json;
32 import javax.json.JsonObject;
35 * The Class TopographicalEntity.
37 public class TopographicalEntity implements Serializable {
39 private static final long serialVersionUID = -5188479658230319058L;
41 protected String entityType;
42 protected String entityPrimaryKeyValue;
43 protected String entityPrimaryKeyName;
44 protected String latitude;
45 protected String longitude;
46 protected String selfLink;
48 // generated, SHA-256 digest
52 * Convert bytes to hex string.
54 * @param bytesToConvert the bytes to convert
57 private static String convertBytesToHexString(byte[] bytesToConvert) {
58 StringBuffer hexString = new StringBuffer();
59 for (int i = 0; i < bytesToConvert.length; i++) {
60 hexString.append(Integer.toHexString(0xFF & bytesToConvert[i]));
62 return hexString.toString();
68 * @param list the list
69 * @param delimiter the delimiter
72 private static String concatArray(List<String> list, char delimiter) {
74 if (list == null || list.size() == 0) {
78 StringBuilder result = new StringBuilder(64);
80 int listSize = list.size();
81 boolean firstValue = true;
83 for (String item : list) {
89 result.append(delimiter).append(item);
94 return result.toString();
99 * We'll try and create a unique identity key that we can use for differencing the previously
100 * imported record sets as we won't have granular control of what is created/removed and when. The
101 * best we can hope for is identification of resources by generated Id until the Identity-Service
102 * UUID is tagged against all resources, then we can use that instead.
106 * Generate unique sha digest.
108 * @param entityType the entity type
109 * @param fieldName the field name
110 * @param fieldValue the field value
112 * @throws NoSuchAlgorithmException the no such algorithm exception
114 public static String generateUniqueShaDigest(String entityType, String fieldName,
115 String fieldValue) throws NoSuchAlgorithmException {
118 * Basically SHA-256 will result in an identity with a guaranteed uniqueness compared to just a
119 * java hashcode value.
121 MessageDigest digest = MessageDigest.getInstance("SHA-256");
122 digest.update(String.format("%s.%s.%s", entityType, fieldName, fieldValue).getBytes());
123 return convertBytesToHexString(digest.digest());
127 * Instantiates a new topographical entity.
129 public TopographicalEntity() {}
134 * @see com.att.queryrouter.dao.DocumentStoreDataEntity#getAsJson()
136 public String getAsJson() throws IOException {
139 Json.createObjectBuilder().add("entityType", entityType).add("pkey", entityPrimaryKeyValue)
140 .add("location", Json.createObjectBuilder().add("lat", latitude).add("lon", longitude))
141 .add("selfLink", selfLink).build();
143 return obj.toString();
148 * @see java.lang.Object#toString()
151 public String toString() {
152 return "TopographicalEntity [" + ("entityType=" + entityType + ", ")
153 + ("entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", ")
154 + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ") + ("ID=" + id + ", ")
155 + ("selfLink=" + selfLink) + "]";
158 public String getId() {
162 public String getEntityType() {
166 public void setEntityType(String entityType) {
167 this.entityType = entityType;
170 public String getEntityPrimaryKeyValue() {
171 return entityPrimaryKeyValue;
174 public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
175 this.entityPrimaryKeyValue = entityPrimaryKeyValue;
178 public String getEntityPrimaryKeyName() {
179 return entityPrimaryKeyName;
182 public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
183 this.entityPrimaryKeyName = entityPrimaryKeyName;
186 public String getLatitude() {
190 public void setLatitude(String latitude) {
191 this.latitude = latitude;
194 public String getLongitude() {
198 public void setLongitude(String longitude) {
199 this.longitude = longitude;
202 public String getSelfLink() {
206 public void setSelfLink(String selfLink) {
207 this.selfLink = selfLink;
210 public static long getSerialversionuid() {
211 return serialVersionUID;
214 public void setId(String id) {