b78377bafb0edd98a56e347d7c2422a48dfdc46e
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / model / Feed.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  *
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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=========================================================
21  */
22
23 package org.onap.dmaap.dbcapi.model;
24
25 import java.nio.charset.StandardCharsets;
26 import java.util.ArrayList;
27
28 import javax.xml.bind.annotation.XmlRootElement;
29
30 import org.json.simple.*;
31 import org.json.simple.parser.*;
32 import org.onap.dmaap.dbcapi.service.DmaapService;
33
34 @XmlRootElement
35 public class Feed extends DmaapObject {
36                 
37                 private String feedId;
38
39                 private String feedName;
40                 private String feedVersion;
41                 private String feedDescription;
42                 private String owner;
43                 private String asprClassification;
44                 private String publishURL;
45                 private String subscribeURL;
46                 private boolean suspended;
47                 private String logURL;
48                 private String formatUuid;
49
50                 private ArrayList<DR_Pub> pubs;
51                 private ArrayList<DR_Sub> subs; 
52
53                 
54
55                 public boolean isSuspended() {
56                         return suspended;
57                 }
58
59                 public void setSuspended(boolean suspended) {
60                         this.suspended = suspended;
61                 }
62
63                 public String getSubscribeURL() {
64                         return subscribeURL;
65                 }
66
67                 public void setSubscribeURL(String subscribeURL) {
68                         this.subscribeURL = subscribeURL;
69                 }
70
71
72                 
73                 public Feed() {
74                         this.pubs = new ArrayList<DR_Pub>();
75                         this.subs = new ArrayList<DR_Sub>();
76                         this.setStatus( DmaapObject_Status.EMPTY );
77                         
78                 }
79                 
80                 public  Feed( String name,
81                                         String version,
82                                         String description,
83                                         String owner,
84                                         String aspr
85                                          ) {
86                         this.feedName = name;
87                         this.feedVersion = version;
88                         this.feedDescription = description;
89                         this.owner = owner;
90                         this.asprClassification = aspr;
91                         this.pubs = new ArrayList<DR_Pub>();
92                         this.subs = new ArrayList<DR_Sub>();
93                         this.setStatus( DmaapObject_Status.NEW );
94                         
95                 }
96                 
97                 // expects a String in JSON format, with known fields to populate Feed object
98                 public Feed ( String json ) {
99                         JSONParser parser = new JSONParser();
100                         JSONObject jsonObj;
101                         try {
102                                 jsonObj = (JSONObject) parser.parse( json );
103                         } catch ( ParseException pe ) {
104                     logger.error( "Error parsing provisioning data: " + json );
105                     this.setStatus( DmaapObject_Status.INVALID );
106                     return;
107                 }
108                         this.setFeedName( (String) jsonObj.get("name"));
109
110                         this.setFeedVersion( (String) jsonObj.get("version"));
111                         this.setFeedDescription( (String) jsonObj.get("description"));
112                         this.setOwner( (String) jsonObj.get("publisher"));
113
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");
125                         int i;
126                         ArrayList<DR_Pub> dr_pub = new ArrayList<DR_Pub>();
127                         this.subs = new ArrayList<DR_Sub>();
128
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"),
134                                                                         this.getFeedId(),
135                                                                         this.getFeedId() + "." +  DR_Pub.nextKey() ));
136                         
137                         }
138                         this.setPubs( dr_pub );
139         
140                         this.setStatus( DmaapObject_Status.VALID );
141
142                 }
143
144                 public String getFeedId() {
145                         return feedId;
146                 }
147
148                 public void setFeedId(String feedId) {
149                         this.feedId = feedId;
150                 }
151
152                 public String getFeedName() {
153                         return feedName;
154                 }
155
156                 public void setFeedName(String feedName) {
157                         this.feedName = feedName;
158                 }
159
160                 public String getFeedVersion() {
161                         return feedVersion;
162                 }
163
164                 public void setFeedVersion(String feedVersion) {
165                         this.feedVersion = feedVersion;
166                 }
167
168                 public String getFeedDescription() {
169                         return feedDescription;
170                 }
171
172                 public void setFeedDescription(String feedDescription) {
173                         this.feedDescription = feedDescription;
174                 }
175
176                 public String getOwner() {
177                         return owner;
178                 }
179
180                 public void setOwner(String owner) {
181                         this.owner = owner;
182                 }
183
184                 public String getAsprClassification() {
185                         return asprClassification;
186                 }
187
188                 public void setAsprClassification(String asprClassification) {
189                         this.asprClassification = asprClassification;
190                 }
191
192                 public String getPublishURL() {
193                         return publishURL;
194                 }
195
196                 public void setPublishURL(String publishURL) {
197                         this.publishURL = publishURL;
198                 }
199
200                 public String getLogURL() {
201                         return logURL;
202                 }
203
204                 public void setLogURL(String logURL) {
205                         this.logURL = logURL;
206                 }
207
208
209                 
210                 public String getFormatUuid() {
211                         return formatUuid;
212                 }
213
214                 public void setFormatUuid(String formatUuid) {
215                         this.formatUuid = formatUuid;
216                 }
217
218                 // returns the Feed object in JSON that conforms to DR Prov Server expectations
219                 public String toProvJSON() {
220
221                         ArrayList<DR_Pub> pubs = this.getPubs();
222                         String postJSON = String.format("{\"name\": \"%s\", \"version\": \"%s\", \"description\": \"%s\", \"suspend\": %s, \"authorization\": { \"classification\": \"%s\", ",  
223                                         this.getFeedName(), 
224                                         this.getFeedVersion(),
225                                         this.getFeedDescription(),
226                                         this.isSuspended() ,
227                                         this.getAsprClassification()
228                                         );
229                         int i;
230                         postJSON += "\"endpoint_addrs\": [],\"endpoint_ids\": [";
231                         String comma = "";
232                         for( i = 0 ; i < pubs.size(); i++) {
233                                 postJSON +=     String.format(" %s{\"id\": \"%s\",\"password\": \"%s\"}", 
234                                                 comma,
235                                                 pubs.get(i).getUsername(),
236                                                 pubs.get(i).getUserpwd()
237                                                 ) ;
238                                 comma = ",";
239                         }
240                         postJSON += "]}}";
241                         
242                         logger.info( "postJSON=" + postJSON);           
243                         return postJSON;
244                 }
245                 
246                 public ArrayList<DR_Pub> getPubs() {
247                         return pubs;
248                 }
249
250                 public void setPubs( ArrayList<DR_Pub> pubs) {
251                         this.pubs = pubs;
252                 }
253
254                 public ArrayList<DR_Sub> getSubs() {
255                         return subs;
256                 }
257
258                 public void setSubs( ArrayList<DR_Sub> subs) {
259                         this.subs = subs;
260                 }
261
262                 public byte[] getBytes() {
263                         return toProvJSON().getBytes(StandardCharsets.UTF_8);
264                 }
265                 
266                 public static String getSubProvURL( String feedId ) {
267                         String ret = new DmaapService().getDmaap().getDrProvUrl() + "/subscribe/" + feedId ;
268                         return ret;
269                 }
270
271                 @Override
272                 public String toString() {
273                         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}",
274                                         feedId,
275                                         feedName,
276                                         feedVersion,
277                                         feedDescription,
278                                         owner,
279                                         asprClassification,
280                                         publishURL,
281                                         subscribeURL,
282                                         suspended,
283                                         logURL,
284                                         formatUuid
285
286                 
287                                         );
288
289                         for( DR_Pub pub: pubs) {
290                                 rc += "\n" + pub.toString();
291                         }
292
293                         for( DR_Sub sub: subs ) {
294                                 rc += "\n" + sub.toString();
295                         }
296                         return rc;
297                 }
298 }