Fix object references to show fields
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / resources / MR_ClusterResource.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 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.MR_Cluster;
46 import org.onap.dmaap.dbcapi.service.ApiService;
47 import org.onap.dmaap.dbcapi.service.MR_ClientService;
48 import org.onap.dmaap.dbcapi.service.MR_ClusterService;
49
50
51 @Path("/mr_clusters")
52 @Api( value= "MR_Clusters", description = "Endpoint for a Message Router servers in a Cluster configuration" )
53 @Consumes(MediaType.APPLICATION_JSON)
54 @Produces(MediaType.APPLICATION_JSON)
55 @Authorization
56 public class MR_ClusterResource extends BaseLoggingClass {
57
58         MR_ClusterService mr_clusterService = new MR_ClusterService();
59         MR_ClientService mr_clients = new MR_ClientService();
60                 
61         @GET
62         @ApiOperation( value = "return MR_Cluster details", 
63         notes = "Returns array of  `MR_Cluster` objects.", 
64         response = MR_Cluster.class)
65         @ApiResponses( value = {
66             @ApiResponse( code = 200, message = "Success", response = MR_Cluster.class),
67             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
68         })
69         public Response getMr_Clusters() {
70                 ApiService resp = new ApiService();
71
72                 List<MR_Cluster> clusters = mr_clusterService.getAllMr_Clusters();
73
74                 GenericEntity<List<MR_Cluster>> list = new GenericEntity<List<MR_Cluster>>(clusters) {
75         };
76         return resp.success(list);
77         }
78                 
79         @POST
80         @ApiOperation( value = "return MR_Cluster details", 
81         notes = "Create an  `MR_Cluster` object.", 
82         response = MR_Cluster.class)
83         @ApiResponses( value = {
84             @ApiResponse( code = 200, message = "Success", response = MR_Cluster.class),
85             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
86         })
87         public Response  addMr_Cluster( 
88                         MR_Cluster cluster
89                         ) {
90                 ApiService resp = new ApiService();
91
92                 try {
93                         resp.required( "dcaeLocationName", cluster.getDcaeLocationName(), "" );  
94                         resp.required( "fqdn", cluster.getFqdn(), "" );
95                 } catch( RequiredFieldException rfe ) {
96                         return resp.error();
97                 }
98                 MR_Cluster mrc =  mr_clusterService.addMr_Cluster(cluster, resp.getErr() );
99                 if ( mrc != null && mrc.isStatusValid() ) {
100                         return resp.success(Status.CREATED.getStatusCode(), mrc);
101                 }
102                 return resp.error();
103
104         }
105                 
106         @PUT
107         @ApiOperation( value = "return MR_Cluster details", 
108         notes = "Update an  `MR_Cluster` object, specified by clusterId.", 
109         response = MR_Cluster.class)
110         @ApiResponses( value = {
111             @ApiResponse( code = 200, message = "Success", response = MR_Cluster.class),
112             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
113         })
114         @Path("/{clusterId}")
115         public Response updateMr_Cluster( 
116                         @PathParam("clusterId") String clusterId, 
117                         MR_Cluster cluster
118                         ) {
119                 ApiService resp = new ApiService();
120
121                 try {
122                         resp.required( "fqdn", clusterId, "" );
123                         resp.required( "dcaeLocationName", cluster.getDcaeLocationName(), "" );  
124                 } catch( RequiredFieldException rfe ) {
125                         return resp.error();
126                 }
127                 cluster.setDcaeLocationName(clusterId);
128                 MR_Cluster mrc =  mr_clusterService.updateMr_Cluster(cluster, resp.getErr() );
129                 if ( mrc != null && mrc.isStatusValid() ) {
130                         return resp.success(Status.CREATED.getStatusCode(), mrc);
131                 }
132                 return resp.error();
133         }
134                 
135         @DELETE
136         @ApiOperation( value = "return MR_Cluster details", 
137         notes = "Delete an  `MR_Cluster` object, specified by clusterId.", 
138         response = MR_Cluster.class)
139         @ApiResponses( value = {
140             @ApiResponse( code = 204, message = "Success", response = MR_Cluster.class),
141             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
142         })
143         @Path("/{clusterId}")
144         public Response deleteMr_Cluster( 
145                         @PathParam("clusterId") String id
146                         ){
147                 ApiService resp = new ApiService();
148
149                 try {
150                         resp.required( "fqdn", id, "" );
151                 } catch( RequiredFieldException rfe ) {
152                         return resp.error();
153                 }
154                 mr_clusterService.removeMr_Cluster(id, resp.getErr() );
155                 if ( resp.getErr().is2xx()) {
156                         return resp.success(Status.NO_CONTENT.getStatusCode(), null);
157                 } 
158                 return resp.error();
159         }
160
161         @GET
162         @ApiOperation( value = "return MR_Cluster details", 
163         notes = "Retrieve an  `MR_Cluster` object, specified by clusterId.", 
164         response = MR_Cluster.class)
165         @ApiResponses( value = {
166             @ApiResponse( code = 200, message = "Success", response = MR_Cluster.class),
167             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
168         })
169         @Path("/{clusterId}")
170         public Response getMR_Cluster( 
171                         @PathParam("clusterId") String id
172                         ) {
173                 ApiService resp = new ApiService();
174
175                 try {
176                         resp.required( "dcaeLocationName", id, "" );
177                 } catch( RequiredFieldException rfe ) {
178                         return resp.error();
179                 }
180                 MR_Cluster mrc =  mr_clusterService.getMr_Cluster( id, resp.getErr() );
181                 if ( mrc != null && mrc.isStatusValid() ) {
182                         return resp.success(Status.CREATED.getStatusCode(), mrc);
183                 }
184                 return resp.error();
185         }
186 }