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 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;
54 @Api( value= "Feeds", description = "Endpoint for a Data Router Feed" )
55 @Consumes(MediaType.APPLICATION_JSON)
56 @Produces(MediaType.APPLICATION_JSON)
58 public class FeedResource extends BaseLoggingClass {
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 )
68 public Response getFeeds(
69 @QueryParam("feedName") String feedName,
70 @QueryParam("version") String version,
71 @QueryParam("match") String match
74 ApiService resp = new ApiService();
76 FeedService feedService = new FeedService();
77 List<Feed> nfeeds = feedService.getAllFeeds( feedName, version, match );
78 GenericEntity<List<Feed>> list = new GenericEntity<List<Feed>>(nfeeds) {
80 return resp.success(list);
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 )
93 public Response addFeed(
94 @WebParam(name = "feed") Feed feed,
95 @QueryParam("useExisting") String useExisting
98 ApiService resp = new ApiService();
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() );
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);
118 logger.error( "Unable to create: " + feed.getFeedName() + ":" + feed.getFeedVersion());
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);
128 logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
132 } else if ( (useExisting != null) && ("true".compareToIgnoreCase( useExisting ) == 0)) {
133 return resp.success(nfeed);
136 resp.setCode(Status.CONFLICT.getStatusCode());
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 )
149 public Response updateFeed(
150 @PathParam("id") String id,
151 @WebParam(name = "feed") Feed feed
154 FeedService feedService = new FeedService();
155 ApiService resp = new ApiService();
158 resp.required( "feedId", id, "");
159 } catch ( RequiredFieldException rfe ) {
160 logger.debug( resp.toString() );
164 Feed nfeed = feedService.getFeed( id, resp.getErr() );
165 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
166 return resp.notFound();
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());
175 nfeed = feedService.updateFeed(nfeed, resp.getErr());
176 if ( nfeed != null ) {
177 return resp.success(nfeed);
179 logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
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 )
194 public Response deleteFeed(
195 @PathParam("id") String id
197 ApiService resp = new ApiService();
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());
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);
210 logger.info( "Unable to delete: " + id + ":" + nfeed.getFeedVersion());
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 )
224 public Response getFeed(
225 @PathParam("id") String id
227 ApiService resp = new ApiService();
229 FeedService feedService = new FeedService();
230 Feed nfeed = feedService.getFeed( id, resp.getErr() );
231 if ( nfeed == null ) {
232 resp.setCode(Status.NOT_FOUND.getStatusCode());
235 return resp.success(nfeed);