2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.dbcapi.resources;
23 import io.swagger.annotations.Api;
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
28 import java.util.List;
30 import jakarta.ws.rs.Consumes;
31 import jakarta.ws.rs.DELETE;
32 import jakarta.ws.rs.GET;
33 import jakarta.ws.rs.POST;
34 import jakarta.ws.rs.PUT;
35 import jakarta.ws.rs.Path;
36 import jakarta.ws.rs.PathParam;
37 import jakarta.ws.rs.Produces;
38 import jakarta.ws.rs.QueryParam;
39 import jakarta.ws.rs.core.GenericEntity;
40 import jakarta.ws.rs.core.MediaType;
41 import jakarta.ws.rs.core.Response;
42 import jakarta.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;
52 @Api( value= "Feeds", description = "Endpoint for a Data Router Feed" )
53 @Consumes(MediaType.APPLICATION_JSON)
54 @Produces(MediaType.APPLICATION_JSON)
56 public class FeedResource extends BaseLoggingClass {
58 private ResponseBuilder responseBuilder = new ResponseBuilder();
59 private RequiredChecker checker = new RequiredChecker();
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 )
69 public Response getFeeds(
70 @QueryParam("feedName") String feedName,
71 @QueryParam("version") String version,
72 @QueryParam("match") String match) {
74 FeedService feedService = new FeedService();
75 List<Feed> nfeeds = feedService.getAllFeeds( feedName, version, match );
76 GenericEntity<List<Feed>> list = new GenericEntity<List<Feed>>(nfeeds) {
78 return responseBuilder.success(list);
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 )
91 public Response addFeed(
93 @QueryParam("useExisting") String useExisting) {
95 ApiError apiError = new ApiError();
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());
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);
115 logger.error( "Unable to create: " + feed.getFeedName() + ":" + feed.getFeedVersion());
117 return responseBuilder.error(apiError);
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);
125 logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
127 return responseBuilder.error(apiError);
129 } else if ( (useExisting != null) && ("true".compareToIgnoreCase( useExisting ) == 0)) {
130 return responseBuilder.success(nfeed);
133 apiError.setCode(Status.CONFLICT.getStatusCode());
134 return responseBuilder.error(apiError);
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 )
146 public Response updateFeed(
147 @PathParam("id") String id,
150 FeedService feedService = new FeedService();
151 ApiError apiError = new ApiError();
154 checker.required( "feedId", id);
155 } catch ( RequiredFieldException rfe ) {
156 logger.debug( rfe.getApiError().toString() );
157 return responseBuilder.error(rfe.getApiError());
160 Feed nfeed = feedService.getFeed(id, apiError);
161 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
162 return responseBuilder.notFound();
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());
171 nfeed = feedService.updateFeed(nfeed, apiError);
172 if ( nfeed != null ) {
173 return responseBuilder.success(nfeed);
175 logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
177 return responseBuilder.error(apiError);
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 )
190 public Response deleteFeed(@PathParam("id") String id){
191 ApiError apiError = new ApiError();
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);
200 nfeed = feedService.removeFeed(nfeed, apiError);
201 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
202 return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null);
204 logger.info( "Unable to delete: " + id + ":" + nfeed.getFeedVersion());
206 return responseBuilder.error(apiError);
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 )
218 public Response getFeed(@PathParam("id") String id) {
219 ApiError apiError = new ApiError();
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);
227 return responseBuilder.success(nfeed);
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 )
241 public Response syncFeeds (@QueryParam("hard") String hardParam) {
242 ApiError error = new ApiError();
244 FeedService feedService = new FeedService();
245 boolean hard = false;
246 if ( hardParam != null && hardParam.equalsIgnoreCase("true")) {
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) {
254 return responseBuilder.success(list);
256 return responseBuilder.error(error);