DMAAP-83 Initial code import
[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                         
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                         this.setFeedName( (String) jsonObj.get("name"));
108
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 String();
268                         ret = new DmaapService().getDmaap().getDrProvUrl() + "/subscribe/" + feedId ;
269                         return ret;
270                 }
271
272                 @Override
273                 public String toString() {
274                         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}",
275                                         feedId,
276                                         feedName,
277                                         feedVersion,
278                                         feedDescription,
279                                         owner,
280                                         asprClassification,
281                                         publishURL,
282                                         subscribeURL,
283                                         suspended,
284                                         logURL,
285                                         formatUuid
286
287                 
288                                         );
289
290                         for( DR_Pub pub: pubs) {
291                                 rc += "\n" + pub.toString();
292                         }
293
294                         for( DR_Sub sub: subs ) {
295                                 rc += "\n" + sub.toString();
296                         }
297                         return rc;
298                 }
299 }