2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.dbcapi.model;
23 import java.nio.charset.StandardCharsets;
25 import javax.xml.bind.annotation.XmlRootElement;
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;
33 public class DR_Sub extends DmaapObject {
35 private String dcaeLocationName;
36 private String username;
37 private String userpwd;
38 private String feedId;
39 private String deliveryURL;
40 private String logURL;
42 private boolean use100;
43 private boolean suspended;
45 private boolean guaranteedDelivery;
46 private boolean guaranteedSequence;
47 private boolean privilegedSubscriber;
49 // NOTE: the following fields are optional in the API but not stored in the DB
50 private String feedName;
51 private String feedVersion;
57 public DR_Sub( String dLN,
64 this.dcaeLocationName = dLN;
68 this.deliveryURL = dU;
71 this.setStatus( DmaapObject_Status.NEW );
75 public DR_Sub ( String json ) {
76 logger.info( "DR_Sub:" + json );
77 JSONParser parser = new JSONParser();
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 );
88 this.setOwner( (String) jsonObj.get("subscriber"));
89 this.setSuspended( (boolean) jsonObj.get("suspend"));
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 ) {
103 this.setGuaranteedDelivery( (boolean) jsonObj.get("guaranteed_delivery"));
104 } catch( NullPointerException npe ) {
105 this.setGuaranteedDelivery(false);
108 this.setGuaranteedSequence( (boolean) jsonObj.get("guaranteed_sequence"));
109 } catch( NullPointerException npe ) {
110 this.setGuaranteedSequence(false);
113 this.setPrivilegedSubscriber((boolean) jsonObj.get("privilegedSubscriber"));
114 } catch( NullPointerException npe ) {
115 this.setPrivilegedSubscriber(false);
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"));
126 this.setStatus( DmaapObject_Status.VALID );
128 logger.info( "new DR_Sub returning");
131 public String getOwner() {
135 public void setOwner(String owner) {
139 public boolean isSuspended() {
143 public void setSuspended(boolean suspended) {
144 this.suspended = suspended;
149 public boolean isUse100() {
153 public void setUse100(boolean use100) {
154 this.use100 = use100;
157 public String getDcaeLocationName() {
158 return dcaeLocationName;
161 public void setDcaeLocationName(String dcaeLocationName) {
162 this.dcaeLocationName = dcaeLocationName;
165 public String getUsername() {
169 public void setUsername(String username) {
170 this.username = username;
173 public String getUserpwd() {
177 public void setUserpwd(String userpwd) {
178 this.userpwd = userpwd;
181 public String getFeedId() {
185 public void setFeedId(String feedId) {
186 this.feedId = feedId;
189 public String getDeliveryURL() {
193 public void setDeliveryURL(String deliveryURL) {
194 this.deliveryURL = deliveryURL;
197 public String getLogURL() {
201 public void setLogURL(String logURL) {
202 this.logURL = logURL;
205 public String getSubId() {
209 public void setSubId(String subId) {
214 public boolean isGuaranteedDelivery() {
215 return guaranteedDelivery;
218 public void setGuaranteedDelivery(boolean guaranteedDelivery) {
219 this.guaranteedDelivery = guaranteedDelivery;
222 public boolean isGuaranteedSequence() {
223 return guaranteedSequence;
226 public void setGuaranteedSequence(boolean guaranteedSequence) {
227 this.guaranteedSequence = guaranteedSequence;
230 public boolean isPrivilegedSubscriber() {
231 return privilegedSubscriber;
234 public void setPrivilegedSubscriber(boolean privilegedSubscriber) {
235 this.privilegedSubscriber = privilegedSubscriber;
240 public String getFeedName() {
244 public void setFeedName(String feedName) {
245 this.feedName = feedName;
248 public String getFeedVersion() {
252 public void setFeedVersion(String feedVersion) {
253 this.feedVersion = feedVersion;
256 public byte[] getBytes(String provApi) {
257 if ( "AT&T".equals(provApi)) {
258 return toProvJSONforATT().getBytes(StandardCharsets.UTF_8);
260 return toProvJSON().getBytes(StandardCharsets.UTF_8);
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 "
271 ,this.getDeliveryURL()
278 ,this.isPrivilegedSubscriber()
281 logger.info( postJSON );
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
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"
306 ,this.getDeliveryURL()
313 ,this.isGuaranteedDelivery()
314 ,this.isGuaranteedSequence()
317 logger.info( postJSON );
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}",