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;
54 import static javax.ws.rs.core.Response.Status.CREATED;
58 @Api( value= "dr_subs", description = "Endpoint for a Data Router client that implements a Subscriber" )
59 @Consumes(MediaType.APPLICATION_JSON)
60 @Produces(MediaType.APPLICATION_JSON)
62 public class DR_SubResource extends BaseLoggingClass {
64 private ResponseBuilder responseBuilder = new ResponseBuilder();
67 @ApiOperation( value = "return DR_Sub details",
68 notes = "Returns array of `DR_Sub` objects. Add filter for feedId.",
69 response = DR_Sub.class)
70 @ApiResponses( value = {
71 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
72 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
74 public Response getDr_Subs() {
75 DR_SubService dr_subService = new DR_SubService();
76 List<DR_Sub> subs = dr_subService.getAllDr_Subs();
78 GenericEntity<List<DR_Sub>> list = new GenericEntity<List<DR_Sub>>(subs) {
80 return responseBuilder.success(list);
84 @ApiOperation( value = "return DR_Sub details",
85 notes = "Create a `DR_Sub` object. ",
86 response = DR_Sub.class)
87 @ApiResponses( value = {
88 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
89 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
91 public Response addDr_Sub(
95 ApiService resp = new ApiService();
96 FeedService feeds = new FeedService();
99 resp.required( "feedId", sub.getFeedId(), "");
100 } catch ( RequiredFieldException rfe ) {
102 resp.required( "feedName", sub.getFeedName(), "");
103 }catch ( RequiredFieldException rfe2 ) {
104 logger.debug( resp.toString() );
105 return responseBuilder.error(resp.getErr());
107 // if we found a FeedName instead of a FeedId then try to look it up.
108 List<Feed> nfeeds = feeds.getAllFeeds( sub.getFeedName(), sub.getFeedVersion(), "equals");
109 if ( nfeeds.size() != 1 ) {
110 logger.debug( "Attempt to match "+ sub.getFeedName() + " ver="+sub.getFeedVersion() + " matched " + nfeeds.size() );
111 return responseBuilder.error(resp.getErr());
113 fnew = nfeeds.get(0);
117 resp.required( "dcaeLocationName", sub.getDcaeLocationName(), "");
118 } catch ( RequiredFieldException rfe ) {
119 logger.debug( resp.toString() );
120 return responseBuilder.error(resp.getErr());
122 // we may have fnew already if located by FeedName
123 if ( fnew == null ) {
124 fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
126 if ( fnew == null ) {
127 logger.warn( "Specified feed " + sub.getFeedId() + " or " + sub.getFeedName() + " not known to Bus Controller");
128 resp.setCode(Status.NOT_FOUND.getStatusCode());
129 return responseBuilder.error(resp.getErr());
131 DR_SubService dr_subService = new DR_SubService( fnew.getSubscribeURL());
132 ArrayList<DR_Sub> subs = fnew.getSubs();
133 logger.info( "num existing subs before = " + subs.size() );
134 DR_Sub snew = dr_subService.addDr_Sub(sub, resp.getErr() );
135 if ( ! resp.getErr().is2xx() ) {
136 return responseBuilder.error(resp.getErr());
139 logger.info( "num existing subs after = " + subs.size() );
142 logger.info( "update feed");
143 return responseBuilder.success(CREATED.getStatusCode(), snew);
148 @ApiOperation( value = "return DR_Sub details",
149 notes = "Update a `DR_Sub` object, selected by subId",
150 response = DR_Sub.class)
151 @ApiResponses( value = {
152 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
153 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
156 public Response updateDr_Sub(
157 @PathParam("subId") String name,
161 ApiService resp = new ApiService();
164 resp.required( "subId", name, "");
165 resp.required( "feedId", sub.getFeedId(), "");
166 resp.required( "dcaeLocationName", sub.getDcaeLocationName(), "");
168 } catch ( RequiredFieldException rfe ) {
169 logger.debug( resp.toString() );
170 return responseBuilder.error(resp.getErr());
172 FeedService feeds = new FeedService();
173 Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
174 if ( fnew == null ) {
175 logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
176 return responseBuilder.error(resp.getErr());
179 DR_SubService dr_subService = new DR_SubService();
181 DR_Sub nsub = dr_subService.updateDr_Sub(sub, resp.getErr() );
182 if ( nsub != null && nsub.isStatusValid() ) {
183 return responseBuilder.success(nsub);
185 return responseBuilder.error(resp.getErr());
189 @ApiOperation( value = "return DR_Sub details",
190 notes = "Delete a `DR_Sub` object, selected by subId",
191 response = DR_Sub.class)
192 @ApiResponses( value = {
193 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
194 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
197 public Response deleteDr_Sub(
198 @PathParam("subId") String id
201 ApiService resp = new ApiService();
204 resp.required( "subId", id, "");
205 } catch ( RequiredFieldException rfe ) {
206 logger.debug( resp.toString() );
207 return responseBuilder.error(resp.getErr());
209 DR_SubService dr_subService = new DR_SubService();
210 dr_subService.removeDr_Sub(id, resp.getErr() );
211 if ( ! resp.getErr().is2xx() ) {
212 return responseBuilder.error(resp.getErr());
214 return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null );
218 @ApiOperation( value = "return DR_Sub details",
219 notes = "Retrieve a `DR_Sub` object, selected by subId",
220 response = DR_Sub.class)
221 @ApiResponses( value = {
222 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
223 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
227 @PathParam("subId") String id
229 ApiService resp = new ApiService();
232 resp.required( "subId", id, "");
233 } catch ( RequiredFieldException rfe ) {
234 logger.debug( resp.toString() );
235 return responseBuilder.error(resp.getErr());
237 DR_SubService dr_subService = new DR_SubService();
238 DR_Sub sub = dr_subService.getDr_Sub( id, resp.getErr() );
239 if ( sub != null && sub.isStatusValid() ) {
240 return responseBuilder.success(sub);
242 return responseBuilder.error(resp.getErr());