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.HeaderParam;
35 import javax.ws.rs.POST;
36 import javax.ws.rs.PUT;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.PathParam;
39 import javax.ws.rs.Produces;
40 import javax.ws.rs.core.Context;
41 import javax.ws.rs.core.GenericEntity;
42 import javax.ws.rs.core.MediaType;
43 import javax.ws.rs.core.Response;
44 import javax.ws.rs.core.Response.Status;
45 import javax.ws.rs.core.UriInfo;
47 import org.apache.log4j.Logger;
67 import org.onap.dmaap.dbcapi.aaf.authentication.AuthenticationErrorException;
68 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
69 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
70 import org.onap.dmaap.dbcapi.model.ApiError;
71 import org.onap.dmaap.dbcapi.model.DR_Pub;
72 import org.onap.dmaap.dbcapi.model.DR_Sub;
73 import org.onap.dmaap.dbcapi.model.Feed;
74 import org.onap.dmaap.dbcapi.model.Topic;
75 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
76 import org.onap.dmaap.dbcapi.service.ApiService;
77 import org.onap.dmaap.dbcapi.service.FeedService;
81 @Api( value= "Feeds", description = "Endpoint for a Data Router Feed" )
82 @Consumes(MediaType.APPLICATION_JSON)
83 @Produces(MediaType.APPLICATION_JSON)
85 public class FeedResource extends BaseLoggingClass {
88 @ApiOperation( value = "return Feed details",
89 notes = "Returns array of `Feed` objects.",
90 response = Feed.class)
91 @ApiResponses( value = {
92 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
93 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
95 public Response getFeeds() {
97 ApiService resp = new ApiService();
99 FeedService feedService = new FeedService();
100 List<Feed> nfeeds = feedService.getAllFeeds();
101 GenericEntity<List<Feed>> list = new GenericEntity<List<Feed>>(nfeeds) {
103 return resp.success(list);
109 @ApiOperation( value = "return Feed details",
110 notes = "Create a of `Feed` object.",
111 response = Feed.class)
112 @ApiResponses( value = {
113 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
114 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
116 public Response addFeed(
117 @WebParam(name = "feed") Feed feed
120 ApiService resp = new ApiService();
123 resp.required( "feedName", feed.getFeedName(), "");
124 resp.required( "feedVersion", feed.getFeedVersion(), "");
125 resp.required( "owner", feed.getOwner(), "" );
126 resp.required( "asprClassification", feed.getAsprClassification(), "" );
127 } catch ( RequiredFieldException rfe ) {
128 logger.debug( resp.toString() );
132 FeedService feedService = new FeedService();
133 Feed nfeed = feedService.getFeedByName( feed.getFeedName(), feed.getFeedVersion(), resp.getErr() );
134 if ( nfeed == null ) {
135 nfeed = feedService.addFeed( feed, resp.getErr() );
136 if ( nfeed != null ) {
137 return resp.success(nfeed);
139 logger.error( "Unable to create: " + feed.getFeedName() + ":" + feed.getFeedVersion());
143 } else if ( nfeed.getStatus() == DmaapObject_Status.DELETED ) {
144 nfeed = feedService.updateFeed(nfeed, resp.getErr());
145 if ( nfeed != null ) {
146 return resp.success(nfeed);
148 logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
154 resp.setCode(Status.CONFLICT.getStatusCode());
159 @ApiOperation( value = "return Feed details",
160 notes = "Update a `Feed` object, specified by id.",
161 response = Feed.class)
162 @ApiResponses( value = {
163 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
164 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
167 public Response updateFeed(
168 @PathParam("id") String id,
169 @WebParam(name = "feed") Feed feed
172 FeedService feedService = new FeedService();
173 ApiService resp = new ApiService();
176 resp.required( "feedId", id, "");
177 } catch ( RequiredFieldException rfe ) {
178 logger.debug( resp.toString() );
182 Feed nfeed = feedService.getFeed( id, resp.getErr() );
183 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
184 return resp.notFound();
187 // we assume there is no updates allowed for pubs and subs objects via this api...
188 // need to update any fields supported by PUT but preserve original field values.
189 nfeed.setSuspended(feed.isSuspended());
190 nfeed.setFeedDescription(feed.getFeedDescription());
191 nfeed.setFormatUuid(feed.getFormatUuid());
193 nfeed = feedService.updateFeed(nfeed, resp.getErr());
194 if ( nfeed != null ) {
195 return resp.success(nfeed);
197 logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
204 @ApiOperation( value = "return Feed details",
205 notes = "Delete a `Feed` object, specified by id.",
206 response = Feed.class)
207 @ApiResponses( value = {
208 @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
209 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
212 public Response deleteFeed(
213 @PathParam("id") String id
215 ApiService resp = new ApiService();
217 logger.debug( "Entry: DELETE " + id);
218 FeedService feedService = new FeedService();
219 Feed nfeed = feedService.getFeed( id, resp.getErr() );
220 if ( nfeed == null ) {
221 resp.setCode(Status.NOT_FOUND.getStatusCode());
224 nfeed = feedService.removeFeed( nfeed, resp.getErr() );
225 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
226 return resp.success(Status.NO_CONTENT.getStatusCode(), null);
228 logger.info( "Unable to delete: " + id + ":" + nfeed.getFeedVersion());
234 @ApiOperation( value = "return Feed details",
235 notes = "Retrieve a `Feed` object, specified by id.",
236 response = Feed.class)
237 @ApiResponses( value = {
238 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
239 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
242 public Response getFeed(
243 @PathParam("id") String id
245 ApiService resp = new ApiService();
247 FeedService feedService = new FeedService();
248 Feed nfeed = feedService.getFeed( id, resp.getErr() );
249 if ( nfeed == null ) {
250 resp.setCode(Status.NOT_FOUND.getStatusCode());
253 return resp.success(nfeed);