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.IOException;
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;
37 * The Class TopographicalEntity.
39 public class TopographicalEntity implements Serializable {
41 private static final long serialVersionUID = -5188479658230319058L;
43 protected String entityType;
44 protected String entityPrimaryKeyValue;
45 protected String entityPrimaryKeyName;
46 protected String latitude;
47 protected String longitude;
48 protected String selfLink;
50 // generated, SHA-256 digest
54 * Convert bytes to hex string.
56 * @param bytesToConvert the bytes to convert
59 private static String convertBytesToHexString(byte[] bytesToConvert) {
60 StringBuffer hexString = new StringBuffer();
61 for (int i = 0; i < bytesToConvert.length; i++) {
62 hexString.append(Integer.toHexString(0xFF & bytesToConvert[i]));
64 return hexString.toString();
70 * @param list the list
71 * @param delimiter the delimiter
74 private static String concatArray(List<String> list, char delimiter) {
76 if (list == null || list.size() == 0) {
80 StringBuilder result = new StringBuilder(64);
82 int listSize = list.size();
83 boolean firstValue = true;
85 for (String item : list) {
91 result.append(delimiter).append(item);
96 return result.toString();
101 * We'll try and create a unique identity key that we can use for differencing the previously
102 * imported record sets as we won't have granular control of what is created/removed and when. The
103 * best we can hope for is identification of resources by generated Id until the Identity-Service
104 * UUID is tagged against all resources, then we can use that instead.
108 * Generate unique sha digest.
110 * @param entityType the entity type
111 * @param fieldName the field name
112 * @param fieldValue the field value
114 * @throws NoSuchAlgorithmException the no such algorithm exception
116 public static String generateUniqueShaDigest(String entityType, String fieldName,
117 String fieldValue) throws NoSuchAlgorithmException {
120 * Basically SHA-256 will result in an identity with a guaranteed uniqueness compared to just a
121 * java hashcode value.
123 MessageDigest digest = MessageDigest.getInstance("SHA-256");
124 digest.update(String.format("%s.%s.%s", entityType, fieldName, fieldValue).getBytes());
125 return convertBytesToHexString(digest.digest());
129 * Instantiates a new topographical entity.
131 public TopographicalEntity() {}
137 public String getAsJson() throws IOException {
140 Json.createObjectBuilder().add("entityType", entityType).add("pkey", entityPrimaryKeyValue)
141 .add("location", Json.createObjectBuilder().add("lat", latitude).add("lon", longitude))
142 .add("selfLink", selfLink).build();
144 return obj.toString();
149 * @see java.lang.Object#toString()
152 public String toString() {
153 return "TopographicalEntity [" + ("entityType=" + entityType + ", ")
154 + ("entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", ")
155 + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ") + ("ID=" + id + ", ")
156 + ("selfLink=" + selfLink) + "]";
159 public String getId() {
163 public String getEntityType() {
167 public void setEntityType(String entityType) {
168 this.entityType = entityType;
171 public String getEntityPrimaryKeyValue() {
172 return entityPrimaryKeyValue;
175 public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
176 this.entityPrimaryKeyValue = entityPrimaryKeyValue;
179 public String getEntityPrimaryKeyName() {
180 return entityPrimaryKeyName;
183 public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
184 this.entityPrimaryKeyName = entityPrimaryKeyName;
187 public String getLatitude() {
191 public void setLatitude(String latitude) {
192 this.latitude = latitude;
195 public String getLongitude() {
199 public void setLongitude(String longitude) {
200 this.longitude = longitude;
203 public String getSelfLink() {
207 public void setSelfLink(String selfLink) {
208 this.selfLink = selfLink;
211 public static long getSerialversionuid() {
212 return serialVersionUID;
215 public void setId(String id) {