97336c1a96ec8bf62f4d67da518a91ba422c1a80
[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                         @QueryParam("useExisting") String useExisting
105                         ) {
106
107                 ApiService resp = new ApiService();
108
109                 try {
110                         resp.required( "feedName", feed.getFeedName(), "");
111                         resp.required( "feedVersion", feed.getFeedVersion(), "");
112                         resp.required( "owner", feed.getOwner(), "" );
113                         resp.required( "asprClassification", feed.getAsprClassification(), "" );
114                 } catch ( RequiredFieldException rfe ) {
115                         logger.debug( resp.toString() );
116                         return resp.error();    
117                 }
118                 
119                 
120                 FeedService feedService = new FeedService();
121                 Feed nfeed =  feedService.getFeedByName( feed.getFeedName(), feed.getFeedVersion(), resp.getErr() );
122                 if ( nfeed == null ) {
123                         nfeed =  feedService.addFeed( feed, resp.getErr() );
124                         if ( nfeed != null ) {
125                                 return resp.success(nfeed);
126                         } else {
127                                 logger.error( "Unable to create: " + feed.getFeedName() + ":" + feed.getFeedVersion());
128
129                                 return resp.error();                    
130                         }
131                 } else if ( nfeed.getStatus() == DmaapObject_Status.DELETED ) {
132                         feed.setFeedId( nfeed.getFeedId());
133                         nfeed =  feedService.updateFeed(feed, resp.getErr());
134                         if ( nfeed != null ) {
135                                 return resp.success(nfeed);
136                         } else {
137                                 logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
138
139                                 return resp.error();    
140                         }
141                 } else if ( (useExisting != null) && ("true".compareToIgnoreCase( useExisting ) == 0)) {
142                         return resp.success(nfeed);
143                 }
144
145                 resp.setCode(Status.CONFLICT.getStatusCode());
146                 return resp.error();
147         }
148         
149         @PUT
150         @ApiOperation( value = "return Feed details", 
151         notes = "Update a  `Feed` object, specified by id.", 
152         response = Feed.class)
153         @ApiResponses( value = {
154             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
155             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
156         })
157         @Path("/{id}")
158         public Response updateFeed( 
159                         @PathParam("id") String id,
160                         @WebParam(name = "feed") Feed feed 
161                         ) {
162
163                 FeedService feedService = new FeedService();
164                 ApiService resp = new ApiService();
165
166                 try {
167                         resp.required( "feedId", id, "");
168                 } catch ( RequiredFieldException rfe ) {
169                         logger.debug( resp.toString() );
170                         return resp.error();    
171                 }
172
173                 Feed nfeed = feedService.getFeed( id, resp.getErr() );
174                 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
175                         return resp.notFound();                                         
176                 }
177         
178                 //  we assume there is no updates allowed for pubs and subs objects via this api...             
179                 // need to update any fields supported by PUT but preserve original field values. 
180                 nfeed.setSuspended(feed.isSuspended());
181                 nfeed.setFeedDescription(feed.getFeedDescription());
182                 nfeed.setFormatUuid(feed.getFormatUuid());
183                 
184                 nfeed =  feedService.updateFeed(nfeed, resp.getErr());
185                 if ( nfeed != null ) {
186                         return resp.success(nfeed);
187                 } else {
188                         logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
189
190                         return resp.error();    
191                 }
192         }
193         
194         @DELETE
195         @ApiOperation( value = "return Feed details", 
196         notes = "Delete a  `Feed` object, specified by id.", 
197         response = Feed.class)
198         @ApiResponses( value = {
199             @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
200             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
201         })
202         @Path("/{id}")
203         public Response deleteFeed( 
204                         @PathParam("id") String id
205                         ){
206                 ApiService resp = new ApiService();
207
208                 logger.debug( "Entry: DELETE  " + id);
209                 FeedService feedService = new FeedService();
210                 Feed nfeed =  feedService.getFeed( id, resp.getErr() );
211                 if ( nfeed == null ) {
212                         resp.setCode(Status.NOT_FOUND.getStatusCode());
213                         return resp.error();
214                 }
215                 nfeed = feedService.removeFeed( nfeed, resp.getErr() );
216                 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
217                         return resp.success(Status.NO_CONTENT.getStatusCode(), null);
218                 }
219                 logger.info( "Unable to delete: " + id + ":" + nfeed.getFeedVersion());
220
221                 return resp.error();
222         }
223
224         @GET
225         @ApiOperation( value = "return Feed details", 
226         notes = "Retrieve a  `Feed` object, specified by id.", 
227         response = Feed.class)
228         @ApiResponses( value = {
229             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
230             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
231         })
232         @Path("/{id}")
233         public Response getFeed( 
234                         @PathParam("id") String id
235                         ) {
236                 ApiService resp = new ApiService();
237
238                 FeedService feedService = new FeedService();
239                 Feed nfeed =  feedService.getFeed( id, resp.getErr() );
240                 if ( nfeed == null ) {
241                         resp.setCode(Status.NOT_FOUND.getStatusCode());
242                         return resp.error();
243                 }
244                 return resp.success(nfeed);
245         }
246 }