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