[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / main / java / org / onap / dmaap / dbcapi / resources / DR_NodeResource.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dcae
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
42 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
43 import org.onap.dmaap.dbcapi.model.ApiError;
44 import org.onap.dmaap.dbcapi.model.DR_Node;
45 import org.onap.dmaap.dbcapi.service.DR_NodeService;
46
47 import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
48 import static javax.ws.rs.core.Response.Status.NO_CONTENT;
49
50 @Path("/dr_nodes")
51 @Api( value= "dr_nodes", description = "Endpoint for a Data Router Node server" )
52 @Consumes(MediaType.APPLICATION_JSON)
53 @Produces(MediaType.APPLICATION_JSON)
54 @Authorization
55 public class DR_NodeResource extends BaseLoggingClass {
56
57         private DR_NodeService dr_nodeService = new DR_NodeService();
58         private ResponseBuilder responseBuilder = new ResponseBuilder();
59         private RequiredChecker checker = new RequiredChecker();
60         
61         @GET
62         @ApiOperation( value = "return DR_Node details", 
63         notes = "Returns array of `DR_Node` object array.  Need to add filter by dcaeLocation.", 
64         response = DR_Node.class)
65         @ApiResponses( value = {
66             @ApiResponse( code = 200, message = "Success", response = DR_Node.class),
67             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
68         })
69         public Response getDr_Nodes() {
70                 List<DR_Node> nodes = dr_nodeService.getAllDr_Nodes();
71
72                 GenericEntity<List<DR_Node>> list = new GenericEntity<List<DR_Node>>(nodes) {
73         };
74         return responseBuilder.success(list);
75         }
76         
77         @POST
78         @ApiOperation( value = "return DR_Node details", 
79         notes = "create a `DR_Node` in a *dcaeLocation*.  Note that multiple `DR_Node`s may exist in the same `dcaeLocation`.", 
80         response = DR_Node.class)
81         @ApiResponses( value = {
82             @ApiResponse( code = 200, message = "Success", response = DR_Node.class),
83             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
84         })
85         public Response addDr_Node(DR_Node node) {
86
87                 ApiError apiError = new ApiError();
88
89                 try {
90                         checker.required( "dcaeLocation", node.getDcaeLocationName());
91                         checker.required( "fqdn", node.getFqdn());
92                 } catch ( RequiredFieldException rfe ) {
93                         return responseBuilder.error(new ApiError(BAD_REQUEST.getStatusCode(),
94                                         "missing required field", "dcaeLocation, fqdn"));
95                 }
96                 DR_Node nNode = dr_nodeService.addDr_Node(node, apiError);
97                 if (apiError.is2xx()) {
98                         return responseBuilder.success(nNode);
99                 }
100                 return responseBuilder.error(apiError);
101         }
102         
103         @PUT
104         @ApiOperation( value = "return DR_Node details", 
105         notes = "Update a single `DR_Node` object.", 
106         response = DR_Node.class)
107         @ApiResponses( value = {
108             @ApiResponse( code = 200, message = "Success", response = DR_Node.class),
109             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
110         })
111         @Path("/{fqdn}")
112         public Response updateDr_Node(@PathParam("fqdn") String name, DR_Node node) {
113
114                 ApiError apiError = new ApiError();
115
116                 try {
117                         checker.required( "dcaeLocation", node.getDcaeLocationName());
118                         checker.required( "fqdn", node.getFqdn());
119                 } catch ( RequiredFieldException rfe ) {
120                         return responseBuilder.error(new ApiError(BAD_REQUEST.getStatusCode(),
121                                         "missing required field", "dcaeLocation, fqdn"));
122                 }
123                 node.setFqdn(name);
124                 DR_Node nNode = dr_nodeService.updateDr_Node(node, apiError);
125                 if (apiError.is2xx()) {
126                         return responseBuilder.success(nNode);
127                 }
128                 return responseBuilder.error(apiError);
129         }
130         
131         @DELETE
132         @ApiOperation( value = "No Content", 
133         notes = "Delete a single `DR_Node` object.", 
134         response = DR_Node.class)
135         @ApiResponses( value = {
136             @ApiResponse( code = 204, message = "Success", response = DR_Node.class),
137             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
138         })
139         @Path("/{fqdn}")
140         public Response deleteDr_Node( 
141                         @PathParam("fqdn") String name){
142
143
144                 ApiError apiError = new ApiError();
145
146                 dr_nodeService.removeDr_Node(name, apiError);
147                 if (apiError.is2xx()) {
148                         return responseBuilder.success(NO_CONTENT.getStatusCode(), null);
149                 }
150                 return responseBuilder.error(apiError);
151         }
152
153         @GET
154         @ApiOperation( value = "return DR_Node details", 
155         notes = "Retrieve a single `DR_Node` object.", 
156         response = DR_Node.class)
157         @ApiResponses( value = {
158             @ApiResponse( code = 200, message = "Success", response = DR_Node.class),
159             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
160         })
161         @Path("/{fqdn}")
162         public Response get(@PathParam("fqdn") String name) {
163
164                 ApiError apiError = new ApiError();
165
166                 DR_Node nNode = dr_nodeService.getDr_Node( name, apiError );
167                 if (apiError.is2xx()) {
168                         return responseBuilder.success(nNode);
169                 }
170                 return responseBuilder.error(apiError);
171         }
172 }