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