[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / main / java / org / onap / dmaap / dbcapi / model / DR_Sub.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.model;
22
23 import java.nio.charset.StandardCharsets;
24
25 import javax.xml.bind.annotation.XmlRootElement;
26
27 import org.json.simple.JSONObject;
28 import org.json.simple.parser.JSONParser;
29 import org.json.simple.parser.ParseException;
30 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
31
32 @XmlRootElement
33 public class DR_Sub extends DmaapObject {
34
35         private String dcaeLocationName;
36         private String username;
37         private String userpwd;
38         private String feedId;
39         private String deliveryURL;
40         private String logURL;
41         private String subId;
42         private boolean use100;
43         private boolean suspended;
44         private String owner;
45         private boolean guaranteedDelivery;
46         private boolean guaranteedSequence;
47         private boolean privilegedSubscriber;
48         private boolean decompress;
49
50         // NOTE: the following fields are optional in the API but not stored in the DB
51
52         private String  feedName;
53         private String  feedVersion;
54         public DR_Sub() {
55
56         }
57
58         public DR_Sub( String dLN,
59                                         String uN,
60                                         String uP,
61                                         String fI,
62                                         String dU,
63                                         String lU,
64                                         boolean u100 ) {
65                 this.dcaeLocationName = dLN;
66                 this.username = uN;
67                 this.userpwd = uP;
68                 this.feedId = fI;
69                 this.deliveryURL = dU;
70                 this.logURL = lU;
71                 this.use100 = u100;
72                 this.setStatus( DmaapObject_Status.NEW );
73                 this.subId = "0";
74         }
75
76         public DR_Sub ( String json ) {
77                 logger.info( "DR_Sub:" + json );
78                 JSONParser parser = new JSONParser();
79                 JSONObject jsonObj;
80
81                 try {
82                         jsonObj = (JSONObject) parser.parse( json );
83                 } catch ( ParseException pe ) {
84                         errorLogger.error( DmaapbcLogMessageEnum.JSON_PARSING_ERROR, "DR_Sub", json );
85             this.setStatus( DmaapObject_Status.INVALID );
86             return;
87         }
88
89                 this.setOwner( (String) jsonObj.get("subscriber"));
90                 this.setSuspended( (boolean) jsonObj.get("suspend"));
91
92                 try {
93                         JSONObject links = (JSONObject) jsonObj.get("links");
94                         String url = (String) links.get("feed");
95                         this.setFeedId( url.substring( url.lastIndexOf('/')+1, url.length() ));
96                         url = (String) links.get("self");
97                         this.setSubId( url.substring( url.lastIndexOf('/')+1, url.length() ));
98                         logger.info( "feedid="+ this.getFeedId() );
99                         this.setLogURL( (String) links.get("log") );
100                 } catch (NullPointerException npe ) {
101
102                 }
103                 try {
104                         this.setGuaranteedDelivery( (boolean) jsonObj.get("guaranteed_delivery"));
105                 } catch( NullPointerException npe ) {
106                         this.setGuaranteedDelivery(false);
107                 }
108                 try {
109                         this.setGuaranteedSequence( (boolean) jsonObj.get("guaranteed_sequence"));
110                 } catch( NullPointerException npe ) {
111                         this.setGuaranteedSequence(false);
112                 }
113                 try {
114                         this.setPrivilegedSubscriber((boolean) jsonObj.get("privilegedSubscriber"));
115                 } catch( NullPointerException npe ) {
116                         this.setPrivilegedSubscriber(false);
117                 }
118                 try {
119                         this.setDecompress((boolean) jsonObj.get("decompress"));
120                 } catch( NullPointerException npe ) {
121                         this.setDecompress(false);
122                 }
123
124                 JSONObject del = (JSONObject) jsonObj.get("delivery");
125                 this.setDeliveryURL( (String) del.get("url") );
126                 this.setUsername( (String) del.get("user"));
127                 this.setUserpwd( (String) del.get( "password"));
128                 this.setUse100((boolean) del.get( "use100"));
129
130                 this.setStatus( DmaapObject_Status.VALID );
131
132                 logger.info( "new DR_Sub returning");
133         }
134
135         public String getOwner() {
136                 return owner;
137         }
138
139         public void setOwner(String owner) {
140                 this.owner = owner;
141         }
142
143         public boolean isSuspended() {
144                 return suspended;
145         }
146
147         public void setSuspended(boolean suspended) {
148                 this.suspended = suspended;
149         }
150
151
152
153         public boolean isUse100() {
154                 return use100;
155         }
156
157         public void setUse100(boolean use100) {
158                 this.use100 = use100;
159         }
160
161         public String getDcaeLocationName() {
162                 return dcaeLocationName;
163         }
164
165         public void setDcaeLocationName(String dcaeLocationName) {
166                 this.dcaeLocationName = dcaeLocationName;
167         }
168
169         public String getUsername() {
170                 return username;
171         }
172
173         public void setUsername(String username) {
174                 this.username = username;
175         }
176
177         public String getUserpwd() {
178                 return userpwd;
179         }
180
181         public void setUserpwd(String userpwd) {
182                 this.userpwd = userpwd;
183         }
184
185         public String getFeedId() {
186                 return feedId;
187         }
188
189         public void setFeedId(String feedId) {
190                 this.feedId = feedId;
191         }
192
193         public String getDeliveryURL() {
194                 return deliveryURL;
195         }
196
197         public void setDeliveryURL(String deliveryURL) {
198                 this.deliveryURL = deliveryURL;
199         }
200
201         public String getLogURL() {
202                 return logURL;
203         }
204
205         public void setLogURL(String logURL) {
206                 this.logURL = logURL;
207         }
208
209         public String getSubId() {
210                 return subId;
211         }
212
213         public void setSubId(String subId) {
214                 this.subId = subId;
215         }
216
217
218         public boolean isGuaranteedDelivery() {
219                 return guaranteedDelivery;
220         }
221
222         public void setGuaranteedDelivery(boolean guaranteedDelivery) {
223                 this.guaranteedDelivery = guaranteedDelivery;
224         }
225
226         public boolean isGuaranteedSequence() {
227                 return guaranteedSequence;
228         }
229
230         public void setGuaranteedSequence(boolean guaranteedSequence) {
231                 this.guaranteedSequence = guaranteedSequence;
232         }
233
234         public boolean isPrivilegedSubscriber() {
235                 return privilegedSubscriber;
236         }
237
238         public void setPrivilegedSubscriber(boolean privilegedSubscriber) {
239                 this.privilegedSubscriber = privilegedSubscriber;
240         }
241
242         public boolean isDecompress() {
243                 return decompress;
244         }
245
246         public void setDecompress(boolean decompressData) {
247                 this.decompress = decompressData;
248         }
249
250         public String getFeedName() {
251                 return feedName;
252         }
253
254         public void setFeedName(String feedName) {
255                 this.feedName = feedName;
256         }
257
258         public String getFeedVersion() {
259                 return feedVersion;
260         }
261
262         public void setFeedVersion(String feedVersion) {
263                 this.feedVersion = feedVersion;
264         }
265
266         public byte[] getBytes(String provApi) {
267                 if ( "AT&T".equals(provApi)) {
268                         return toProvJSONforATT().getBytes(StandardCharsets.UTF_8);
269                 }
270                 return toProvJSON().getBytes(StandardCharsets.UTF_8);
271         }
272         // returns the DR_Sub object in JSON that conforms to ONAP DR Prov Server expectations
273         public String toProvJSON() {
274                 // this is the original DR API that was contributed to ONAP
275                 String postJSON = String.format("{\"suspend\": %s, \"delivery\":"
276                                 + "{\"url\": \"%s\", \"user\": \"%s\", \"password\": \"%s\", \"use100\":  %s }"
277                                 + ", \"metadataOnly\": %s, \"groupid\": \"%s\", \"follow_redirect\": %s "
278                                 + ", \"privilegedSubscriber\": %s, \"decompress\": %s "
279                                 + "}"
280                                 ,this.suspended
281                                 ,this.getDeliveryURL()
282                                 ,this.getUsername()
283                                 ,this.getUserpwd()
284                                 ,this.isUse100()
285                                 ,"false"
286                                 ,"0"
287                                 ,"true"
288                                 ,this.isPrivilegedSubscriber()
289                                 ,this.isDecompress()
290                         );
291
292                 logger.info( postJSON );
293                 return postJSON;
294         }
295         // returns the DR_Sub object in JSON that conforms to AT&T DR Prov Server expectations
296         // In Jan, 2019, the DR API used internally at AT&T diverged, so this function can be used in
297         // that runtime environment
298         public String toProvJSONforATT() {
299                 // in DR 3.0, API v2.1 a new groupid field is added.  We are not using this required field so just set it to 0.
300                 // we send this regardless of DR Release because older versions of DR seem to safely ignore it
301                 // and soon those versions won't be around anyway...
302                 // Similarly, in the 1704 Release, a new subscriber attribute "follow_redirect" was introduced.
303                 // We are setting it to "true" because that is the general behavior desired in OpenDCAE.
304                 // But it is really a no-op for OpenDCAE because we've deployed DR with the SYSTEM-level parameter for FOLLOW_REDIRECTS set to true.
305                 // In the event we abandon that, then setting the sub attribute to true will be a good thing.
306                 // Update Jan, 2019: added guaranteed_delivery and guaranteed_sequence with value false for
307                 // backwards compatibility
308                 // TODO:
309                 //   - introduce Bus Controller API support for these attributes
310                 //   - store the default values in the DB
311                 String postJSON = String.format("{\"suspend\": %s, \"delivery\":"
312                                 + "{\"url\": \"%s\", \"user\": \"%s\", \"password\": \"%s\", \"use100\":  %s}"
313                                 + ", \"metadataOnly\": %s, \"groupid\": \"%s\", \"follow_redirect\": %s "
314                                 + ", \"guaranteed_delivery\": %s, \"guaranteed_sequence\": %s"
315                                 + "}"
316                                 ,this.suspended
317                                 ,this.getDeliveryURL()
318                                 ,this.getUsername()
319                                 ,this.getUserpwd()
320                                 ,this.isUse100()
321                                 ,"false"
322                                 ,"0"
323                                 ,"true"
324                                 ,this.isGuaranteedDelivery()
325                                 ,this.isGuaranteedSequence()
326                                 );
327
328                 logger.info( postJSON );
329                 return postJSON;
330         }
331
332         @Override
333         public String toString() {
334                 return String.format ( "DR_Sub: {dcaeLocationName=%s username=%s userpwd=%s feedId=%s deliveryURL=%s logURL=%s subid=%s use100=%s suspended=%s owner=%s}",
335                                 dcaeLocationName,
336                                 username,
337                                 userpwd,
338                                 feedId,
339                                 deliveryURL,
340                                 logURL,
341                                 subId,
342                                 use100,
343                                 suspended,
344                                 owner
345                                 );
346         }
347
348         @Override
349         public boolean equals(Object o) {
350                 if (this == o) {
351                         return true;
352                 }
353                 if (o == null || getClass() != o.getClass()) {
354                         return false;
355                 }
356
357                 DR_Sub drSub = (DR_Sub) o;
358
359                 if (use100 != drSub.use100) {
360                         return false;
361                 }
362                 if (suspended != drSub.suspended) {
363                         return false;
364                 }
365                 if (!dcaeLocationName.equals(drSub.dcaeLocationName)) {
366                         return false;
367                 }
368                 if (!username.equals(drSub.username)) {
369                         return false;
370                 }
371                 if (!userpwd.equals(drSub.userpwd)) {
372                         return false;
373                 }
374                 if (!feedId.equals(drSub.feedId)) {
375                         return false;
376                 }
377                 return subId.equals(drSub.subId);
378         }
379
380         @Override
381         public int hashCode() {
382                 int result = dcaeLocationName.hashCode();
383                 result = 31 * result + username.hashCode();
384                 result = 31 * result + userpwd.hashCode();
385                 result = 31 * result + feedId.hashCode();
386                 result = 31 * result + subId.hashCode();
387                 result = 31 * result + (use100 ? 1 : 0);
388                 result = 31 * result + (suspended ? 1 : 0);
389                 return result;
390         }
391 }