2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.dbcapi.resources;
23 import io.swagger.annotations.Api;
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
28 import java.util.List;
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;
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;
53 import static javax.ws.rs.core.Response.Status.NO_CONTENT;
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)
61 public class MR_ClientResource extends BaseLoggingClass {
63 private MR_ClientService mr_clientService = new MR_ClientService();
64 private ResponseBuilder responseBuilder = new ResponseBuilder();
65 private RequiredChecker checker = new RequiredChecker();
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 )
75 public Response getMr_Clients() {
76 List<MR_Client> clients = mr_clientService.getAllMr_Clients();
78 GenericEntity<List<MR_Client>> list = new GenericEntity<List<MR_Client>>(clients) {
80 return responseBuilder.success(list);
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 )
94 public Response addMr_Client(
96 ApiService resp = new ApiService();
99 checker.required( "fqtn", client.getFqtn());
100 checker.required( "dcaeLocationName", client.getDcaeLocationName());
101 String s = client.getClientRole();
103 s = client.getClientIdentity();
105 checker.required( "clientRole or clientIdentity", s);
106 checker.required( "action", client.getAction());
108 } catch ( RequiredFieldException rfe ) {
109 logger.debug( rfe.getApiError().toString() );
110 return responseBuilder.error(rfe.getApiError());
112 MR_ClusterService clusters = new MR_ClusterService();
114 MR_Cluster cluster = clusters.getMr_Cluster(client.getDcaeLocationName(), resp.getErr());
115 if ( cluster == null ) {
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());
123 String url = cluster.getFqdn();
124 if ( url == null || url.isEmpty() ) {
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());
132 TopicService topics = new TopicService();
134 Topic t = topics.getTopic(client.getFqtn(), resp.getErr() );
136 return responseBuilder.error(resp.getErr());
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);
145 return responseBuilder.error(resp.getErr());
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 )
158 public Response updateMr_Client(
159 @PathParam("clientId") String clientId,
162 ApiService resp = new ApiService();
165 checker.required( "fqtn", client.getFqtn());
166 checker.required( "dcaeLocationName", client.getDcaeLocationName());
167 checker.required( "clientRole", client.getClientRole());
168 checker.required( "action", client.getAction());
170 } catch ( RequiredFieldException rfe ) {
171 logger.debug( rfe.getApiError().toString() );
172 return responseBuilder.error(rfe.getApiError());
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)
180 return Response.status(resp.getErr().getCode())
181 .entity( resp.getErr() )
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 )
194 public Response deleteMr_Client(
195 @PathParam("subId") String id
197 ApiService resp = new ApiService();
200 checker.required( "clientId", id);
201 } catch ( RequiredFieldException rfe ) {
202 logger.debug( rfe.getApiError().toString() );
203 return responseBuilder.error(rfe.getApiError());
205 mr_clientService.removeMr_Client(id, true, resp.getErr() );
206 if ( resp.getErr().is2xx()) {
207 return responseBuilder.success(NO_CONTENT.getStatusCode(), null);
210 return responseBuilder.error(resp.getErr());
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 )
222 public Response test(
223 @PathParam("subId") String id
225 ApiService resp = new ApiService();
228 checker.required( "clientId", id);
229 } catch ( RequiredFieldException rfe ) {
230 logger.debug( rfe.getApiError().toString() );
231 return responseBuilder.error(rfe.getApiError());
233 MR_Client nClient = mr_clientService.getMr_Client( id, resp.getErr() );
234 if ( resp.getErr().is2xx()) {
235 return responseBuilder.success(nClient);
237 return responseBuilder.error(resp.getErr());