* org.onap.dmaap
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ *
+ * Modifications Copyright (C) 2019 IBM.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.onap.dmaap.dbcapi.resources;
+import com.google.common.collect.Iterables;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import javax.ws.rs.Consumes;
import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
import org.onap.dmaap.dbcapi.model.ApiError;
-import org.onap.dmaap.dbcapi.model.DR_Pub;
import org.onap.dmaap.dbcapi.model.DR_Sub;
import org.onap.dmaap.dbcapi.model.Feed;
import org.onap.dmaap.dbcapi.service.ApiService;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
+import static javax.ws.rs.core.Response.Status.CREATED;
+
@Path("/dr_subs")
@Api( value= "dr_subs", description = "Endpoint for a Data Router client that implements a Subscriber" )
@Produces(MediaType.APPLICATION_JSON)
@Authorization
public class DR_SubResource extends BaseLoggingClass {
+
+ private ResponseBuilder responseBuilder = new ResponseBuilder();
+ private RequiredChecker checker = new RequiredChecker();
@GET
@ApiOperation( value = "return DR_Sub details",
notes = "Returns array of `DR_Sub` objects. Add filter for feedId.",
response = DR_Sub.class)
@ApiResponses( value = {
- @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
+ @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
@ApiResponse( code = 400, message = "Error", response = ApiError.class )
})
public Response getDr_Subs() {
-
- ApiService resp = new ApiService();
-
DR_SubService dr_subService = new DR_SubService();
List<DR_Sub> subs = dr_subService.getAllDr_Subs();
GenericEntity<List<DR_Sub>> list = new GenericEntity<List<DR_Sub>>(subs) {
};
- return resp.success(list);
+ return responseBuilder.success(list);
}
@POST
notes = "Create a `DR_Sub` object. ",
response = DR_Sub.class)
@ApiResponses( value = {
- @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
+ @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
@ApiResponse( code = 400, message = "Error", response = ApiError.class )
})
public Response addDr_Sub(
) {
ApiService resp = new ApiService();
-
+ FeedService feeds = new FeedService();
+ Feed fnew = null;
try {
- resp.required( "feedId", sub.getFeedId(), "");
- resp.required( "dcaeLocationName", sub.getDcaeLocationName(), "");
-
+ checker.required( "feedId", sub.getFeedId());
} catch ( RequiredFieldException rfe ) {
- logger.debug( resp.toString() );
- return resp.error();
+ try {
+ checker.required( "feedName", sub.getFeedName());
+ }catch ( RequiredFieldException rfe2 ) {
+ logger.debug( rfe2.getApiError().toString() );
+ return responseBuilder.error(rfe2.getApiError());
+ }
+ // if we found a FeedName instead of a FeedId then try to look it up.
+ List<Feed> nfeeds = feeds.getAllFeeds( sub.getFeedName(), sub.getFeedVersion(), "equals");
+ if ( nfeeds.isEmpty() ) {
+ resp.setCode(Status.NOT_FOUND.getStatusCode());
+ resp.setFields("feedName");
+ return responseBuilder.error(resp.getErr());
+ } else if (nfeeds.size() > 1) {
+ logger.debug( "Attempt to match "+ sub.getFeedName() + " ver="+sub.getFeedVersion() + " matched " + nfeeds.size() );
+ resp.setCode(Status.CONFLICT.getStatusCode());
+ resp.setFields("feedName");
+ return responseBuilder.error(resp.getErr());
+ }
+ fnew = Iterables.getOnlyElement(nfeeds);
}
-
- FeedService feeds = new FeedService();
- Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
+
+ try {
+ checker.required( "dcaeLocationName", sub.getDcaeLocationName());
+ } catch ( RequiredFieldException rfe ) {
+ logger.debug( rfe.getApiError().toString() );
+ return responseBuilder.error(rfe.getApiError());
+ }
+ // we may have fnew already if located by FeedName
if ( fnew == null ) {
- logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
+ fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
+ }
+ if ( fnew == null ) {
+ logger.warn( "Specified feed " + sub.getFeedId() + " or " + sub.getFeedName() + " not known to Bus Controller");
resp.setCode(Status.NOT_FOUND.getStatusCode());
- return resp.error();
+ return responseBuilder.error(resp.getErr());
}
-
DR_SubService dr_subService = new DR_SubService( fnew.getSubscribeURL());
ArrayList<DR_Sub> 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 resp.error();
+ return responseBuilder.error(resp.getErr());
}
subs.add( snew );
logger.info( "num existing subs after = " + subs.size() );
fnew.setSubs(subs);
logger.info( "update feed");
- //feeds.updateFeed( fnew, err );
-
- return resp.success(Status.CREATED.getStatusCode(), snew);
+ return responseBuilder.success(CREATED.getStatusCode(), snew);
}
notes = "Update a `DR_Sub` object, selected by subId",
response = DR_Sub.class)
@ApiResponses( value = {
- @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
+ @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
@ApiResponse( code = 400, message = "Error", response = ApiError.class )
})
@Path("/{subId}")
ApiService resp = new ApiService();
try {
- resp.required( "subId", name, "");
- resp.required( "feedId", sub.getFeedId(), "");
- resp.required( "dcaeLocationName", sub.getDcaeLocationName(), "");
+ checker.required( "subId", name);
+ checker.required( "feedId", sub.getFeedId());
+ checker.required( "dcaeLocationName", sub.getDcaeLocationName());
} catch ( RequiredFieldException rfe ) {
- logger.debug( resp.toString() );
- return resp.error();
+ logger.debug( rfe.getApiError().toString() );
+ return responseBuilder.error(rfe.getApiError());
}
FeedService feeds = new FeedService();
Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
if ( fnew == null ) {
logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
- return resp.error();
+ return responseBuilder.error(resp.getErr());
}
-
+
DR_SubService dr_subService = new DR_SubService();
sub.setSubId(name);
DR_Sub nsub = dr_subService.updateDr_Sub(sub, resp.getErr() );
if ( nsub != null && nsub.isStatusValid() ) {
- return resp.success(nsub);
+ return responseBuilder.success(nsub);
}
- return resp.error();
+ return responseBuilder.error(resp.getErr());
}
@DELETE
notes = "Delete a `DR_Sub` object, selected by subId",
response = DR_Sub.class)
@ApiResponses( value = {
- @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
+ @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
@ApiResponse( code = 400, message = "Error", response = ApiError.class )
})
@Path("/{subId}")
ApiService resp = new ApiService();
try {
- resp.required( "subId", id, "");
+ checker.required( "subId", id);
} catch ( RequiredFieldException rfe ) {
- logger.debug( resp.toString() );
- return resp.error();
+ logger.debug( rfe.getApiError().toString() );
+ return responseBuilder.error(rfe.getApiError());
}
DR_SubService dr_subService = new DR_SubService();
dr_subService.removeDr_Sub(id, resp.getErr() );
if ( ! resp.getErr().is2xx() ) {
- return resp.error();
+ return responseBuilder.error(resp.getErr());
}
- return resp.success(Status.NO_CONTENT.getStatusCode(), null );
+ return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null );
}
@GET
notes = "Retrieve a `DR_Sub` object, selected by subId",
response = DR_Sub.class)
@ApiResponses( value = {
- @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
+ @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
@ApiResponse( code = 400, message = "Error", response = ApiError.class )
})
@Path("/{subId}")
ApiService resp = new ApiService();
try {
- resp.required( "subId", id, "");
+ checker.required( "subId", id);
} catch ( RequiredFieldException rfe ) {
- logger.debug( resp.toString() );
- return resp.error();
+ logger.debug( rfe.getApiError().toString() );
+ return responseBuilder.error(rfe.getApiError());
}
DR_SubService dr_subService = new DR_SubService();
DR_Sub sub = dr_subService.getDr_Sub( id, resp.getErr() );
if ( sub != null && sub.isStatusValid() ) {
- return resp.success(sub);
+ return responseBuilder.success(sub);
}
- return resp.error();
+ return responseBuilder.error(resp.getErr());
}
}