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 java.util.ArrayList;
24 import java.util.List;
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.DELETE;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.POST;
30 import javax.ws.rs.PUT;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.PathParam;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.core.GenericEntity;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.Response;
37 import javax.ws.rs.core.Response.Status;
39 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
40 import org.onap.dmaap.dbcapi.model.ApiError;
41 import org.onap.dmaap.dbcapi.model.DR_Sub;
42 import org.onap.dmaap.dbcapi.model.Feed;
43 import org.onap.dmaap.dbcapi.service.ApiService;
44 import org.onap.dmaap.dbcapi.service.DR_SubService;
45 import org.onap.dmaap.dbcapi.service.FeedService;
47 import io.swagger.annotations.Api;
48 import io.swagger.annotations.ApiOperation;
49 import io.swagger.annotations.ApiResponse;
50 import io.swagger.annotations.ApiResponses;
54 @Api( value= "dr_subs", description = "Endpoint for a Data Router client that implements a Subscriber" )
55 @Consumes(MediaType.APPLICATION_JSON)
56 @Produces(MediaType.APPLICATION_JSON)
58 public class DR_SubResource extends BaseLoggingClass {
61 @ApiOperation( value = "return DR_Sub details",
62 notes = "Returns array of `DR_Sub` objects. Add filter for feedId.",
63 response = DR_Sub.class)
64 @ApiResponses( value = {
65 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
66 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
68 public Response getDr_Subs() {
70 ApiService resp = new ApiService();
72 DR_SubService dr_subService = new DR_SubService();
73 List<DR_Sub> subs = dr_subService.getAllDr_Subs();
75 GenericEntity<List<DR_Sub>> list = new GenericEntity<List<DR_Sub>>(subs) {
77 return resp.success(list);
81 @ApiOperation( value = "return DR_Sub details",
82 notes = "Create a `DR_Sub` object. ",
83 response = DR_Sub.class)
84 @ApiResponses( value = {
85 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
86 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
88 public Response addDr_Sub(
92 ApiService resp = new ApiService();
95 resp.required( "feedId", sub.getFeedId(), "");
96 resp.required( "dcaeLocationName", sub.getDcaeLocationName(), "");
98 } catch ( RequiredFieldException rfe ) {
99 logger.debug( resp.toString() );
103 FeedService feeds = new FeedService();
104 Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
105 if ( fnew == null ) {
106 logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
107 resp.setCode(Status.NOT_FOUND.getStatusCode());
111 DR_SubService dr_subService = new DR_SubService( fnew.getSubscribeURL());
112 ArrayList<DR_Sub> subs = fnew.getSubs();
113 logger.info( "num existing subs before = " + subs.size() );
114 DR_Sub snew = dr_subService.addDr_Sub(sub, resp.getErr() );
115 if ( ! resp.getErr().is2xx() ) {
119 logger.info( "num existing subs after = " + subs.size() );
122 logger.info( "update feed");
123 //feeds.updateFeed( fnew, err );
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);