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;
46 public String getOwner() {
50 public void setOwner(String owner) {
54 public boolean isSuspended() {
58 public void setSuspended(boolean suspended) {
59 this.suspended = suspended;
64 public boolean isUse100() {
68 public void setUse100(boolean use100) {
76 public DR_Sub( String dLN,
83 this.dcaeLocationName = dLN;
87 this.deliveryURL = dU;
90 this.setStatus( DmaapObject_Status.NEW );
94 public DR_Sub ( String json ) {
95 logger.info( "DR_Sub:" + json );
96 JSONParser parser = new JSONParser();
100 jsonObj = (JSONObject) parser.parse( json );
101 } catch ( ParseException pe ) {
102 errorLogger.error( DmaapbcLogMessageEnum.JSON_PARSING_ERROR, "DR_Sub", json );
103 this.setStatus( DmaapObject_Status.INVALID );
107 this.setOwner( (String) jsonObj.get("subscriber"));
108 this.setSuspended( (boolean) jsonObj.get("suspend"));
110 JSONObject links = (JSONObject) jsonObj.get("links");
111 String url = (String) links.get("feed");
112 this.setFeedId( url.substring( url.lastIndexOf('/')+1, url.length() ));
113 url = (String) links.get("self");
114 this.setSubId( url.substring( url.lastIndexOf('/')+1, url.length() ));
115 logger.info( "feedid="+ this.getFeedId() );
116 this.setLogURL( (String) links.get("log") );
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"));
124 this.setStatus( DmaapObject_Status.VALID );
126 logger.info( "new DR_Sub returning");
129 public String getDcaeLocationName() {
130 return dcaeLocationName;
133 public void setDcaeLocationName(String dcaeLocationName) {
134 this.dcaeLocationName = dcaeLocationName;
137 public String getUsername() {
141 public void setUsername(String username) {
142 this.username = username;
145 public String getUserpwd() {
149 public void setUserpwd(String userpwd) {
150 this.userpwd = userpwd;
153 public String getFeedId() {
157 public void setFeedId(String feedId) {
158 this.feedId = feedId;
161 public String getDeliveryURL() {
165 public void setDeliveryURL(String deliveryURL) {
166 this.deliveryURL = deliveryURL;
169 public String getLogURL() {
173 public void setLogURL(String logURL) {
174 this.logURL = logURL;
177 public String getSubId() {
181 public void setSubId(String subId) {
187 public byte[] getBytes() {
188 return toProvJSON().getBytes(StandardCharsets.UTF_8);
190 // returns the DR_Sub object in JSON that conforms to DR Prov Server expectations
191 public String toProvJSON() {
193 // 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.
194 // we send this regardless of DR Release because older versions of DR seem to safely ignore it
195 // and soon those versions won't be around anyway...
196 // Similarly, in the 1704 Release, a new subscriber attribute "follow_redirect" was introduced.
197 // We are setting it to "true" because that is the general behavior desired in OpenDCAE.
198 // 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.
199 // In the event we abandon that, then setting the sub attribute to true will be a good thing.
201 // - introduce Bus Controller API support for these attributes
202 // - store the default values in the DB
203 String postJSON = String.format("{\"suspend\": \"%s\", \"delivery\": {\"url\": \"%s\", \"user\": \"%s\", \"password\": \"%s\", \"use100\": \"%s\"}, \"metadataOnly\": %s, \"groupid\": \"%s\", \"follow_redirect\": %s }",
205 this.getDeliveryURL(),
213 logger.info( postJSON );
218 public String toString() {
219 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}",