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