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