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