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 = "return MR_Client details",
83 notes = "Create a `MR_Client` object.",
84 response = MR_Client.class)
85 @ApiResponses( value = {
86 @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
87 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
89 public Response addMr_Client(
92 ApiService resp = new ApiService();
95 resp.required( "fqtn", client.getFqtn(), "");
96 resp.required( "dcaeLocationName", client.getDcaeLocationName(), "");
97 resp.required( "clientRole", client.getClientRole(), "" );
98 resp.required( "action", client.getAction(), "");
100 } catch ( RequiredFieldException rfe ) {
101 logger.debug( resp.toString() );
104 MR_ClusterService clusters = new MR_ClusterService();
106 MR_Cluster cluster = clusters.getMr_Cluster(client.getDcaeLocationName(), resp.getErr());
107 if ( cluster == null ) {
109 resp.setCode(Status.BAD_REQUEST.getStatusCode());
110 resp.setMessage( "MR_Cluster alias not found for dcaeLocation: " + client.getDcaeLocationName());
111 resp.setFields("dcaeLocationName");
112 logger.warn( resp.toString() );
115 String url = cluster.getFqdn();
116 if ( url == null || url.isEmpty() ) {
118 resp.setCode(Status.BAD_REQUEST.getStatusCode());
119 resp.setMessage("FQDN not set for dcaeLocation " + client.getDcaeLocationName() );
120 resp.setFields("fqdn");
121 logger.warn( resp.toString() );
124 TopicService topics = new TopicService();
126 Topic t = topics.getTopic(client.getFqtn(), resp.getErr() );
130 MR_Client nClient = mr_clientService.addMr_Client(client, t, resp.getErr());
131 if ( resp.getErr().is2xx()) {
132 t = topics.getTopic(client.getFqtn(), resp.getErr());
133 topics.checkForBridge(t, resp.getErr());
134 return resp.success(nClient);
142 @ApiOperation( value = "return MR_Client details",
143 notes = "Update a `MR_Client` object, specified by clientId",
144 response = MR_Client.class)
145 @ApiResponses( value = {
146 @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
147 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
150 public Response updateMr_Client(
151 @PathParam("clientId") String clientId,
154 ApiService resp = new ApiService();
157 resp.required( "fqtn", client.getFqtn(), "");
158 resp.required( "dcaeLocationName", client.getDcaeLocationName(), "");
159 resp.required( "clientRole", client.getClientRole(), "" );
160 resp.required( "action", client.getAction(), "");
162 } catch ( RequiredFieldException rfe ) {
163 logger.debug( resp.toString() );
166 client.setMrClientId(clientId);
167 MR_Client nClient = mr_clientService.updateMr_Client(client, resp.getErr() );
168 if ( resp.getErr().is2xx()) {
169 return Response.ok(nClient)
172 return Response.status(resp.getErr().getCode())
173 .entity( resp.getErr() )
178 @ApiOperation( value = "return MR_Client details",
179 notes = "Delete a `MR_Client` object, specified by clientId",
180 response = MR_Client.class)
181 @ApiResponses( value = {
182 @ApiResponse( code = 204, message = "Success", response = MR_Client.class),
183 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
186 public Response deleteMr_Client(
187 @PathParam("subId") String id
189 ApiService resp = new ApiService();
192 resp.required( "clientId", id, "");
193 } catch ( RequiredFieldException rfe ) {
194 logger.debug( resp.toString() );
197 mr_clientService.removeMr_Client(id, true, resp.getErr() );
198 if ( resp.getErr().is2xx()) {
199 return resp.success(Status.NO_CONTENT.getStatusCode(), null);
206 @ApiOperation( value = "return MR_Client details",
207 notes = "Retrieve a `MR_Client` object, specified by clientId",
208 response = MR_Client.class)
209 @ApiResponses( value = {
210 @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
211 @ApiResponse( code = 400, message = "Error", response = ApiError.class )
214 public Response test(
215 @PathParam("subId") String id
217 ApiService resp = new ApiService();
220 resp.required( "clientId", id, "");
221 } catch ( RequiredFieldException rfe ) {
222 logger.debug( resp.toString() );
225 MR_Client nClient = mr_clientService.getMr_Client( id, resp.getErr() );
226 if ( resp.getErr().is2xx()) {
227 return resp.success(nClient);