2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dmaap.dbcapi.resources;
23 import io.swagger.annotations.Api;
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
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.QueryParam;
39 import javax.ws.rs.core.GenericEntity;
40 import javax.ws.rs.core.MediaType;
41 import javax.ws.rs.core.Response;
42 import javax.ws.rs.core.Response.Status;
44 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
45 import org.onap.dmaap.dbcapi.model.ApiError;
46 import org.onap.dmaap.dbcapi.model.DR_Pub;
47 import org.onap.dmaap.dbcapi.model.ReplicationType;
48 import org.onap.dmaap.dbcapi.model.FqtnType;
49 import org.onap.dmaap.dbcapi.model.Topic;
50 import org.onap.dmaap.dbcapi.service.ApiService;
51 import org.onap.dmaap.dbcapi.service.TopicService;
52 import org.onap.dmaap.dbcapi.util.DmaapConfig;
55 @Api( value= "topics", description = "Endpoint for retreiving MR Topics" )
56 @Consumes(MediaType.APPLICATION_JSON)
57 @Produces(MediaType.APPLICATION_JSON)
59 public class TopicResource extends BaseLoggingClass {
60 private static FqtnType defaultTopicStyle;
61 private static String defaultPartitionCount;
62 private static String defaultReplicationCount;
63 TopicService mr_topicService = new TopicService();
65 public TopicResource() {
66 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
67 defaultTopicStyle = FqtnType.Validator( p.getProperty("MR.topicStyle", "FQTN_LEGACY_FORMAT"));
68 defaultPartitionCount = p.getProperty( "MR.partitionCount", "2");
69 defaultReplicationCount = p.getProperty( "MR.replicationCount", "1");
71 logger.info( "Setting defaultTopicStyle=" + defaultTopicStyle );
75 @ApiOperation( value = "return Topic details",
76 notes = "Returns array of `Topic` objects.",
77 response = Topic.class)
78 @ApiResponses( value = {
79 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
80 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
82 public Response getTopics() {
84 ApiService check = new ApiService();
86 List<Topic> allTopics = mr_topicService.getAllTopics();
88 GenericEntity<List<Topic>> list = new GenericEntity<List<Topic>>(allTopics) {
90 return check.success(list);
95 @ApiOperation( value = "return Topic details",
96 notes = "Create `Topic` object.",
97 response = Topic.class)
98 @ApiResponses( value = {
99 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
100 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
102 public Response addTopic(
104 @QueryParam("useExisting") String useExisting
106 logger.info( "addTopic request: " + topic + " useExisting=" + useExisting );
107 ApiService check = new ApiService();
110 check.required( "topicName", topic.getTopicName(), "^\\S+$" ); //no white space allowed in topicName
111 check.required( "topicDescription", topic.getTopicDescription(), "" );
112 check.required( "owner", topic.getOwner(), "" );
113 } catch( RequiredFieldException rfe ) {
114 logger.error("Error", rfe);
115 return check.error();
118 ReplicationType t = topic.getReplicationCase();
119 if ( t == null || t == ReplicationType.REPLICATION_NOT_SPECIFIED ) {
120 topic.setReplicationCase( mr_topicService.reviewTopic(topic));
122 FqtnType ft = topic.getFqtnStyle();
123 if ( ft == null || ft == FqtnType.FQTN_NOT_SPECIFIED ) {
124 logger.info( "setting defaultTopicStyle=" + defaultTopicStyle + " for topic " + topic.getTopicName() );
125 topic.setFqtnStyle( defaultTopicStyle );
127 String pc = topic.getPartitionCount();
129 topic.setPartitionCount(defaultPartitionCount);
131 String rc = topic.getReplicationCount();
133 topic.setReplicationCount(defaultReplicationCount);
136 Boolean flag = false;
137 if (useExisting != null) {
138 flag = "true".compareToIgnoreCase( useExisting ) == 0;
141 Topic mrc = mr_topicService.addTopic(topic, check.getErr(), flag);
142 if ( mrc != null && check.getErr().is2xx() ) {
143 return check.success(Status.CREATED.getStatusCode(), mrc);
145 return check.error();
149 @ApiOperation( value = "return Topic details",
150 notes = "Update a `Topic` object, identified by topicId",
151 response = Topic.class)
152 @ApiResponses( value = {
153 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
154 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
157 public Response updateTopic(
158 @PathParam("topicId") String topicId
160 ApiService check = new ApiService();
162 check.setCode(Status.BAD_REQUEST.getStatusCode());
163 check.setMessage( "Method /PUT not supported for /topics");
165 return check.error();
169 @ApiOperation( value = "return Topic details",
170 notes = "Delete a `Topic` object, identified by topicId",
171 response = Topic.class)
172 @ApiResponses( value = {
173 @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
174 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
177 public Response deleteTopic(
178 @PathParam("topicId") String id
180 ApiService check = new ApiService();
183 check.required( "fqtn", id, "" );
184 } catch( RequiredFieldException rfe ) {
185 logger.error("Error", rfe);
186 return check.error();
189 mr_topicService.removeTopic(id, check.getErr());
190 if ( check.getErr().is2xx()) {
191 return check.success(Status.NO_CONTENT.getStatusCode(), null);
193 return check.error();
198 @ApiOperation( value = "return Topic details",
199 notes = "Retrieve a `Topic` object, identified by topicId",
200 response = Topic.class)
201 @ApiResponses( value = {
202 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
203 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
206 public Response getTopic(
207 @PathParam("topicId") String id
209 logger.info("Entry: /GET " + id);
210 ApiService check = new ApiService();
213 check.required( "topicName", id, "^\\S+$" ); //no white space allowed in topicName
214 } catch( RequiredFieldException rfe ) {
215 logger.error("Error", rfe);
216 return check.error();
218 Topic mrc = mr_topicService.getTopic( id, check.getErr() );
220 return check.error();
222 return check.success(mrc);