More unit tests to pass 50%
[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  * 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.model;
22
23 import java.nio.charset.StandardCharsets;
24 import java.util.ArrayList;
25
26 import javax.xml.bind.annotation.XmlRootElement;
27
28 import org.json.simple.*;
29 import org.json.simple.parser.*;
30 import org.onap.dmaap.dbcapi.service.DmaapService;
31
32 @XmlRootElement
33 public class Feed extends DmaapObject {
34                 
35                 private String feedId;
36
37                 private String feedName;
38                 private String feedVersion;
39                 private String feedDescription;
40                 private String owner;
41                 private String asprClassification;
42                 private String publishURL;
43                 private String subscribeURL;
44                 private boolean suspended;
45                 private String logURL;
46                 private String formatUuid;
47
48                 private ArrayList<DR_Pub> pubs;
49                 private ArrayList<DR_Sub> subs; 
50
51                 
52
53                 public boolean isSuspended() {
54                         return suspended;
55                 }
56
57                 public void setSuspended(boolean suspended) {
58                         this.suspended = suspended;
59                 }
60
61                 public String getSubscribeURL() {
62                         return subscribeURL;
63                 }
64
65                 public void setSubscribeURL(String subscribeURL) {
66                         this.subscribeURL = subscribeURL;
67                 }
68
69
70                 
71                 public Feed() {
72                         this.pubs = new ArrayList<DR_Pub>();
73                         this.subs = new ArrayList<DR_Sub>();
74                         this.setStatus( DmaapObject_Status.EMPTY );
75                         
76                 }
77                 
78                 public  Feed( String name,
79                                         String version,
80                                         String description,
81                                         String owner,
82                                         String aspr
83                                          ) {
84                         this.feedName = name;
85                         this.feedVersion = version;
86                         this.feedDescription = description;
87                         this.owner = owner;
88                         this.asprClassification = aspr;
89                         this.pubs = new ArrayList<DR_Pub>();
90                         this.subs = new ArrayList<DR_Sub>();
91                         this.setStatus( DmaapObject_Status.NEW );
92                         
93                 }
94                 
95                 // expects a String in JSON format, with known fields to populate Feed object
96                 public Feed ( String json ) {
97                         JSONParser parser = new JSONParser();
98                         JSONObject jsonObj;
99                         try {
100                                 jsonObj = (JSONObject) parser.parse( json );
101                         } catch ( ParseException pe ) {
102                     logger.error( "Error parsing provisioning data: " + json );
103                     this.setStatus( DmaapObject_Status.INVALID );
104                     return;
105                 }
106                         this.setFeedName( (String) jsonObj.get("name"));
107
108                         this.setFeedVersion( (String) jsonObj.get("version"));
109                         this.setFeedDescription( (String) jsonObj.get("description"));
110                         this.setOwner( (String) jsonObj.get("publisher"));
111
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");
123                         int i;
124                         ArrayList<DR_Pub> dr_pub = new ArrayList<DR_Pub>();
125                         this.subs = new ArrayList<DR_Sub>();
126
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"),
132                                                                         this.getFeedId(),
133                                                                         this.getFeedId() + "." +  DR_Pub.nextKey() ));
134                         
135                         }
136                         this.setPubs( dr_pub );
137         
138                         this.setStatus( DmaapObject_Status.VALID );
139
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                         ArrayList<DR_Pub> pubs = this.getPubs();
220                         String postJSON = String.format("{\"name\": \"%s\", \"version\": \"%s\", \"description\": \"%s\", \"suspend\": %s, \"authorization\": { \"classification\": \"%s\", ",  
221                                         this.getFeedName(), 
222                                         this.getFeedVersion(),
223                                         this.getFeedDescription(),
224                                         this.isSuspended() ,
225                                         this.getAsprClassification()
226                                         );
227                         int i;
228                         postJSON += "\"endpoint_addrs\": [],\"endpoint_ids\": [";
229                         String comma = "";
230                         for( i = 0 ; i < pubs.size(); i++) {
231                                 postJSON +=     String.format(" %s{\"id\": \"%s\",\"password\": \"%s\"}", 
232                                                 comma,
233                                                 pubs.get(i).getUsername(),
234                                                 pubs.get(i).getUserpwd()
235                                                 ) ;
236                                 comma = ",";
237                         }
238                         postJSON += "]}}";
239                         
240                         logger.info( "postJSON=" + postJSON);           
241                         return postJSON;
242                 }
243                 
244                 public ArrayList<DR_Pub> getPubs() {
245                         return pubs;
246                 }
247
248                 public void setPubs( ArrayList<DR_Pub> pubs) {
249                         this.pubs = pubs;
250                 }
251
252                 public ArrayList<DR_Sub> getSubs() {
253                         return subs;
254                 }
255
256                 public void setSubs( ArrayList<DR_Sub> subs) {
257                         this.subs = subs;
258                 }
259
260                 public byte[] getBytes() {
261                         return toProvJSON().getBytes(StandardCharsets.UTF_8);
262                 }
263                 
264                 public static String getSubProvURL( String feedId ) {
265                         String ret = new String();
266                         ret = new DmaapService().getDmaap().getDrProvUrl() + "/subscribe/" + feedId ;
267                         return ret;
268                 }
269
270                 @Override
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}",
273                                         feedId,
274                                         feedName,
275                                         feedVersion,
276                                         feedDescription,
277                                         owner,
278                                         asprClassification,
279                                         publishURL,
280                                         subscribeURL,
281                                         suspended,
282                                         logURL,
283                                         formatUuid
284
285                 
286                                         );
287
288                         for( DR_Pub pub: pubs) {
289                                 rc += "\n" + pub.toString();
290                         }
291
292                         for( DR_Sub sub: subs ) {
293                                 rc += "\n" + sub.toString();
294                         }
295                         return rc;
296                 }
297 }