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_Pub;
42 import org.onap.dmaap.dbcapi.model.DR_Sub;
43 import org.onap.dmaap.dbcapi.model.Feed;
44 import org.onap.dmaap.dbcapi.service.ApiService;
45 import org.onap.dmaap.dbcapi.service.DR_SubService;
46 import org.onap.dmaap.dbcapi.service.FeedService;
48 import io.swagger.annotations.Api;
49 import io.swagger.annotations.ApiOperation;
50 import io.swagger.annotations.ApiResponse;
51 import io.swagger.annotations.ApiResponses;
55 @Api( value= "dr_subs", description = "Endpoint for a Data Router client that implements a Subscriber" )
56 @Consumes(MediaType.APPLICATION_JSON)
57 @Produces(MediaType.APPLICATION_JSON)
59 public class DR_SubResource extends BaseLoggingClass {
62 @ApiOperation( value = "return DR_Sub details",
63 notes = "Returns array of `DR_Sub` objects. Add filter for feedId.",
64 response = DR_Sub.class)
65 @ApiResponses( value = {
66 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
67 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
69 public Response getDr_Subs() {
71 ApiService resp = new ApiService();
73 DR_SubService dr_subService = new DR_SubService();
74 List<DR_Sub> subs = dr_subService.getAllDr_Subs();
76 GenericEntity<List<DR_Sub>> list = new GenericEntity<List<DR_Sub>>(subs) {
78 return resp.success(list);
82 @ApiOperation( value = "return DR_Sub details",
83 notes = "Create a `DR_Sub` object. ",
84 response = DR_Sub.class)
85 @ApiResponses( value = {
86 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
87 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
89 public Response addDr_Sub(
93 ApiService resp = new ApiService();
96 resp.required( "feedId", sub.getFeedId(), "");
97 resp.required( "dcaeLocationName", sub.getDcaeLocationName(), "");
99 } catch ( RequiredFieldException rfe ) {
100 logger.debug( resp.toString() );
104 FeedService feeds = new FeedService();
105 Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
106 if ( fnew == null ) {
107 logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
108 resp.setCode(Status.NOT_FOUND.getStatusCode());
112 DR_SubService dr_subService = new DR_SubService( fnew.getSubscribeURL());
113 ArrayList<DR_Sub> subs = fnew.getSubs();
114 logger.info( "num existing subs before = " + subs.size() );
115 DR_Sub snew = dr_subService.addDr_Sub(sub, resp.getErr() );
116 if ( ! resp.getErr().is2xx() ) {
120 logger.info( "num existing subs after = " + subs.size() );
123 logger.info( "update feed");
124 //feeds.updateFeed( fnew, err );
126 return resp.success(Status.CREATED.getStatusCode(), snew);
131 @ApiOperation( value = "return DR_Sub details",
132 notes = "Update a `DR_Sub` object, selected by subId",
133 response = DR_Sub.class)
134 @ApiResponses( value = {
135 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
136 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
139 public Response updateDr_Sub(
140 @PathParam("subId") String name,
144 ApiService resp = new ApiService();
147 resp.required( "subId", name, "");
148 resp.required( "feedId", sub.getFeedId(), "");
149 resp.required( "dcaeLocationName", sub.getDcaeLocationName(), "");
151 } catch ( RequiredFieldException rfe ) {
152 logger.debug( resp.toString() );
155 FeedService feeds = new FeedService();
156 Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
157 if ( fnew == null ) {
158 logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
162 DR_SubService dr_subService = new DR_SubService();
164 DR_Sub nsub = dr_subService.updateDr_Sub(sub, resp.getErr() );
165 if ( nsub != null && nsub.isStatusValid() ) {
166 return resp.success(nsub);
172 @ApiOperation( value = "return DR_Sub details",
173 notes = "Delete a `DR_Sub` object, selected by subId",
174 response = DR_Sub.class)
175 @ApiResponses( value = {
176 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
177 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
180 public Response deleteDr_Sub(
181 @PathParam("subId") String id
184 ApiService resp = new ApiService();
187 resp.required( "subId", id, "");
188 } catch ( RequiredFieldException rfe ) {
189 logger.debug( resp.toString() );
192 DR_SubService dr_subService = new DR_SubService();
193 dr_subService.removeDr_Sub(id, resp.getErr() );
194 if ( ! resp.getErr().is2xx() ) {
197 return resp.success(Status.NO_CONTENT.getStatusCode(), null );
201 @ApiOperation( value = "return DR_Sub details",
202 notes = "Retrieve a `DR_Sub` object, selected by subId",
203 response = DR_Sub.class)
204 @ApiResponses( value = {
205 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
206 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
210 @PathParam("subId") String id
212 ApiService resp = new ApiService();
215 resp.required( "subId", id, "");
216 } catch ( RequiredFieldException rfe ) {
217 logger.debug( resp.toString() );
220 DR_SubService dr_subService = new DR_SubService();
221 DR_Sub sub = dr_subService.getDr_Sub( id, resp.getErr() );
222 if ( sub != null && sub.isStatusValid() ) {
223 return resp.success(sub);