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