2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 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 package org.onap.aai.sparky.inventory.entity;
23 import java.io.IOException;
24 import java.io.Serializable;
25 import java.security.MessageDigest;
26 import java.security.NoSuchAlgorithmException;
27 import java.util.List;
29 import javax.json.Json;
30 import javax.json.JsonObject;
33 * The Class TopographicalEntity.
35 public class TopographicalEntity implements Serializable {
37 private static final long serialVersionUID = -5188479658230319058L;
39 protected String entityType;
40 protected String entityPrimaryKeyValue;
41 protected String entityPrimaryKeyName;
42 protected String latitude;
43 protected String longitude;
44 protected String selfLink;
46 // generated, SHA-256 digest
50 * Convert bytes to hex string.
52 * @param bytesToConvert the bytes to convert
55 private static String convertBytesToHexString(byte[] bytesToConvert) {
56 StringBuffer hexString = new StringBuffer();
57 for (int i = 0; i < bytesToConvert.length; i++) {
58 hexString.append(Integer.toHexString(0xFF & bytesToConvert[i]));
60 return hexString.toString();
66 * @param list the list
67 * @param delimiter the delimiter
70 private static String concatArray(List<String> list, char delimiter) {
72 if (list == null || list.size() == 0) {
76 StringBuilder result = new StringBuilder(64);
78 int listSize = list.size();
79 boolean firstValue = true;
81 for (String item : list) {
87 result.append(delimiter).append(item);
92 return result.toString();
97 * We'll try and create a unique identity key that we can use for differencing the previously
98 * imported record sets as we won't have granular control of what is created/removed and when. The
99 * best we can hope for is identification of resources by generated Id until the Identity-Service
100 * UUID is tagged against all resources, then we can use that instead.
104 * Generate unique sha digest.
106 * @param entityType the entity type
107 * @param fieldName the field name
108 * @param fieldValue the field value
110 * @throws NoSuchAlgorithmException the no such algorithm exception
112 public static String generateUniqueShaDigest(String entityType, String fieldName,
113 String fieldValue) throws NoSuchAlgorithmException {
116 * Basically SHA-256 will result in an identity with a guaranteed uniqueness compared to just a
117 * java hashcode value.
119 MessageDigest digest = MessageDigest.getInstance("SHA-256");
120 digest.update(String.format("%s.%s.%s", entityType, fieldName, fieldValue).getBytes());
121 return convertBytesToHexString(digest.digest());
125 * Instantiates a new topographical entity.
127 public TopographicalEntity() {}
133 public String getAsJson() throws IOException {
136 Json.createObjectBuilder().add("entityType", entityType).add("pkey", entityPrimaryKeyValue)
137 .add("location", Json.createObjectBuilder().add("lat", latitude).add("lon", longitude))
138 .add("selfLink", selfLink).build();
140 return obj.toString();
145 * @see java.lang.Object#toString()
148 public String toString() {
149 return "TopographicalEntity [" + ("entityType=" + entityType + ", ")
150 + ("entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", ")
151 + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ") + ("ID=" + id + ", ")
152 + ("selfLink=" + selfLink) + "]";
155 public String getId() {
159 public String getEntityType() {
163 public void setEntityType(String entityType) {
164 this.entityType = entityType;
167 public String getEntityPrimaryKeyValue() {
168 return entityPrimaryKeyValue;
171 public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
172 this.entityPrimaryKeyValue = entityPrimaryKeyValue;
175 public String getEntityPrimaryKeyName() {
176 return entityPrimaryKeyName;
179 public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
180 this.entityPrimaryKeyName = entityPrimaryKeyName;
183 public String getLatitude() {
187 public void setLatitude(String latitude) {
188 this.latitude = latitude;
191 public String getLongitude() {
195 public void setLongitude(String longitude) {
196 this.longitude = longitude;
199 public String getSelfLink() {
203 public void setSelfLink(String selfLink) {
204 this.selfLink = selfLink;
207 public static long getSerialversionuid() {
208 return serialVersionUID;
211 public void setId(String id) {