UI Exensibility config cleanup
[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   /* (non-Javadoc)
188    * @see org.openecomp.sparky.synchronizer.entity.IndexDocument#deriveFields()
189    */
190   @Override
191   public void deriveFields() {
192
193     /*
194      * We'll try and create a unique identity key that we can use for differencing the previously
195      * imported record sets as we won't have granular control of what is created/removed and when.
196      * The best we can hope for is identification of resources by generated Id until the
197      * Identity-Service UUID is tagged against all resources, then we can use that instead.
198      */
199
200     OxmEntityDescriptor descriptor = oxmEntityLookup.getEntityDescriptors().get(entityType);
201     String entityPrimaryKeyName = NodeUtils.concatArray(
202         descriptor.getPrimaryKeyAttributeNames(), "/");
203
204     this.id =
205         NodeUtils.generateUniqueShaDigest(entityType, entityPrimaryKeyName, entityPrimaryKeyValue);
206   }
207
208   /* (non-Javadoc)
209    * @see java.lang.Object#toString()
210    */
211   @Override
212   public String toString() {
213     return "TopographicalEntity [" + ("entityType=" + entityType + ", ")
214         + ("entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", ")
215         + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ") + ("ID=" + id + ", ")
216         + ("selfLink=" + selfLink) + "]";
217   }
218
219   @Override
220   @JsonIgnore
221   public String getId() {
222     return this.id;
223   }
224
225   @JsonProperty("entityType")
226   public String getEntityType() {
227     return entityType;
228   }
229
230   public void setEntityType(String entityType) {
231     this.entityType = entityType;
232   }
233
234   @JsonProperty("entityPrimaryKeyValue")
235   public String getEntityPrimaryKeyValue() {
236     return entityPrimaryKeyValue;
237   }
238
239   public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
240     this.entityPrimaryKeyValue = entityPrimaryKeyValue;
241   }
242
243   @JsonProperty("entityPrimaryKeyName")
244   public String getEntityPrimaryKeyName() {
245     return entityPrimaryKeyName;
246   }
247
248   public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
249     this.entityPrimaryKeyName = entityPrimaryKeyName;
250   }
251
252   @JsonProperty("lat")
253   public String getLatitude() {
254     return latitude;
255   }
256
257   public void setLatitude(String latitude) {
258     this.latitude = latitude;
259   }
260
261   @JsonProperty("long")
262   public String getLongitude() {
263     return longitude;
264   }
265
266   public void setLongitude(String longitude) {
267     this.longitude = longitude;
268   }
269
270   @JsonProperty("link")
271   public String getSelfLink() {
272     return selfLink;
273   }
274
275   public void setSelfLink(String selfLink) {
276     this.selfLink = selfLink;
277   }
278
279   @JsonIgnore
280   public static long getSerialversionuid() {
281     return serialVersionUID;
282   }
283
284   public void setId(String id) {
285     this.id = id;
286   }
287   
288 }