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