Cleaning code
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / inventory / entity / GeoIndexDocument.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.onap.aai.sparky.inventory.entity;
24
25 import java.io.Serializable;
26 import java.security.MessageDigest;
27 import java.security.NoSuchAlgorithmException;
28 import java.util.List;
29
30 import org.onap.aai.sparky.config.oxm.OxmEntityDescriptor;
31 import org.onap.aai.sparky.config.oxm.OxmEntityLookup;
32 import org.onap.aai.sparky.sync.entity.IndexDocument;
33 import org.onap.aai.sparky.util.NodeUtils;
34
35 import com.fasterxml.jackson.annotation.JsonIgnore;
36 import com.fasterxml.jackson.annotation.JsonProperty;
37 import com.fasterxml.jackson.core.JsonProcessingException;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39
40 /**
41  * The Class GeoIndexDocument.
42  */
43 public class GeoIndexDocument implements Serializable, IndexDocument {
44
45   @JsonIgnore
46   private static final long serialVersionUID = -5188479658230319058L;
47
48   protected String entityType;
49   protected String entityPrimaryKeyValue;
50   protected String entityPrimaryKeyName;
51   protected String latitude;
52   protected String longitude;
53   protected String selfLink;
54   
55   @JsonIgnore
56   protected OxmEntityLookup oxmEntityLookup;
57   
58   @JsonIgnore
59   protected ObjectMapper mapper = new ObjectMapper();
60   // generated, SHA-256 digest
61   @JsonIgnore
62   protected String id;
63
64   /**
65    * Convert bytes to hex string.
66    *
67    * @param bytesToConvert the bytes to convert
68    * @return the string
69    */
70   private static String convertBytesToHexString(byte[] bytesToConvert) {
71     StringBuffer hexString = new StringBuffer();
72     for (int i = 0; i < bytesToConvert.length; i++) {
73       hexString.append(Integer.toHexString(0xFF & bytesToConvert[i]));
74     }
75     return hexString.toString();
76   }
77
78
79   @JsonIgnore
80   public boolean isValidGeoDocument() {
81
82     boolean isValid = true;
83
84     isValid &= (this.getEntityType() != null);
85     isValid &= (this.getLatitude() != null);
86     isValid &= (this.getLongitude() != null);
87     isValid &= (this.getId() != null);
88     isValid &= (this.getSelfLink() != null);
89
90     isValid &= NodeUtils.isNumeric(this.getLatitude());
91     isValid &= NodeUtils.isNumeric(this.getLongitude());
92
93     return isValid;
94   }
95
96   /**
97    * Concat array.
98    *
99    * @param list the list
100    * @param delimiter the delimiter
101    * @return the string
102    */
103   private static String concatArray(List<String> list, char delimiter) {
104
105     if (list == null || list.size() == 0) {
106       return "";
107     }
108
109     StringBuilder result = new StringBuilder(64);
110
111     int listSize = list.size();
112     boolean firstValue = true;
113
114     for (String item : list) {
115
116       if (firstValue) {
117         result.append(item);
118         firstValue = false;
119       } else {
120         result.append(delimiter).append(item);
121       }
122
123     }
124
125     return result.toString();
126
127   }
128
129   /*
130    * We'll try and create a unique identity key that we can use for differencing the previously
131    * imported record sets as we won't have granular control of what is created/removed and when. The
132    * best we can hope for is identification of resources by generated Id until the Identity-Service
133    * UUID is tagged against all resources, then we can use that instead.
134    */
135
136   /**
137    * Generate unique sha digest.
138    *
139    * @param entityType the entity type
140    * @param fieldName the field name
141    * @param fieldValue the field value
142    * @return the string
143    * @throws NoSuchAlgorithmException the no such algorithm exception
144    */
145   public static String generateUniqueShaDigest(String entityType, String fieldName,
146       String fieldValue) throws NoSuchAlgorithmException {
147
148     /*
149      * Basically SHA-256 will result in an identity with a guaranteed uniqueness compared to just a
150      * java hashcode value.
151      */
152     MessageDigest digest = MessageDigest.getInstance("SHA-256");
153     digest.update(String.format("%s.%s.%s", entityType, fieldName, fieldValue).getBytes());
154     return convertBytesToHexString(digest.digest());
155   }
156
157   /**
158    * Instantiates a new geo index document.
159    */
160   public GeoIndexDocument() {}
161
162   /*
163    * (non-Javadoc)
164    * 
165    */
166   
167   @Override
168   @JsonIgnore
169   public String getAsJson() throws JsonProcessingException {
170
171     if (latitude != null && longitude != null) {
172       
173       /**
174        * A valid entry from this class is one that has both lat and long. If one or both is missing
175        * we shouldn't be indexing anything.
176        */
177       
178       return NodeUtils.convertObjectToJson(this, true);
179       
180     }
181     
182     return null;
183
184   }
185
186   /* (non-Javadoc)
187    * @see org.openecomp.sparky.synchronizer.entity.IndexDocument#deriveFields()
188    */
189   @Override
190   public void deriveFields() {
191
192     /*
193      * We'll try and create a unique identity key that we can use for differencing the previously
194      * imported record sets as we won't have granular control of what is created/removed and when.
195      * The best we can hope for is identification of resources by generated Id until the
196      * Identity-Service UUID is tagged against all resources, then we can use that instead.
197      */
198
199     OxmEntityDescriptor descriptor = oxmEntityLookup.getEntityDescriptors().get(entityType);
200     String entityPrimaryKeyName = NodeUtils.concatArray(
201         descriptor.getPrimaryKeyAttributeNames(), "/");
202
203     this.id =
204         NodeUtils.generateUniqueShaDigest(entityType, entityPrimaryKeyName, entityPrimaryKeyValue);
205   }
206
207   /* (non-Javadoc)
208    * @see java.lang.Object#toString()
209    */
210   @Override
211   public String toString() {
212     return "TopographicalEntity [" + ("entityType=" + entityType + ", ")
213         + ("entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", ")
214         + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ") + ("ID=" + id + ", ")
215         + ("selfLink=" + selfLink) + "]";
216   }
217
218   @Override
219   @JsonIgnore
220   public String getId() {
221     return this.id;
222   }
223
224   @JsonProperty("entityType")
225   public String getEntityType() {
226     return entityType;
227   }
228
229   public void setEntityType(String entityType) {
230     this.entityType = entityType;
231   }
232
233   @JsonProperty("entityPrimaryKeyValue")
234   public String getEntityPrimaryKeyValue() {
235     return entityPrimaryKeyValue;
236   }
237
238   public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
239     this.entityPrimaryKeyValue = entityPrimaryKeyValue;
240   }
241
242   @JsonProperty("entityPrimaryKeyName")
243   public String getEntityPrimaryKeyName() {
244     return entityPrimaryKeyName;
245   }
246
247   public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
248     this.entityPrimaryKeyName = entityPrimaryKeyName;
249   }
250
251   @JsonProperty("lat")
252   public String getLatitude() {
253     return latitude;
254   }
255
256   public void setLatitude(String latitude) {
257     this.latitude = latitude;
258   }
259
260   @JsonProperty("long")
261   public String getLongitude() {
262     return longitude;
263   }
264
265   public void setLongitude(String longitude) {
266     this.longitude = longitude;
267   }
268
269   @JsonProperty("link")
270   public String getSelfLink() {
271     return selfLink;
272   }
273
274   public void setSelfLink(String selfLink) {
275     this.selfLink = selfLink;
276   }
277
278   @JsonIgnore
279   public static long getSerialversionuid() {
280     return serialVersionUID;
281   }
282
283   public void setId(String id) {
284     this.id = id;
285   }
286   
287 }