Fix object references to show fields
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / resources / TopicResource.java
index 0f99fb6..5f027aa 100644 (file)
@@ -35,6 +35,7 @@ import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.GenericEntity;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
@@ -42,11 +43,12 @@ import javax.ws.rs.core.Response.Status;
 
 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.ReplicationType;
+import org.onap.dmaap.dbcapi.model.FqtnType;
 import org.onap.dmaap.dbcapi.model.Topic;
 import org.onap.dmaap.dbcapi.service.ApiService;
 import org.onap.dmaap.dbcapi.service.TopicService;
+import org.onap.dmaap.dbcapi.util.DmaapConfig;
 
 @Path("/topics")
 @Api( value= "topics", description = "Endpoint for retreiving MR Topics" )
@@ -54,15 +56,26 @@ import org.onap.dmaap.dbcapi.service.TopicService;
 @Produces(MediaType.APPLICATION_JSON)
 @Authorization
 public class TopicResource extends BaseLoggingClass {
-
+       private static FqtnType defaultTopicStyle;
+       private static String defaultPartitionCount;
+       private static String defaultReplicationCount;
        TopicService mr_topicService = new TopicService();
+       
+       public TopicResource() {
+               DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
+               defaultTopicStyle = FqtnType.Validator( p.getProperty("MR.topicStyle", "FQTN_LEGACY_FORMAT"));
+               defaultPartitionCount = p.getProperty( "MR.partitionCount", "2");
+               defaultReplicationCount = p.getProperty( "MR.replicationCount", "1");
+               
+               logger.info( "Setting defaultTopicStyle=" + defaultTopicStyle );
+       }
                
        @GET
        @ApiOperation( value = "return Topic details", 
        notes = "Returns array of  `Topic` objects.", 
        response = Topic.class)
        @ApiResponses( value = {
-           @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
+           @ApiResponse( code = 200, message = "Success", response = Topic.class),
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        public Response getTopics() {
@@ -82,13 +95,14 @@ public class TopicResource extends BaseLoggingClass {
        notes = "Create  `Topic` object.", 
        response = Topic.class)
        @ApiResponses( value = {
-           @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
+           @ApiResponse( code = 200, message = "Success", response = Topic.class),
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        public Response  addTopic( 
-                       Topic topic
+                       Topic topic,
+                       @QueryParam("useExisting") String useExisting
                        ) {
-               logger.info( "addTopic request: " + String.valueOf(topic) );
+               logger.info( "addTopic request: " + topic  + " useExisting=" + useExisting );
                ApiService check = new ApiService();
 
                try {
@@ -96,19 +110,35 @@ public class TopicResource extends BaseLoggingClass {
                        check.required( "topicDescription", topic.getTopicDescription(), "" );
                        check.required( "owner", topic.getOwner(), "" );
                } catch( RequiredFieldException rfe ) {
+                       logger.error("Error", rfe);
                        return check.error();
                }
                
-               //String repReq = topic.getReplicationRequest();
                ReplicationType t = topic.getReplicationCase();
                if ( t == null || t == ReplicationType.REPLICATION_NOT_SPECIFIED ) {
                        topic.setReplicationCase( mr_topicService.reviewTopic(topic));
                } 
-               
+               FqtnType ft = topic.getFqtnStyle();
+               if ( ft == null || ft == FqtnType.FQTN_NOT_SPECIFIED ) {
+                       logger.info( "setting defaultTopicStyle=" + defaultTopicStyle + " for topic " + topic.getTopicName() );
+                       topic.setFqtnStyle( defaultTopicStyle );
+               }
+               String pc = topic.getPartitionCount();
+               if ( pc == null ) {
+                       topic.setPartitionCount(defaultPartitionCount);
+               }
+               String rc = topic.getReplicationCount();
+               if ( rc == null ) {
+                       topic.setReplicationCount(defaultReplicationCount);
+               }
                topic.setLastMod();
+               Boolean flag = false;
+               if (useExisting != null) {
+                       flag = "true".compareToIgnoreCase( useExisting ) == 0;
+               }
                
-               Topic mrc =  mr_topicService.addTopic(topic, check.getErr());
-               if ( mrc != null && mrc.isStatusValid() ) {
+               Topic mrc =  mr_topicService.addTopic(topic, check.getErr(), flag);
+               if ( mrc != null && check.getErr().is2xx() ) {
                        return check.success(Status.CREATED.getStatusCode(), mrc);
                }
                return check.error();
@@ -119,7 +149,7 @@ public class TopicResource extends BaseLoggingClass {
        notes = "Update a  `Topic` object, identified by topicId", 
        response = Topic.class)
        @ApiResponses( value = {
-           @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
+           @ApiResponse( code = 200, message = "Success", response = Topic.class),
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{topicId}")
@@ -139,7 +169,7 @@ public class TopicResource extends BaseLoggingClass {
        notes = "Delete a  `Topic` object, identified by topicId", 
        response = Topic.class)
        @ApiResponses( value = {
-           @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
+           @ApiResponse( code = 204, message = "Success", response = Topic.class),
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{topicId}")
@@ -151,6 +181,7 @@ public class TopicResource extends BaseLoggingClass {
                try {
                        check.required( "fqtn", id, "" );
                } catch( RequiredFieldException rfe ) {
+                       logger.error("Error", rfe);
                        return check.error();
                }
                
@@ -167,7 +198,7 @@ public class TopicResource extends BaseLoggingClass {
        notes = "Retrieve a  `Topic` object, identified by topicId", 
        response = Topic.class)
        @ApiResponses( value = {
-           @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
+           @ApiResponse( code = 200, message = "Success", response = Topic.class),
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{topicId}")
@@ -180,6 +211,7 @@ public class TopicResource extends BaseLoggingClass {
                try {
                        check.required( "topicName", id, "^\\S+$" );  //no white space allowed in topicName
                } catch( RequiredFieldException rfe ) {
+                       logger.error("Error", rfe);
                        return check.error();
                }
                Topic mrc =  mr_topicService.getTopic( id, check.getErr() );