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;
68 import org.onap.dmaap.dbcapi.aaf.authentication.AuthenticationErrorException;
69 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
70 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
71 import org.onap.dmaap.dbcapi.model.ApiError;
72 import org.onap.dmaap.dbcapi.model.DR_Pub;
73 import org.onap.dmaap.dbcapi.model.DR_Sub;
74 import org.onap.dmaap.dbcapi.model.Feed;
75 import org.onap.dmaap.dbcapi.model.Topic;
76 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
77 import org.onap.dmaap.dbcapi.service.ApiService;
78 import org.onap.dmaap.dbcapi.service.FeedService;
82 @Api( value= "Feeds", description = "Endpoint for a Data Router Feed" )
83 @Consumes(MediaType.APPLICATION_JSON)
84 @Produces(MediaType.APPLICATION_JSON)
86 public class FeedResource extends BaseLoggingClass {
89 @ApiOperation( value = "return Feed details",
90 notes = "Returns array of `Feed` objects.",
91 response = Feed.class)
92 @ApiResponses( value = {
93 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
94 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
96 public Response getFeeds(
97 @QueryParam("feedName") String feedName,
98 @QueryParam("version") String version,
99 @QueryParam("match") String match
102 ApiService resp = new ApiService();
104 FeedService feedService = new FeedService();
105 List<Feed> nfeeds = feedService.getAllFeeds( feedName, version, match );
106 GenericEntity<List<Feed>> list = new GenericEntity<List<Feed>>(nfeeds) {
108 return resp.success(list);
114 @ApiOperation( value = "return Feed details",
115 notes = "Create a of `Feed` object.",
116 response = Feed.class)
117 @ApiResponses( value = {
118 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
119 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
121 public Response addFeed(
122 @WebParam(name = "feed") Feed feed
125 ApiService resp = new ApiService();
128 resp.required( "feedName", feed.getFeedName(), "");
129 resp.required( "feedVersion", feed.getFeedVersion(), "");
130 resp.required( "owner", feed.getOwner(), "" );
131 resp.required( "asprClassification", feed.getAsprClassification(), "" );
132 } catch ( RequiredFieldException rfe ) {
133 logger.debug( resp.toString() );
137 FeedService feedService = new FeedService();
138 Feed nfeed = feedService.getFeedByName( feed.getFeedName(), feed.getFeedVersion(), resp.getErr() );
139 if ( nfeed == null ) {
140 nfeed = feedService.addFeed( feed, resp.getErr() );
141 if ( nfeed != null ) {
142 return resp.success(nfeed);
144 logger.error( "Unable to create: " + feed.getFeedName() + ":" + feed.getFeedVersion());
148 } else if ( nfeed.getStatus() == DmaapObject_Status.DELETED ) {
149 nfeed = feedService.updateFeed(nfeed, resp.getErr());
150 if ( nfeed != null ) {
151 return resp.success(nfeed);
153 logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
159 resp.setCode(Status.CONFLICT.getStatusCode());
164 @ApiOperation( value = "return Feed details",
165 notes = "Update a `Feed` object, specified by id.",
166 response = Feed.class)
167 @ApiResponses( value = {
168 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
169 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
172 public Response updateFeed(
173 @PathParam("id") String id,
174 @WebParam(name = "feed") Feed feed
177 FeedService feedService = new FeedService();
178 ApiService resp = new ApiService();
181 resp.required( "feedId", id, "");
182 } catch ( RequiredFieldException rfe ) {
183 logger.debug( resp.toString() );
187 Feed nfeed = feedService.getFeed( id, resp.getErr() );
188 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
189 return resp.notFound();
192 // we assume there is no updates allowed for pubs and subs objects via this api...
193 // need to update any fields supported by PUT but preserve original field values.
194 nfeed.setSuspended(feed.isSuspended());
195 nfeed.setFeedDescription(feed.getFeedDescription());
196 nfeed.setFormatUuid(feed.getFormatUuid());
198 nfeed = feedService.updateFeed(nfeed, resp.getErr());
199 if ( nfeed != null ) {
200 return resp.success(nfeed);
202 logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
209 @ApiOperation( value = "return Feed details",
210 notes = "Delete a `Feed` object, specified by id.",
211 response = Feed.class)
212 @ApiResponses( value = {
213 @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
214 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
217 public Response deleteFeed(
218 @PathParam("id") String id
220 ApiService resp = new ApiService();
222 logger.debug( "Entry: DELETE " + id);
223 FeedService feedService = new FeedService();
224 Feed nfeed = feedService.getFeed( id, resp.getErr() );
225 if ( nfeed == null ) {
226 resp.setCode(Status.NOT_FOUND.getStatusCode());
229 nfeed = feedService.removeFeed( nfeed, resp.getErr() );
230 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
231 return resp.success(Status.NO_CONTENT.getStatusCode(), null);
233 logger.info( "Unable to delete: " + id + ":" + nfeed.getFeedVersion());
239 @ApiOperation( value = "return Feed details",
240 notes = "Retrieve a `Feed` object, specified by id.",
241 response = Feed.class)
242 @ApiResponses( value = {
243 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
244 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
247 public Response getFeed(
248 @PathParam("id") String id
250 ApiService resp = new ApiService();
252 FeedService feedService = new FeedService();
253 Feed nfeed = feedService.getFeed( id, resp.getErr() );
254 if ( nfeed == null ) {
255 resp.setCode(Status.NOT_FOUND.getStatusCode());
258 return resp.success(nfeed);