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.ApiService;
50 import org.onap.dmaap.dbcapi.service.FeedService;
54 @Api( value= "Feeds", description = "Endpoint for a Data Router Feed" )
55 @Consumes(MediaType.APPLICATION_JSON)
56 @Produces(MediaType.APPLICATION_JSON)
58 public class FeedResource extends BaseLoggingClass {
60 private ResponseBuilder responseBuilder = new ResponseBuilder();
61 private RequiredChecker checker = new RequiredChecker();
64 @ApiOperation( value = "return Feed details",
65 notes = "Returns array of `Feed` objects.",
66 response = Feed.class)
67 @ApiResponses( value = {
68 @ApiResponse( code = 200, message = "Success", response = Feed.class),
69 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
71 public Response getFeeds(
72 @QueryParam("feedName") String feedName,
73 @QueryParam("version") String version,
74 @QueryParam("match") String match) {
76 FeedService feedService = new FeedService();
77 List<Feed> nfeeds = feedService.getAllFeeds( feedName, version, match );
78 GenericEntity<List<Feed>> list = new GenericEntity<List<Feed>>(nfeeds) {
80 return responseBuilder.success(list);
86 @ApiOperation( value = "return Feed details",
87 notes = "Create a of `Feed` object.",
88 response = Feed.class)
89 @ApiResponses( value = {
90 @ApiResponse( code = 200, message = "Success", response = Feed.class),
91 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
93 public Response addFeed(
94 @WebParam(name = "feed") Feed feed,
95 @QueryParam("useExisting") String useExisting) {
97 ApiService resp = new ApiService();
100 checker.required( "feedName", feed.getFeedName());
101 checker.required( "feedVersion", feed.getFeedVersion());
102 checker.required( "owner", feed.getOwner());
103 checker.required( "asprClassification", feed.getAsprClassification());
104 } catch ( RequiredFieldException rfe ) {
105 logger.debug( rfe.getApiError().toString() );
106 return responseBuilder.error(rfe.getApiError());
110 FeedService feedService = new FeedService();
111 Feed nfeed = feedService.getFeedByName( feed.getFeedName(), feed.getFeedVersion(), resp.getErr() );
112 if ( nfeed == null ) {
113 nfeed = feedService.addFeed( feed, resp.getErr() );
114 if ( nfeed != null ) {
115 return responseBuilder.success(nfeed);
117 logger.error( "Unable to create: " + feed.getFeedName() + ":" + feed.getFeedVersion());
119 return responseBuilder.error(resp.getErr());
121 } else if ( nfeed.getStatus() == DmaapObject_Status.DELETED ) {
122 feed.setFeedId( nfeed.getFeedId());
123 nfeed = feedService.updateFeed(feed, resp.getErr());
124 if ( nfeed != null ) {
125 return responseBuilder.success(nfeed);
127 logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
129 return responseBuilder.error(resp.getErr());
131 } else if ( (useExisting != null) && ("true".compareToIgnoreCase( useExisting ) == 0)) {
132 return responseBuilder.success(nfeed);
135 resp.setCode(Status.CONFLICT.getStatusCode());
136 return responseBuilder.error(resp.getErr());
140 @ApiOperation( value = "return Feed details",
141 notes = "Update a `Feed` object, specified by id.",
142 response = Feed.class)
143 @ApiResponses( value = {
144 @ApiResponse( code = 200, message = "Success", response = Feed.class),
145 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
148 public Response updateFeed(
149 @PathParam("id") String id,
150 @WebParam(name = "feed") Feed feed) {
152 FeedService feedService = new FeedService();
153 ApiService resp = new ApiService();
156 checker.required( "feedId", id);
157 } catch ( RequiredFieldException rfe ) {
158 logger.debug( rfe.getApiError().toString() );
159 return responseBuilder.error(rfe.getApiError());
162 Feed nfeed = feedService.getFeed( id, resp.getErr() );
163 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
164 return responseBuilder.notFound();
167 // we assume there is no updates allowed for pubs and subs objects via this api...
168 // need to update any fields supported by PUT but preserve original field values.
169 nfeed.setSuspended(feed.isSuspended());
170 nfeed.setFeedDescription(feed.getFeedDescription());
171 nfeed.setFormatUuid(feed.getFormatUuid());
173 nfeed = feedService.updateFeed(nfeed, resp.getErr());
174 if ( nfeed != null ) {
175 return responseBuilder.success(nfeed);
177 logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
179 return responseBuilder.error(resp.getErr());
184 @ApiOperation( value = "return Feed details",
185 notes = "Delete a `Feed` object, specified by id.",
186 response = Feed.class)
187 @ApiResponses( value = {
188 @ApiResponse( code = 204, message = "Success", response = Feed.class),
189 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
192 public Response deleteFeed(
193 @PathParam("id") String id
195 ApiService resp = new ApiService();
197 logger.debug( "Entry: DELETE " + id);
198 FeedService feedService = new FeedService();
199 Feed nfeed = feedService.getFeed( id, resp.getErr() );
200 if ( nfeed == null ) {
201 resp.setCode(Status.NOT_FOUND.getStatusCode());
202 return responseBuilder.error(resp.getErr());
204 nfeed = feedService.removeFeed( nfeed, resp.getErr() );
205 if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
206 return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null);
208 logger.info( "Unable to delete: " + id + ":" + nfeed.getFeedVersion());
210 return responseBuilder.error(resp.getErr());
214 @ApiOperation( value = "return Feed details",
215 notes = "Retrieve a `Feed` object, specified by id.",
216 response = Feed.class)
217 @ApiResponses( value = {
218 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
219 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
222 public Response getFeed(
223 @PathParam("id") String id
225 ApiService resp = new ApiService();
227 FeedService feedService = new FeedService();
228 Feed nfeed = feedService.getFeed( id, resp.getErr() );
229 if ( nfeed == null ) {
230 resp.setCode(Status.NOT_FOUND.getStatusCode());
231 return responseBuilder.error(resp.getErr());
233 return responseBuilder.success(nfeed);
237 @ApiOperation( value = "sync feeds to existing DR",
238 notes = "When Bus Controller is deployed after DR, then it is possible"
239 + "that DR has previous provisioning data that needs to be imported"
240 + "into Bus Controller.",
241 response = Feed.class )
242 @ApiResponses( value = {
243 @ApiResponse( code = 200, message = "Success", response = Feed.class),
244 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
247 public Response syncFeeds (
248 @QueryParam("hard") String hardParam
250 ApiService resp = new ApiService();
252 FeedService feedService = new FeedService();
253 boolean hard = false;
254 if ( hardParam != null && hardParam.equalsIgnoreCase("true")) {
257 feedService.sync( hard, resp.getErr() );
258 if ( resp.getErr().is2xx()) {
259 List<Feed> nfeeds = feedService.getAllFeeds();
260 GenericEntity<List<Feed>> list = new GenericEntity<List<Feed>>(nfeeds) {
262 return responseBuilder.success(list);
264 return responseBuilder.error(resp.getErr());