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);
83 @ApiOperation( value = "return DR_Pub details",
84 notes = "create a DR Publisher in the specified environment.",
85 response = DR_Pub.class)
86 @ApiResponses( value = {
87 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
88 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
90 public Response addDr_Pub(DR_Pub pub) {
91 ApiError apiError = new ApiError();
92 FeedService feeds = new FeedService();
95 logger.info( "Entry: POST /dr_pubs");
98 checker.required( "feedId", pub.getFeedId());
99 } catch ( RequiredFieldException rfe ) {
101 checker.required( "feedName", pub.getFeedName());
102 }catch ( RequiredFieldException rfe2 ) {
103 logger.debug( rfe2.getApiError().toString() );
104 return responseBuilder.error(rfe2.getApiError());
106 // if we found a FeedName instead of a FeedId then try to look it up.
107 List<Feed> nfeeds = feeds.getAllFeeds( pub.getFeedName(), pub.getFeedVersion(), "equals");
108 if ( nfeeds.isEmpty() ) {
109 apiError.setCode(Status.NOT_FOUND.getStatusCode());
110 apiError.setFields("feedName");
111 return responseBuilder.error(apiError);
113 fnew = nfeeds.get(0);
116 checker.required( "dcaeLocationName", pub.getDcaeLocationName());
117 } catch ( RequiredFieldException rfe ) {
118 logger.debug( rfe.getApiError().toString() );
119 return responseBuilder.error(rfe.getApiError());
123 // we may have fnew already if located by FeedName
124 if ( fnew == null ) {
125 fnew = feeds.getFeed(pub.getFeedId(), apiError);
127 if ( fnew == null ) {
128 logger.info( "Specified feed " + pub.getFeedId() + " or " + pub.getFeedName() + " not known to Bus Controller");
129 return responseBuilder.error(apiError);
132 ArrayList<DR_Pub> pubs = fnew.getPubs();
133 logger.info( "num existing pubs before = " + pubs.size() );
135 logger.info( "update feed");
137 if ( pub.getUsername() == null ) {
138 pub.setRandomUserName();
140 if ( pub.getUserpwd() == null ) {
141 pub.setRandomPassword();
145 fnew = feeds.updateFeed(fnew, apiError);
147 if (!apiError.is2xx()) {
148 return responseBuilder.error(apiError);
150 pubs = fnew.getPubs();
151 logger.info( "num existing pubs after = " + pubs.size() );
153 DR_Pub pnew = dr_pubService.getDr_Pub(pub.getPubId(), apiError);
154 return responseBuilder.success(Status.CREATED.getStatusCode(), pnew);
158 @ApiOperation( value = "return DR_Pub details",
159 notes = "update a DR Publisher in the specified environment. Update a `DR_Pub` object by pubId",
160 response = DR_Pub.class)
161 @ApiResponses( value = {
162 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
163 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
166 public Response updateDr_Pub(@PathParam("pubId") String name, DR_Pub pub) {
167 logger.info( "Entry: PUT /dr_pubs");
169 DR_Pub res = dr_pubService.updateDr_Pub(pub);
170 return responseBuilder.success(res);
174 @ApiOperation( value = "return DR_Pub details",
175 notes = "delete a DR Publisher in the specified environment. Delete a `DR_Pub` object by pubId",
176 response = DR_Pub.class)
177 @ApiResponses( value = {
178 @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
179 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
182 public Response deleteDr_Pub(@PathParam("pubId") String id){
184 ApiError apiError = new ApiError();
187 checker.required( "pubId", id);
188 } catch ( RequiredFieldException rfe ) {
189 return responseBuilder.error(rfe.getApiError());
192 DR_Pub pub = dr_pubService.getDr_Pub(id, apiError);
193 if ( !apiError.is2xx()) {
194 return responseBuilder.error(apiError);
196 FeedService feeds = new FeedService();
197 Feed fnew = feeds.getFeed(pub.getFeedId(), apiError);
198 if ( fnew == null ) {
199 logger.info( "Specified feed " + pub.getFeedId() + " not known to Bus Controller");
200 return responseBuilder.error(apiError);
202 ArrayList<DR_Pub> pubs = fnew.getPubs();
203 if ( pubs.size() == 1 ) {
204 apiError.setCode(Status.BAD_REQUEST.getStatusCode());
205 apiError.setMessage( "Can't delete the last publisher of a feed");
206 return responseBuilder.error(apiError);
209 for( Iterator<DR_Pub> i = pubs.iterator(); i.hasNext(); ) {
210 DR_Pub listItem = i.next();
211 if ( listItem.getPubId().equals(id)) {
216 fnew = feeds.updateFeed(fnew,apiError);
217 if (!apiError.is2xx()) {
218 return responseBuilder.error(apiError);
221 dr_pubService.removeDr_Pub(id, apiError);
222 if (!apiError.is2xx()) {
223 return responseBuilder.error(apiError);
225 return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null);
229 @ApiOperation( value = "return DR_Pub details",
230 notes = "returns a DR Publisher in the specified environment. Gets a `DR_Pub` object by pubId",
231 response = DR_Pub.class)
232 @ApiResponses( value = {
233 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
234 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
237 public Response get(@PathParam("pubId") String id) {
238 ApiError apiError = new ApiError();
241 checker.required( "feedId", id);
242 } catch ( RequiredFieldException rfe ) {
243 return responseBuilder.error(rfe.getApiError());
246 DR_Pub pub = dr_pubService.getDr_Pub(id, apiError);
247 if (!apiError.is2xx()) {
248 return responseBuilder.error(apiError);
250 return responseBuilder.success(Status.OK.getStatusCode(), pub);