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