Create ResponseBuilder class
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / resources / DmaapResource.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 //
22 // $Id$
23
24 package org.onap.dmaap.dbcapi.resources;
25
26
27
28 import io.swagger.annotations.Api;
29 import io.swagger.annotations.ApiOperation;
30 import io.swagger.annotations.ApiResponse;
31 import io.swagger.annotations.ApiResponses;
32
33 import javax.ws.rs.Consumes;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.POST;
36 import javax.ws.rs.PUT;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.Produces;
39 import javax.ws.rs.core.Context;
40 import javax.ws.rs.core.MediaType;
41 import javax.ws.rs.core.Response;
42 import javax.ws.rs.core.UriInfo;
43
44 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
45 import org.onap.dmaap.dbcapi.model.ApiError;
46 import org.onap.dmaap.dbcapi.model.Dmaap;
47 import org.onap.dmaap.dbcapi.service.ApiService;
48 import org.onap.dmaap.dbcapi.service.DmaapService;
49
50
51
52 @Path("/dmaap")
53 @Api( value= "dmaap", description = "Endpoint for this instance of DMaaP object containing values for this OpenDCAE deployment" )
54 @Consumes(MediaType.APPLICATION_JSON)
55 @Produces(MediaType.APPLICATION_JSON)
56 @Authorization
57 public class DmaapResource extends BaseLoggingClass {
58
59
60         private DmaapService dmaapService = new DmaapService();
61         private ResponseBuilder responseBuilder = new ResponseBuilder();
62         
63         @GET
64         @ApiOperation( value = "return dmaap details", notes = "returns the `dmaap` object, which contains system wide configuration settings", response = Dmaap.class)
65     @ApiResponses( value = {
66         @ApiResponse( code = 200, message = "Success", response = Dmaap.class),
67         @ApiResponse( code = 400, message = "Error", response = ApiError.class )
68     })
69
70         public Response getDmaap(@Context UriInfo uriInfo)  {
71                 Dmaap d =  dmaapService.getDmaap();
72                 return responseBuilder.success(d);
73         }
74         
75         @POST
76         @ApiOperation( value = "return dmaap details", notes = "Create a new DMaaP set system wide configuration settings for the *dcaeEnvironment*.  Deprecated with introduction of persistence in 1610.", response = Dmaap.class)
77     @ApiResponses( value = {
78         @ApiResponse( code = 200, message = "Success", response = Dmaap.class),
79         @ApiResponse( code = 400, message = "Error", response = ApiError.class )
80     })
81         public Response addDmaap( Dmaap obj ) {
82                 ApiService check = new ApiService();    
83
84                 try { //check for required fields
85                         check.required( "dmaapName", obj.getDmaapName(), "^\\S+$" );  //no white space allowed in dmaapName
86                         check.required( "dmaapProvUrl", obj.getDrProvUrl(), "" );
87                         check.required( "topicNsRoot", obj.getTopicNsRoot(), "" );
88                         check.required( "bridgeAdminTopic", obj.getBridgeAdminTopic(), "" );
89                 } catch( RequiredFieldException rfe ) {
90                         return responseBuilder.error(check.getErr());
91                 }
92         
93                 Dmaap d =  dmaapService.addDmaap(obj);
94                 if ( d == null ) {
95                         return responseBuilder.notFound();
96
97                 } 
98
99                 return responseBuilder.success(d);
100         }
101         
102         @PUT
103         @ApiOperation( value = "return dmaap details", notes = "Update system settings for *dcaeEnvironment*.", response = Dmaap.class)
104     @ApiResponses( value = {
105         @ApiResponse( code = 200, message = "Success", response = Dmaap.class),
106         @ApiResponse( code = 400, message = "Error", response = ApiError.class )
107     })
108         public Response updateDmaap(  Dmaap obj ) {
109                 ApiService check = new ApiService();
110
111                 try { //check for required fields
112                         check.required( "dmaapName", obj.getDmaapName(), "^\\S+$" );  //no white space allowed in dmaapName
113                         check.required( "dmaapProvUrl", obj.getDrProvUrl(), "" );
114                         check.required( "topicNsRoot", obj.getTopicNsRoot(), "" );
115                         check.required( "bridgeAdminTopic", obj.getBridgeAdminTopic(), "" );
116                 } catch( RequiredFieldException rfe ) {
117                         return responseBuilder.error(check.getErr());
118                 }
119                 Dmaap d =  dmaapService.updateDmaap(obj);
120                 if ( d != null ) {
121                         return responseBuilder.success(d);
122                 } else {
123                         return responseBuilder.notFound();
124                 }       
125         }
126         
127         
128 }