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