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.ApiService;
50 import org.onap.dmaap.dbcapi.service.DR_PubService;
51 import org.onap.dmaap.dbcapi.service.FeedService;
55 @Api( value= "dr_pubs", description = "Endpoint for a Data Router client that implements a Publisher" )
56 @Consumes(MediaType.APPLICATION_JSON)
57 @Produces(MediaType.APPLICATION_JSON)
59 public class DR_PubResource extends BaseLoggingClass {
61 DR_PubService dr_pubService = new DR_PubService();
64 @ApiOperation( value = "return DR_Pub details",
65 notes = "Returns array of `DR_Pub` objects. Add filter for feedId.",
66 response = DR_Pub.class)
67 @ApiResponses( value = {
68 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
69 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
71 public Response getDr_Pubs() {
72 ApiService resp = new ApiService();
74 logger.info( "Entry: GET /dr_pubs");
75 List<DR_Pub> pubs = dr_pubService.getAllDr_Pubs();
77 GenericEntity<List<DR_Pub>> list = new GenericEntity<List<DR_Pub>>(pubs) {
79 return resp.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(
93 ApiService resp = new ApiService();
95 logger.info( "Entry: POST /dr_pubs");
98 resp.required( "feedId", pub.getFeedId(), "");
99 resp.required( "dcaeLocationName", pub.getDcaeLocationName(), "");
100 } catch ( RequiredFieldException rfe ) {
101 logger.debug( resp.getErr().toString() );
105 FeedService feeds = new FeedService();
106 Feed fnew = feeds.getFeed( pub.getFeedId(), resp.getErr() );
107 if ( fnew == null ) {
108 logger.info( "Specified feed " + pub.getFeedId() + " not known to Bus Controller");
112 ArrayList<DR_Pub> pubs = fnew.getPubs();
113 logger.info( "num existing pubs before = " + pubs.size() );
115 logger.info( "update feed");
117 if ( pub.getUsername() == null ) {
118 pub.setRandomUserName();
120 if ( pub.getUserpwd() == null ) {
121 pub.setRandomPassword();
125 fnew = feeds.updateFeed( fnew, resp.getErr() );
127 if ( ! resp.getErr().is2xx()) {
130 pubs = fnew.getPubs();
131 logger.info( "num existing pubs after = " + pubs.size() );
133 DR_Pub pnew = dr_pubService.getDr_Pub(pub.getPubId(), resp.getErr());
134 return resp.success(Status.CREATED.getStatusCode(), pnew);
138 @ApiOperation( value = "return DR_Pub details",
139 notes = "update a DR Publisher in the specified environment. Update a `DR_Pub` object by pubId",
140 response = DR_Pub.class)
141 @ApiResponses( value = {
142 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
143 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
146 public Response updateDr_Pub(
147 @PathParam("pubId") String name,
150 ApiService resp = new ApiService();
152 logger.info( "Entry: PUT /dr_pubs");
154 DR_Pub res = dr_pubService.updateDr_Pub(pub);
155 return resp.success(res);
159 @ApiOperation( value = "return DR_Pub details",
160 notes = "delete a DR Publisher in the specified environment. Delete a `DR_Pub` object by pubId",
161 response = DR_Pub.class)
162 @ApiResponses( value = {
163 @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
164 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
167 public Response deleteDr_Pub(
168 @PathParam("pubId") String id
171 ApiService resp = new ApiService();
174 resp.required( "pubId", id, "");
175 } catch ( RequiredFieldException rfe ) {
179 DR_Pub pub = dr_pubService.getDr_Pub( id, resp.getErr() );
180 if ( ! resp.getErr().is2xx()) {
183 FeedService feeds = new FeedService();
184 Feed fnew = feeds.getFeed( pub.getFeedId(), resp.getErr() );
185 if ( fnew == null ) {
186 logger.info( "Specified feed " + pub.getFeedId() + " not known to Bus Controller");
189 ArrayList<DR_Pub> pubs = fnew.getPubs();
190 if ( pubs.size() == 1 ) {
191 resp.setCode(Status.BAD_REQUEST.getStatusCode());
192 resp.setMessage( "Can't delete the last publisher of a feed");
196 for( Iterator<DR_Pub> i = pubs.iterator(); i.hasNext(); ) {
197 DR_Pub listItem = i.next();
198 if ( listItem.getPubId().equals(id)) {
203 fnew = feeds.updateFeed( fnew, resp.getErr() );
204 if ( ! resp.getErr().is2xx()) {
208 dr_pubService.removeDr_Pub(id, resp.getErr() );
209 if ( ! resp.getErr().is2xx()) {
212 return resp.success(Status.NO_CONTENT.getStatusCode(), null);
216 @ApiOperation( value = "return DR_Pub details",
217 notes = "returns a DR Publisher in the specified environment. Gets a `DR_Pub` object by pubId",
218 response = DR_Pub.class)
219 @ApiResponses( value = {
220 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
221 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
225 @PathParam("pubId") String id
227 ApiService resp = new ApiService();
230 resp.required( "feedId", id, "");
231 } catch ( RequiredFieldException rfe ) {
235 DR_Pub pub = dr_pubService.getDr_Pub( id, resp.getErr() );
236 if ( ! resp.getErr().is2xx()) {
239 return resp.success(Status.OK.getStatusCode(), pub);