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