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;
27 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
28 import org.onap.dmaap.dbcapi.model.ApiError;
29 import org.onap.dmaap.dbcapi.model.MR_Client;
30 import org.onap.dmaap.dbcapi.model.MR_Cluster;
31 import org.onap.dmaap.dbcapi.model.Topic;
32 import org.onap.dmaap.dbcapi.service.MR_ClientService;
33 import org.onap.dmaap.dbcapi.service.MR_ClusterService;
34 import org.onap.dmaap.dbcapi.service.TopicService;
36 import javax.ws.rs.Consumes;
37 import javax.ws.rs.DELETE;
38 import javax.ws.rs.GET;
39 import javax.ws.rs.POST;
40 import javax.ws.rs.PUT;
41 import javax.ws.rs.Path;
42 import javax.ws.rs.PathParam;
43 import javax.ws.rs.Produces;
44 import javax.ws.rs.core.GenericEntity;
45 import javax.ws.rs.core.MediaType;
46 import javax.ws.rs.core.Response;
47 import javax.ws.rs.core.Response.Status;
48 import java.util.List;
50 import static javax.ws.rs.core.Response.Status.NO_CONTENT;
54 @Api( value= "MR_Clients", description = "Endpoint for a Message Router Client that implements a Publisher or a Subscriber" )
55 @Consumes(MediaType.APPLICATION_JSON)
56 @Produces(MediaType.APPLICATION_JSON)
58 public class MR_ClientResource extends BaseLoggingClass {
60 private MR_ClientService mr_clientService = new MR_ClientService();
61 private ResponseBuilder responseBuilder = new ResponseBuilder();
62 private RequiredChecker checker = new RequiredChecker();
65 @ApiOperation( value = "return MR_Client details",
66 notes = "Returns array of `MR_Client` objects.",
67 response = MR_Client.class)
68 @ApiResponses( value = {
69 @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
70 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
72 public Response getMr_Clients() {
73 List<MR_Client> clients = mr_clientService.getAllMr_Clients();
75 GenericEntity<List<MR_Client>> list = new GenericEntity<List<MR_Client>>(clients) {
77 return responseBuilder.success(list);
81 @ApiOperation( value = "Associate an MR_Client object to a Topic",
82 notes = "Create a `MR_Client` object."
83 + "The `dcaeLocation` attribute is used to match an `MR_Cluster` object with the same value, with the intent of localizing message traffic."
84 + " In legacy implementation, the `clientRole` is granted appropriate permission in AAF."
85 + " Newer implementions may instead specify an AAF Identity, which will be added to the appropriate `Topic` role.",
86 response = MR_Client.class)
87 @ApiResponses( value = {
88 @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
89 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
91 public Response addMr_Client(MR_Client client) {
92 ApiError apiError = new ApiError();
95 checker.required( "fqtn", client.getFqtn());
96 checker.required( "dcaeLocationName", client.getDcaeLocationName());
97 String s = client.getClientRole();
99 s = client.getClientIdentity();
101 checker.required( "clientRole or clientIdentity", s);
102 checker.required( "action", client.getAction());
104 } catch ( RequiredFieldException rfe ) {
105 logger.debug( rfe.getApiError().toString() );
106 return responseBuilder.error(rfe.getApiError());
108 MR_ClusterService clusters = new MR_ClusterService();
110 MR_Cluster cluster = clusters.getMr_Cluster(client.getDcaeLocationName(), apiError);
111 if ( cluster == null ) {
113 apiError.setCode(Status.BAD_REQUEST.getStatusCode());
114 apiError.setMessage( "MR_Cluster alias not found for dcaeLocation: " + client.getDcaeLocationName());
115 apiError.setFields("dcaeLocationName");
116 logger.warn(apiError.toString());
117 return responseBuilder.error(apiError);
120 TopicService topics = new TopicService();
122 Topic t = topics.getTopic(client.getFqtn(), apiError);
124 return responseBuilder.error(apiError);
126 MR_Client nClient = mr_clientService.addMr_Client(client, t, apiError);
127 if (apiError.is2xx()) {
128 t = topics.getTopic(client.getFqtn(), apiError);
129 topics.checkForBridge(t, apiError);
130 return responseBuilder.success(nClient);
133 return responseBuilder.error(apiError);
138 @ApiOperation( value = "Update an MR_Client object",
139 notes = "Update a `MR_Client` object, specified by clientId",
140 response = MR_Client.class)
141 @ApiResponses( value = {
142 @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
143 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
146 public Response updateMr_Client(@PathParam("clientId") String clientId, MR_Client client) {
147 ApiError apiError = new ApiError();
150 checker.required( "fqtn", client.getFqtn());
151 checker.required( "dcaeLocationName", client.getDcaeLocationName());
152 checker.required( "clientRole", client.getClientRole());
153 checker.required( "action", client.getAction());
155 } catch ( RequiredFieldException rfe ) {
156 logger.debug( rfe.getApiError().toString() );
157 return responseBuilder.error(rfe.getApiError());
159 client.setMrClientId(clientId);
160 MR_Client nClient = mr_clientService.updateMr_Client(client, apiError);
161 if (apiError.is2xx()) {
162 return Response.ok(nClient)
165 return Response.status(apiError.getCode())
171 @ApiOperation( value = "Delete an MR_Client object",
172 notes = "Delete a `MR_Client` object, specified by clientId",
173 response = MR_Client.class)
174 @ApiResponses( value = {
175 @ApiResponse( code = 204, message = "Success", response = MR_Client.class),
176 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
179 public Response deleteMr_Client(@PathParam("subId") String id){
180 ApiError apiError = new ApiError();
182 mr_clientService.removeMr_Client(id, true, apiError);
183 if (apiError.is2xx()) {
184 return responseBuilder.success(NO_CONTENT.getStatusCode(), null);
187 return responseBuilder.error(apiError);
191 @ApiOperation( value = "return MR_Client details",
192 notes = "Retrieve a `MR_Client` object, specified by clientId",
193 response = MR_Client.class)
194 @ApiResponses( value = {
195 @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
196 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
199 public Response getMr_Client(@PathParam("subId") String id) {
200 ApiError apiError = new ApiError();
202 MR_Client nClient = mr_clientService.getMr_Client(id, apiError);
203 if (apiError.is2xx()) {
204 return responseBuilder.success(nClient);
206 return responseBuilder.error(apiError);