Extract required(...) method
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / resources / MR_ClientResource.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_Client;
46 import org.onap.dmaap.dbcapi.model.MR_Cluster;
47 import org.onap.dmaap.dbcapi.model.Topic;
48 import org.onap.dmaap.dbcapi.service.ApiService;
49 import org.onap.dmaap.dbcapi.service.MR_ClientService;
50 import org.onap.dmaap.dbcapi.service.MR_ClusterService;
51 import org.onap.dmaap.dbcapi.service.TopicService;
52
53 import static javax.ws.rs.core.Response.Status.NO_CONTENT;
54
55
56 @Path("/mr_clients")
57 @Api( value= "MR_Clients", description = "Endpoint for a Message Router Client that implements a Publisher or a Subscriber" )
58 @Consumes(MediaType.APPLICATION_JSON)
59 @Produces(MediaType.APPLICATION_JSON)
60 @Authorization
61 public class MR_ClientResource extends BaseLoggingClass {
62
63         private MR_ClientService mr_clientService = new MR_ClientService();
64         private ResponseBuilder responseBuilder = new ResponseBuilder();
65         private RequiredChecker checker = new RequiredChecker();
66                 
67         @GET
68         @ApiOperation( value = "return MR_Client details", 
69         notes = "Returns array of  `MR_Client` objects.", 
70         response = MR_Client.class)
71         @ApiResponses( value = {
72             @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
73             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
74         })
75         public Response getMr_Clients() {
76                 List<MR_Client> clients = mr_clientService.getAllMr_Clients();
77
78                 GenericEntity<List<MR_Client>> list = new GenericEntity<List<MR_Client>>(clients) {
79         };
80         return responseBuilder.success(list);
81         }
82                 
83         @POST
84         @ApiOperation( value = "Associate an MR_Client object to a Topic", 
85         notes = "Create a  `MR_Client` object."
86                         + "The `dcaeLocation` attribute is used to match an `MR_Cluster` object with the same value, with the intent of localizing message traffic."
87                         + "  In legacy implementation, the `clientRole` is granted appropriate permission in AAF."
88                         + "  Newer implementions may instead specify an AAF Identity, which will be added to the appropriate `Topic` role.", 
89         response = MR_Client.class)
90         @ApiResponses( value = {
91             @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
92             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
93         })
94         public Response addMr_Client( 
95                         MR_Client client) {
96                 ApiService resp = new ApiService();
97
98                 try {
99                         checker.required( "fqtn", client.getFqtn());
100                         checker.required( "dcaeLocationName", client.getDcaeLocationName());
101                         String s = client.getClientRole();
102                         if ( s == null ) {
103                                 s = client.getClientIdentity();
104                         }
105                         checker.required( "clientRole or clientIdentity", s);
106                         checker.required( "action", client.getAction());
107
108                 } catch ( RequiredFieldException rfe ) {
109                         logger.debug( rfe.getApiError().toString() );
110                         return responseBuilder.error(rfe.getApiError());
111                 }
112                 MR_ClusterService clusters = new MR_ClusterService();
113
114                 MR_Cluster cluster = clusters.getMr_Cluster(client.getDcaeLocationName(), resp.getErr());
115                 if ( cluster == null ) {
116
117                         resp.setCode(Status.BAD_REQUEST.getStatusCode());
118                         resp.setMessage( "MR_Cluster alias not found for dcaeLocation: " + client.getDcaeLocationName());
119                         resp.setFields("dcaeLocationName");
120                         logger.warn( resp.toString() );
121                         return responseBuilder.error(resp.getErr());
122                 }
123                 String url = cluster.getFqdn();
124                 if ( url == null || url.isEmpty() ) {
125
126                         resp.setCode(Status.BAD_REQUEST.getStatusCode());
127                         resp.setMessage("FQDN not set for dcaeLocation " + client.getDcaeLocationName() );
128                         resp.setFields("fqdn");
129                         logger.warn( resp.toString() );
130                         return responseBuilder.error(resp.getErr());
131                 }
132                 TopicService topics = new TopicService();
133
134                 Topic t = topics.getTopic(client.getFqtn(), resp.getErr() );
135                 if ( t == null ) {
136                         return responseBuilder.error(resp.getErr());
137                 }
138                 MR_Client nClient =  mr_clientService.addMr_Client(client, t, resp.getErr());
139                 if ( resp.getErr().is2xx()) {
140                         t = topics.getTopic(client.getFqtn(),  resp.getErr());
141                         topics.checkForBridge(t, resp.getErr());
142                         return responseBuilder.success(nClient);
143                 }
144                 else {
145                         return responseBuilder.error(resp.getErr());
146                 }
147         }
148                 
149         @PUT
150         @ApiOperation( value = "Update an MR_Client object", 
151         notes = "Update a  `MR_Client` object, specified by clientId", 
152         response = MR_Client.class)
153         @ApiResponses( value = {
154             @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
155             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
156         })
157         @Path("/{clientId}")
158         public Response updateMr_Client( 
159                         @PathParam("clientId") String clientId, 
160                         MR_Client client
161                         ) {
162                 ApiService resp = new ApiService();
163
164                 try {
165                         checker.required( "fqtn", client.getFqtn());
166                         checker.required( "dcaeLocationName", client.getDcaeLocationName());
167                         checker.required( "clientRole", client.getClientRole());
168                         checker.required( "action", client.getAction());
169
170                 } catch ( RequiredFieldException rfe ) {
171                         logger.debug( rfe.getApiError().toString() );
172                         return responseBuilder.error(rfe.getApiError());
173                 }
174                 client.setMrClientId(clientId);
175                 MR_Client nClient = mr_clientService.updateMr_Client(client, resp.getErr() );
176                 if ( resp.getErr().is2xx()) {
177                         return Response.ok(nClient)
178                                 .build();
179                 }
180                 return Response.status(resp.getErr().getCode())
181                                 .entity( resp.getErr() )
182                                 .build();
183         }
184                 
185         @DELETE
186         @ApiOperation( value = "Delete an MR_Client object", 
187         notes = "Delete a  `MR_Client` object, specified by clientId", 
188         response = MR_Client.class)
189         @ApiResponses( value = {
190             @ApiResponse( code = 204, message = "Success", response = MR_Client.class),
191             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
192         })
193         @Path("/{subId}")
194         public Response deleteMr_Client( 
195                         @PathParam("subId") String id
196                         ){
197                 ApiService resp = new ApiService();
198
199                 try {
200                         checker.required( "clientId", id);
201                 } catch ( RequiredFieldException rfe ) {
202                         logger.debug( rfe.getApiError().toString() );
203                         return responseBuilder.error(rfe.getApiError());
204                 }
205                 mr_clientService.removeMr_Client(id, true, resp.getErr() );
206                 if ( resp.getErr().is2xx()) {
207                         return responseBuilder.success(NO_CONTENT.getStatusCode(), null);
208                 }
209                 
210                 return responseBuilder.error(resp.getErr());
211         }
212
213         @GET
214         @ApiOperation( value = "return MR_Client details", 
215         notes = "Retrieve a  `MR_Client` object, specified by clientId", 
216         response = MR_Client.class)
217         @ApiResponses( value = {
218             @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
219             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
220         })
221         @Path("/{subId}")
222         public Response test( 
223                         @PathParam("subId") String id
224                         ) {
225                 ApiService resp = new ApiService();
226
227                 try {
228                         checker.required( "clientId", id);
229                 } catch ( RequiredFieldException rfe ) {
230                         logger.debug( rfe.getApiError().toString() );
231                         return responseBuilder.error(rfe.getApiError());
232                 }
233                 MR_Client nClient =  mr_clientService.getMr_Client( id, resp.getErr() );
234                 if ( resp.getErr().is2xx()) {
235                         return responseBuilder.success(nClient);
236                 }
237                 return responseBuilder.error(resp.getErr());
238         }
239 }