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