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.isEmpty() ) {
108 apiError.setCode(Status.NOT_FOUND.getStatusCode());
109 apiError.setFields("feedName");
110 return responseBuilder.error(apiError);
112 fnew = nfeeds.get(0);
115 checker.required( "dcaeLocationName", pub.getDcaeLocationName());
116 } catch ( RequiredFieldException rfe ) {
117 logger.debug( rfe.getApiError().toString() );
118 return responseBuilder.error(rfe.getApiError());
122 // we may have fnew already if located by FeedName
123 if ( fnew == null ) {
124 fnew = feeds.getFeed(pub.getFeedId(), apiError);
126 if ( fnew == null ) {
127 logger.info( "Specified feed " + pub.getFeedId() + " or " + pub.getFeedName() + " not known to Bus Controller");
128 return responseBuilder.error(apiError);
131 ArrayList<DR_Pub> pubs = fnew.getPubs();
132 logger.info( "num existing pubs before = " + pubs.size() );
134 logger.info( "update feed");
136 if ( pub.getUsername() == null ) {
137 pub.setRandomUserName();
139 if ( pub.getUserpwd() == null ) {
140 pub.setRandomPassword();
144 fnew = feeds.updateFeed(fnew, apiError);
146 if (!apiError.is2xx()) {
147 return responseBuilder.error(apiError);
149 pubs = fnew.getPubs();
150 logger.info( "num existing pubs after = " + pubs.size() );
152 DR_Pub pnew = dr_pubService.getDr_Pub(pub.getPubId(), apiError);
153 return responseBuilder.success(Status.CREATED.getStatusCode(), pnew);
157 @ApiOperation( value = "return DR_Pub details",
158 notes = "update a DR Publisher in the specified environment. Update a `DR_Pub` object by pubId",
159 response = DR_Pub.class)
160 @ApiResponses( value = {
161 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
162 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
165 public Response updateDr_Pub(@PathParam("pubId") String name, DR_Pub pub) {
166 logger.info( "Entry: PUT /dr_pubs");
168 DR_Pub res = dr_pubService.updateDr_Pub(pub);
169 return responseBuilder.success(res);
173 @ApiOperation( value = "return DR_Pub details",
174 notes = "delete a DR Publisher in the specified environment. Delete a `DR_Pub` object by pubId",
175 response = DR_Pub.class)
176 @ApiResponses( value = {
177 @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
178 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
181 public Response deleteDr_Pub(@PathParam("pubId") String id){
183 ApiError apiError = new ApiError();
186 checker.required( "pubId", id);
187 } catch ( RequiredFieldException rfe ) {
188 return responseBuilder.error(rfe.getApiError());
191 DR_Pub pub = dr_pubService.getDr_Pub(id, apiError);
192 if ( !apiError.is2xx()) {
193 return responseBuilder.error(apiError);
195 FeedService feeds = new FeedService();
196 Feed fnew = feeds.getFeed(pub.getFeedId(), apiError);
197 if ( fnew == null ) {
198 logger.info( "Specified feed " + pub.getFeedId() + " not known to Bus Controller");
199 return responseBuilder.error(apiError);
201 ArrayList<DR_Pub> pubs = fnew.getPubs();
202 if ( pubs.size() == 1 ) {
203 apiError.setCode(Status.BAD_REQUEST.getStatusCode());
204 apiError.setMessage( "Can't delete the last publisher of a feed");
205 return responseBuilder.error(apiError);
208 for( Iterator<DR_Pub> i = pubs.iterator(); i.hasNext(); ) {
209 DR_Pub listItem = i.next();
210 if ( listItem.getPubId().equals(id)) {
215 fnew = feeds.updateFeed(fnew,apiError);
216 if (!apiError.is2xx()) {
217 return responseBuilder.error(apiError);
220 dr_pubService.removeDr_Pub(id, apiError);
221 if (!apiError.is2xx()) {
222 return responseBuilder.error(apiError);
224 return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null);
228 @ApiOperation( value = "return DR_Pub details",
229 notes = "returns a DR Publisher in the specified environment. Gets a `DR_Pub` object by pubId",
230 response = DR_Pub.class)
231 @ApiResponses( value = {
232 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
233 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
236 public Response get(@PathParam("pubId") String id) {
237 ApiError apiError = new ApiError();
240 checker.required( "feedId", id);
241 } catch ( RequiredFieldException rfe ) {
242 return responseBuilder.error(rfe.getApiError());
245 DR_Pub pub = dr_pubService.getDr_Pub(id, apiError);
246 if (!apiError.is2xx()) {
247 return responseBuilder.error(apiError);
249 return responseBuilder.success(Status.OK.getStatusCode(), pub);