dd0b85dc623f455f4ee234b5484433dad089fca4
[dmaap/dbcapi.git] / 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
49         public DR_Sub() {
50
51         }
52         
53         public DR_Sub( String dLN,
54                                         String uN,
55                                         String uP,
56                                         String fI,
57                                         String dU,
58                                         String lU,
59                                         boolean u100 ) {
60                 this.dcaeLocationName = dLN;
61                 this.username = uN;
62                 this.userpwd = uP;
63                 this.feedId = fI;
64                 this.deliveryURL = dU;
65                 this.logURL = lU;
66                 this.use100 = u100;
67                 this.setStatus( DmaapObject_Status.NEW );
68                 this.subId = "0";
69         }
70         
71         public DR_Sub ( String json ) {
72                 logger.info( "DR_Sub:" + json );
73                 JSONParser parser = new JSONParser();
74                 JSONObject jsonObj;
75                 
76                 try {
77                         jsonObj = (JSONObject) parser.parse( json );
78                 } catch ( ParseException pe ) {
79                         errorLogger.error( DmaapbcLogMessageEnum.JSON_PARSING_ERROR, "DR_Sub", json );
80             this.setStatus( DmaapObject_Status.INVALID );
81             return;
82         }
83
84                 this.setOwner( (String) jsonObj.get("subscriber"));
85                 this.setSuspended( (boolean) jsonObj.get("suspend"));
86                 
87                 try {
88                         JSONObject links = (JSONObject) jsonObj.get("links");
89                         String url = (String) links.get("feed");
90                         this.setFeedId( url.substring( url.lastIndexOf('/')+1, url.length() ));
91                         url = (String) links.get("self");
92                         this.setSubId( url.substring( url.lastIndexOf('/')+1, url.length() ));
93                         logger.info( "feedid="+ this.getFeedId() );
94                         this.setLogURL( (String) links.get("log") );
95                 } catch (NullPointerException npe ) {
96                         
97                 }
98                 try {
99                         this.setGuaranteedDelivery( (boolean) jsonObj.get("guaranteed_delivery"));
100                 } catch( NullPointerException npe ) {
101                         this.setGuaranteedDelivery(false);
102                 }
103                 try {
104                         this.setGuaranteedSequence( (boolean) jsonObj.get("guaranteed_sequence"));
105                 } catch( NullPointerException npe ) {
106                         this.setGuaranteedSequence(false);
107                 }
108                 try {
109                         this.setPrivilegedSubscriber((boolean) jsonObj.get("privilegedSubscriber"));
110                 } catch( NullPointerException npe ) {
111                         this.setPrivilegedSubscriber(false);
112                 }
113                 
114                 JSONObject del = (JSONObject) jsonObj.get("delivery");
115                 this.setDeliveryURL( (String) del.get("url") ); 
116                 this.setUsername( (String) del.get("user"));
117                 this.setUserpwd( (String) del.get( "password"));
118                 this.setUse100((boolean) del.get( "use100"));
119
120                 
121
122                 this.setStatus( DmaapObject_Status.VALID );
123
124                 logger.info( "new DR_Sub returning");
125         }
126         
127         public String getOwner() {
128                 return owner;
129         }
130
131         public void setOwner(String owner) {
132                 this.owner = owner;
133         }
134
135         public boolean isSuspended() {
136                 return suspended;
137         }
138
139         public void setSuspended(boolean suspended) {
140                 this.suspended = suspended;
141         }
142
143
144
145         public boolean isUse100() {
146                 return use100;
147         }
148
149         public void setUse100(boolean use100) {
150                 this.use100 = use100;
151         }
152
153         public String getDcaeLocationName() {
154                 return dcaeLocationName;
155         }
156
157         public void setDcaeLocationName(String dcaeLocationName) {
158                 this.dcaeLocationName = dcaeLocationName;
159         }
160
161         public String getUsername() {
162                 return username;
163         }
164
165         public void setUsername(String username) {
166                 this.username = username;
167         }
168
169         public String getUserpwd() {
170                 return userpwd;
171         }
172
173         public void setUserpwd(String userpwd) {
174                 this.userpwd = userpwd;
175         }
176
177         public String getFeedId() {
178                 return feedId;
179         }
180
181         public void setFeedId(String feedId) {
182                 this.feedId = feedId;
183         }
184
185         public String getDeliveryURL() {
186                 return deliveryURL;
187         }
188
189         public void setDeliveryURL(String deliveryURL) {
190                 this.deliveryURL = deliveryURL;
191         }
192
193         public String getLogURL() {
194                 return logURL;
195         }
196
197         public void setLogURL(String logURL) {
198                 this.logURL = logURL;
199         }
200
201         public String getSubId() {
202                 return subId;
203         }
204
205         public void setSubId(String subId) {
206                 this.subId = subId;
207         }
208
209
210         public boolean isGuaranteedDelivery() {
211                 return guaranteedDelivery;
212         }
213
214         public void setGuaranteedDelivery(boolean guaranteedDelivery) {
215                 this.guaranteedDelivery = guaranteedDelivery;
216         }
217
218         public boolean isGuaranteedSequence() {
219                 return guaranteedSequence;
220         }
221
222         public void setGuaranteedSequence(boolean guaranteedSequence) {
223                 this.guaranteedSequence = guaranteedSequence;
224         }
225
226         public boolean isPrivilegedSubscriber() {
227                 return privilegedSubscriber;
228         }
229
230         public void setPrivilegedSubscriber(boolean privilegedSubscriber) {
231                 this.privilegedSubscriber = privilegedSubscriber;
232         }
233
234         public byte[] getBytes(String provApi) {
235                 if ( "AT&T".equals(provApi)) {
236                         return toProvJSONforATT().getBytes(StandardCharsets.UTF_8);
237                 }
238                 return toProvJSON().getBytes(StandardCharsets.UTF_8);
239         }
240         // returns the DR_Sub object in JSON that conforms to ONAP DR Prov Server expectations
241         public String toProvJSON() {    
242                 // this is the original DR API that was contributed to ONAP
243                 String postJSON = String.format("{\"suspend\": %s, \"delivery\":"
244                                 + "{\"url\": \"%s\", \"user\": \"%s\", \"password\": \"%s\", \"use100\":  %s }"
245                                 + ", \"metadataOnly\": %s, \"groupid\": \"%s\", \"follow_redirect\": %s "
246                                 + ", \"privilegedSubscriber\": %s "
247                                 + "}"
248                                 ,this.suspended
249                                 ,this.getDeliveryURL()
250                                 ,this.getUsername()
251                                 ,this.getUserpwd()
252                                 ,this.isUse100()                
253                                 ,"false"
254                                 ,"0"
255                                 ,"true"
256                                 ,this.isPrivilegedSubscriber()
257                         );      
258                 
259                 logger.info( postJSON );
260                 return postJSON;
261         }
262         // returns the DR_Sub object in JSON that conforms to AT&T DR Prov Server expectations
263         // In Jan, 2019, the DR API used internally at AT&T diverged, so this function can be used in
264         // that runtime environment
265         public String toProvJSONforATT() {      
266                 // 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.
267                 // we send this regardless of DR Release because older versions of DR seem to safely ignore it
268                 // and soon those versions won't be around anyway...
269                 // Similarly, in the 1704 Release, a new subscriber attribute "follow_redirect" was introduced.
270                 // We are setting it to "true" because that is the general behavior desired in OpenDCAE.
271                 // 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.
272                 // In the event we abandon that, then setting the sub attribute to true will be a good thing.
273                 // Update Jan, 2019: added guaranteed_delivery and guaranteed_sequence with value false for
274                 // backwards compatibility
275                 // TODO:
276                 //   - introduce Bus Controller API support for these attributes
277                 //   - store the default values in the DB
278                 String postJSON = String.format("{\"suspend\": %s, \"delivery\":"
279                                 + "{\"url\": \"%s\", \"user\": \"%s\", \"password\": \"%s\", \"use100\":  %s}"
280                                 + ", \"metadataOnly\": %s, \"groupid\": \"%s\", \"follow_redirect\": %s "
281                                 + ", \"guaranteed_delivery\": %s, \"guaranteed_sequence\": %s"
282                                 + "}"
283                                 ,this.suspended
284                                 ,this.getDeliveryURL()
285                                 ,this.getUsername()
286                                 ,this.getUserpwd()
287                                 ,this.isUse100()
288                                 ,"false"
289                                 ,"0"
290                                 ,"true"
291                                 ,this.isGuaranteedDelivery()
292                                 ,this.isGuaranteedSequence()
293                                 );      
294                 
295                 logger.info( postJSON );
296                 return postJSON;
297         }
298         
299         @Override
300         public String toString() {
301                 String rc = String.format ( "DR_Sub: {dcaeLocationName=%s username=%s userpwd=%s feedId=%s deliveryURL=%s logURL=%s subid=%s use100=%s suspended=%s owner=%s}",
302                                 dcaeLocationName,
303                                 username,
304                                 userpwd,
305                                 feedId,
306                                 deliveryURL,
307                                 logURL,
308                                 subId,
309                                 use100,
310                                 suspended,
311                                 owner
312                                 );
313                 return rc;
314         }
315 }