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();
101 jsonObj = (JSONObject) parser.parse( json );
102 } catch ( ParseException pe ) {
103 logger.error( "Error parsing provisioning data: " + json );
104 this.setStatus( DmaapObject_Status.INVALID );
107 this.setFeedName( (String) jsonObj.get("name"));
110 this.setFeedVersion( (String) jsonObj.get("version"));
111 this.setFeedDescription( (String) jsonObj.get("description"));
112 this.setOwner( (String) jsonObj.get("publisher"));
114 this.setSuspended( (boolean) jsonObj.get("suspend"));
115 JSONObject links = (JSONObject) jsonObj.get("links");
116 String url = (String) links.get("publish");
117 this.setPublishURL( url );
118 this.setFeedId( url.substring( url.lastIndexOf('/')+1, url.length() ));
119 logger.info( "feedid="+ this.getFeedId() );
120 this.setSubscribeURL( (String) links.get("subscribe") );
121 this.setLogURL( (String) links.get("log") );
122 JSONObject auth = (JSONObject) jsonObj.get("authorization");
123 this.setAsprClassification( (String) auth.get("classification"));
124 JSONArray pubs = (JSONArray) auth.get( "endpoint_ids");
126 ArrayList<DR_Pub> dr_pub = new ArrayList<DR_Pub>();
127 this.subs = new ArrayList<DR_Sub>();
129 for( i = 0; i < pubs.size(); i++ ) {
130 JSONObject entry = (JSONObject) pubs.get(i);
131 dr_pub.add( new DR_Pub( "someLocation",
132 (String) entry.get("id"),
133 (String) entry.get("password"),
135 this.getFeedId() + "." + DR_Pub.nextKey() ));
138 this.setPubs( dr_pub );
140 this.setStatus( DmaapObject_Status.VALID );
144 public String getFeedId() {
148 public void setFeedId(String feedId) {
149 this.feedId = feedId;
152 public String getFeedName() {
156 public void setFeedName(String feedName) {
157 this.feedName = feedName;
160 public String getFeedVersion() {
164 public void setFeedVersion(String feedVersion) {
165 this.feedVersion = feedVersion;
168 public String getFeedDescription() {
169 return feedDescription;
172 public void setFeedDescription(String feedDescription) {
173 this.feedDescription = feedDescription;
176 public String getOwner() {
180 public void setOwner(String owner) {
184 public String getAsprClassification() {
185 return asprClassification;
188 public void setAsprClassification(String asprClassification) {
189 this.asprClassification = asprClassification;
192 public String getPublishURL() {
196 public void setPublishURL(String publishURL) {
197 this.publishURL = publishURL;
200 public String getLogURL() {
204 public void setLogURL(String logURL) {
205 this.logURL = logURL;
210 public String getFormatUuid() {
214 public void setFormatUuid(String formatUuid) {
215 this.formatUuid = formatUuid;
218 // returns the Feed object in JSON that conforms to DR Prov Server expectations
219 public String toProvJSON() {
221 ArrayList<DR_Pub> pubs = this.getPubs();
222 String postJSON = String.format("{\"name\": \"%s\", \"version\": \"%s\", \"description\": \"%s\", \"suspend\": %s, \"authorization\": { \"classification\": \"%s\", ",
224 this.getFeedVersion(),
225 this.getFeedDescription(),
227 this.getAsprClassification()
230 postJSON += "\"endpoint_addrs\": [],\"endpoint_ids\": [";
232 for( i = 0 ; i < pubs.size(); i++) {
233 postJSON += String.format(" %s{\"id\": \"%s\",\"password\": \"%s\"}",
235 pubs.get(i).getUsername(),
236 pubs.get(i).getUserpwd()
242 logger.info( "postJSON=" + postJSON);
246 public ArrayList<DR_Pub> getPubs() {
250 public void setPubs( ArrayList<DR_Pub> pubs) {
254 public ArrayList<DR_Sub> getSubs() {
258 public void setSubs( ArrayList<DR_Sub> subs) {
262 public byte[] getBytes() {
263 return toProvJSON().getBytes(StandardCharsets.UTF_8);
266 public static String getSubProvURL( String feedId ) {
267 String ret = new String();
268 ret = new DmaapService().getDmaap().getDrProvUrl() + "/subscribe/" + feedId ;
273 public String toString() {
274 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}",
290 for( DR_Pub pub: pubs) {
291 rc += "\n" + pub.toString();
294 for( DR_Sub sub: subs ) {
295 rc += "\n" + sub.toString();