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;
55 @Api( value= "MR_Clients", description = "Endpoint for a Message Router Client that implements a Publisher or a Subscriber" )
56 @Consumes(MediaType.APPLICATION_JSON)
57 @Produces(MediaType.APPLICATION_JSON)
59 public class MR_ClientResource extends BaseLoggingClass {
61 private MR_ClientService mr_clientService = new MR_ClientService();
64 @ApiOperation( value = "return MR_Client details",
65 notes = "Returns array of `MR_Client` objects.",
66 response = MR_Client.class)
67 @ApiResponses( value = {
68 @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
69 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
71 public Response getMr_Clients() {
72 ApiService resp = new ApiService();
74 List<MR_Client> clients = mr_clientService.getAllMr_Clients();
76 GenericEntity<List<MR_Client>> list = new GenericEntity<List<MR_Client>>(clients) {
78 return resp.success(list);
82 @ApiOperation( value = "Associate an MR_Client object to a Topic",
83 notes = "Create a `MR_Client` object."
84 + "The `dcaeLocation` attribute is used to match an `MR_Cluster` object with the same value, with the intent of localizing message traffic."
85 + " In legacy implementation, the `clientRole` is granted appropriate permission in AAF."
86 + " Newer implementions may instead specify an AAF Identity, which will be added to the appropriate `Topic` role.",
87 response = MR_Client.class)
88 @ApiResponses( value = {
89 @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
90 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
92 public Response addMr_Client(
95 ApiService resp = new ApiService();
98 resp.required( "fqtn", client.getFqtn(), "");
99 resp.required( "dcaeLocationName", client.getDcaeLocationName(), "");
100 String s = client.getClientRole();
102 s = client.getClientIdentity();
104 resp.required( "clientRole or clientIdentity", s, "" );
105 resp.required( "action", client.getAction(), "");
107 } catch ( RequiredFieldException rfe ) {
108 logger.debug( resp.toString() );
111 MR_ClusterService clusters = new MR_ClusterService();
113 MR_Cluster cluster = clusters.getMr_Cluster(client.getDcaeLocationName(), resp.getErr());
114 if ( cluster == null ) {
116 resp.setCode(Status.BAD_REQUEST.getStatusCode());
117 resp.setMessage( "MR_Cluster alias not found for dcaeLocation: " + client.getDcaeLocationName());
118 resp.setFields("dcaeLocationName");
119 logger.warn( resp.toString() );
122 String url = cluster.getFqdn();
123 if ( url == null || url.isEmpty() ) {
125 resp.setCode(Status.BAD_REQUEST.getStatusCode());
126 resp.setMessage("FQDN not set for dcaeLocation " + client.getDcaeLocationName() );
127 resp.setFields("fqdn");
128 logger.warn( resp.toString() );
131 TopicService topics = new TopicService();
133 Topic t = topics.getTopic(client.getFqtn(), resp.getErr() );
137 MR_Client nClient = mr_clientService.addMr_Client(client, t, resp.getErr());
138 if ( resp.getErr().is2xx()) {
139 t = topics.getTopic(client.getFqtn(), resp.getErr());
140 topics.checkForBridge(t, resp.getErr());
141 return resp.success(nClient);
149 @ApiOperation( value = "Update an MR_Client object",
150 notes = "Update a `MR_Client` object, specified by clientId",
151 response = MR_Client.class)
152 @ApiResponses( value = {
153 @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
154 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
157 public Response updateMr_Client(
158 @PathParam("clientId") String clientId,
161 ApiService resp = new ApiService();
164 resp.required( "fqtn", client.getFqtn(), "");
165 resp.required( "dcaeLocationName", client.getDcaeLocationName(), "");
166 resp.required( "clientRole", client.getClientRole(), "" );
167 resp.required( "action", client.getAction(), "");
169 } catch ( RequiredFieldException rfe ) {
170 logger.debug( resp.toString() );
173 client.setMrClientId(clientId);
174 MR_Client nClient = mr_clientService.updateMr_Client(client, resp.getErr() );
175 if ( resp.getErr().is2xx()) {
176 return Response.ok(nClient)
179 return Response.status(resp.getErr().getCode())
180 .entity( resp.getErr() )
185 @ApiOperation( value = "Delete an MR_Client object",
186 notes = "Delete a `MR_Client` object, specified by clientId",
187 response = MR_Client.class)
188 @ApiResponses( value = {
189 @ApiResponse( code = 204, message = "Success", response = MR_Client.class),
190 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
193 public Response deleteMr_Client(
194 @PathParam("subId") String id
196 ApiService resp = new ApiService();
199 resp.required( "clientId", id, "");
200 } catch ( RequiredFieldException rfe ) {
201 logger.debug( resp.toString() );
204 mr_clientService.removeMr_Client(id, true, resp.getErr() );
205 if ( resp.getErr().is2xx()) {
206 return resp.success(Status.NO_CONTENT.getStatusCode(), null);
213 @ApiOperation( value = "return MR_Client details",
214 notes = "Retrieve a `MR_Client` object, specified by clientId",
215 response = MR_Client.class)
216 @ApiResponses( value = {
217 @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
218 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
221 public Response test(
222 @PathParam("subId") String id
224 ApiService resp = new ApiService();
227 resp.required( "clientId", id, "");
228 } catch ( RequiredFieldException rfe ) {
229 logger.debug( resp.toString() );
232 MR_Client nClient = mr_clientService.getMr_Client( id, resp.getErr() );
233 if ( resp.getErr().is2xx()) {
234 return resp.success(nClient);