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.QueryParam;
41 import javax.ws.rs.core.Context;
42 import javax.ws.rs.core.GenericEntity;
43 import javax.ws.rs.core.MediaType;
44 import javax.ws.rs.core.Response;
45 import javax.ws.rs.core.Response.Status;
46 import javax.ws.rs.core.UriInfo;
48 import org.apache.log4j.Logger;
49 import org.onap.dmaap.dbcapi.authentication.AuthenticationErrorException;
50 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
51 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
52 import org.onap.dmaap.dbcapi.model.ApiError;
53 import org.onap.dmaap.dbcapi.model.DR_Pub;
54 import org.onap.dmaap.dbcapi.model.DR_Sub;
55 import org.onap.dmaap.dbcapi.model.Feed;
56 import org.onap.dmaap.dbcapi.model.Topic;
57 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
58 import org.onap.dmaap.dbcapi.service.ApiService;
59 import org.onap.dmaap.dbcapi.service.FeedService;
63 @Api( value= "Feeds", description = "Endpoint for a Data Router Feed" )
64 @Consumes(MediaType.APPLICATION_JSON)
65 @Produces(MediaType.APPLICATION_JSON)
67 public class FeedResource extends BaseLoggingClass {
70 @ApiOperation( value = "return Feed details",
71 notes = "Returns array of `Feed` objects.",
72 response = Feed.class)
73 @ApiResponses( value = {
74 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
75 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
77 public Response getFeeds(
78 @QueryParam("feedName") String feedName,
79 @QueryParam("version") String version,
80 @QueryParam("match") String match
83 ApiService resp = new ApiService();
85 FeedService feedService = new FeedService();
86 List<Feed> nfeeds = feedService.getAllFeeds( feedName, version, match );
87 GenericEntity<List<Feed>> list = new GenericEntity<List<Feed>>(nfeeds) {
89 return resp.success(list);
95 @ApiOperation( value = "return Feed details",
96 notes = "Create a of `Feed` object.",
97 response = Feed.class)
98 @ApiResponses( value = {
99 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
100 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
102 public Response addFeed(
103 @WebParam(name = "feed") Feed feed
106 ApiService resp = new ApiService();
109 resp.required( "feedName", feed.getFeedName(), "");
110 resp.required( "feedVersion", feed.getFeedVersion(), "");
111 resp.required( "owner", feed.getOwner(), "" );
112 resp.required( "asprClassification", feed.getAsprClassification(), "" );
113 } catch ( RequiredFieldException rfe ) {
114 logger.debug( resp.toString() );
118 FeedService feedService = new FeedService();
119 Feed nfeed = feedService.getFeedByName( feed.getFeedName(), feed.getFeedVersion(), resp.getErr() );
120 if ( nfeed == null ) {
121 nfeed = feedService.addFeed( feed, resp.getErr() );
122 if ( nfeed != null ) {
123 return resp.success(nfeed);
125 logger.error( "Unable to create: " + feed.getFeedName() + ":" + feed.getFeedVersion());
129 } else if ( nfeed.getStatus() == DmaapObject_Status.DELETED ) {
130 feed.setFeedId( nfeed.getFeedId());
131 nfeed = feedService.updateFeed(feed, resp.getErr());
132 if ( nfeed != null ) {
133 return resp.success(nfeed);
135 logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
141 resp.setCode(Status.CONFLICT.getStatusCode());
146 @ApiOperation( value = "return Feed details",
147 notes = "Update a `Feed` object, specified by id.",
148 response = Feed.class)
149 @ApiResponses( value = {
150 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
151 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
154 public Response updateFeed(
155 @PathParam("id") String id,
156 @WebParam(name = "feed") Feed feed
159 FeedService feedService = new FeedService();
160 ApiService resp = new ApiService();
163 resp.required( "feedId", id, "");
164 } catch ( RequiredFieldException rfe ) {
165 logger.debug( resp.toString() );
169 Feed nfeed = feedService.getFeed( id, resp.getErr() );
170 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
171 return resp.notFound();
174 // we assume there is no updates allowed for pubs and subs objects via this api...
175 // need to update any fields supported by PUT but preserve original field values.
176 nfeed.setSuspended(feed.isSuspended());
177 nfeed.setFeedDescription(feed.getFeedDescription());
178 nfeed.setFormatUuid(feed.getFormatUuid());
180 nfeed = feedService.updateFeed(nfeed, resp.getErr());
181 if ( nfeed != null ) {
182 return resp.success(nfeed);
184 logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
191 @ApiOperation( value = "return Feed details",
192 notes = "Delete a `Feed` object, specified by id.",
193 response = Feed.class)
194 @ApiResponses( value = {
195 @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
196 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
199 public Response deleteFeed(
200 @PathParam("id") String id
202 ApiService resp = new ApiService();
204 logger.debug( "Entry: DELETE " + id);
205 FeedService feedService = new FeedService();
206 Feed nfeed = feedService.getFeed( id, resp.getErr() );
207 if ( nfeed == null ) {
208 resp.setCode(Status.NOT_FOUND.getStatusCode());
211 nfeed = feedService.removeFeed( nfeed, resp.getErr() );
212 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
213 return resp.success(Status.NO_CONTENT.getStatusCode(), null);
215 logger.info( "Unable to delete: " + id + ":" + nfeed.getFeedVersion());
221 @ApiOperation( value = "return Feed details",
222 notes = "Retrieve a `Feed` object, specified by id.",
223 response = Feed.class)
224 @ApiResponses( value = {
225 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
226 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
229 public Response getFeed(
230 @PathParam("id") String id
232 ApiService resp = new ApiService();
234 FeedService feedService = new FeedService();
235 Feed nfeed = feedService.getFeed( id, resp.getErr() );
236 if ( nfeed == null ) {
237 resp.setCode(Status.NOT_FOUND.getStatusCode());
240 return resp.success(nfeed);