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