Adding UI extensibility
[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    * @see com.att.queryrouter.dao.DocumentStoreDataEntity#getAsJson()
166    */
167
168   @Override
169   @JsonIgnore
170   public String getAsJson() throws JsonProcessingException {
171
172     if (latitude != null && longitude != null) {
173
174       /**
175        * A valid entry from this class is one that has both lat and long. If one or both is missing
176        * we shouldn't be indexing anything.
177        */
178
179       return NodeUtils.convertObjectToJson(this, true);
180
181     }
182
183     return null;
184
185   }
186
187   /*
188    * (non-Javadoc)
189    * 
190    * @see org.openecomp.sparky.synchronizer.entity.IndexDocument#deriveFields()
191    */
192   @Override
193   public void deriveFields() {
194
195     /*
196      * We'll try and create a unique identity key that we can use for differencing the previously
197      * imported record sets as we won't have granular control of what is created/removed and when.
198      * The best we can hope for is identification of resources by generated Id until the
199      * Identity-Service UUID is tagged against all resources, then we can use that instead.
200      */
201
202     OxmEntityDescriptor descriptor = oxmEntityLookup.getEntityDescriptors().get(entityType);
203     String entityPrimaryKeyName =
204         NodeUtils.concatArray(descriptor.getPrimaryKeyAttributeNames(), "/");
205
206     this.id =
207         NodeUtils.generateUniqueShaDigest(entityType, entityPrimaryKeyName, entityPrimaryKeyValue);
208   }
209
210   /*
211    * (non-Javadoc)
212    * 
213    * @see java.lang.Object#toString()
214    */
215   @Override
216   public String toString() {
217     return "TopographicalEntity [" + ("entityType=" + entityType + ", ")
218         + ("entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", ")
219         + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ") + ("ID=" + id + ", ")
220         + ("selfLink=" + selfLink) + "]";
221   }
222
223   @Override
224   @JsonIgnore
225   public String getId() {
226     return this.id;
227   }
228
229   @JsonProperty("entityType")
230   public String getEntityType() {
231     return entityType;
232   }
233
234   public void setEntityType(String entityType) {
235     this.entityType = entityType;
236   }
237
238   @JsonProperty("entityPrimaryKeyValue")
239   public String getEntityPrimaryKeyValue() {
240     return entityPrimaryKeyValue;
241   }
242
243   public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
244     this.entityPrimaryKeyValue = entityPrimaryKeyValue;
245   }
246
247   @JsonProperty("entityPrimaryKeyName")
248   public String getEntityPrimaryKeyName() {
249     return entityPrimaryKeyName;
250   }
251
252   public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
253     this.entityPrimaryKeyName = entityPrimaryKeyName;
254   }
255
256   @JsonProperty("lat")
257   public String getLatitude() {
258     return latitude;
259   }
260
261   public void setLatitude(String latitude) {
262     this.latitude = latitude;
263   }
264
265   @JsonProperty("long")
266   public String getLongitude() {
267     return longitude;
268   }
269
270   public void setLongitude(String longitude) {
271     this.longitude = longitude;
272   }
273
274   @JsonProperty("link")
275   public String getSelfLink() {
276     return selfLink;
277   }
278
279   public void setSelfLink(String selfLink) {
280     this.selfLink = selfLink;
281   }
282
283   @JsonIgnore
284   public static long getSerialversionuid() {
285     return serialVersionUID;
286   }
287
288   public void setId(String id) {
289     this.id = id;
290   }
291
292 }