dbeea633398221d1b3e53dab7a84b3459881a76f
[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.DR_Pub;
47 import org.onap.dmaap.dbcapi.model.ReplicationType;
48 import org.onap.dmaap.dbcapi.model.FqtnType;
49 import org.onap.dmaap.dbcapi.model.Topic;
50 import org.onap.dmaap.dbcapi.service.ApiService;
51 import org.onap.dmaap.dbcapi.service.TopicService;
52 import org.onap.dmaap.dbcapi.util.DmaapConfig;
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         TopicService mr_topicService = new TopicService();
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 = DR_Pub.class),
80             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
81         })
82         public Response getTopics() {
83
84                 ApiService check = new ApiService();
85
86                 List<Topic> allTopics = mr_topicService.getAllTopics();
87                 
88                 GenericEntity<List<Topic>> list = new GenericEntity<List<Topic>>(allTopics) {
89                         };
90                 return check.success(list);
91                 
92         }
93                 
94         @POST
95         @ApiOperation( value = "return Topic details", 
96         notes = "Create  `Topic` object.", 
97         response = Topic.class)
98         @ApiResponses( value = {
99             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
100             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
101         })
102         public Response  addTopic( 
103                         Topic topic,
104                         @QueryParam("useExisting") String useExisting
105                         ) {
106                 logger.info( "addTopic request: " + topic  + " useExisting=" + useExisting );
107                 ApiService check = new ApiService();
108
109                 try {
110                         check.required( "topicName", topic.getTopicName(), "^\\S+$" );  //no white space allowed in topicName
111                         check.required( "topicDescription", topic.getTopicDescription(), "" );
112                         check.required( "owner", topic.getOwner(), "" );
113                 } catch( RequiredFieldException rfe ) {
114                         logger.error("Error", rfe);
115                         return check.error();
116                 }
117                 
118                 ReplicationType t = topic.getReplicationCase();
119                 if ( t == null || t == ReplicationType.REPLICATION_NOT_SPECIFIED ) {
120                         topic.setReplicationCase( mr_topicService.reviewTopic(topic));
121                 } 
122                 FqtnType ft = topic.getFqtnStyle();
123                 if ( ft == null || ft == FqtnType.FQTN_NOT_SPECIFIED ) {
124                         logger.info( "setting defaultTopicStyle=" + defaultTopicStyle + " for topic " + topic.getTopicName() );
125                         topic.setFqtnStyle( defaultTopicStyle );
126                 }
127                 String pc = topic.getPartitionCount();
128                 if ( pc == null ) {
129                         topic.setPartitionCount(defaultPartitionCount);
130                 }
131                 String rc = topic.getReplicationCount();
132                 if ( rc == null ) {
133                         topic.setReplicationCount(defaultReplicationCount);
134                 }
135                 topic.setLastMod();
136                 Boolean flag = false;
137                 if (useExisting != null) {
138                         flag = "true".compareToIgnoreCase( useExisting ) == 0;
139                 }
140                 
141                 Topic mrc =  mr_topicService.addTopic(topic, check.getErr(), flag);
142                 if ( mrc != null && check.getErr().is2xx() ) {
143                         return check.success(Status.CREATED.getStatusCode(), mrc);
144                 }
145                 return check.error();
146         }
147         
148         @PUT
149         @ApiOperation( value = "return Topic details", 
150         notes = "Update a  `Topic` object, identified by topicId", 
151         response = Topic.class)
152         @ApiResponses( value = {
153             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
154             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
155         })
156         @Path("/{topicId}")
157         public Response updateTopic( 
158                         @PathParam("topicId") String topicId
159                         ) {
160                 ApiService check = new ApiService();
161
162                 check.setCode(Status.BAD_REQUEST.getStatusCode());
163                 check.setMessage( "Method /PUT not supported for /topics");
164                 
165                 return check.error();
166         }
167                 
168         @DELETE
169         @ApiOperation( value = "return Topic details", 
170         notes = "Delete a  `Topic` object, identified by topicId", 
171         response = Topic.class)
172         @ApiResponses( value = {
173             @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
174             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
175         })
176         @Path("/{topicId}")
177         public Response deleteTopic( 
178                         @PathParam("topicId") String id
179                         ){
180                 ApiService check = new ApiService();
181
182                 try {
183                         check.required( "fqtn", id, "" );
184                 } catch( RequiredFieldException rfe ) {
185                         logger.error("Error", rfe);
186                         return check.error();
187                 }
188                 
189                 mr_topicService.removeTopic(id, check.getErr());
190                 if ( check.getErr().is2xx()) {
191                         return check.success(Status.NO_CONTENT.getStatusCode(), null);
192                 } 
193                 return check.error();
194         }
195         
196
197         @GET
198         @ApiOperation( value = "return Topic details", 
199         notes = "Retrieve a  `Topic` object, identified by topicId", 
200         response = Topic.class)
201         @ApiResponses( value = {
202             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
203             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
204         })
205         @Path("/{topicId}")
206         public Response getTopic( 
207                         @PathParam("topicId") String id
208                         ) {
209                 logger.info("Entry: /GET " + id);
210                 ApiService check = new ApiService();
211
212                 try {
213                         check.required( "topicName", id, "^\\S+$" );  //no white space allowed in topicName
214                 } catch( RequiredFieldException rfe ) {
215                         logger.error("Error", rfe);
216                         return check.error();
217                 }
218                 Topic mrc =  mr_topicService.getTopic( id, check.getErr() );
219                 if ( mrc == null ) {
220                         return check.error();
221                 }
222                 return check.success(mrc);
223                 }
224 }