update link to upper-constraints.txt
[dmaap/buscontroller.git] / dmaap-bc / src / main / java / org / onap / dmaap / dbcapi / resources / TopicResource.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.resources;
22
23 import static jakarta.ws.rs.core.Response.Status.CREATED;
24
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;
50
51 @Path("/topics")
52 @Api( value= "topics", description = "Endpoint for retreiving MR Topics" )
53 @Consumes(MediaType.APPLICATION_JSON)
54 @Produces(MediaType.APPLICATION_JSON)
55 @Authorization
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";
64         
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");
70                 
71                 logger.info( "Setting defaultTopicStyle=" + defaultTopicStyle );
72         }
73                 
74         @GET
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 )
81         })
82         public Response getTopics() {
83                 List<Topic> allTopics = mr_topicService.getAllTopics();
84                 
85                 GenericEntity<List<Topic>> list = new GenericEntity<List<Topic>>(allTopics) {
86                         };
87                 return responseBuilder.success(list);
88                 
89         }
90                 
91         @POST
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 )
101         })
102         public Response  addTopic(Topic topic, @QueryParam("useExisting") String useExisting) {
103                 logger.info( "addTopic request: " + topic  + " useExisting=" + useExisting );
104                 ApiError apiError = new ApiError();
105
106                 try {
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());
113                 }
114                 
115                 ReplicationType t = topic.getReplicationCase();
116                 if ( t == null || t == ReplicationType.REPLICATION_NOT_SPECIFIED ) {
117                         topic.setReplicationCase( mr_topicService.reviewTopic(topic));
118                 } 
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 );
123                 }
124                 String pc = topic.getPartitionCount();
125                 if ( pc == null ) {
126                         topic.setPartitionCount(defaultPartitionCount);
127                 }
128                 String rc = topic.getReplicationCount();
129                 if ( rc == null ) {
130                         topic.setReplicationCount(defaultReplicationCount);
131                 }
132                 topic.setLastMod();
133                 Boolean flag = false;
134                 if (useExisting != null) {
135                         flag = "true".compareToIgnoreCase( useExisting ) == 0;
136                 }
137                 
138                 Topic mrc =  mr_topicService.addTopic(topic, apiError, flag);
139                 if ( mrc != null && apiError.is2xx() ) {
140                         return responseBuilder.success(CREATED.getStatusCode(), mrc);
141                 }
142                 return responseBuilder.error(apiError);
143         }
144         
145         @PUT
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 )
152         })
153         @Path("/{topicId}")
154         public Response updateTopic(@PathParam("topicId") String topicId) {
155                 ApiError apiError = new ApiError();
156
157                 apiError.setCode(Status.BAD_REQUEST.getStatusCode());
158                 apiError.setMessage(UNSUPPORTED_PUT_MSG);
159                 
160                 return responseBuilder.error(apiError);
161         }
162                 
163         @DELETE
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 )
170         })
171         @Path("/{topicId}")
172         public Response deleteTopic(@PathParam("topicId") String id){
173                 ApiError apiError = new ApiError();
174
175                 try {
176                         checker.required( "fqtn", id);
177                 } catch( RequiredFieldException rfe ) {
178                         logger.error("Error", rfe.getApiError());
179                         return responseBuilder.error(rfe.getApiError());
180                 }
181                 
182                 mr_topicService.removeTopic(id, apiError);
183                 if (apiError.is2xx()) {
184                         return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null);
185                 } 
186                 return responseBuilder.error(apiError);
187         }
188         
189
190         @GET
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 )
197         })
198         @Path("/{topicId}")
199         public Response getTopic(@PathParam("topicId") String id) {
200                 logger.info("Entry: /GET " + id);
201                 ApiError apiError = new ApiError();
202
203                 try {
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());
208                 }
209                 Topic mrc =  mr_topicService.getTopic(id, apiError);
210                 if ( mrc == null ) {
211                         return responseBuilder.error(apiError);
212                 }
213                 return responseBuilder.success(mrc);
214                 }
215 }