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