Extract required(...) method
[dmaap/dbcapi.git] / 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 io.swagger.annotations.Api;
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
27
28 import java.util.List;
29
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;
43
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;
52
53 import static javax.ws.rs.core.Response.Status.CREATED;
54
55 @Path("/topics")
56 @Api( value= "topics", description = "Endpoint for retreiving MR Topics" )
57 @Consumes(MediaType.APPLICATION_JSON)
58 @Produces(MediaType.APPLICATION_JSON)
59 @Authorization
60 public class TopicResource extends BaseLoggingClass {
61         private static FqtnType defaultTopicStyle;
62         private static String defaultPartitionCount;
63         private static String defaultReplicationCount;
64         private TopicService mr_topicService = new TopicService();
65         private ResponseBuilder responseBuilder = new ResponseBuilder();
66         private RequiredChecker checker = new RequiredChecker();
67         
68         public TopicResource() {
69                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
70                 defaultTopicStyle = FqtnType.Validator( p.getProperty("MR.topicStyle", "FQTN_LEGACY_FORMAT"));
71                 defaultPartitionCount = p.getProperty( "MR.partitionCount", "2");
72                 defaultReplicationCount = p.getProperty( "MR.replicationCount", "1");
73                 
74                 logger.info( "Setting defaultTopicStyle=" + defaultTopicStyle );
75         }
76                 
77         @GET
78         @ApiOperation( value = "return Topic details", 
79         notes = "Returns array of  `Topic` objects.", 
80         response = Topic.class)
81         @ApiResponses( value = {
82             @ApiResponse( code = 200, message = "Success", response = Topic.class),
83             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
84         })
85         public Response getTopics() {
86                 List<Topic> allTopics = mr_topicService.getAllTopics();
87                 
88                 GenericEntity<List<Topic>> list = new GenericEntity<List<Topic>>(allTopics) {
89                         };
90                 return responseBuilder.success(list);
91                 
92         }
93                 
94         @POST
95         @ApiOperation( value = "Create a Topic object", 
96         notes = "Create  `Topic` object."
97                         + "For convenience, the message body may populate the `clients` array, in which case each entry will be added as an `MR_Client`."
98                         + "  Beginning in ONAP Dublin Release, dbcapi will create two AAF Roles by default, one each for the publisher and subscriber per topic."
99                         + "  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.", 
100         response = Topic.class)
101         @ApiResponses( value = {
102             @ApiResponse( code = 200, message = "Success", response = Topic.class),
103             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
104         })
105         public Response  addTopic( 
106                         Topic topic,
107                         @QueryParam("useExisting") String useExisting
108                         ) {
109                 logger.info( "addTopic request: " + topic  + " useExisting=" + useExisting );
110                 ApiService check = new ApiService();
111
112                 try {
113                         checker.required( "topicName", topic.getTopicName(), "^\\S+$" );  //no white space allowed in topicName
114                         checker.required( "topicDescription", topic.getTopicDescription());
115                         checker.required( "owner", topic.getOwner());
116                 } catch( RequiredFieldException rfe ) {
117                         logger.error("Error", rfe.getApiError());
118                         return responseBuilder.error(rfe.getApiError());
119                 }
120                 
121                 ReplicationType t = topic.getReplicationCase();
122                 if ( t == null || t == ReplicationType.REPLICATION_NOT_SPECIFIED ) {
123                         topic.setReplicationCase( mr_topicService.reviewTopic(topic));
124                 } 
125                 FqtnType ft = topic.getFqtnStyle();
126                 if ( ft == null || ft == FqtnType.FQTN_NOT_SPECIFIED ) {
127                         logger.info( "setting defaultTopicStyle=" + defaultTopicStyle + " for topic " + topic.getTopicName() );
128                         topic.setFqtnStyle( defaultTopicStyle );
129                 }
130                 String pc = topic.getPartitionCount();
131                 if ( pc == null ) {
132                         topic.setPartitionCount(defaultPartitionCount);
133                 }
134                 String rc = topic.getReplicationCount();
135                 if ( rc == null ) {
136                         topic.setReplicationCount(defaultReplicationCount);
137                 }
138                 topic.setLastMod();
139                 Boolean flag = false;
140                 if (useExisting != null) {
141                         flag = "true".compareToIgnoreCase( useExisting ) == 0;
142                 }
143                 
144                 Topic mrc =  mr_topicService.addTopic(topic, check.getErr(), flag);
145                 if ( mrc != null && check.getErr().is2xx() ) {
146                         return responseBuilder.success(CREATED.getStatusCode(), mrc);
147                 }
148                 return responseBuilder.error(check.getErr());
149         }
150         
151         @PUT
152         @ApiOperation( value = "return Topic details", 
153         notes = "Update a  `Topic` object, identified by topicId", 
154         response = Topic.class)
155         @ApiResponses( value = {
156             @ApiResponse( code = 200, message = "Success", response = Topic.class),
157             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
158         })
159         @Path("/{topicId}")
160         public Response updateTopic( 
161                         @PathParam("topicId") String topicId
162                         ) {
163                 ApiService check = new ApiService();
164
165                 check.setCode(Status.BAD_REQUEST.getStatusCode());
166                 check.setMessage( "Method /PUT not supported for /topics");
167                 
168                 return responseBuilder.error(check.getErr());
169         }
170                 
171         @DELETE
172         @ApiOperation( value = "return Topic details", 
173         notes = "Delete a  `Topic` object, identified by topicId", 
174         response = Topic.class)
175         @ApiResponses( value = {
176             @ApiResponse( code = 204, message = "Success", response = Topic.class),
177             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
178         })
179         @Path("/{topicId}")
180         public Response deleteTopic( 
181                         @PathParam("topicId") String id
182                         ){
183                 ApiService check = new ApiService();
184
185                 try {
186                         checker.required( "fqtn", id);
187                 } catch( RequiredFieldException rfe ) {
188                         logger.error("Error", rfe.getApiError());
189                         return responseBuilder.error(rfe.getApiError());
190                 }
191                 
192                 mr_topicService.removeTopic(id, check.getErr());
193                 if ( check.getErr().is2xx()) {
194                         return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null);
195                 } 
196                 return responseBuilder.error(check.getErr());
197         }
198         
199
200         @GET
201         @ApiOperation( value = "return Topic details", 
202         notes = "Retrieve a  `Topic` object, identified by topicId", 
203         response = Topic.class)
204         @ApiResponses( value = {
205             @ApiResponse( code = 200, message = "Success", response = Topic.class),
206             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
207         })
208         @Path("/{topicId}")
209         public Response getTopic( 
210                         @PathParam("topicId") String id
211                         ) {
212                 logger.info("Entry: /GET " + id);
213                 ApiService check = new ApiService();
214
215                 try {
216                         checker.required( "topicName", id, "^\\S+$" );  //no white space allowed in topicName
217                 } catch( RequiredFieldException rfe ) {
218                         logger.error("Error", rfe.getApiError());
219                         return responseBuilder.error(rfe.getApiError());
220                 }
221                 Topic mrc =  mr_topicService.getTopic( id, check.getErr() );
222                 if ( mrc == null ) {
223                         return responseBuilder.error(check.getErr());
224                 }
225                 return responseBuilder.success(mrc);
226                 }
227 }