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