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,
104 @QueryParam("useExisting") String useExisting
107 ApiService resp = new ApiService();
110 resp.required( "feedName", feed.getFeedName(), "");
111 resp.required( "feedVersion", feed.getFeedVersion(), "");
112 resp.required( "owner", feed.getOwner(), "" );
113 resp.required( "asprClassification", feed.getAsprClassification(), "" );
114 } catch ( RequiredFieldException rfe ) {
115 logger.debug( resp.toString() );
120 FeedService feedService = new FeedService();
121 Feed nfeed = feedService.getFeedByName( feed.getFeedName(), feed.getFeedVersion(), resp.getErr() );
122 if ( nfeed == null ) {
123 nfeed = feedService.addFeed( feed, resp.getErr() );
124 if ( nfeed != null ) {
125 return resp.success(nfeed);
127 logger.error( "Unable to create: " + feed.getFeedName() + ":" + feed.getFeedVersion());
131 } else if ( nfeed.getStatus() == DmaapObject_Status.DELETED ) {
132 feed.setFeedId( nfeed.getFeedId());
133 nfeed = feedService.updateFeed(feed, resp.getErr());
134 if ( nfeed != null ) {
135 return resp.success(nfeed);
137 logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
141 } else if ( (useExisting != null) && ("true".compareToIgnoreCase( useExisting ) == 0)) {
142 return resp.success(nfeed);
145 resp.setCode(Status.CONFLICT.getStatusCode());
150 @ApiOperation( value = "return Feed details",
151 notes = "Update a `Feed` object, specified by id.",
152 response = Feed.class)
153 @ApiResponses( value = {
154 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
155 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
158 public Response updateFeed(
159 @PathParam("id") String id,
160 @WebParam(name = "feed") Feed feed
163 FeedService feedService = new FeedService();
164 ApiService resp = new ApiService();
167 resp.required( "feedId", id, "");
168 } catch ( RequiredFieldException rfe ) {
169 logger.debug( resp.toString() );
173 Feed nfeed = feedService.getFeed( id, resp.getErr() );
174 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
175 return resp.notFound();
178 // we assume there is no updates allowed for pubs and subs objects via this api...
179 // need to update any fields supported by PUT but preserve original field values.
180 nfeed.setSuspended(feed.isSuspended());
181 nfeed.setFeedDescription(feed.getFeedDescription());
182 nfeed.setFormatUuid(feed.getFormatUuid());
184 nfeed = feedService.updateFeed(nfeed, resp.getErr());
185 if ( nfeed != null ) {
186 return resp.success(nfeed);
188 logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
195 @ApiOperation( value = "return Feed details",
196 notes = "Delete a `Feed` object, specified by id.",
197 response = Feed.class)
198 @ApiResponses( value = {
199 @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
200 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
203 public Response deleteFeed(
204 @PathParam("id") String id
206 ApiService resp = new ApiService();
208 logger.debug( "Entry: DELETE " + id);
209 FeedService feedService = new FeedService();
210 Feed nfeed = feedService.getFeed( id, resp.getErr() );
211 if ( nfeed == null ) {
212 resp.setCode(Status.NOT_FOUND.getStatusCode());
215 nfeed = feedService.removeFeed( nfeed, resp.getErr() );
216 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
217 return resp.success(Status.NO_CONTENT.getStatusCode(), null);
219 logger.info( "Unable to delete: " + id + ":" + nfeed.getFeedVersion());
225 @ApiOperation( value = "return Feed details",
226 notes = "Retrieve a `Feed` object, specified by id.",
227 response = Feed.class)
228 @ApiResponses( value = {
229 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
230 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
233 public Response getFeed(
234 @PathParam("id") String id
236 ApiService resp = new ApiService();
238 FeedService feedService = new FeedService();
239 Feed nfeed = feedService.getFeed( id, resp.getErr() );
240 if ( nfeed == null ) {
241 resp.setCode(Status.NOT_FOUND.getStatusCode());
244 return resp.success(nfeed);