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 static jakarta.ws.rs.core.Response.Status.CREATED;
25 import io.swagger.annotations.Api;
26 import io.swagger.annotations.ApiOperation;
27 import io.swagger.annotations.ApiResponse;
28 import io.swagger.annotations.ApiResponses;
29 import jakarta.ws.rs.Consumes;
30 import jakarta.ws.rs.DELETE;
31 import jakarta.ws.rs.GET;
32 import jakarta.ws.rs.POST;
33 import jakarta.ws.rs.PUT;
34 import jakarta.ws.rs.Path;
35 import jakarta.ws.rs.PathParam;
36 import jakarta.ws.rs.Produces;
37 import jakarta.ws.rs.QueryParam;
38 import jakarta.ws.rs.core.GenericEntity;
39 import jakarta.ws.rs.core.MediaType;
40 import jakarta.ws.rs.core.Response;
41 import jakarta.ws.rs.core.Response.Status;
42 import java.util.List;
43 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
44 import org.onap.dmaap.dbcapi.model.ApiError;
45 import org.onap.dmaap.dbcapi.model.FqtnType;
46 import org.onap.dmaap.dbcapi.model.ReplicationType;
47 import org.onap.dmaap.dbcapi.model.Topic;
48 import org.onap.dmaap.dbcapi.service.TopicService;
49 import org.onap.dmaap.dbcapi.util.DmaapConfig;
52 @Api( value= "topics", description = "Endpoint for retreiving MR Topics" )
53 @Consumes(MediaType.APPLICATION_JSON)
54 @Produces(MediaType.APPLICATION_JSON)
56 public class TopicResource extends BaseLoggingClass {
57 private static FqtnType defaultTopicStyle;
58 private static String defaultPartitionCount;
59 private static String defaultReplicationCount;
60 private TopicService mr_topicService = new TopicService();
61 private ResponseBuilder responseBuilder = new ResponseBuilder();
62 private RequiredChecker checker = new RequiredChecker();
63 static final String UNSUPPORTED_PUT_MSG = "Method /PUT not supported for /topics";
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 = Topic.class),
80 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
82 public Response getTopics() {
83 List<Topic> allTopics = mr_topicService.getAllTopics();
85 GenericEntity<List<Topic>> list = new GenericEntity<List<Topic>>(allTopics) {
87 return responseBuilder.success(list);
92 @ApiOperation( value = "Create a Topic object",
93 notes = "Create `Topic` object."
94 + "For convenience, the message body may populate the `clients` array, in which case each entry will be added as an `MR_Client`."
95 + " Beginning in ONAP Dublin Release, dbcapi will create two AAF Roles by default, one each for the publisher and subscriber per topic."
96 + " MR_Clients can then specify an AAF Identity to be added to the appropriate default Role, avoiding the need to create Role(s) in advance.",
97 response = Topic.class)
98 @ApiResponses( value = {
99 @ApiResponse( code = 200, message = "Success", response = Topic.class),
100 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
102 public Response addTopic(Topic topic, @QueryParam("useExisting") String useExisting) {
103 logger.info( "addTopic request: " + topic + " useExisting=" + useExisting );
104 ApiError apiError = new ApiError();
107 checker.required( "topicName", topic.getTopicName(), "^\\S+$" ); //no white space allowed in topicName
108 checker.required( "topicDescription", topic.getTopicDescription());
109 checker.required( "owner", topic.getOwner());
110 } catch( RequiredFieldException rfe ) {
111 logger.error("Error", rfe.getApiError());
112 return responseBuilder.error(rfe.getApiError());
115 ReplicationType t = topic.getReplicationCase();
116 if ( t == null || t == ReplicationType.REPLICATION_NOT_SPECIFIED ) {
117 topic.setReplicationCase( mr_topicService.reviewTopic(topic));
119 FqtnType ft = topic.getFqtnStyle();
120 if ( ft == null || ft == FqtnType.FQTN_NOT_SPECIFIED ) {
121 logger.info( "setting defaultTopicStyle=" + defaultTopicStyle + " for topic " + topic.getTopicName() );
122 topic.setFqtnStyle( defaultTopicStyle );
124 String pc = topic.getPartitionCount();
126 topic.setPartitionCount(defaultPartitionCount);
128 String rc = topic.getReplicationCount();
130 topic.setReplicationCount(defaultReplicationCount);
133 Boolean flag = false;
134 if (useExisting != null) {
135 flag = "true".compareToIgnoreCase( useExisting ) == 0;
138 Topic mrc = mr_topicService.addTopic(topic, apiError, flag);
139 if ( mrc != null && apiError.is2xx() ) {
140 return responseBuilder.success(CREATED.getStatusCode(), mrc);
142 return responseBuilder.error(apiError);
146 @ApiOperation( value = "return Topic details",
147 notes = "Update a `Topic` object, identified by topicId",
148 response = Topic.class)
149 @ApiResponses( value = {
150 @ApiResponse( code = 200, message = "Success", response = Topic.class),
151 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
154 public Response updateTopic(@PathParam("topicId") String topicId) {
155 ApiError apiError = new ApiError();
157 apiError.setCode(Status.BAD_REQUEST.getStatusCode());
158 apiError.setMessage(UNSUPPORTED_PUT_MSG);
160 return responseBuilder.error(apiError);
164 @ApiOperation( value = "return Topic details",
165 notes = "Delete a `Topic` object, identified by topicId",
166 response = Topic.class)
167 @ApiResponses( value = {
168 @ApiResponse( code = 204, message = "Success", response = Topic.class),
169 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
172 public Response deleteTopic(@PathParam("topicId") String id){
173 ApiError apiError = new ApiError();
176 checker.required( "fqtn", id);
177 } catch( RequiredFieldException rfe ) {
178 logger.error("Error", rfe.getApiError());
179 return responseBuilder.error(rfe.getApiError());
182 mr_topicService.removeTopic(id, apiError);
183 if (apiError.is2xx()) {
184 return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null);
186 return responseBuilder.error(apiError);
191 @ApiOperation( value = "return Topic details",
192 notes = "Retrieve a `Topic` object, identified by topicId",
193 response = Topic.class)
194 @ApiResponses( value = {
195 @ApiResponse( code = 200, message = "Success", response = Topic.class),
196 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
199 public Response getTopic(@PathParam("topicId") String id) {
200 logger.info("Entry: /GET " + id);
201 ApiError apiError = new ApiError();
204 checker.required( "topicName", id, "^\\S+$" ); //no white space allowed in topicName
205 } catch( RequiredFieldException rfe ) {
206 logger.error("Error", rfe.getApiError());
207 return responseBuilder.error(rfe.getApiError());
209 Topic mrc = mr_topicService.getTopic(id, apiError);
211 return responseBuilder.error(apiError);
213 return responseBuilder.success(mrc);