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();
99 logger.info( "templog:Feed at 10" );
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 logger.info( "templog:Feed at 11" );
108 this.setFeedName( (String) jsonObj.get("name"));
110 logger.info( "templog:Feed at 12" );
112 this.setFeedVersion( (String) jsonObj.get("version"));
113 logger.info( "templog:Feed at 13" );
114 this.setFeedDescription( (String) jsonObj.get("description"));
115 logger.info( "templog:Feed at 14" );
116 this.setOwner( (String) jsonObj.get("publisher"));
117 logger.info( "templog:Feed at 15" );
119 this.setSuspended( (boolean) jsonObj.get("suspend"));
120 logger.info( "templog:Feed at 16" );
121 JSONObject links = (JSONObject) jsonObj.get("links");
122 logger.info( "templog:Feed at 17" );
123 String url = (String) links.get("publish");
124 logger.info( "templog:Feed at 18" );
125 this.setPublishURL( url );
126 logger.info( "templog:Feed at 19" );
127 this.setFeedId( url.substring( url.lastIndexOf('/')+1, url.length() ));
128 logger.info( "templog:Feed at 20" );
129 logger.info( "feedid="+ this.getFeedId() );
130 logger.info( "templog:Feed at 21" );
131 this.setSubscribeURL( (String) links.get("subscribe") );
132 logger.info( "templog:Feed at 22" );
133 this.setLogURL( (String) links.get("log") );
134 logger.info( "templog:Feed at 23" );
135 JSONObject auth = (JSONObject) jsonObj.get("authorization");
136 logger.info( "templog:Feed at 24" );
137 this.setAsprClassification( (String) auth.get("classification"));
138 logger.info( "templog:Feed at 25" );
139 JSONArray pubs = (JSONArray) auth.get( "endpoint_ids");
140 logger.info( "templog:Feed at 26" );
142 logger.info( "templog:Feed at 27" );
143 ArrayList<DR_Pub> dr_pub = new ArrayList<DR_Pub>();
144 logger.info( "templog:Feed at 28" );
145 this.subs = new ArrayList<DR_Sub>();
147 for( i = 0; i < pubs.size(); i++ ) {
148 logger.info( "templog:Feed at 29 " + i );
149 JSONObject entry = (JSONObject) pubs.get(i);
150 logger.info( "templog:Feed at 30" );
151 dr_pub.add( new DR_Pub( "someLocation",
152 (String) entry.get("id"),
153 (String) entry.get("password"),
155 this.getFeedId() + "." + DR_Pub.nextKey() ));
158 logger.info( "templog:Feed at 31" );
159 this.setPubs( dr_pub );
161 logger.info( "templog:Feed at 32" );
162 this.setStatus( DmaapObject_Status.VALID );
163 logger.info( "templog:Feed at 33" );
167 public String getFeedId() {
171 public void setFeedId(String feedId) {
172 this.feedId = feedId;
175 public String getFeedName() {
179 public void setFeedName(String feedName) {
180 this.feedName = feedName;
183 public String getFeedVersion() {
187 public void setFeedVersion(String feedVersion) {
188 this.feedVersion = feedVersion;
191 public String getFeedDescription() {
192 return feedDescription;
195 public void setFeedDescription(String feedDescription) {
196 this.feedDescription = feedDescription;
199 public String getOwner() {
203 public void setOwner(String owner) {
207 public String getAsprClassification() {
208 return asprClassification;
211 public void setAsprClassification(String asprClassification) {
212 this.asprClassification = asprClassification;
215 public String getPublishURL() {
219 public void setPublishURL(String publishURL) {
220 this.publishURL = publishURL;
223 public String getLogURL() {
227 public void setLogURL(String logURL) {
228 this.logURL = logURL;
233 public String getFormatUuid() {
237 public void setFormatUuid(String formatUuid) {
238 this.formatUuid = formatUuid;
241 // returns the Feed object in JSON that conforms to DR Prov Server expectations
242 public String toProvJSON() {
244 ArrayList<DR_Pub> pubs = this.getPubs();
245 String postJSON = String.format("{\"name\": \"%s\", \"version\": \"%s\", \"description\": \"%s\", \"suspend\": %s, \"authorization\": { \"classification\": \"%s\", ",
247 this.getFeedVersion(),
248 this.getFeedDescription(),
250 this.getAsprClassification()
253 postJSON += "\"endpoint_addrs\": [],\"endpoint_ids\": [";
255 for( i = 0 ; i < pubs.size(); i++) {
256 postJSON += String.format(" %s{\"id\": \"%s\",\"password\": \"%s\"}",
258 pubs.get(i).getUsername(),
259 pubs.get(i).getUserpwd()
265 logger.info( "postJSON=" + postJSON);
269 public ArrayList<DR_Pub> getPubs() {
273 public void setPubs( ArrayList<DR_Pub> pubs) {
277 public ArrayList<DR_Sub> getSubs() {
281 public void setSubs( ArrayList<DR_Sub> subs) {
285 public byte[] getBytes() {
286 return toProvJSON().getBytes(StandardCharsets.UTF_8);
289 public static String getSubProvURL( String feedId ) {
290 String ret = new String();
291 ret = new DmaapService().getDmaap().getDrProvUrl() + "/subscribe/" + feedId ;
296 public String toString() {
297 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}",
313 for( DR_Pub pub: pubs) {
314 rc += "\n" + pub.toString();
317 for( DR_Sub sub: subs ) {
318 rc += "\n" + sub.toString();