af02ed15742e482548d1418f6d18b4a4c743524a
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / inventory / entity / TopographicalEntity.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.openecomp.sparky.inventory.entity;
24
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;
30
31 import javax.json.Json;
32 import javax.json.JsonObject;
33
34 /**
35  * The Class TopographicalEntity.
36  */
37 public class TopographicalEntity implements Serializable {
38
39   private static final long serialVersionUID = -5188479658230319058L;
40
41   protected String entityType;
42   protected String entityPrimaryKeyValue;
43   protected String entityPrimaryKeyName;
44   protected String latitude;
45   protected String longitude;
46   protected String selfLink;
47
48   // generated, SHA-256 digest
49   protected String id;
50
51   /**
52    * Convert bytes to hex string.
53    *
54    * @param bytesToConvert the bytes to convert
55    * @return the string
56    */
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]));
61     }
62     return hexString.toString();
63   }
64
65   /**
66    * Concat array.
67    *
68    * @param list the list
69    * @param delimiter the delimiter
70    * @return the string
71    */
72   private static String concatArray(List<String> list, char delimiter) {
73
74     if (list == null || list.size() == 0) {
75       return "";
76     }
77
78     StringBuilder result = new StringBuilder(64);
79
80     int listSize = list.size();
81     boolean firstValue = true;
82
83     for (String item : list) {
84
85       if (firstValue) {
86         result.append(item);
87         firstValue = false;
88       } else {
89         result.append(delimiter).append(item);
90       }
91
92     }
93
94     return result.toString();
95
96   }
97
98   /*
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.
103    */
104
105   /**
106    * Generate unique sha digest.
107    *
108    * @param entityType the entity type
109    * @param fieldName the field name
110    * @param fieldValue the field value
111    * @return the string
112    * @throws NoSuchAlgorithmException the no such algorithm exception
113    */
114   public static String generateUniqueShaDigest(String entityType, String fieldName,
115       String fieldValue) throws NoSuchAlgorithmException {
116
117     /*
118      * Basically SHA-256 will result in an identity with a guaranteed uniqueness compared to just a
119      * java hashcode value.
120      */
121     MessageDigest digest = MessageDigest.getInstance("SHA-256");
122     digest.update(String.format("%s.%s.%s", entityType, fieldName, fieldValue).getBytes());
123     return convertBytesToHexString(digest.digest());
124   }
125
126   /**
127    * Instantiates a new topographical entity.
128    */
129   public TopographicalEntity() {}
130
131   /*
132    * (non-Javadoc)
133    * 
134    * @see com.att.queryrouter.dao.DocumentStoreDataEntity#getAsJson()
135    */
136   public String getAsJson() throws IOException {
137
138     JsonObject obj =
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();
142
143     return obj.toString();
144   }
145
146
147   /* (non-Javadoc)
148    * @see java.lang.Object#toString()
149    */
150   @Override
151   public String toString() {
152     return "TopographicalEntity [" + ("entityType=" + entityType + ", ")
153         + ("entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", ")
154         + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ") + ("ID=" + id + ", ")
155         + ("selfLink=" + selfLink) + "]";
156   }
157
158   public String getId() {
159     return this.id;
160   }
161
162   public String getEntityType() {
163     return entityType;
164   }
165
166   public void setEntityType(String entityType) {
167     this.entityType = entityType;
168   }
169
170   public String getEntityPrimaryKeyValue() {
171     return entityPrimaryKeyValue;
172   }
173
174   public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
175     this.entityPrimaryKeyValue = entityPrimaryKeyValue;
176   }
177
178   public String getEntityPrimaryKeyName() {
179     return entityPrimaryKeyName;
180   }
181
182   public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
183     this.entityPrimaryKeyName = entityPrimaryKeyName;
184   }
185
186   public String getLatitude() {
187     return latitude;
188   }
189
190   public void setLatitude(String latitude) {
191     this.latitude = latitude;
192   }
193
194   public String getLongitude() {
195     return longitude;
196   }
197
198   public void setLongitude(String longitude) {
199     this.longitude = longitude;
200   }
201
202   public String getSelfLink() {
203     return selfLink;
204   }
205
206   public void setSelfLink(String selfLink) {
207     this.selfLink = selfLink;
208   }
209
210   public static long getSerialversionuid() {
211     return serialVersionUID;
212   }
213
214   public void setId(String id) {
215     this.id = id;
216   }
217
218 }