417b18ab1df375f7c6c7ee2e763c0eb02f2fdb57
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / resources / FeedResource.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.resources;
22
23 import io.swagger.annotations.Api;
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
27
28 import java.util.List;
29
30 import javax.jws.WebParam;
31 import javax.ws.rs.Consumes;
32 import javax.ws.rs.DELETE;
33 import javax.ws.rs.GET;
34 import javax.ws.rs.HeaderParam;
35 import javax.ws.rs.POST;
36 import javax.ws.rs.PUT;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.PathParam;
39 import javax.ws.rs.Produces;
40 import javax.ws.rs.QueryParam;
41 import javax.ws.rs.core.Context;
42 import javax.ws.rs.core.GenericEntity;
43 import javax.ws.rs.core.MediaType;
44 import javax.ws.rs.core.Response;
45 import javax.ws.rs.core.Response.Status;
46 import javax.ws.rs.core.UriInfo;
47
48 import org.apache.log4j.Logger;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 import org.onap.dmaap.dbcapi.aaf.authentication.AuthenticationErrorException;
69 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
70 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
71 import org.onap.dmaap.dbcapi.model.ApiError;
72 import org.onap.dmaap.dbcapi.model.DR_Pub;
73 import org.onap.dmaap.dbcapi.model.DR_Sub;
74 import org.onap.dmaap.dbcapi.model.Feed;
75 import org.onap.dmaap.dbcapi.model.Topic;
76 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
77 import org.onap.dmaap.dbcapi.service.ApiService;
78 import org.onap.dmaap.dbcapi.service.FeedService;
79
80
81 @Path("/feeds")
82 @Api( value= "Feeds", description = "Endpoint for a Data Router Feed" )
83 @Consumes(MediaType.APPLICATION_JSON)
84 @Produces(MediaType.APPLICATION_JSON)
85 @Authorization
86 public class FeedResource extends BaseLoggingClass {
87         
88         @GET
89         @ApiOperation( value = "return Feed details", 
90         notes = "Returns array of  `Feed` objects.", 
91         response = Feed.class)
92         @ApiResponses( value = {
93             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
94             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
95         })
96         public Response getFeeds(
97                         @QueryParam("feedName") String feedName,
98                         @QueryParam("version") String version,
99                         @QueryParam("match") String match
100                         ) {
101
102                 ApiService resp = new ApiService();
103
104                 FeedService feedService = new FeedService();
105                 List<Feed> nfeeds =  feedService.getAllFeeds( feedName, version, match );
106                 GenericEntity<List<Feed>> list = new GenericEntity<List<Feed>>(nfeeds) {
107         };
108         return resp.success(list);
109         }
110         
111
112         
113         @POST
114         @ApiOperation( value = "return Feed details", 
115         notes = "Create a of  `Feed` object.", 
116         response = Feed.class)
117         @ApiResponses( value = {
118             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
119             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
120         })
121         public Response addFeed( 
122                         @WebParam(name = "feed") Feed feed 
123                         ) {
124
125                 ApiService resp = new ApiService();
126
127                 try {
128                         resp.required( "feedName", feed.getFeedName(), "");
129                         resp.required( "feedVersion", feed.getFeedVersion(), "");
130                         resp.required( "owner", feed.getOwner(), "" );
131                         resp.required( "asprClassification", feed.getAsprClassification(), "" );
132                 } catch ( RequiredFieldException rfe ) {
133                         logger.debug( resp.toString() );
134                         return resp.error();    
135                 }
136                 
137                 FeedService feedService = new FeedService();
138                 Feed nfeed =  feedService.getFeedByName( feed.getFeedName(), feed.getFeedVersion(), resp.getErr() );
139                 if ( nfeed == null ) {
140                         nfeed =  feedService.addFeed( feed, resp.getErr() );
141                         if ( nfeed != null ) {
142                                 return resp.success(nfeed);
143                         } else {
144                                 logger.error( "Unable to create: " + feed.getFeedName() + ":" + feed.getFeedVersion());
145
146                                 return resp.error();                    
147                         }
148                 } else if ( nfeed.getStatus() == DmaapObject_Status.DELETED ) {
149                         nfeed =  feedService.updateFeed(nfeed, resp.getErr());
150                         if ( nfeed != null ) {
151                                 return resp.success(nfeed);
152                         } else {
153                                 logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
154
155                                 return resp.error();    
156                         }
157                 }
158
159                 resp.setCode(Status.CONFLICT.getStatusCode());
160                 return resp.error();
161         }
162         
163         @PUT
164         @ApiOperation( value = "return Feed details", 
165         notes = "Update a  `Feed` object, specified by id.", 
166         response = Feed.class)
167         @ApiResponses( value = {
168             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
169             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
170         })
171         @Path("/{id}")
172         public Response updateFeed( 
173                         @PathParam("id") String id,
174                         @WebParam(name = "feed") Feed feed 
175                         ) {
176
177                 FeedService feedService = new FeedService();
178                 ApiService resp = new ApiService();
179
180                 try {
181                         resp.required( "feedId", id, "");
182                 } catch ( RequiredFieldException rfe ) {
183                         logger.debug( resp.toString() );
184                         return resp.error();    
185                 }
186
187                 Feed nfeed = feedService.getFeed( id, resp.getErr() );
188                 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
189                         return resp.notFound();                                         
190                 }
191         
192                 //  we assume there is no updates allowed for pubs and subs objects via this api...             
193                 // need to update any fields supported by PUT but preserve original field values. 
194                 nfeed.setSuspended(feed.isSuspended());
195                 nfeed.setFeedDescription(feed.getFeedDescription());
196                 nfeed.setFormatUuid(feed.getFormatUuid());
197                 
198                 nfeed =  feedService.updateFeed(nfeed, resp.getErr());
199                 if ( nfeed != null ) {
200                         return resp.success(nfeed);
201                 } else {
202                         logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
203
204                         return resp.error();    
205                 }
206         }
207         
208         @DELETE
209         @ApiOperation( value = "return Feed details", 
210         notes = "Delete a  `Feed` object, specified by id.", 
211         response = Feed.class)
212         @ApiResponses( value = {
213             @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
214             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
215         })
216         @Path("/{id}")
217         public Response deleteFeed( 
218                         @PathParam("id") String id
219                         ){
220                 ApiService resp = new ApiService();
221
222                 logger.debug( "Entry: DELETE  " + id);
223                 FeedService feedService = new FeedService();
224                 Feed nfeed =  feedService.getFeed( id, resp.getErr() );
225                 if ( nfeed == null ) {
226                         resp.setCode(Status.NOT_FOUND.getStatusCode());
227                         return resp.error();
228                 }
229                 nfeed = feedService.removeFeed( nfeed, resp.getErr() );
230                 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
231                         return resp.success(Status.NO_CONTENT.getStatusCode(), null);
232                 }
233                 logger.info( "Unable to delete: " + id + ":" + nfeed.getFeedVersion());
234
235                 return resp.error();
236         }
237
238         @GET
239         @ApiOperation( value = "return Feed details", 
240         notes = "Retrieve a  `Feed` object, specified by id.", 
241         response = Feed.class)
242         @ApiResponses( value = {
243             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
244             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
245         })
246         @Path("/{id}")
247         public Response getFeed( 
248                         @PathParam("id") String id
249                         ) {
250                 ApiService resp = new ApiService();
251
252                 FeedService feedService = new FeedService();
253                 Feed nfeed =  feedService.getFeed( id, resp.getErr() );
254                 if ( nfeed == null ) {
255                         resp.setCode(Status.NOT_FOUND.getStatusCode());
256                         return resp.error();
257                 }
258                 return resp.success(nfeed);
259         }
260 }