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.DR_Pub;
46 import org.onap.dmaap.dbcapi.model.MR_Client;
47 import org.onap.dmaap.dbcapi.model.MR_Cluster;
48 import org.onap.dmaap.dbcapi.model.Topic;
49 import org.onap.dmaap.dbcapi.service.ApiService;
50 import org.onap.dmaap.dbcapi.service.MR_ClientService;
51 import org.onap.dmaap.dbcapi.service.MR_ClusterService;
52 import org.onap.dmaap.dbcapi.service.TopicService;
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();
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 = DR_Pub.class),
70 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
72 public Response getMr_Clients() {
73 ApiService resp = new ApiService();
75 List<MR_Client> clients = mr_clientService.getAllMr_Clients();
77 GenericEntity<List<MR_Client>> list = new GenericEntity<List<MR_Client>>(clients) {
79 return resp.success(list);
83 @ApiOperation( value = "return MR_Client details",
84 notes = "Create a `MR_Client` object.",
85 response = MR_Client.class)
86 @ApiResponses( value = {
87 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
88 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
90 public Response addMr_Client(
93 ApiService resp = new ApiService();
96 resp.required( "fqtn", client.getFqtn(), "");
97 resp.required( "dcaeLocationName", client.getDcaeLocationName(), "");
98 resp.required( "clientRole", client.getClientRole(), "" );
99 resp.required( "action", client.getAction(), "");
101 } catch ( RequiredFieldException rfe ) {
102 logger.debug( resp.toString() );
105 MR_ClusterService clusters = new MR_ClusterService();
107 MR_Cluster cluster = clusters.getMr_Cluster(client.getDcaeLocationName(), resp.getErr());
108 if ( cluster == null ) {
110 resp.setCode(Status.BAD_REQUEST.getStatusCode());
111 resp.setMessage( "MR_Cluster alias not found for dcaeLocation: " + client.getDcaeLocationName());
112 resp.setFields("dcaeLocationName");
113 logger.warn( resp.toString() );
116 String url = cluster.getFqdn();
117 if ( url == null || url.isEmpty() ) {
119 resp.setCode(Status.BAD_REQUEST.getStatusCode());
120 resp.setMessage("FQDN not set for dcaeLocation " + client.getDcaeLocationName() );
121 resp.setFields("fqdn");
122 logger.warn( resp.toString() );
125 TopicService topics = new TopicService();
127 Topic t = topics.getTopic(client.getFqtn(), resp.getErr() );
131 MR_Client nClient = mr_clientService.addMr_Client(client, t, resp.getErr());
132 if ( resp.getErr().is2xx()) {
133 t = topics.getTopic(client.getFqtn(), resp.getErr());
134 topics.checkForBridge(t, resp.getErr());
135 return resp.success(nClient);
143 @ApiOperation( value = "return MR_Client details",
144 notes = "Update a `MR_Client` object, specified by clientId",
145 response = MR_Client.class)
146 @ApiResponses( value = {
147 @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
148 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
151 public Response updateMr_Client(
152 @PathParam("clientId") String clientId,
155 ApiService resp = new ApiService();
158 resp.required( "fqtn", client.getFqtn(), "");
159 resp.required( "dcaeLocationName", client.getDcaeLocationName(), "");
160 resp.required( "clientRole", client.getClientRole(), "" );
161 resp.required( "action", client.getAction(), "");
163 } catch ( RequiredFieldException rfe ) {
164 logger.debug( resp.toString() );
167 client.setMrClientId(clientId);
168 MR_Client nClient = mr_clientService.updateMr_Client(client, resp.getErr() );
169 if ( resp.getErr().is2xx()) {
170 return Response.ok(nClient)
173 return Response.status(resp.getErr().getCode())
174 .entity( resp.getErr() )
179 @ApiOperation( value = "return MR_Client details",
180 notes = "Delete a `MR_Client` object, specified by clientId",
181 response = MR_Client.class)
182 @ApiResponses( value = {
183 @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
184 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
187 public Response deleteMr_Client(
188 @PathParam("subId") String id
190 ApiService resp = new ApiService();
193 resp.required( "clientId", id, "");
194 } catch ( RequiredFieldException rfe ) {
195 logger.debug( resp.toString() );
198 mr_clientService.removeMr_Client(id, true, resp.getErr() );
199 if ( resp.getErr().is2xx()) {
200 return resp.success(Status.NO_CONTENT.getStatusCode(), null);
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 = DR_Pub.class),
212 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
215 public Response test(
216 @PathParam("subId") String id
218 ApiService resp = new ApiService();
221 resp.required( "clientId", id, "");
222 } catch ( RequiredFieldException rfe ) {
223 logger.debug( resp.toString() );
226 MR_Client nClient = mr_clientService.getMr_Client( id, resp.getErr() );
227 if ( resp.getErr().is2xx()) {
228 return resp.success(nClient);