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