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.FeedService;
53 @Api( value= "Feeds", description = "Endpoint for a Data Router Feed" )
54 @Consumes(MediaType.APPLICATION_JSON)
55 @Produces(MediaType.APPLICATION_JSON)
57 public class FeedResource extends BaseLoggingClass {
59 private ResponseBuilder responseBuilder = new ResponseBuilder();
60 private RequiredChecker checker = new RequiredChecker();
63 @ApiOperation( value = "return Feed details",
64 notes = "Returns array of `Feed` objects.",
65 response = Feed.class)
66 @ApiResponses( value = {
67 @ApiResponse( code = 200, message = "Success", response = Feed.class),
68 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
70 public Response getFeeds(
71 @QueryParam("feedName") String feedName,
72 @QueryParam("version") String version,
73 @QueryParam("match") String match) {
75 FeedService feedService = new FeedService();
76 List<Feed> nfeeds = feedService.getAllFeeds( feedName, version, match );
77 GenericEntity<List<Feed>> list = new GenericEntity<List<Feed>>(nfeeds) {
79 return responseBuilder.success(list);
85 @ApiOperation( value = "return Feed details",
86 notes = "Create a of `Feed` object.",
87 response = Feed.class)
88 @ApiResponses( value = {
89 @ApiResponse( code = 200, message = "Success", response = Feed.class),
90 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
92 public Response addFeed(
93 @WebParam(name = "feed") Feed feed,
94 @QueryParam("useExisting") String useExisting) {
96 ApiError apiError = new ApiError();
99 checker.required( "feedName", feed.getFeedName());
100 checker.required( "feedVersion", feed.getFeedVersion());
101 checker.required( "owner", feed.getOwner());
102 checker.required( "asprClassification", feed.getAsprClassification());
103 } catch ( RequiredFieldException rfe ) {
104 logger.debug( rfe.getApiError().toString() );
105 return responseBuilder.error(rfe.getApiError());
109 FeedService feedService = new FeedService();
110 Feed nfeed = feedService.getFeedByName( feed.getFeedName(), feed.getFeedVersion(), apiError);
111 if ( nfeed == null ) {
112 nfeed = feedService.addFeed(feed, apiError);
113 if ( nfeed != null ) {
114 return responseBuilder.success(nfeed);
116 logger.error( "Unable to create: " + feed.getFeedName() + ":" + feed.getFeedVersion());
118 return responseBuilder.error(apiError);
120 } else if ( nfeed.getStatus() == DmaapObject_Status.DELETED ) {
121 feed.setFeedId( nfeed.getFeedId());
122 nfeed = feedService.updateFeed(feed, apiError);
123 if ( nfeed != null ) {
124 return responseBuilder.success(nfeed);
126 logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
128 return responseBuilder.error(apiError);
130 } else if ( (useExisting != null) && ("true".compareToIgnoreCase( useExisting ) == 0)) {
131 return responseBuilder.success(nfeed);
134 apiError.setCode(Status.CONFLICT.getStatusCode());
135 return responseBuilder.error(apiError);
139 @ApiOperation( value = "return Feed details",
140 notes = "Update a `Feed` object, specified by id.",
141 response = Feed.class)
142 @ApiResponses( value = {
143 @ApiResponse( code = 200, message = "Success", response = Feed.class),
144 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
147 public Response updateFeed(
148 @PathParam("id") String id,
149 @WebParam(name = "feed") Feed feed) {
151 FeedService feedService = new FeedService();
152 ApiError apiError = new ApiError();
155 checker.required( "feedId", id);
156 } catch ( RequiredFieldException rfe ) {
157 logger.debug( rfe.getApiError().toString() );
158 return responseBuilder.error(rfe.getApiError());
161 Feed nfeed = feedService.getFeed(id, apiError);
162 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
163 return responseBuilder.notFound();
166 // we assume there is no updates allowed for pubs and subs objects via this api...
167 // need to update any fields supported by PUT but preserve original field values.
168 nfeed.setSuspended(feed.isSuspended());
169 nfeed.setFeedDescription(feed.getFeedDescription());
170 nfeed.setFormatUuid(feed.getFormatUuid());
172 nfeed = feedService.updateFeed(nfeed, apiError);
173 if ( nfeed != null ) {
174 return responseBuilder.success(nfeed);
176 logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
178 return responseBuilder.error(apiError);
183 @ApiOperation( value = "return Feed details",
184 notes = "Delete a `Feed` object, specified by id.",
185 response = Feed.class)
186 @ApiResponses( value = {
187 @ApiResponse( code = 204, message = "Success", response = Feed.class),
188 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
191 public Response deleteFeed(@PathParam("id") String id){
192 ApiError apiError = new ApiError();
194 logger.debug( "Entry: DELETE " + id);
195 FeedService feedService = new FeedService();
196 Feed nfeed = feedService.getFeed(id, apiError);
197 if ( nfeed == null ) {
198 apiError.setCode(Status.NOT_FOUND.getStatusCode());
199 return responseBuilder.error(apiError);
201 nfeed = feedService.removeFeed(nfeed, apiError);
202 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
203 return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null);
205 logger.info( "Unable to delete: " + id + ":" + nfeed.getFeedVersion());
207 return responseBuilder.error(apiError);
211 @ApiOperation( value = "return Feed details",
212 notes = "Retrieve a `Feed` object, specified by id.",
213 response = Feed.class)
214 @ApiResponses( value = {
215 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
216 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
219 public Response getFeed(@PathParam("id") String id) {
220 ApiError apiError = new ApiError();
222 FeedService feedService = new FeedService();
223 Feed nfeed = feedService.getFeed(id, apiError);
224 if ( nfeed == null ) {
225 apiError.setCode(Status.NOT_FOUND.getStatusCode());
226 return responseBuilder.error(apiError);
228 return responseBuilder.success(nfeed);
232 @ApiOperation( value = "sync feeds to existing DR",
233 notes = "When Bus Controller is deployed after DR, then it is possible"
234 + "that DR has previous provisioning data that needs to be imported"
235 + "into Bus Controller.",
236 response = Feed.class )
237 @ApiResponses( value = {
238 @ApiResponse( code = 200, message = "Success", response = Feed.class),
239 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
242 public Response syncFeeds (@QueryParam("hard") String hardParam) {
243 ApiError error = new ApiError();
245 FeedService feedService = new FeedService();
246 boolean hard = false;
247 if ( hardParam != null && hardParam.equalsIgnoreCase("true")) {
250 feedService.sync( hard, error );
251 if ( error.is2xx()) {
252 List<Feed> nfeeds = feedService.getAllFeeds();
253 GenericEntity<List<Feed>> list = new GenericEntity<List<Feed>>(nfeeds) {
255 return responseBuilder.success(list);
257 return responseBuilder.error(error);