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;
48 private boolean decompress;
50 // NOTE: the following fields are optional in the API but not stored in the DB
52 private String feedName;
53 private String feedVersion;
58 public DR_Sub( String dLN,
65 this.dcaeLocationName = dLN;
69 this.deliveryURL = dU;
72 this.setStatus( DmaapObject_Status.NEW );
76 public DR_Sub ( String json ) {
77 logger.info( "DR_Sub:" + json );
78 JSONParser parser = new JSONParser();
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 );
89 this.setOwner( (String) jsonObj.get("subscriber"));
90 this.setSuspended( (boolean) jsonObj.get("suspend"));
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 ) {
104 this.setGuaranteedDelivery( (boolean) jsonObj.get("guaranteed_delivery"));
105 } catch( NullPointerException npe ) {
106 this.setGuaranteedDelivery(false);
109 this.setGuaranteedSequence( (boolean) jsonObj.get("guaranteed_sequence"));
110 } catch( NullPointerException npe ) {
111 this.setGuaranteedSequence(false);
114 this.setPrivilegedSubscriber((boolean) jsonObj.get("privilegedSubscriber"));
115 } catch( NullPointerException npe ) {
116 this.setPrivilegedSubscriber(false);
119 this.setDecompress((boolean) jsonObj.get("decompress"));
120 } catch( NullPointerException npe ) {
121 this.setDecompress(false);
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"));
130 this.setStatus( DmaapObject_Status.VALID );
132 logger.info( "new DR_Sub returning");
135 public String getOwner() {
139 public void setOwner(String owner) {
143 public boolean isSuspended() {
147 public void setSuspended(boolean suspended) {
148 this.suspended = suspended;
153 public boolean isUse100() {
157 public void setUse100(boolean use100) {
158 this.use100 = use100;
161 public String getDcaeLocationName() {
162 return dcaeLocationName;
165 public void setDcaeLocationName(String dcaeLocationName) {
166 this.dcaeLocationName = dcaeLocationName;
169 public String getUsername() {
173 public void setUsername(String username) {
174 this.username = username;
177 public String getUserpwd() {
181 public void setUserpwd(String userpwd) {
182 this.userpwd = userpwd;
185 public String getFeedId() {
189 public void setFeedId(String feedId) {
190 this.feedId = feedId;
193 public String getDeliveryURL() {
197 public void setDeliveryURL(String deliveryURL) {
198 this.deliveryURL = deliveryURL;
201 public String getLogURL() {
205 public void setLogURL(String logURL) {
206 this.logURL = logURL;
209 public String getSubId() {
213 public void setSubId(String subId) {
218 public boolean isGuaranteedDelivery() {
219 return guaranteedDelivery;
222 public void setGuaranteedDelivery(boolean guaranteedDelivery) {
223 this.guaranteedDelivery = guaranteedDelivery;
226 public boolean isGuaranteedSequence() {
227 return guaranteedSequence;
230 public void setGuaranteedSequence(boolean guaranteedSequence) {
231 this.guaranteedSequence = guaranteedSequence;
234 public boolean isPrivilegedSubscriber() {
235 return privilegedSubscriber;
238 public void setPrivilegedSubscriber(boolean privilegedSubscriber) {
239 this.privilegedSubscriber = privilegedSubscriber;
242 public boolean isDecompress() {
246 public void setDecompress(boolean decompressData) {
247 this.decompress = decompressData;
250 public String getFeedName() {
254 public void setFeedName(String feedName) {
255 this.feedName = feedName;
258 public String getFeedVersion() {
262 public void setFeedVersion(String feedVersion) {
263 this.feedVersion = feedVersion;
266 public byte[] getBytes(String provApi) {
267 if ( "AT&T".equals(provApi)) {
268 return toProvJSONforATT().getBytes(StandardCharsets.UTF_8);
270 return toProvJSON().getBytes(StandardCharsets.UTF_8);
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 "
281 ,this.getDeliveryURL()
288 ,this.isPrivilegedSubscriber()
292 logger.info( postJSON );
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
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"
317 ,this.getDeliveryURL()
324 ,this.isGuaranteedDelivery()
325 ,this.isGuaranteedSequence()
328 logger.info( postJSON );
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}",
349 public boolean equals(Object o) {
353 if (o == null || getClass() != o.getClass()) {
357 DR_Sub drSub = (DR_Sub) o;
359 if (use100 != drSub.use100) {
362 if (suspended != drSub.suspended) {
365 if (!dcaeLocationName.equals(drSub.dcaeLocationName)) {
368 if (!username.equals(drSub.username)) {
371 if (!userpwd.equals(drSub.userpwd)) {
374 if (!feedId.equals(drSub.feedId)) {
377 return subId.equals(drSub.subId);
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);