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