including missing files
[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                 logger.info( "templog:Feed at 10" );    
100                         try {
101                                 jsonObj = (JSONObject) parser.parse( json );
102                         } catch ( ParseException pe ) {
103                     logger.error( "Error parsing provisioning data: " + json );
104                     this.setStatus( DmaapObject_Status.INVALID );
105                     return;
106                 }
107                 logger.info( "templog:Feed at 11" );    
108                         this.setFeedName( (String) jsonObj.get("name"));
109
110                 logger.info( "templog:Feed at 12" );    
111
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" );    
118
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" );    
141                         int i;
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>();
146
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"),
154                                                                         this.getFeedId(),
155                                                                         this.getFeedId() + "." +  DR_Pub.nextKey() ));
156                         
157                         }
158                 logger.info( "templog:Feed at 31" );    
159                         this.setPubs( dr_pub );
160         
161                 logger.info( "templog:Feed at 32" );    
162                         this.setStatus( DmaapObject_Status.VALID );
163                 logger.info( "templog:Feed at 33" );    
164
165                 }
166
167                 public String getFeedId() {
168                         return feedId;
169                 }
170
171                 public void setFeedId(String feedId) {
172                         this.feedId = feedId;
173                 }
174
175                 public String getFeedName() {
176                         return feedName;
177                 }
178
179                 public void setFeedName(String feedName) {
180                         this.feedName = feedName;
181                 }
182
183                 public String getFeedVersion() {
184                         return feedVersion;
185                 }
186
187                 public void setFeedVersion(String feedVersion) {
188                         this.feedVersion = feedVersion;
189                 }
190
191                 public String getFeedDescription() {
192                         return feedDescription;
193                 }
194
195                 public void setFeedDescription(String feedDescription) {
196                         this.feedDescription = feedDescription;
197                 }
198
199                 public String getOwner() {
200                         return owner;
201                 }
202
203                 public void setOwner(String owner) {
204                         this.owner = owner;
205                 }
206
207                 public String getAsprClassification() {
208                         return asprClassification;
209                 }
210
211                 public void setAsprClassification(String asprClassification) {
212                         this.asprClassification = asprClassification;
213                 }
214
215                 public String getPublishURL() {
216                         return publishURL;
217                 }
218
219                 public void setPublishURL(String publishURL) {
220                         this.publishURL = publishURL;
221                 }
222
223                 public String getLogURL() {
224                         return logURL;
225                 }
226
227                 public void setLogURL(String logURL) {
228                         this.logURL = logURL;
229                 }
230
231
232                 
233                 public String getFormatUuid() {
234                         return formatUuid;
235                 }
236
237                 public void setFormatUuid(String formatUuid) {
238                         this.formatUuid = formatUuid;
239                 }
240
241                 // returns the Feed object in JSON that conforms to DR Prov Server expectations
242                 public String toProvJSON() {
243
244                         ArrayList<DR_Pub> pubs = this.getPubs();
245                         String postJSON = String.format("{\"name\": \"%s\", \"version\": \"%s\", \"description\": \"%s\", \"suspend\": %s, \"authorization\": { \"classification\": \"%s\", ",  
246                                         this.getFeedName(), 
247                                         this.getFeedVersion(),
248                                         this.getFeedDescription(),
249                                         this.isSuspended() ,
250                                         this.getAsprClassification()
251                                         );
252                         int i;
253                         postJSON += "\"endpoint_addrs\": [],\"endpoint_ids\": [";
254                         String comma = "";
255                         for( i = 0 ; i < pubs.size(); i++) {
256                                 postJSON +=     String.format(" %s{\"id\": \"%s\",\"password\": \"%s\"}", 
257                                                 comma,
258                                                 pubs.get(i).getUsername(),
259                                                 pubs.get(i).getUserpwd()
260                                                 ) ;
261                                 comma = ",";
262                         }
263                         postJSON += "]}}";
264                         
265                         logger.info( "postJSON=" + postJSON);           
266                         return postJSON;
267                 }
268                 
269                 public ArrayList<DR_Pub> getPubs() {
270                         return pubs;
271                 }
272
273                 public void setPubs( ArrayList<DR_Pub> pubs) {
274                         this.pubs = pubs;
275                 }
276
277                 public ArrayList<DR_Sub> getSubs() {
278                         return subs;
279                 }
280
281                 public void setSubs( ArrayList<DR_Sub> subs) {
282                         this.subs = subs;
283                 }
284
285                 public byte[] getBytes() {
286                         return toProvJSON().getBytes(StandardCharsets.UTF_8);
287                 }
288                 
289                 public static String getSubProvURL( String feedId ) {
290                         String ret = new String();
291                         ret = new DmaapService().getDmaap().getDrProvUrl() + "/subscribe/" + feedId ;
292                         return ret;
293                 }
294
295                 @Override
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}",
298                                         feedId,
299                                         feedName,
300                                         feedVersion,
301                                         feedDescription,
302                                         owner,
303                                         asprClassification,
304                                         publishURL,
305                                         subscribeURL,
306                                         suspended,
307                                         logURL,
308                                         formatUuid
309
310                 
311                                         );
312
313                         for( DR_Pub pub: pubs) {
314                                 rc += "\n" + pub.toString();
315                         }
316
317                         for( DR_Sub sub: subs ) {
318                                 rc += "\n" + sub.toString();
319                         }
320                         return rc;
321                 }
322 }