2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright (C) 2019 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.dmaap.dbcapi.model;
25 import java.nio.charset.StandardCharsets;
26 import java.util.ArrayList;
28 import javax.xml.bind.annotation.XmlRootElement;
30 import org.json.simple.*;
31 import org.json.simple.parser.*;
32 import org.onap.dmaap.dbcapi.service.DmaapService;
35 public class Feed extends DmaapObject {
37 private String feedId;
39 private String feedName;
40 private String feedVersion;
41 private String feedDescription;
43 private String asprClassification;
44 private String publishURL;
45 private String subscribeURL;
46 private boolean suspended;
47 private String logURL;
48 private String formatUuid;
50 private ArrayList<DR_Pub> pubs;
51 private ArrayList<DR_Sub> subs;
55 this.pubs = new ArrayList<>();
56 this.subs = new ArrayList<>();
57 this.setStatus( DmaapObject_Status.EMPTY );
61 public Feed( String name,
67 this.feedVersion = version;
68 this.feedDescription = description;
70 this.asprClassification = aspr;
71 this.pubs = new ArrayList<>();
72 this.subs = new ArrayList<>();
73 this.setStatus( DmaapObject_Status.NEW );
77 // expects a String in JSON format, with known fields to populate Feed object
78 public Feed ( String json ) {
79 JSONParser parser = new JSONParser();
82 jsonObj = (JSONObject) parser.parse( json );
83 } catch ( ParseException pe ) {
84 logger.error( "Error parsing provisioning data: " + json );
85 this.setStatus( DmaapObject_Status.INVALID );
88 this.setFeedName( (String) jsonObj.get("name"));
90 this.setFeedVersion( (String) jsonObj.get("version"));
91 this.setFeedDescription( (String) jsonObj.get("description"));
92 this.setOwner( (String) jsonObj.get("publisher"));
94 this.setSuspended( (boolean) jsonObj.get("suspend"));
95 JSONObject links = (JSONObject) jsonObj.get("links");
96 String url = (String) links.get("publish");
97 this.setPublishURL( url );
98 this.setFeedId( url.substring( url.lastIndexOf('/')+1, url.length() ));
99 logger.info( "feedid="+ this.getFeedId() );
100 this.setSubscribeURL( (String) links.get("subscribe") );
101 this.setLogURL( (String) links.get("log") );
102 JSONObject auth = (JSONObject) jsonObj.get("authorization");
103 this.setAsprClassification( (String) auth.get("classification"));
104 JSONArray pubs = (JSONArray) auth.get( "endpoint_ids");
106 ArrayList<DR_Pub> dr_pub = new ArrayList<>();
107 this.subs = new ArrayList<>();
109 for( i = 0; i < pubs.size(); i++ ) {
110 JSONObject entry = (JSONObject) pubs.get(i);
111 dr_pub.add( new DR_Pub( "someLocation",
112 (String) entry.get("id"),
113 (String) entry.get("password"),
115 this.getFeedId() + "." + DR_Pub.nextKey() ));
118 this.setPubs( dr_pub );
120 this.setStatus( DmaapObject_Status.VALID );
126 public boolean isSuspended() {
130 public void setSuspended(boolean suspended) {
131 this.suspended = suspended;
134 public String getSubscribeURL() {
138 public void setSubscribeURL(String subscribeURL) {
139 this.subscribeURL = subscribeURL;
142 public String getFeedId() {
146 public void setFeedId(String feedId) {
147 this.feedId = feedId;
150 public String getFeedName() {
154 public void setFeedName(String feedName) {
155 this.feedName = feedName;
158 public String getFeedVersion() {
162 public void setFeedVersion(String feedVersion) {
163 this.feedVersion = feedVersion;
166 public String getFeedDescription() {
167 return feedDescription;
170 public void setFeedDescription(String feedDescription) {
171 this.feedDescription = feedDescription;
174 public String getOwner() {
178 public void setOwner(String owner) {
182 public String getAsprClassification() {
183 return asprClassification;
186 public void setAsprClassification(String asprClassification) {
187 this.asprClassification = asprClassification;
190 public String getPublishURL() {
194 public void setPublishURL(String publishURL) {
195 this.publishURL = publishURL;
198 public String getLogURL() {
202 public void setLogURL(String logURL) {
203 this.logURL = logURL;
208 public String getFormatUuid() {
212 public void setFormatUuid(String formatUuid) {
213 this.formatUuid = formatUuid;
216 // returns the Feed object in JSON that conforms to DR Prov Server expectations
217 public String toProvJSON() {
219 String postJSON = String.format("{\"name\": \"%s\", \"version\": \"%s\", \"description\": \"%s\", \"suspend\": %s, \"authorization\": { \"classification\": \"%s\", ",
221 this.getFeedVersion(),
222 this.getFeedDescription(),
224 this.getAsprClassification()
227 postJSON += "\"endpoint_addrs\": [],\"endpoint_ids\": [";
229 for( i = 0 ; i < pubs.size(); i++) {
230 postJSON += String.format(" %s{\"id\": \"%s\",\"password\": \"%s\"}",
232 pubs.get(i).getUsername(),
233 pubs.get(i).getUserpwd()
239 logger.info( "postJSON=" + postJSON);
243 public ArrayList<DR_Pub> getPubs() {
247 public void setPubs( ArrayList<DR_Pub> pubs) {
251 public ArrayList<DR_Sub> getSubs() {
255 public void setSubs( ArrayList<DR_Sub> subs) {
259 public byte[] getBytes() {
260 return toProvJSON().getBytes(StandardCharsets.UTF_8);
263 public static String getSubProvURL( String feedId ) {
264 return new DmaapService().getDmaap().getDrProvUrl() + "/subscribe/" + feedId;
268 public String toString() {
269 String rc = String.format ( "Feed: {feedId=%s feedName=%s feedVersion=%s feedDescription=%s owner=%s asprClassification=%s publishURL=%s subscriberURL=%s suspended=%s logURL=%s formatUuid=%s}",
285 for( DR_Pub pub: pubs) {
286 rc += "\n" + pub.toString();
289 for( DR_Sub sub: subs ) {
290 rc += "\n" + sub.toString();