Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / inventory / entity / TopographicalEntity.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
26 package org.openecomp.sparky.inventory.entity;
27
28 import java.io.IOException;
29 import java.io.Serializable;
30 import java.security.MessageDigest;
31 import java.security.NoSuchAlgorithmException;
32 import java.util.List;
33
34 import javax.json.Json;
35 import javax.json.JsonObject;
36
37 /**
38  * The Class TopographicalEntity.
39  */
40 public class TopographicalEntity implements Serializable {
41
42   private static final long serialVersionUID = -5188479658230319058L;
43
44   protected String entityType;
45   protected String entityPrimaryKeyValue;
46   protected String entityPrimaryKeyName;
47   protected String latitude;
48   protected String longitude;
49   protected String selfLink;
50
51   // generated, SHA-256 digest
52   protected String id;
53
54   /**
55    * Convert bytes to hex string.
56    *
57    * @param bytesToConvert the bytes to convert
58    * @return the string
59    */
60   private static String convertBytesToHexString(byte[] bytesToConvert) {
61     StringBuffer hexString = new StringBuffer();
62     for (int i = 0; i < bytesToConvert.length; i++) {
63       hexString.append(Integer.toHexString(0xFF & bytesToConvert[i]));
64     }
65     return hexString.toString();
66   }
67
68   /**
69    * Concat array.
70    *
71    * @param list the list
72    * @param delimiter the delimiter
73    * @return the string
74    */
75   private static String concatArray(List<String> list, char delimiter) {
76
77     if (list == null || list.size() == 0) {
78       return "";
79     }
80
81     StringBuilder result = new StringBuilder(64);
82
83     int listSize = list.size();
84     boolean firstValue = true;
85
86     for (String item : list) {
87
88       if (firstValue) {
89         result.append(item);
90         firstValue = false;
91       } else {
92         result.append(delimiter).append(item);
93       }
94
95     }
96
97     return result.toString();
98
99   }
100
101   /*
102    * We'll try and create a unique identity key that we can use for differencing the previously
103    * imported record sets as we won't have granular control of what is created/removed and when. The
104    * best we can hope for is identification of resources by generated Id until the Identity-Service
105    * UUID is tagged against all resources, then we can use that instead.
106    */
107
108   /**
109    * Generate unique sha digest.
110    *
111    * @param entityType the entity type
112    * @param fieldName the field name
113    * @param fieldValue the field value
114    * @return the string
115    * @throws NoSuchAlgorithmException the no such algorithm exception
116    */
117   public static String generateUniqueShaDigest(String entityType, String fieldName,
118       String fieldValue) throws NoSuchAlgorithmException {
119
120     /*
121      * Basically SHA-256 will result in an identity with a guaranteed uniqueness compared to just a
122      * java hashcode value.
123      */
124     MessageDigest digest = MessageDigest.getInstance("SHA-256");
125     digest.update(String.format("%s.%s.%s", entityType, fieldName, fieldValue).getBytes());
126     return convertBytesToHexString(digest.digest());
127   }
128
129   /**
130    * Instantiates a new topographical entity.
131    */
132   public TopographicalEntity() {}
133
134   /*
135    * (non-Javadoc)
136    * 
137    * @see com.att.queryrouter.dao.DocumentStoreDataEntity#getAsJson()
138    */
139   public String getAsJson() throws IOException {
140
141     JsonObject obj =
142         Json.createObjectBuilder().add("entityType", entityType).add("pkey", entityPrimaryKeyValue)
143             .add("location", Json.createObjectBuilder().add("lat", latitude).add("lon", longitude))
144             .add("selfLink", selfLink).build();
145
146     return obj.toString();
147   }
148
149
150   /* (non-Javadoc)
151    * @see java.lang.Object#toString()
152    */
153   @Override
154   public String toString() {
155     return "TopographicalEntity [" + ("entityType=" + entityType + ", ")
156         + ("entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", ")
157         + ("latitude=" + latitude + ", ") + ("longitude=" + longitude + ", ") + ("ID=" + id + ", ")
158         + ("selfLink=" + selfLink) + "]";
159   }
160
161   public String getId() {
162     return this.id;
163   }
164
165   public String getEntityType() {
166     return entityType;
167   }
168
169   public void setEntityType(String entityType) {
170     this.entityType = entityType;
171   }
172
173   public String getEntityPrimaryKeyValue() {
174     return entityPrimaryKeyValue;
175   }
176
177   public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
178     this.entityPrimaryKeyValue = entityPrimaryKeyValue;
179   }
180
181   public String getEntityPrimaryKeyName() {
182     return entityPrimaryKeyName;
183   }
184
185   public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
186     this.entityPrimaryKeyName = entityPrimaryKeyName;
187   }
188
189   public String getLatitude() {
190     return latitude;
191   }
192
193   public void setLatitude(String latitude) {
194     this.latitude = latitude;
195   }
196
197   public String getLongitude() {
198     return longitude;
199   }
200
201   public void setLongitude(String longitude) {
202     this.longitude = longitude;
203   }
204
205   public String getSelfLink() {
206     return selfLink;
207   }
208
209   public void setSelfLink(String selfLink) {
210     this.selfLink = selfLink;
211   }
212
213   public static long getSerialversionuid() {
214     return serialVersionUID;
215   }
216
217   public void setId(String id) {
218     this.id = id;
219   }
220
221 }