49dc69a855b758140c436ee318c9dd16aaa0f903
[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", name);
122                         checker.required( "fqdn", node.getFqdn());
123                 } catch ( RequiredFieldException rfe ) {
124                         return responseBuilder.error(rfe.getApiError());
125                 }
126                 node.setFqdn(name);
127                 DR_Node nNode = dr_nodeService.updateDr_Node(node, resp.getErr());
128                 if ( resp.getErr().is2xx()) {
129                         return responseBuilder.success(nNode);
130                 }
131                 return responseBuilder.error(resp.getErr());
132         }
133         
134         @DELETE
135         @ApiOperation( value = "No Content", 
136         notes = "Delete a single `DR_Node` object.", 
137         response = DR_Node.class)
138         @ApiResponses( value = {
139             @ApiResponse( code = 204, message = "Success", response = DR_Node.class),
140             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
141         })
142         @Path("/{fqdn}")
143         public Response deleteDr_Node( 
144                         @PathParam("fqdn") String name
145                         ){
146
147                 ApiService resp = new ApiService();
148
149                 try {
150                         checker.required( "fqdn", name);
151                 } catch ( RequiredFieldException rfe ) {
152                         logger.debug( rfe.getApiError().toString() );
153                         return responseBuilder.error(rfe.getApiError());
154                 }
155                 dr_nodeService.removeDr_Node(name, resp.getErr());
156                 if ( resp.getErr().is2xx() ) {
157                         return responseBuilder.success(NO_CONTENT.getStatusCode(), null);
158                 }
159                 return responseBuilder.error(resp.getErr());
160         }
161
162         @GET
163         @ApiOperation( value = "return DR_Node details", 
164         notes = "Retrieve a single `DR_Node` object.", 
165         response = DR_Node.class)
166         @ApiResponses( value = {
167             @ApiResponse( code = 200, message = "Success", response = DR_Node.class),
168             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
169         })
170         @Path("/{fqdn}")
171         public Response get( 
172                         @PathParam("fqdn") String name
173                         ) {
174                 ApiService resp = new ApiService();
175
176                 DR_Node nNode = dr_nodeService.getDr_Node( name, resp.getErr() );
177                 if ( resp.getErr().is2xx() ) {
178                         return responseBuilder.success(nNode);
179                 }
180                 return responseBuilder.error(resp.getErr());
181         }
182 }