Introduce useExisting query param for feeds/topics
[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         TopicService mr_topicService = new TopicService();
62         
63         public TopicResource() {
64                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
65                 defaultTopicStyle = FqtnType.Validator( p.getProperty("MR.topicStyle", "FQTN_LEGACY_FORMAT"));
66                 logger.info( "Setting defaultTopicStyle=" + defaultTopicStyle );
67         }
68                 
69         @GET
70         @ApiOperation( value = "return Topic details", 
71         notes = "Returns array of  `Topic` objects.", 
72         response = Topic.class)
73         @ApiResponses( value = {
74             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
75             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
76         })
77         public Response getTopics() {
78
79                 ApiService check = new ApiService();
80
81                 List<Topic> allTopics = mr_topicService.getAllTopics();
82                 
83                 GenericEntity<List<Topic>> list = new GenericEntity<List<Topic>>(allTopics) {
84                         };
85                 return check.success(list);
86                 
87         }
88                 
89         @POST
90         @ApiOperation( value = "return Topic details", 
91         notes = "Create  `Topic` object.", 
92         response = Topic.class)
93         @ApiResponses( value = {
94             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
95             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
96         })
97         public Response  addTopic( 
98                         Topic topic,
99                         @QueryParam("useExisting") String useExisting
100                         ) {
101                 logger.info( "addTopic request: " + topic  + " useExisting=" + useExisting );
102                 ApiService check = new ApiService();
103
104                 try {
105                         check.required( "topicName", topic.getTopicName(), "^\\S+$" );  //no white space allowed in topicName
106                         check.required( "topicDescription", topic.getTopicDescription(), "" );
107                         check.required( "owner", topic.getOwner(), "" );
108                 } catch( RequiredFieldException rfe ) {
109                         logger.error("Error", rfe);
110                         return check.error();
111                 }
112                 
113                 ReplicationType t = topic.getReplicationCase();
114                 if ( t == null || t == ReplicationType.REPLICATION_NOT_SPECIFIED ) {
115                         topic.setReplicationCase( mr_topicService.reviewTopic(topic));
116                 } 
117                 FqtnType ft = topic.getFqtnStyle();
118                 if ( ft == null || ft == FqtnType.FQTN_NOT_SPECIFIED ) {
119                         logger.info( "setting defaultTopicStyle=" + defaultTopicStyle + " for topic " + topic.getTopicName() );
120                         topic.setFqtnStyle( defaultTopicStyle );
121                 }
122                 topic.setLastMod();
123                 Boolean flag = false;
124                 if (useExisting != null) {
125                         flag = "true".compareToIgnoreCase( useExisting ) == 0;
126                 }
127                 
128                 Topic mrc =  mr_topicService.addTopic(topic, check.getErr(), flag);
129                 if ( mrc != null && check.getErr().is2xx() ) {
130                         return check.success(Status.CREATED.getStatusCode(), mrc);
131                 }
132                 return check.error();
133         }
134         
135         @PUT
136         @ApiOperation( value = "return Topic details", 
137         notes = "Update a  `Topic` object, identified by topicId", 
138         response = Topic.class)
139         @ApiResponses( value = {
140             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
141             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
142         })
143         @Path("/{topicId}")
144         public Response updateTopic( 
145                         @PathParam("topicId") String topicId
146                         ) {
147                 ApiService check = new ApiService();
148
149                 check.setCode(Status.BAD_REQUEST.getStatusCode());
150                 check.setMessage( "Method /PUT not supported for /topics");
151                 
152                 return check.error();
153         }
154                 
155         @DELETE
156         @ApiOperation( value = "return Topic details", 
157         notes = "Delete a  `Topic` object, identified by topicId", 
158         response = Topic.class)
159         @ApiResponses( value = {
160             @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
161             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
162         })
163         @Path("/{topicId}")
164         public Response deleteTopic( 
165                         @PathParam("topicId") String id
166                         ){
167                 ApiService check = new ApiService();
168
169                 try {
170                         check.required( "fqtn", id, "" );
171                 } catch( RequiredFieldException rfe ) {
172                         logger.error("Error", rfe);
173                         return check.error();
174                 }
175                 
176                 mr_topicService.removeTopic(id, check.getErr());
177                 if ( check.getErr().is2xx()) {
178                         return check.success(Status.NO_CONTENT.getStatusCode(), null);
179                 } 
180                 return check.error();
181         }
182         
183
184         @GET
185         @ApiOperation( value = "return Topic details", 
186         notes = "Retrieve a  `Topic` object, identified by topicId", 
187         response = Topic.class)
188         @ApiResponses( value = {
189             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
190             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
191         })
192         @Path("/{topicId}")
193         public Response getTopic( 
194                         @PathParam("topicId") String id
195                         ) {
196                 logger.info("Entry: /GET " + id);
197                 ApiService check = new ApiService();
198
199                 try {
200                         check.required( "topicName", id, "^\\S+$" );  //no white space allowed in topicName
201                 } catch( RequiredFieldException rfe ) {
202                         logger.error("Error", rfe);
203                         return check.error();
204                 }
205                 Topic mrc =  mr_topicService.getTopic( id, check.getErr() );
206                 if ( mrc == null ) {
207                         return check.error();
208                 }
209                 return check.success(mrc);
210                 }
211 }