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.MR_ClientService;
49 import org.onap.dmaap.dbcapi.service.MR_ClusterService;
50 import org.onap.dmaap.dbcapi.service.TopicService;
52 import static javax.ws.rs.core.Response.Status.NO_CONTENT;
56 @Api( value= "MR_Clients", description = "Endpoint for a Message Router Client that implements a Publisher or a Subscriber" )
57 @Consumes(MediaType.APPLICATION_JSON)
58 @Produces(MediaType.APPLICATION_JSON)
60 public class MR_ClientResource extends BaseLoggingClass {
62 private MR_ClientService mr_clientService = new MR_ClientService();
63 private ResponseBuilder responseBuilder = new ResponseBuilder();
64 private RequiredChecker checker = new RequiredChecker();
67 @ApiOperation( value = "return MR_Client details",
68 notes = "Returns array of `MR_Client` objects.",
69 response = MR_Client.class)
70 @ApiResponses( value = {
71 @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
72 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
74 public Response getMr_Clients() {
75 List<MR_Client> clients = mr_clientService.getAllMr_Clients();
77 GenericEntity<List<MR_Client>> list = new GenericEntity<List<MR_Client>>(clients) {
79 return responseBuilder.success(list);
83 @ApiOperation( value = "Associate an MR_Client object to a Topic",
84 notes = "Create a `MR_Client` object."
85 + "The `dcaeLocation` attribute is used to match an `MR_Cluster` object with the same value, with the intent of localizing message traffic."
86 + " In legacy implementation, the `clientRole` is granted appropriate permission in AAF."
87 + " Newer implementions may instead specify an AAF Identity, which will be added to the appropriate `Topic` role.",
88 response = MR_Client.class)
89 @ApiResponses( value = {
90 @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
91 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
93 public Response addMr_Client(MR_Client client) {
94 ApiError apiError = new ApiError();
97 checker.required( "fqtn", client.getFqtn());
98 checker.required( "dcaeLocationName", client.getDcaeLocationName());
99 String s = client.getClientRole();
101 s = client.getClientIdentity();
103 checker.required( "clientRole or clientIdentity", s);
104 checker.required( "action", client.getAction());
106 } catch ( RequiredFieldException rfe ) {
107 logger.debug( rfe.getApiError().toString() );
108 return responseBuilder.error(rfe.getApiError());
110 MR_ClusterService clusters = new MR_ClusterService();
112 MR_Cluster cluster = clusters.getMr_Cluster(client.getDcaeLocationName(), apiError);
113 if ( cluster == null ) {
115 apiError.setCode(Status.BAD_REQUEST.getStatusCode());
116 apiError.setMessage( "MR_Cluster alias not found for dcaeLocation: " + client.getDcaeLocationName());
117 apiError.setFields("dcaeLocationName");
118 logger.warn(apiError.toString());
119 return responseBuilder.error(apiError);
121 String url = cluster.getFqdn();
122 if ( url == null || url.isEmpty() ) {
124 apiError.setCode(Status.BAD_REQUEST.getStatusCode());
125 apiError.setMessage("FQDN not set for dcaeLocation " + client.getDcaeLocationName() );
126 apiError.setFields("fqdn");
127 logger.warn(apiError.toString());
128 return responseBuilder.error(apiError);
130 TopicService topics = new TopicService();
132 Topic t = topics.getTopic(client.getFqtn(), apiError);
134 return responseBuilder.error(apiError);
136 MR_Client nClient = mr_clientService.addMr_Client(client, t, apiError);
137 if (apiError.is2xx()) {
138 t = topics.getTopic(client.getFqtn(), apiError);
139 topics.checkForBridge(t, apiError);
140 return responseBuilder.success(nClient);
143 return responseBuilder.error(apiError);
148 @ApiOperation( value = "Update an MR_Client object",
149 notes = "Update a `MR_Client` object, specified by clientId",
150 response = MR_Client.class)
151 @ApiResponses( value = {
152 @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
153 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
156 public Response updateMr_Client(@PathParam("clientId") String clientId, MR_Client client) {
157 ApiError apiError = new ApiError();
160 checker.required( "fqtn", client.getFqtn());
161 checker.required( "dcaeLocationName", client.getDcaeLocationName());
162 checker.required( "clientRole", client.getClientRole());
163 checker.required( "action", client.getAction());
165 } catch ( RequiredFieldException rfe ) {
166 logger.debug( rfe.getApiError().toString() );
167 return responseBuilder.error(rfe.getApiError());
169 client.setMrClientId(clientId);
170 MR_Client nClient = mr_clientService.updateMr_Client(client, apiError);
171 if (apiError.is2xx()) {
172 return Response.ok(nClient)
175 return Response.status(apiError.getCode())
181 @ApiOperation( value = "Delete an MR_Client object",
182 notes = "Delete a `MR_Client` object, specified by clientId",
183 response = MR_Client.class)
184 @ApiResponses( value = {
185 @ApiResponse( code = 204, message = "Success", response = MR_Client.class),
186 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
189 public Response deleteMr_Client(@PathParam("subId") String id){
190 ApiError apiError = new ApiError();
193 checker.required( "clientId", id);
194 } catch ( RequiredFieldException rfe ) {
195 logger.debug( rfe.getApiError().toString() );
196 return responseBuilder.error(rfe.getApiError());
198 mr_clientService.removeMr_Client(id, true, apiError);
199 if (apiError.is2xx()) {
200 return responseBuilder.success(NO_CONTENT.getStatusCode(), null);
203 return responseBuilder.error(apiError);
207 @ApiOperation( value = "return MR_Client details",
208 notes = "Retrieve a `MR_Client` object, specified by clientId",
209 response = MR_Client.class)
210 @ApiResponses( value = {
211 @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
212 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
215 public Response test(@PathParam("subId") String id) {
216 ApiError apiError = new ApiError();
219 checker.required( "clientId", id);
220 } catch ( RequiredFieldException rfe ) {
221 logger.debug( rfe.getApiError().toString() );
222 return responseBuilder.error(rfe.getApiError());
224 MR_Client nClient = mr_clientService.getMr_Client(id, apiError);
225 if (apiError.is2xx()) {
226 return responseBuilder.success(nClient);
228 return responseBuilder.error(apiError);