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