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