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.List;
29 import javax.ws.rs.Consumes;
30 import javax.ws.rs.DELETE;
31 import javax.ws.rs.GET;
32 import javax.ws.rs.POST;
33 import javax.ws.rs.PUT;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.core.GenericEntity;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.Response;
40 import javax.ws.rs.core.Response.Status;
42 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
43 import org.onap.dmaap.dbcapi.model.ApiError;
44 import org.onap.dmaap.dbcapi.model.DR_Sub;
45 import org.onap.dmaap.dbcapi.model.Feed;
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();
65 private RequiredChecker checker = new RequiredChecker();
68 @ApiOperation( value = "return DR_Sub details",
69 notes = "Returns array of `DR_Sub` objects. Add filter for feedId.",
70 response = DR_Sub.class)
71 @ApiResponses( value = {
72 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
73 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
75 public Response getDr_Subs() {
76 DR_SubService dr_subService = new DR_SubService();
77 List<DR_Sub> subs = dr_subService.getAllDr_Subs();
79 GenericEntity<List<DR_Sub>> list = new GenericEntity<List<DR_Sub>>(subs) {
81 return responseBuilder.success(list);
85 @ApiOperation( value = "return DR_Sub details",
86 notes = "Create a `DR_Sub` object. ",
87 response = DR_Sub.class)
88 @ApiResponses( value = {
89 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
90 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
92 public Response addDr_Sub(DR_Sub sub) {
94 ApiError apiError = new ApiError();
95 FeedService feeds = new FeedService();
98 checker.required( "feedId", sub.getFeedId());
99 } catch ( RequiredFieldException rfe ) {
101 checker.required( "feedName", sub.getFeedName());
102 }catch ( RequiredFieldException rfe2 ) {
103 logger.debug( rfe2.getApiError().toString() );
104 return responseBuilder.error(rfe2.getApiError());
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.isEmpty() ) {
109 apiError.setCode(Status.NOT_FOUND.getStatusCode());
110 apiError.setFields("feedName");
111 return responseBuilder.error(apiError);
112 } else if (nfeeds.size() > 1) {
113 logger.debug( "Attempt to match "+ sub.getFeedName() + " ver="+sub.getFeedVersion() + " matched " + nfeeds.size() );
114 apiError.setCode(Status.CONFLICT.getStatusCode());
115 apiError.setFields("feedName");
116 return responseBuilder.error(apiError);
118 fnew = Iterables.getOnlyElement(nfeeds);
122 checker.required( "dcaeLocationName", sub.getDcaeLocationName());
123 } catch ( RequiredFieldException rfe ) {
124 logger.debug( rfe.getApiError().toString() );
125 return responseBuilder.error(rfe.getApiError());
127 // we may have fnew already if located by FeedName
128 if ( fnew == null ) {
129 fnew = feeds.getFeed( sub.getFeedId(), apiError);
131 if ( fnew == null ) {
132 logger.warn( "Specified feed " + sub.getFeedId() + " or " + sub.getFeedName() + " not known to Bus Controller");
133 apiError.setCode(Status.NOT_FOUND.getStatusCode());
134 return responseBuilder.error(apiError);
136 DR_SubService dr_subService = new DR_SubService( fnew.getSubscribeURL());
137 ArrayList<DR_Sub> subs = fnew.getSubs();
138 logger.info( "num existing subs before = " + subs.size() );
139 DR_Sub snew = dr_subService.addDr_Sub(sub, apiError);
140 if (!apiError.is2xx()) {
141 return responseBuilder.error(apiError);
144 logger.info( "num existing subs after = " + subs.size() );
147 logger.info( "update feed");
148 return responseBuilder.success(CREATED.getStatusCode(), snew);
153 @ApiOperation( value = "return DR_Sub details",
154 notes = "Update a `DR_Sub` object, selected by subId",
155 response = DR_Sub.class)
156 @ApiResponses( value = {
157 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
158 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
161 public Response updateDr_Sub(@PathParam("subId") String name, DR_Sub sub) {
163 ApiError apiError = new ApiError();
166 checker.required( "subId", name);
167 checker.required( "feedId", sub.getFeedId());
168 checker.required( "dcaeLocationName", sub.getDcaeLocationName());
170 } catch ( RequiredFieldException rfe ) {
171 logger.debug( rfe.getApiError().toString() );
172 return responseBuilder.error(rfe.getApiError());
174 FeedService feeds = new FeedService();
175 Feed fnew = feeds.getFeed(sub.getFeedId(), apiError);
176 if ( fnew == null ) {
177 logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
178 return responseBuilder.error(apiError);
181 DR_SubService dr_subService = new DR_SubService();
183 DR_Sub nsub = dr_subService.updateDr_Sub(sub, apiError);
184 if ( nsub != null && nsub.isStatusValid() ) {
185 return responseBuilder.success(nsub);
187 return responseBuilder.error(apiError);
191 @ApiOperation( value = "return DR_Sub details",
192 notes = "Delete a `DR_Sub` object, selected by subId",
193 response = DR_Sub.class)
194 @ApiResponses( value = {
195 @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
196 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
199 public Response deleteDr_Sub(@PathParam("subId") String id){
201 ApiError apiError = new ApiError();
204 checker.required( "subId", id);
205 } catch ( RequiredFieldException rfe ) {
206 logger.debug( rfe.getApiError().toString() );
207 return responseBuilder.error(rfe.getApiError());
209 DR_SubService dr_subService = new DR_SubService();
210 dr_subService.removeDr_Sub(id, apiError);
211 if (!apiError.is2xx() ) {
212 return responseBuilder.error(apiError);
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 )
226 public Response get(@PathParam("subId") String id) {
228 ApiError apiError = new ApiError();
231 checker.required( "subId", id);
232 } catch ( RequiredFieldException rfe ) {
233 logger.debug( rfe.getApiError().toString() );
234 return responseBuilder.error(rfe.getApiError());
236 DR_SubService dr_subService = new DR_SubService();
237 DR_Sub sub = dr_subService.getDr_Sub(id, apiError);
238 if ( sub != null && sub.isStatusValid() ) {
239 return responseBuilder.success(sub);
241 return responseBuilder.error(apiError);