[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / main / java / org / onap / dmaap / dbcapi / resources / MR_ClientResource.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.resources;
22
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;
35
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;
49
50 import static javax.ws.rs.core.Response.Status.NO_CONTENT;
51
52
53 @Path("/mr_clients")
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)
57 @Authorization
58 public class MR_ClientResource extends BaseLoggingClass {
59
60         private MR_ClientService mr_clientService = new MR_ClientService();
61         private ResponseBuilder responseBuilder = new ResponseBuilder();
62         private RequiredChecker checker = new RequiredChecker();
63                 
64         @GET
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 )
71         })
72         public Response getMr_Clients() {
73                 List<MR_Client> clients = mr_clientService.getAllMr_Clients();
74
75                 GenericEntity<List<MR_Client>> list = new GenericEntity<List<MR_Client>>(clients) {
76         };
77         return responseBuilder.success(list);
78         }
79                 
80         @POST
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 )
90         })
91         public Response addMr_Client(MR_Client client) {
92                 ApiError apiError = new ApiError();
93
94                 try {
95                         checker.required( "fqtn", client.getFqtn());
96                         checker.required( "dcaeLocationName", client.getDcaeLocationName());
97                         String s = client.getClientRole();
98                         if ( s == null ) {
99                                 s = client.getClientIdentity();
100                         }
101                         checker.required( "clientRole or clientIdentity", s);
102                         checker.required( "action", client.getAction());
103
104                 } catch ( RequiredFieldException rfe ) {
105                         logger.debug( rfe.getApiError().toString() );
106                         return responseBuilder.error(rfe.getApiError());
107                 }
108                 MR_ClusterService clusters = new MR_ClusterService();
109
110                 MR_Cluster cluster = clusters.getMr_Cluster(client.getDcaeLocationName(), apiError);
111                 if ( cluster == null ) {
112
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);
118                 }
119
120                 TopicService topics = new TopicService();
121
122                 Topic t = topics.getTopic(client.getFqtn(), apiError);
123                 if ( t == null ) {
124                         return responseBuilder.error(apiError);
125                 }
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);
131                 }
132                 else {
133                         return responseBuilder.error(apiError);
134                 }
135         }
136                 
137         @PUT
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 )
144         })
145         @Path("/{clientId}")
146         public Response updateMr_Client(@PathParam("clientId") String clientId, MR_Client client) {
147                 ApiError apiError = new ApiError();
148
149                 try {
150                         checker.required( "fqtn", client.getFqtn());
151                         checker.required( "dcaeLocationName", client.getDcaeLocationName());
152                         checker.required( "clientRole", client.getClientRole());
153                         checker.required( "action", client.getAction());
154
155                 } catch ( RequiredFieldException rfe ) {
156                         logger.debug( rfe.getApiError().toString() );
157                         return responseBuilder.error(rfe.getApiError());
158                 }
159                 client.setMrClientId(clientId);
160                 MR_Client nClient = mr_clientService.updateMr_Client(client, apiError);
161                 if (apiError.is2xx()) {
162                         return Response.ok(nClient)
163                                 .build();
164                 }
165                 return Response.status(apiError.getCode())
166                                 .entity(apiError)
167                                 .build();
168         }
169                 
170         @DELETE
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 )
177         })
178         @Path("/{subId}")
179         public Response deleteMr_Client(@PathParam("subId") String id){
180                 ApiError apiError = new ApiError();
181
182                 mr_clientService.removeMr_Client(id, true, apiError);
183                 if (apiError.is2xx()) {
184                         return responseBuilder.success(NO_CONTENT.getStatusCode(), null);
185                 }
186                 
187                 return responseBuilder.error(apiError);
188         }
189
190         @GET
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 )
197         })
198         @Path("/{subId}")
199         public Response getMr_Client(@PathParam("subId") String id) {
200                 ApiError apiError = new ApiError();
201
202                 MR_Client nClient =  mr_clientService.getMr_Client(id, apiError);
203                 if (apiError.is2xx()) {
204                         return responseBuilder.success(nClient);
205                 }
206                 return responseBuilder.error(apiError);
207         }
208 }