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.ArrayList;
29 import java.util.Iterator;
30 import java.util.List;
32 import javax.ws.rs.Consumes;
33 import javax.ws.rs.DELETE;
34 import javax.ws.rs.GET;
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.core.GenericEntity;
41 import javax.ws.rs.core.MediaType;
42 import javax.ws.rs.core.Response;
43 import javax.ws.rs.core.Response.Status;
45 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
46 import org.onap.dmaap.dbcapi.model.ApiError;
47 import org.onap.dmaap.dbcapi.model.DR_Pub;
48 import org.onap.dmaap.dbcapi.model.Feed;
49 import org.onap.dmaap.dbcapi.service.DR_PubService;
50 import org.onap.dmaap.dbcapi.service.FeedService;
54 @Api( value= "dr_pubs", description = "Endpoint for a Data Router client that implements a Publisher" )
55 @Consumes(MediaType.APPLICATION_JSON)
56 @Produces(MediaType.APPLICATION_JSON)
58 public class DR_PubResource extends BaseLoggingClass {
60 private DR_PubService dr_pubService = new DR_PubService();
61 private ResponseBuilder responseBuilder = new ResponseBuilder();
62 private RequiredChecker checker = new RequiredChecker();
65 @ApiOperation( value = "return DR_Pub details",
66 notes = "Returns array of `DR_Pub` objects. Add filter for feedId.",
67 response = DR_Pub.class)
68 @ApiResponses( value = {
69 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
70 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
72 public Response getDr_Pubs() {
73 logger.info( "Entry: GET /dr_pubs");
74 List<DR_Pub> pubs = dr_pubService.getAllDr_Pubs();
76 GenericEntity<List<DR_Pub>> list = new GenericEntity<List<DR_Pub>>(pubs) {
78 return responseBuilder.success(list);
82 @ApiOperation( value = "return DR_Pub details",
83 notes = "create a DR Publisher in the specified environment.",
84 response = DR_Pub.class)
85 @ApiResponses( value = {
86 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
87 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
89 public Response addDr_Pub(DR_Pub pub) {
90 ApiError apiError = new ApiError();
91 FeedService feeds = new FeedService();
94 logger.info( "Entry: POST /dr_pubs");
97 checker.required( "feedId", pub.getFeedId());
98 } catch ( RequiredFieldException rfe ) {
100 checker.required( "feedName", pub.getFeedName());
101 }catch ( RequiredFieldException rfe2 ) {
102 logger.debug( rfe2.getApiError().toString() );
103 return responseBuilder.error(rfe2.getApiError());
105 // if we found a FeedName instead of a FeedId then try to look it up.
106 List<Feed> nfeeds = feeds.getAllFeeds( pub.getFeedName(), pub.getFeedVersion(), "equals");
107 if ( nfeeds.size() != 1 ) {
108 logger.debug( "Attempt to match "+ pub.getFeedName() + " ver="+pub.getFeedVersion() + " matched " + nfeeds.size() );
109 return responseBuilder.error(apiError);
111 fnew = nfeeds.get(0);
114 checker.required( "dcaeLocationName", pub.getDcaeLocationName());
115 } catch ( RequiredFieldException rfe ) {
116 logger.debug( rfe.getApiError().toString() );
117 return responseBuilder.error(rfe.getApiError());
121 // we may have fnew already if located by FeedName
122 if ( fnew == null ) {
123 fnew = feeds.getFeed(pub.getFeedId(), apiError);
125 if ( fnew == null ) {
126 logger.info( "Specified feed " + pub.getFeedId() + " or " + pub.getFeedName() + " not known to Bus Controller");
127 return responseBuilder.error(apiError);
130 ArrayList<DR_Pub> pubs = fnew.getPubs();
131 logger.info( "num existing pubs before = " + pubs.size() );
133 logger.info( "update feed");
135 if ( pub.getUsername() == null ) {
136 pub.setRandomUserName();
138 if ( pub.getUserpwd() == null ) {
139 pub.setRandomPassword();
143 fnew = feeds.updateFeed(fnew, apiError);
145 if (!apiError.is2xx()) {
146 return responseBuilder.error(apiError);
148 pubs = fnew.getPubs();
149 logger.info( "num existing pubs after = " + pubs.size() );
151 DR_Pub pnew = dr_pubService.getDr_Pub(pub.getPubId(), apiError);
152 return responseBuilder.success(Status.CREATED.getStatusCode(), pnew);
156 @ApiOperation( value = "return DR_Pub details",
157 notes = "update a DR Publisher in the specified environment. Update a `DR_Pub` object by pubId",
158 response = DR_Pub.class)
159 @ApiResponses( value = {
160 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
161 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
164 public Response updateDr_Pub(@PathParam("pubId") String name, DR_Pub pub) {
165 logger.info( "Entry: PUT /dr_pubs");
167 DR_Pub res = dr_pubService.updateDr_Pub(pub);
168 return responseBuilder.success(res);
172 @ApiOperation( value = "return DR_Pub details",
173 notes = "delete a DR Publisher in the specified environment. Delete a `DR_Pub` object by pubId",
174 response = DR_Pub.class)
175 @ApiResponses( value = {
176 @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
177 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
180 public Response deleteDr_Pub(@PathParam("pubId") String id){
182 ApiError apiError = new ApiError();
185 checker.required( "pubId", id);
186 } catch ( RequiredFieldException rfe ) {
187 return responseBuilder.error(rfe.getApiError());
190 DR_Pub pub = dr_pubService.getDr_Pub(id, apiError);
191 if ( !apiError.is2xx()) {
192 return responseBuilder.error(apiError);
194 FeedService feeds = new FeedService();
195 Feed fnew = feeds.getFeed(pub.getFeedId(), apiError);
196 if ( fnew == null ) {
197 logger.info( "Specified feed " + pub.getFeedId() + " not known to Bus Controller");
198 return responseBuilder.error(apiError);
200 ArrayList<DR_Pub> pubs = fnew.getPubs();
201 if ( pubs.size() == 1 ) {
202 apiError.setCode(Status.BAD_REQUEST.getStatusCode());
203 apiError.setMessage( "Can't delete the last publisher of a feed");
204 return responseBuilder.error(apiError);
207 for( Iterator<DR_Pub> i = pubs.iterator(); i.hasNext(); ) {
208 DR_Pub listItem = i.next();
209 if ( listItem.getPubId().equals(id)) {
214 fnew = feeds.updateFeed(fnew,apiError);
215 if (!apiError.is2xx()) {
216 return responseBuilder.error(apiError);
219 dr_pubService.removeDr_Pub(id, apiError);
220 if (!apiError.is2xx()) {
221 return responseBuilder.error(apiError);
223 return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null);
227 @ApiOperation( value = "return DR_Pub details",
228 notes = "returns a DR Publisher in the specified environment. Gets a `DR_Pub` object by pubId",
229 response = DR_Pub.class)
230 @ApiResponses( value = {
231 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
232 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
235 public Response get(@PathParam("pubId") String id) {
236 ApiError apiError = new ApiError();
239 checker.required( "feedId", id);
240 } catch ( RequiredFieldException rfe ) {
241 return responseBuilder.error(rfe.getApiError());
244 DR_Pub pub = dr_pubService.getDr_Pub(id, apiError);
245 if (!apiError.is2xx()) {
246 return responseBuilder.error(apiError);
248 return responseBuilder.success(Status.OK.getStatusCode(), pub);