Added new feeds/sync API
[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         private ResponseBuilder responseBuilder = new ResponseBuilder();
61         private RequiredChecker checker = new RequiredChecker();
62
63         @GET
64         @ApiOperation( value = "return Feed details", 
65         notes = "Returns array of  `Feed` objects.", 
66         response = Feed.class)
67         @ApiResponses( value = {
68             @ApiResponse( code = 200, message = "Success", response = Feed.class),
69             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
70         })
71         public Response getFeeds(
72                         @QueryParam("feedName") String feedName,
73                         @QueryParam("version") String version,
74                         @QueryParam("match") String match) {
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 responseBuilder.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                 ApiService resp = new ApiService();
98
99                 try {
100                         checker.required( "feedName", feed.getFeedName());
101                         checker.required( "feedVersion", feed.getFeedVersion());
102                         checker.required( "owner", feed.getOwner());
103                         checker.required( "asprClassification", feed.getAsprClassification());
104                 } catch ( RequiredFieldException rfe ) {
105                         logger.debug( rfe.getApiError().toString() );
106                         return responseBuilder.error(rfe.getApiError());
107                 }
108                 
109                 
110                 FeedService feedService = new FeedService();
111                 Feed nfeed =  feedService.getFeedByName( feed.getFeedName(), feed.getFeedVersion(), resp.getErr() );
112                 if ( nfeed == null ) {
113                         nfeed =  feedService.addFeed( feed, resp.getErr() );
114                         if ( nfeed != null ) {
115                                 return responseBuilder.success(nfeed);
116                         } else {
117                                 logger.error( "Unable to create: " + feed.getFeedName() + ":" + feed.getFeedVersion());
118
119                                 return responseBuilder.error(resp.getErr());
120                         }
121                 } else if ( nfeed.getStatus() == DmaapObject_Status.DELETED ) {
122                         feed.setFeedId( nfeed.getFeedId());
123                         nfeed =  feedService.updateFeed(feed, resp.getErr());
124                         if ( nfeed != null ) {
125                                 return responseBuilder.success(nfeed);
126                         } else {
127                                 logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
128
129                                 return responseBuilder.error(resp.getErr());
130                         }
131                 } else if ( (useExisting != null) && ("true".compareToIgnoreCase( useExisting ) == 0)) {
132                         return responseBuilder.success(nfeed);
133                 }
134
135                 resp.setCode(Status.CONFLICT.getStatusCode());
136                 return responseBuilder.error(resp.getErr());
137         }
138         
139         @PUT
140         @ApiOperation( value = "return Feed details", 
141         notes = "Update a  `Feed` object, specified by id.", 
142         response = Feed.class)
143         @ApiResponses( value = {
144             @ApiResponse( code = 200, message = "Success", response = Feed.class),
145             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
146         })
147         @Path("/{id}")
148         public Response updateFeed( 
149                         @PathParam("id") String id,
150                         @WebParam(name = "feed") Feed feed) {
151
152                 FeedService feedService = new FeedService();
153                 ApiService resp = new ApiService();
154
155                 try {
156                         checker.required( "feedId", id);
157                 } catch ( RequiredFieldException rfe ) {
158                         logger.debug( rfe.getApiError().toString() );
159                         return responseBuilder.error(rfe.getApiError());
160                 }
161
162                 Feed nfeed = feedService.getFeed( id, resp.getErr() );
163                 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
164                         return responseBuilder.notFound();
165                 }
166         
167                 //  we assume there is no updates allowed for pubs and subs objects via this api...             
168                 // need to update any fields supported by PUT but preserve original field values. 
169                 nfeed.setSuspended(feed.isSuspended());
170                 nfeed.setFeedDescription(feed.getFeedDescription());
171                 nfeed.setFormatUuid(feed.getFormatUuid());
172                 
173                 nfeed =  feedService.updateFeed(nfeed, resp.getErr());
174                 if ( nfeed != null ) {
175                         return responseBuilder.success(nfeed);
176                 } else {
177                         logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
178
179                         return responseBuilder.error(resp.getErr());
180                 }
181         }
182         
183         @DELETE
184         @ApiOperation( value = "return Feed details", 
185         notes = "Delete a  `Feed` object, specified by id.", 
186         response = Feed.class)
187         @ApiResponses( value = {
188             @ApiResponse( code = 204, message = "Success", response = Feed.class),
189             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
190         })
191         @Path("/{id}")
192         public Response deleteFeed( 
193                         @PathParam("id") String id
194                         ){
195                 ApiService resp = new ApiService();
196
197                 logger.debug( "Entry: DELETE  " + id);
198                 FeedService feedService = new FeedService();
199                 Feed nfeed =  feedService.getFeed( id, resp.getErr() );
200                 if ( nfeed == null ) {
201                         resp.setCode(Status.NOT_FOUND.getStatusCode());
202                         return responseBuilder.error(resp.getErr());
203                 }
204                 nfeed = feedService.removeFeed( nfeed, resp.getErr() );
205                 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
206                         return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null);
207                 }
208                 logger.info( "Unable to delete: " + id + ":" + nfeed.getFeedVersion());
209
210                 return responseBuilder.error(resp.getErr());
211         }
212
213         @GET
214         @ApiOperation( value = "return Feed details", 
215         notes = "Retrieve a  `Feed` object, specified by id.", 
216         response = Feed.class)
217         @ApiResponses( value = {
218             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
219             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
220         })
221         @Path("/{id}")
222         public Response getFeed( 
223                         @PathParam("id") String id
224                         ) {
225                 ApiService resp = new ApiService();
226
227                 FeedService feedService = new FeedService();
228                 Feed nfeed =  feedService.getFeed( id, resp.getErr() );
229                 if ( nfeed == null ) {
230                         resp.setCode(Status.NOT_FOUND.getStatusCode());
231                         return responseBuilder.error(resp.getErr());
232                 }
233                 return responseBuilder.success(nfeed);
234         }
235         
236         @PUT
237         @ApiOperation( value = "sync feeds to existing DR",
238         notes = "When Bus Controller is deployed after DR, then it is possible"
239                         + "that DR has previous provisioning data that needs to be imported"
240                         + "into Bus Controller.",
241         response = Feed.class )
242         @ApiResponses( value =  {
243                         @ApiResponse( code = 200, message = "Success", response = Feed.class),
244                         @ApiResponse( code = 400, message = "Error", response = ApiError.class )
245         })
246         @Path( "/sync")
247         public Response syncFeeds ( 
248                         @QueryParam("hard") String hardParam
249                         ) {
250                 ApiService resp = new ApiService();
251                 
252                 FeedService feedService = new FeedService();
253                 boolean hard = false;
254                 if (  hardParam != null && hardParam.equalsIgnoreCase("true")) {
255                         hard = true;
256                 }
257                 feedService.sync( hard, resp.getErr() );
258                 if ( resp.getErr().is2xx()) {   
259                         List<Feed> nfeeds =  feedService.getAllFeeds();
260                         GenericEntity<List<Feed>> list = new GenericEntity<List<Feed>>(nfeeds) {
261                         };
262                         return responseBuilder.success(list);
263                 }
264                 return responseBuilder.error(resp.getErr());
265         }
266         
267 }