[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / 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                 public Feed() {
55                         this.pubs = new ArrayList<>();
56                         this.subs = new ArrayList<>();
57                         this.setStatus( DmaapObject_Status.EMPTY );
58
59                 }
60
61                 public  Feed( String name,
62                                                 String version,
63                                                 String description,
64                                                 String owner,
65                                                 String aspr) {
66                         this.feedName = name;
67                         this.feedVersion = version;
68                         this.feedDescription = description;
69                         this.owner = owner;
70                         this.asprClassification = aspr;
71                         this.pubs = new ArrayList<>();
72                         this.subs = new ArrayList<>();
73                         this.setStatus( DmaapObject_Status.NEW );
74
75                 }
76
77                 // expects a String in JSON format, with known fields to populate Feed object
78                 public Feed ( String json ) {
79                         JSONParser parser = new JSONParser();
80                         JSONObject jsonObj;
81                         try {
82                                 jsonObj = (JSONObject) parser.parse( json );
83                         } catch ( ParseException pe ) {
84                                 logger.error( "Error parsing provisioning data: " + json );
85                                 this.setStatus( DmaapObject_Status.INVALID );
86                                 return;
87                         }
88                         this.setFeedName( (String) jsonObj.get("name"));
89
90                         this.setFeedVersion( (String) jsonObj.get("version"));
91                         this.setFeedDescription( (String) jsonObj.get("description"));
92                         this.setOwner( (String) jsonObj.get("publisher"));
93
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");
105                         int i;
106                         ArrayList<DR_Pub> dr_pub = new ArrayList<>();
107                         this.subs = new ArrayList<>();
108
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"),
114                                                 this.getFeedId(),
115                                                 this.getFeedId() + "." +  DR_Pub.nextKey() ));
116
117                         }
118                         this.setPubs( dr_pub );
119
120                         this.setStatus( DmaapObject_Status.VALID );
121
122                 }
123
124                 
125
126                 public boolean isSuspended() {
127                         return suspended;
128                 }
129
130                 public void setSuspended(boolean suspended) {
131                         this.suspended = suspended;
132                 }
133
134                 public String getSubscribeURL() {
135                         return subscribeURL;
136                 }
137
138                 public void setSubscribeURL(String subscribeURL) {
139                         this.subscribeURL = subscribeURL;
140                 }
141
142                 public String getFeedId() {
143                         return feedId;
144                 }
145
146                 public void setFeedId(String feedId) {
147                         this.feedId = feedId;
148                 }
149
150                 public String getFeedName() {
151                         return feedName;
152                 }
153
154                 public void setFeedName(String feedName) {
155                         this.feedName = feedName;
156                 }
157
158                 public String getFeedVersion() {
159                         return feedVersion;
160                 }
161
162                 public void setFeedVersion(String feedVersion) {
163                         this.feedVersion = feedVersion;
164                 }
165
166                 public String getFeedDescription() {
167                         return feedDescription;
168                 }
169
170                 public void setFeedDescription(String feedDescription) {
171                         this.feedDescription = feedDescription;
172                 }
173
174                 public String getOwner() {
175                         return owner;
176                 }
177
178                 public void setOwner(String owner) {
179                         this.owner = owner;
180                 }
181
182                 public String getAsprClassification() {
183                         return asprClassification;
184                 }
185
186                 public void setAsprClassification(String asprClassification) {
187                         this.asprClassification = asprClassification;
188                 }
189
190                 public String getPublishURL() {
191                         return publishURL;
192                 }
193
194                 public void setPublishURL(String publishURL) {
195                         this.publishURL = publishURL;
196                 }
197
198                 public String getLogURL() {
199                         return logURL;
200                 }
201
202                 public void setLogURL(String logURL) {
203                         this.logURL = logURL;
204                 }
205
206
207                 
208                 public String getFormatUuid() {
209                         return formatUuid;
210                 }
211
212                 public void setFormatUuid(String formatUuid) {
213                         this.formatUuid = formatUuid;
214                 }
215
216                 // returns the Feed object in JSON that conforms to DR Prov Server expectations
217                 public String toProvJSON() {
218
219                         String postJSON = String.format("{\"name\": \"%s\", \"version\": \"%s\", \"description\": \"%s\", \"suspend\": %s, \"authorization\": { \"classification\": \"%s\", ",
220                                         this.getFeedName(), 
221                                         this.getFeedVersion(),
222                                         this.getFeedDescription(),
223                                         this.isSuspended() ,
224                                         this.getAsprClassification()
225                                         );
226                         int i;
227                         postJSON += "\"endpoint_addrs\": [],\"endpoint_ids\": [";
228                         String comma = "";
229                         for( i = 0 ; i < pubs.size(); i++) {
230                                 postJSON +=     String.format(" %s{\"id\": \"%s\",\"password\": \"%s\"}", 
231                                                 comma,
232                                                 pubs.get(i).getUsername(),
233                                                 pubs.get(i).getUserpwd()
234                                                 ) ;
235                                 comma = ",";
236                         }
237                         postJSON += "]}}";
238                         
239                         logger.info( "postJSON=" + postJSON);           
240                         return postJSON;
241                 }
242                 
243                 public ArrayList<DR_Pub> getPubs() {
244                         return pubs;
245                 }
246
247                 public void setPubs( ArrayList<DR_Pub> pubs) {
248                         this.pubs = pubs;
249                 }
250
251                 public ArrayList<DR_Sub> getSubs() {
252                         return subs;
253                 }
254
255                 public void setSubs( ArrayList<DR_Sub> subs) {
256                         this.subs = subs;
257                 }
258
259                 public byte[] getBytes() {
260                         return toProvJSON().getBytes(StandardCharsets.UTF_8);
261                 }
262                 
263                 public static String getSubProvURL( String feedId ) {
264                         return new DmaapService().getDmaap().getDrProvUrl() + "/subscribe/" + feedId;
265                 }
266
267                 @Override
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}",
270                                         feedId,
271                                         feedName,
272                                         feedVersion,
273                                         feedDescription,
274                                         owner,
275                                         asprClassification,
276                                         publishURL,
277                                         subscribeURL,
278                                         suspended,
279                                         logURL,
280                                         formatUuid
281
282                 
283                                         );
284
285                         for( DR_Pub pub: pubs) {
286                                 rc += "\n" + pub.toString();
287                         }
288
289                         for( DR_Sub sub: subs ) {
290                                 rc += "\n" + sub.toString();
291                         }
292                         return rc;
293                 }
294 }