2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright (C) 2019 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.dmaap.dbcapi.resources;
25 import java.util.ArrayList;
26 import java.util.List;
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.PUT;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.PathParam;
35 import javax.ws.rs.Produces;
36 import javax.ws.rs.core.GenericEntity;
37 import javax.ws.rs.core.MediaType;
38 import javax.ws.rs.core.Response;
39 import javax.ws.rs.core.Response.Status;
41 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
42 import org.onap.dmaap.dbcapi.model.ApiError;
43 import org.onap.dmaap.dbcapi.model.DR_Sub;
44 import org.onap.dmaap.dbcapi.model.Feed;
45 import org.onap.dmaap.dbcapi.service.ApiService;
46 import org.onap.dmaap.dbcapi.service.DR_SubService;
47 import org.onap.dmaap.dbcapi.service.FeedService;
49 import io.swagger.annotations.Api;
50 import io.swagger.annotations.ApiOperation;
51 import io.swagger.annotations.ApiResponse;
52 import io.swagger.annotations.ApiResponses;
56 @Api( value= "dr_subs", description = "Endpoint for a Data Router client that implements a Subscriber" )
57 @Consumes(MediaType.APPLICATION_JSON)
58 @Produces(MediaType.APPLICATION_JSON)
60 public class DR_SubResource extends BaseLoggingClass {
63 @ApiOperation( value = "return DR_Sub details",
64 notes = "Returns array of `DR_Sub` objects. Add filter for feedId.",
65 response = DR_Sub.class)
66 @ApiResponses( value = {
67 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
68 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
70 public Response getDr_Subs() {
72 ApiService resp = new ApiService();
74 DR_SubService dr_subService = new DR_SubService();
75 List<DR_Sub> subs = dr_subService.getAllDr_Subs();
77 GenericEntity<List<DR_Sub>> list = new GenericEntity<List<DR_Sub>>(subs) {
79 return resp.success(list);
83 @ApiOperation( value = "return DR_Sub details",
84 notes = "Create a `DR_Sub` object. ",
85 response = DR_Sub.class)
86 @ApiResponses( value = {
87 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
88 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
90 public Response addDr_Sub(
94 ApiService resp = new ApiService();
97 resp.required( "feedId", sub.getFeedId(), "");
98 resp.required( "dcaeLocationName", sub.getDcaeLocationName(), "");
100 } catch ( RequiredFieldException rfe ) {
101 logger.debug( resp.toString() );
105 FeedService feeds = new FeedService();
106 Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
107 if ( fnew == null ) {
108 logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
109 resp.setCode(Status.NOT_FOUND.getStatusCode());
113 DR_SubService dr_subService = new DR_SubService( fnew.getSubscribeURL());
114 ArrayList<DR_Sub> subs = fnew.getSubs();
115 logger.info( "num existing subs before = " + subs.size() );
116 DR_Sub snew = dr_subService.addDr_Sub(sub, resp.getErr() );
117 if ( ! resp.getErr().is2xx() ) {
121 logger.info( "num existing subs after = " + subs.size() );
124 logger.info( "update feed");
125 return resp.success(Status.CREATED.getStatusCode(), snew);
130 @ApiOperation( value = "return DR_Sub details",
131 notes = "Update a `DR_Sub` object, selected by subId",
132 response = DR_Sub.class)
133 @ApiResponses( value = {
134 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
135 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
138 public Response updateDr_Sub(
139 @PathParam("subId") String name,
143 ApiService resp = new ApiService();
146 resp.required( "subId", name, "");
147 resp.required( "feedId", sub.getFeedId(), "");
148 resp.required( "dcaeLocationName", sub.getDcaeLocationName(), "");
150 } catch ( RequiredFieldException rfe ) {
151 logger.debug( resp.toString() );
154 FeedService feeds = new FeedService();
155 Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
156 if ( fnew == null ) {
157 logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
161 DR_SubService dr_subService = new DR_SubService();
163 DR_Sub nsub = dr_subService.updateDr_Sub(sub, resp.getErr() );
164 if ( nsub != null && nsub.isStatusValid() ) {
165 return resp.success(nsub);
171 @ApiOperation( value = "return DR_Sub details",
172 notes = "Delete a `DR_Sub` object, selected by subId",
173 response = DR_Sub.class)
174 @ApiResponses( value = {
175 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
176 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
179 public Response deleteDr_Sub(
180 @PathParam("subId") String id
183 ApiService resp = new ApiService();
186 resp.required( "subId", id, "");
187 } catch ( RequiredFieldException rfe ) {
188 logger.debug( resp.toString() );
191 DR_SubService dr_subService = new DR_SubService();
192 dr_subService.removeDr_Sub(id, resp.getErr() );
193 if ( ! resp.getErr().is2xx() ) {
196 return resp.success(Status.NO_CONTENT.getStatusCode(), null );
200 @ApiOperation( value = "return DR_Sub details",
201 notes = "Retrieve a `DR_Sub` object, selected by subId",
202 response = DR_Sub.class)
203 @ApiResponses( value = {
204 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
205 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
209 @PathParam("subId") String id
211 ApiService resp = new ApiService();
214 resp.required( "subId", id, "");
215 } catch ( RequiredFieldException rfe ) {
216 logger.debug( resp.toString() );
219 DR_SubService dr_subService = new DR_SubService();
220 DR_Sub sub = dr_subService.getDr_Sub( id, resp.getErr() );
221 if ( sub != null && sub.isStatusValid() ) {
222 return resp.success(sub);