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.ReplicationType;
47 import org.onap.dmaap.dbcapi.model.FqtnType;
48 import org.onap.dmaap.dbcapi.model.Topic;
49 import org.onap.dmaap.dbcapi.service.ApiService;
50 import org.onap.dmaap.dbcapi.service.TopicService;
51 import org.onap.dmaap.dbcapi.util.DmaapConfig;
54 @Api( value= "topics", description = "Endpoint for retreiving MR Topics" )
55 @Consumes(MediaType.APPLICATION_JSON)
56 @Produces(MediaType.APPLICATION_JSON)
58 public class TopicResource extends BaseLoggingClass {
59 private static FqtnType defaultTopicStyle;
60 private static String defaultPartitionCount;
61 private static String defaultReplicationCount;
62 TopicService mr_topicService = new TopicService();
64 public TopicResource() {
65 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
66 defaultTopicStyle = FqtnType.Validator( p.getProperty("MR.topicStyle", "FQTN_LEGACY_FORMAT"));
67 defaultPartitionCount = p.getProperty( "MR.partitionCount", "2");
68 defaultReplicationCount = p.getProperty( "MR.replicationCount", "1");
70 logger.info( "Setting defaultTopicStyle=" + defaultTopicStyle );
74 @ApiOperation( value = "return Topic details",
75 notes = "Returns array of `Topic` objects.",
76 response = Topic.class)
77 @ApiResponses( value = {
78 @ApiResponse( code = 200, message = "Success", response = Topic.class),
79 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
81 public Response getTopics() {
83 ApiService check = new ApiService();
85 List<Topic> allTopics = mr_topicService.getAllTopics();
87 GenericEntity<List<Topic>> list = new GenericEntity<List<Topic>>(allTopics) {
89 return check.success(list);
94 @ApiOperation( value = "return Topic details",
95 notes = "Create `Topic` object.",
96 response = Topic.class)
97 @ApiResponses( value = {
98 @ApiResponse( code = 200, message = "Success", response = Topic.class),
99 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
101 public Response addTopic(
103 @QueryParam("useExisting") String useExisting
105 logger.info( "addTopic request: " + topic + " useExisting=" + useExisting );
106 ApiService check = new ApiService();
109 check.required( "topicName", topic.getTopicName(), "^\\S+$" ); //no white space allowed in topicName
110 check.required( "topicDescription", topic.getTopicDescription(), "" );
111 check.required( "owner", topic.getOwner(), "" );
112 } catch( RequiredFieldException rfe ) {
113 logger.error("Error", rfe);
114 return check.error();
117 ReplicationType t = topic.getReplicationCase();
118 if ( t == null || t == ReplicationType.REPLICATION_NOT_SPECIFIED ) {
119 topic.setReplicationCase( mr_topicService.reviewTopic(topic));
121 FqtnType ft = topic.getFqtnStyle();
122 if ( ft == null || ft == FqtnType.FQTN_NOT_SPECIFIED ) {
123 logger.info( "setting defaultTopicStyle=" + defaultTopicStyle + " for topic " + topic.getTopicName() );
124 topic.setFqtnStyle( defaultTopicStyle );
126 String pc = topic.getPartitionCount();
128 topic.setPartitionCount(defaultPartitionCount);
130 String rc = topic.getReplicationCount();
132 topic.setReplicationCount(defaultReplicationCount);
135 Boolean flag = false;
136 if (useExisting != null) {
137 flag = "true".compareToIgnoreCase( useExisting ) == 0;
140 Topic mrc = mr_topicService.addTopic(topic, check.getErr(), flag);
141 if ( mrc != null && check.getErr().is2xx() ) {
142 return check.success(Status.CREATED.getStatusCode(), mrc);
144 return check.error();
148 @ApiOperation( value = "return Topic details",
149 notes = "Update a `Topic` object, identified by topicId",
150 response = Topic.class)
151 @ApiResponses( value = {
152 @ApiResponse( code = 200, message = "Success", response = Topic.class),
153 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
156 public Response updateTopic(
157 @PathParam("topicId") String topicId
159 ApiService check = new ApiService();
161 check.setCode(Status.BAD_REQUEST.getStatusCode());
162 check.setMessage( "Method /PUT not supported for /topics");
164 return check.error();
168 @ApiOperation( value = "return Topic details",
169 notes = "Delete a `Topic` object, identified by topicId",
170 response = Topic.class)
171 @ApiResponses( value = {
172 @ApiResponse( code = 204, message = "Success", response = Topic.class),
173 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
176 public Response deleteTopic(
177 @PathParam("topicId") String id
179 ApiService check = new ApiService();
182 check.required( "fqtn", id, "" );
183 } catch( RequiredFieldException rfe ) {
184 logger.error("Error", rfe);
185 return check.error();
188 mr_topicService.removeTopic(id, check.getErr());
189 if ( check.getErr().is2xx()) {
190 return check.success(Status.NO_CONTENT.getStatusCode(), null);
192 return check.error();
197 @ApiOperation( value = "return Topic details",
198 notes = "Retrieve a `Topic` object, identified by topicId",
199 response = Topic.class)
200 @ApiResponses( value = {
201 @ApiResponse( code = 200, message = "Success", response = Topic.class),
202 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
205 public Response getTopic(
206 @PathParam("topicId") String id
208 logger.info("Entry: /GET " + id);
209 ApiService check = new ApiService();
212 check.required( "topicName", id, "^\\S+$" ); //no white space allowed in topicName
213 } catch( RequiredFieldException rfe ) {
214 logger.error("Error", rfe);
215 return check.error();
217 Topic mrc = mr_topicService.getTopic( id, check.getErr() );
219 return check.error();
221 return check.success(mrc);