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 com.google.common.collect.Iterables;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
30 import javax.ws.rs.Consumes;
31 import javax.ws.rs.DELETE;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.POST;
34 import javax.ws.rs.PUT;
35 import javax.ws.rs.Path;
36 import javax.ws.rs.PathParam;
37 import javax.ws.rs.Produces;
38 import javax.ws.rs.core.GenericEntity;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.Response.Status;
43 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
44 import org.onap.dmaap.dbcapi.model.ApiError;
45 import org.onap.dmaap.dbcapi.model.DR_Sub;
46 import org.onap.dmaap.dbcapi.model.Feed;
47 import org.onap.dmaap.dbcapi.service.ApiService;
48 import org.onap.dmaap.dbcapi.service.DR_SubService;
49 import org.onap.dmaap.dbcapi.service.FeedService;
51 import io.swagger.annotations.Api;
52 import io.swagger.annotations.ApiOperation;
53 import io.swagger.annotations.ApiResponse;
54 import io.swagger.annotations.ApiResponses;
56 import static javax.ws.rs.core.Response.Status.CREATED;
60 @Api( value= "dr_subs", description = "Endpoint for a Data Router client that implements a Subscriber" )
61 @Consumes(MediaType.APPLICATION_JSON)
62 @Produces(MediaType.APPLICATION_JSON)
64 public class DR_SubResource extends BaseLoggingClass {
66 private ResponseBuilder responseBuilder = new ResponseBuilder();
67 private RequiredChecker checker = new RequiredChecker();
70 @ApiOperation( value = "return DR_Sub details",
71 notes = "Returns array of `DR_Sub` objects. Add filter for feedId.",
72 response = DR_Sub.class)
73 @ApiResponses( value = {
74 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
75 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
77 public Response getDr_Subs() {
78 DR_SubService dr_subService = new DR_SubService();
79 List<DR_Sub> subs = dr_subService.getAllDr_Subs();
81 GenericEntity<List<DR_Sub>> list = new GenericEntity<List<DR_Sub>>(subs) {
83 return responseBuilder.success(list);
87 @ApiOperation( value = "return DR_Sub details",
88 notes = "Create a `DR_Sub` object. ",
89 response = DR_Sub.class)
90 @ApiResponses( value = {
91 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
92 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
94 public Response addDr_Sub(
98 ApiService resp = new ApiService();
99 FeedService feeds = new FeedService();
102 checker.required( "feedId", sub.getFeedId());
103 } catch ( RequiredFieldException rfe ) {
105 checker.required( "feedName", sub.getFeedName());
106 }catch ( RequiredFieldException rfe2 ) {
107 logger.debug( rfe2.getApiError().toString() );
108 return responseBuilder.error(rfe2.getApiError());
110 // if we found a FeedName instead of a FeedId then try to look it up.
111 List<Feed> nfeeds = feeds.getAllFeeds( sub.getFeedName(), sub.getFeedVersion(), "equals");
112 if ( nfeeds.isEmpty() ) {
113 resp.setCode(Status.NOT_FOUND.getStatusCode());
114 resp.setFields("feedName");
115 return responseBuilder.error(resp.getErr());
116 } else if (nfeeds.size() > 1) {
117 logger.debug( "Attempt to match "+ sub.getFeedName() + " ver="+sub.getFeedVersion() + " matched " + nfeeds.size() );
118 resp.setCode(Status.CONFLICT.getStatusCode());
119 resp.setFields("feedName");
120 return responseBuilder.error(resp.getErr());
122 fnew = Iterables.getOnlyElement(nfeeds);
126 checker.required( "dcaeLocationName", sub.getDcaeLocationName());
127 } catch ( RequiredFieldException rfe ) {
128 logger.debug( rfe.getApiError().toString() );
129 return responseBuilder.error(rfe.getApiError());
131 // we may have fnew already if located by FeedName
132 if ( fnew == null ) {
133 fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
135 if ( fnew == null ) {
136 logger.warn( "Specified feed " + sub.getFeedId() + " or " + sub.getFeedName() + " not known to Bus Controller");
137 resp.setCode(Status.NOT_FOUND.getStatusCode());
138 return responseBuilder.error(resp.getErr());
140 DR_SubService dr_subService = new DR_SubService( fnew.getSubscribeURL());
141 ArrayList<DR_Sub> subs = fnew.getSubs();
142 logger.info( "num existing subs before = " + subs.size() );
143 DR_Sub snew = dr_subService.addDr_Sub(sub, resp.getErr() );
144 if ( ! resp.getErr().is2xx() ) {
145 return responseBuilder.error(resp.getErr());
148 logger.info( "num existing subs after = " + subs.size() );
151 logger.info( "update feed");
152 return responseBuilder.success(CREATED.getStatusCode(), snew);
157 @ApiOperation( value = "return DR_Sub details",
158 notes = "Update a `DR_Sub` object, selected by subId",
159 response = DR_Sub.class)
160 @ApiResponses( value = {
161 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
162 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
165 public Response updateDr_Sub(
166 @PathParam("subId") String name,
170 ApiService resp = new ApiService();
173 checker.required( "subId", name);
174 checker.required( "feedId", sub.getFeedId());
175 checker.required( "dcaeLocationName", sub.getDcaeLocationName());
177 } catch ( RequiredFieldException rfe ) {
178 logger.debug( rfe.getApiError().toString() );
179 return responseBuilder.error(rfe.getApiError());
181 FeedService feeds = new FeedService();
182 Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
183 if ( fnew == null ) {
184 logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
185 return responseBuilder.error(resp.getErr());
188 DR_SubService dr_subService = new DR_SubService();
190 DR_Sub nsub = dr_subService.updateDr_Sub(sub, resp.getErr() );
191 if ( nsub != null && nsub.isStatusValid() ) {
192 return responseBuilder.success(nsub);
194 return responseBuilder.error(resp.getErr());
198 @ApiOperation( value = "return DR_Sub details",
199 notes = "Delete a `DR_Sub` object, selected by subId",
200 response = DR_Sub.class)
201 @ApiResponses( value = {
202 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
203 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
206 public Response deleteDr_Sub(
207 @PathParam("subId") String id
210 ApiService resp = new ApiService();
213 checker.required( "subId", id);
214 } catch ( RequiredFieldException rfe ) {
215 logger.debug( rfe.getApiError().toString() );
216 return responseBuilder.error(rfe.getApiError());
218 DR_SubService dr_subService = new DR_SubService();
219 dr_subService.removeDr_Sub(id, resp.getErr() );
220 if ( ! resp.getErr().is2xx() ) {
221 return responseBuilder.error(resp.getErr());
223 return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null );
227 @ApiOperation( value = "return DR_Sub details",
228 notes = "Retrieve a `DR_Sub` object, selected by subId",
229 response = DR_Sub.class)
230 @ApiResponses( value = {
231 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
232 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
236 @PathParam("subId") String id
238 ApiService resp = new ApiService();
241 checker.required( "subId", id);
242 } catch ( RequiredFieldException rfe ) {
243 logger.debug( rfe.getApiError().toString() );
244 return responseBuilder.error(rfe.getApiError());
246 DR_SubService dr_subService = new DR_SubService();
247 DR_Sub sub = dr_subService.getDr_Sub( id, resp.getErr() );
248 if ( sub != null && sub.isStatusValid() ) {
249 return responseBuilder.success(sub);
251 return responseBuilder.error(resp.getErr());