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