DMAAP-83 Initial code import
[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.Topic;
48 import org.onap.dmaap.dbcapi.service.ApiService;
49 import org.onap.dmaap.dbcapi.service.TopicService;
50
51 @Path("/topics")
52 @Api( value= "topics", description = "Endpoint for retreiving MR Topics" )
53 @Consumes(MediaType.APPLICATION_JSON)
54 @Produces(MediaType.APPLICATION_JSON)
55 @Authorization
56 public class TopicResource extends BaseLoggingClass {
57
58         TopicService mr_topicService = new TopicService();
59                 
60         @GET
61         @ApiOperation( value = "return Topic details", 
62         notes = "Returns array of  `Topic` objects.", 
63         response = Topic.class)
64         @ApiResponses( value = {
65             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
66             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
67         })
68         public Response getTopics() {
69
70                 ApiService check = new ApiService();
71
72                 List<Topic> allTopics = mr_topicService.getAllTopics();
73                 
74                 GenericEntity<List<Topic>> list = new GenericEntity<List<Topic>>(allTopics) {
75                         };
76                 return check.success(list);
77                 
78         }
79                 
80         @POST
81         @ApiOperation( value = "return Topic details", 
82         notes = "Create  `Topic` object.", 
83         response = Topic.class)
84         @ApiResponses( value = {
85             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
86             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
87         })
88         public Response  addTopic( 
89                         Topic topic
90                         ) {
91                 logger.info( "addTopic request: " + String.valueOf(topic) );
92                 ApiService check = new ApiService();
93
94                 try {
95                         check.required( "topicName", topic.getTopicName(), "^\\S+$" );  //no white space allowed in topicName
96                         check.required( "topicDescription", topic.getTopicDescription(), "" );
97                         check.required( "owner", topic.getOwner(), "" );
98                 } catch( RequiredFieldException rfe ) {
99                         return check.error();
100                 }
101                 
102                 //String repReq = topic.getReplicationRequest();
103                 ReplicationType t = topic.getReplicationCase();
104                 if ( t == null || t == ReplicationType.REPLICATION_NOT_SPECIFIED ) {
105                         topic.setReplicationCase( mr_topicService.reviewTopic(topic));
106                 } 
107                 
108                 topic.setLastMod();
109                 
110                 Topic mrc =  mr_topicService.addTopic(topic, check.getErr());
111                 if ( mrc != null && mrc.isStatusValid() ) {
112                         return check.success(Status.CREATED.getStatusCode(), mrc);
113                 }
114                 return check.error();
115         }
116         
117         @PUT
118         @ApiOperation( value = "return Topic details", 
119         notes = "Update a  `Topic` object, identified by topicId", 
120         response = Topic.class)
121         @ApiResponses( value = {
122             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
123             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
124         })
125         @Path("/{topicId}")
126         public Response updateTopic( 
127                         @PathParam("topicId") String topicId
128                         ) {
129                 ApiService check = new ApiService();
130
131                 check.setCode(Status.BAD_REQUEST.getStatusCode());
132                 check.setMessage( "Method /PUT not supported for /topics");
133                 
134                 return check.error();
135         }
136                 
137         @DELETE
138         @ApiOperation( value = "return Topic details", 
139         notes = "Delete a  `Topic` object, identified by topicId", 
140         response = Topic.class)
141         @ApiResponses( value = {
142             @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
143             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
144         })
145         @Path("/{topicId}")
146         public Response deleteTopic( 
147                         @PathParam("topicId") String id
148                         ){
149                 ApiService check = new ApiService();
150
151                 try {
152                         check.required( "fqtn", id, "" );
153                 } catch( RequiredFieldException rfe ) {
154                         return check.error();
155                 }
156                 
157                 mr_topicService.removeTopic(id, check.getErr());
158                 if ( check.getErr().is2xx()) {
159                         return check.success(Status.NO_CONTENT.getStatusCode(), null);
160                 } 
161                 return check.error();
162         }
163         
164
165         @GET
166         @ApiOperation( value = "return Topic details", 
167         notes = "Retrieve a  `Topic` object, identified by topicId", 
168         response = Topic.class)
169         @ApiResponses( value = {
170             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
171             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
172         })
173         @Path("/{topicId}")
174         public Response getTopic( 
175                         @PathParam("topicId") String id
176                         ) {
177                 logger.info("Entry: /GET " + id);
178                 ApiService check = new ApiService();
179
180                 try {
181                         check.required( "topicName", id, "^\\S+$" );  //no white space allowed in topicName
182                 } catch( RequiredFieldException rfe ) {
183                         return check.error();
184                 }
185                 Topic mrc =  mr_topicService.getTopic( id, check.getErr() );
186                 if ( mrc == null ) {
187                         return check.error();
188                 }
189                 return check.success(mrc);
190                 }
191 }