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;
24 import java.util.ArrayList;
26 import javax.xml.bind.annotation.XmlRootElement;
28 import org.json.simple.*;
29 import org.json.simple.parser.*;
30 import org.onap.dmaap.dbcapi.service.DmaapService;
33 public class Feed extends DmaapObject {
35 private String feedId;
37 private String feedName;
38 private String feedVersion;
39 private String feedDescription;
41 private String asprClassification;
42 private String publishURL;
43 private String subscribeURL;
44 private boolean suspended;
45 private String logURL;
46 private String formatUuid;
48 private ArrayList<DR_Pub> pubs;
49 private ArrayList<DR_Sub> subs;
53 public boolean isSuspended() {
57 public void setSuspended(boolean suspended) {
58 this.suspended = suspended;
61 public String getSubscribeURL() {
65 public void setSubscribeURL(String subscribeURL) {
66 this.subscribeURL = subscribeURL;
72 this.pubs = new ArrayList<DR_Pub>();
73 this.subs = new ArrayList<DR_Sub>();
74 this.setStatus( DmaapObject_Status.EMPTY );
78 public Feed( String name,
85 this.feedVersion = version;
86 this.feedDescription = description;
88 this.asprClassification = aspr;
89 this.pubs = new ArrayList<DR_Pub>();
90 this.subs = new ArrayList<DR_Sub>();
91 this.setStatus( DmaapObject_Status.NEW );
95 // expects a String in JSON format, with known fields to populate Feed object
96 public Feed ( String json ) {
97 JSONParser parser = new JSONParser();
100 jsonObj = (JSONObject) parser.parse( json );
101 } catch ( ParseException pe ) {
102 logger.error( "Error parsing provisioning data: " + json );
103 this.setStatus( DmaapObject_Status.INVALID );
106 this.setFeedName( (String) jsonObj.get("name"));
108 this.setFeedVersion( (String) jsonObj.get("version"));
109 this.setFeedDescription( (String) jsonObj.get("description"));
110 this.setOwner( (String) jsonObj.get("publisher"));
112 this.setSuspended( (boolean) jsonObj.get("suspend"));
113 JSONObject links = (JSONObject) jsonObj.get("links");
114 String url = (String) links.get("publish");
115 this.setPublishURL( url );
116 this.setFeedId( url.substring( url.lastIndexOf('/')+1, url.length() ));
117 logger.info( "feedid="+ this.getFeedId() );
118 this.setSubscribeURL( (String) links.get("subscribe") );
119 this.setLogURL( (String) links.get("log") );
120 JSONObject auth = (JSONObject) jsonObj.get("authorization");
121 this.setAsprClassification( (String) auth.get("classification"));
122 JSONArray pubs = (JSONArray) auth.get( "endpoint_ids");
124 ArrayList<DR_Pub> dr_pub = new ArrayList<DR_Pub>();
125 this.subs = new ArrayList<DR_Sub>();
127 for( i = 0; i < pubs.size(); i++ ) {
128 JSONObject entry = (JSONObject) pubs.get(i);
129 dr_pub.add( new DR_Pub( "someLocation",
130 (String) entry.get("id"),
131 (String) entry.get("password"),
133 this.getFeedId() + "." + DR_Pub.nextKey() ));
136 this.setPubs( dr_pub );
138 this.setStatus( DmaapObject_Status.VALID );
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 ArrayList<DR_Pub> pubs = this.getPubs();
220 String postJSON = String.format("{\"name\": \"%s\", \"version\": \"%s\", \"description\": \"%s\", \"suspend\": %s, \"authorization\": { \"classification\": \"%s\", ",
222 this.getFeedVersion(),
223 this.getFeedDescription(),
225 this.getAsprClassification()
228 postJSON += "\"endpoint_addrs\": [],\"endpoint_ids\": [";
230 for( i = 0 ; i < pubs.size(); i++) {
231 postJSON += String.format(" %s{\"id\": \"%s\",\"password\": \"%s\"}",
233 pubs.get(i).getUsername(),
234 pubs.get(i).getUserpwd()
240 logger.info( "postJSON=" + postJSON);
244 public ArrayList<DR_Pub> getPubs() {
248 public void setPubs( ArrayList<DR_Pub> pubs) {
252 public ArrayList<DR_Sub> getSubs() {
256 public void setSubs( ArrayList<DR_Sub> subs) {
260 public byte[] getBytes() {
261 return toProvJSON().getBytes(StandardCharsets.UTF_8);
264 public static String getSubProvURL( String feedId ) {
265 String ret = new String();
266 ret = new DmaapService().getDmaap().getDrProvUrl() + "/subscribe/" + feedId ;
271 public String toString() {
272 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}",
288 for( DR_Pub pub: pubs) {
289 rc += "\n" + pub.toString();
292 for( DR_Sub sub: subs ) {
293 rc += "\n" + sub.toString();