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();
95 FeedService feeds = new FeedService();
98 resp.required( "feedId", sub.getFeedId(), "");
99 } catch ( RequiredFieldException rfe ) {
101 resp.required( "feedName", sub.getFeedName(), "");
102 }catch ( RequiredFieldException rfe2 ) {
103 logger.debug( resp.toString() );
106 // if we found a FeedName instead of a FeedId then try to look it up.
107 List<Feed> nfeeds = feeds.getAllFeeds( sub.getFeedName(), sub.getFeedVersion(), "equals");
108 if ( nfeeds.size() != 1 ) {
109 logger.debug( "Attempt to match "+ sub.getFeedName() + " ver="+sub.getFeedVersion() + " matched " + nfeeds.size() );
112 fnew = nfeeds.get(0);
116 resp.required( "dcaeLocationName", sub.getDcaeLocationName(), "");
117 } catch ( RequiredFieldException rfe ) {
118 logger.debug( resp.toString() );
121 // we may have fnew already if located by FeedName
122 if ( fnew == null ) {
123 fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
125 if ( fnew == null ) {
126 logger.warn( "Specified feed " + sub.getFeedId() + " or " + sub.getFeedName() + " not known to Bus Controller");
127 resp.setCode(Status.NOT_FOUND.getStatusCode());
130 DR_SubService dr_subService = new DR_SubService( fnew.getSubscribeURL());
131 ArrayList<DR_Sub> subs = fnew.getSubs();
132 logger.info( "num existing subs before = " + subs.size() );
133 DR_Sub snew = dr_subService.addDr_Sub(sub, resp.getErr() );
134 if ( ! resp.getErr().is2xx() ) {
138 logger.info( "num existing subs after = " + subs.size() );
141 logger.info( "update feed");
142 return resp.success(Status.CREATED.getStatusCode(), snew);
147 @ApiOperation( value = "return DR_Sub details",
148 notes = "Update a `DR_Sub` object, selected by subId",
149 response = DR_Sub.class)
150 @ApiResponses( value = {
151 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
152 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
155 public Response updateDr_Sub(
156 @PathParam("subId") String name,
160 ApiService resp = new ApiService();
163 resp.required( "subId", name, "");
164 resp.required( "feedId", sub.getFeedId(), "");
165 resp.required( "dcaeLocationName", sub.getDcaeLocationName(), "");
167 } catch ( RequiredFieldException rfe ) {
168 logger.debug( resp.toString() );
171 FeedService feeds = new FeedService();
172 Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
173 if ( fnew == null ) {
174 logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
178 DR_SubService dr_subService = new DR_SubService();
180 DR_Sub nsub = dr_subService.updateDr_Sub(sub, resp.getErr() );
181 if ( nsub != null && nsub.isStatusValid() ) {
182 return resp.success(nsub);
188 @ApiOperation( value = "return DR_Sub details",
189 notes = "Delete a `DR_Sub` object, selected by subId",
190 response = DR_Sub.class)
191 @ApiResponses( value = {
192 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
193 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
196 public Response deleteDr_Sub(
197 @PathParam("subId") String id
200 ApiService resp = new ApiService();
203 resp.required( "subId", id, "");
204 } catch ( RequiredFieldException rfe ) {
205 logger.debug( resp.toString() );
208 DR_SubService dr_subService = new DR_SubService();
209 dr_subService.removeDr_Sub(id, resp.getErr() );
210 if ( ! resp.getErr().is2xx() ) {
213 return resp.success(Status.NO_CONTENT.getStatusCode(), null );
217 @ApiOperation( value = "return DR_Sub details",
218 notes = "Retrieve a `DR_Sub` object, selected by subId",
219 response = DR_Sub.class)
220 @ApiResponses( value = {
221 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
222 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
226 @PathParam("subId") String id
228 ApiService resp = new ApiService();
231 resp.required( "subId", id, "");
232 } catch ( RequiredFieldException rfe ) {
233 logger.debug( resp.toString() );
236 DR_SubService dr_subService = new DR_SubService();
237 DR_Sub sub = dr_subService.getDr_Sub( id, resp.getErr() );
238 if ( sub != null && sub.isStatusValid() ) {
239 return resp.success(sub);