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