X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdbcapi%2Fresources%2FDR_SubResource.java;h=2fa6ccdd44d0446d75a114125bfdeb71cfc56182;hb=ea44032b89d04201924f7ca1551cb33ed49b947e;hp=b74d5a16160194ab88a9ac1c65358ebc401a32c4;hpb=31cbc922a7971a5f20d5790a1fbbd6b38a49af13;p=dmaap%2Fdbcapi.git diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/DR_SubResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/DR_SubResource.java index b74d5a1..2fa6ccd 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/DR_SubResource.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/DR_SubResource.java @@ -22,6 +22,7 @@ package org.onap.dmaap.dbcapi.resources; +import com.google.common.collect.Iterables; import java.util.ArrayList; import java.util.List; @@ -42,7 +43,6 @@ import org.onap.dmaap.dbcapi.logging.BaseLoggingClass; import org.onap.dmaap.dbcapi.model.ApiError; import org.onap.dmaap.dbcapi.model.DR_Sub; import org.onap.dmaap.dbcapi.model.Feed; -import org.onap.dmaap.dbcapi.service.ApiService; import org.onap.dmaap.dbcapi.service.DR_SubService; import org.onap.dmaap.dbcapi.service.FeedService; @@ -89,11 +89,9 @@ public class DR_SubResource extends BaseLoggingClass { @ApiResponse( code = 200, message = "Success", response = DR_Sub.class), @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) - public Response addDr_Sub( - DR_Sub sub - ) { - - ApiService resp = new ApiService(); + public Response addDr_Sub(DR_Sub sub) { + + ApiError apiError = new ApiError(); FeedService feeds = new FeedService(); Feed fnew = null; try { @@ -107,11 +105,17 @@ public class DR_SubResource extends BaseLoggingClass { } // if we found a FeedName instead of a FeedId then try to look it up. List nfeeds = feeds.getAllFeeds( sub.getFeedName(), sub.getFeedVersion(), "equals"); - if ( nfeeds.size() != 1 ) { + if ( nfeeds.isEmpty() ) { + apiError.setCode(Status.NOT_FOUND.getStatusCode()); + apiError.setFields("feedName"); + return responseBuilder.error(apiError); + } else if (nfeeds.size() > 1) { logger.debug( "Attempt to match "+ sub.getFeedName() + " ver="+sub.getFeedVersion() + " matched " + nfeeds.size() ); - return responseBuilder.error(resp.getErr()); + apiError.setCode(Status.CONFLICT.getStatusCode()); + apiError.setFields("feedName"); + return responseBuilder.error(apiError); } - fnew = nfeeds.get(0); + fnew = Iterables.getOnlyElement(nfeeds); } try { @@ -122,19 +126,19 @@ public class DR_SubResource extends BaseLoggingClass { } // we may have fnew already if located by FeedName if ( fnew == null ) { - fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() ); + fnew = feeds.getFeed( sub.getFeedId(), apiError); } if ( fnew == null ) { logger.warn( "Specified feed " + sub.getFeedId() + " or " + sub.getFeedName() + " not known to Bus Controller"); - resp.setCode(Status.NOT_FOUND.getStatusCode()); - return responseBuilder.error(resp.getErr()); + apiError.setCode(Status.NOT_FOUND.getStatusCode()); + return responseBuilder.error(apiError); } DR_SubService dr_subService = new DR_SubService( fnew.getSubscribeURL()); ArrayList subs = fnew.getSubs(); logger.info( "num existing subs before = " + subs.size() ); - DR_Sub snew = dr_subService.addDr_Sub(sub, resp.getErr() ); - if ( ! resp.getErr().is2xx() ) { - return responseBuilder.error(resp.getErr()); + DR_Sub snew = dr_subService.addDr_Sub(sub, apiError); + if (!apiError.is2xx()) { + return responseBuilder.error(apiError); } subs.add( snew ); logger.info( "num existing subs after = " + subs.size() ); @@ -154,12 +158,9 @@ public class DR_SubResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{subId}") - public Response updateDr_Sub( - @PathParam("subId") String name, - DR_Sub sub - ) { + public Response updateDr_Sub(@PathParam("subId") String name, DR_Sub sub) { - ApiService resp = new ApiService(); + ApiError apiError = new ApiError(); try { checker.required( "subId", name); @@ -171,19 +172,19 @@ public class DR_SubResource extends BaseLoggingClass { return responseBuilder.error(rfe.getApiError()); } FeedService feeds = new FeedService(); - Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() ); + Feed fnew = feeds.getFeed(sub.getFeedId(), apiError); if ( fnew == null ) { logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller"); - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } DR_SubService dr_subService = new DR_SubService(); sub.setSubId(name); - DR_Sub nsub = dr_subService.updateDr_Sub(sub, resp.getErr() ); + DR_Sub nsub = dr_subService.updateDr_Sub(sub, apiError); if ( nsub != null && nsub.isStatusValid() ) { return responseBuilder.success(nsub); } - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } @DELETE @@ -195,11 +196,9 @@ public class DR_SubResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{subId}") - public Response deleteDr_Sub( - @PathParam("subId") String id - ){ + public Response deleteDr_Sub(@PathParam("subId") String id){ - ApiService resp = new ApiService(); + ApiError apiError = new ApiError(); try { checker.required( "subId", id); @@ -208,9 +207,9 @@ public class DR_SubResource extends BaseLoggingClass { return responseBuilder.error(rfe.getApiError()); } DR_SubService dr_subService = new DR_SubService(); - dr_subService.removeDr_Sub(id, resp.getErr() ); - if ( ! resp.getErr().is2xx() ) { - return responseBuilder.error(resp.getErr()); + dr_subService.removeDr_Sub(id, apiError); + if (!apiError.is2xx() ) { + return responseBuilder.error(apiError); } return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null ); } @@ -224,10 +223,9 @@ public class DR_SubResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{subId}") - public Response get( - @PathParam("subId") String id - ) { - ApiService resp = new ApiService(); + public Response get(@PathParam("subId") String id) { + + ApiError apiError = new ApiError(); try { checker.required( "subId", id); @@ -236,10 +234,10 @@ public class DR_SubResource extends BaseLoggingClass { return responseBuilder.error(rfe.getApiError()); } DR_SubService dr_subService = new DR_SubService(); - DR_Sub sub = dr_subService.getDr_Sub( id, resp.getErr() ); + DR_Sub sub = dr_subService.getDr_Sub(id, apiError); if ( sub != null && sub.isStatusValid() ) { return responseBuilder.success(sub); } - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } }