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