6df8ef6dbb28e53fc9331abff86c4d0c0d811be4
[dmaap/dbcapi.git] / 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
28 import java.util.List;
29
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;
42
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;
51
52 import static javax.ws.rs.core.Response.Status.NO_CONTENT;
53
54
55 @Path("/mr_clients")
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)
59 @Authorization
60 public class MR_ClientResource extends BaseLoggingClass {
61
62         private MR_ClientService mr_clientService = new MR_ClientService();
63         private ResponseBuilder responseBuilder = new ResponseBuilder();
64         private RequiredChecker checker = new RequiredChecker();
65                 
66         @GET
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 )
73         })
74         public Response getMr_Clients() {
75                 List<MR_Client> clients = mr_clientService.getAllMr_Clients();
76
77                 GenericEntity<List<MR_Client>> list = new GenericEntity<List<MR_Client>>(clients) {
78         };
79         return responseBuilder.success(list);
80         }
81                 
82         @POST
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 )
92         })
93         public Response addMr_Client(MR_Client client) {
94                 ApiError apiError = new ApiError();
95
96                 try {
97                         checker.required( "fqtn", client.getFqtn());
98                         checker.required( "dcaeLocationName", client.getDcaeLocationName());
99                         String s = client.getClientRole();
100                         if ( s == null ) {
101                                 s = client.getClientIdentity();
102                         }
103                         checker.required( "clientRole or clientIdentity", s);
104                         checker.required( "action", client.getAction());
105
106                 } catch ( RequiredFieldException rfe ) {
107                         logger.debug( rfe.getApiError().toString() );
108                         return responseBuilder.error(rfe.getApiError());
109                 }
110                 MR_ClusterService clusters = new MR_ClusterService();
111
112                 MR_Cluster cluster = clusters.getMr_Cluster(client.getDcaeLocationName(), apiError);
113                 if ( cluster == null ) {
114
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);
120                 }
121                 String url = cluster.getFqdn();
122                 if ( url == null || url.isEmpty() ) {
123
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);
129                 }
130                 TopicService topics = new TopicService();
131
132                 Topic t = topics.getTopic(client.getFqtn(), apiError);
133                 if ( t == null ) {
134                         return responseBuilder.error(apiError);
135                 }
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);
141                 }
142                 else {
143                         return responseBuilder.error(apiError);
144                 }
145         }
146                 
147         @PUT
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 )
154         })
155         @Path("/{clientId}")
156         public Response updateMr_Client(@PathParam("clientId") String clientId, MR_Client client) {
157                 ApiError apiError = new ApiError();
158
159                 try {
160                         checker.required( "fqtn", client.getFqtn());
161                         checker.required( "dcaeLocationName", client.getDcaeLocationName());
162                         checker.required( "clientRole", client.getClientRole());
163                         checker.required( "action", client.getAction());
164
165                 } catch ( RequiredFieldException rfe ) {
166                         logger.debug( rfe.getApiError().toString() );
167                         return responseBuilder.error(rfe.getApiError());
168                 }
169                 client.setMrClientId(clientId);
170                 MR_Client nClient = mr_clientService.updateMr_Client(client, apiError);
171                 if (apiError.is2xx()) {
172                         return Response.ok(nClient)
173                                 .build();
174                 }
175                 return Response.status(apiError.getCode())
176                                 .entity(apiError)
177                                 .build();
178         }
179                 
180         @DELETE
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 )
187         })
188         @Path("/{subId}")
189         public Response deleteMr_Client(@PathParam("subId") String id){
190                 ApiError apiError = new ApiError();
191
192                 try {
193                         checker.required( "clientId", id);
194                 } catch ( RequiredFieldException rfe ) {
195                         logger.debug( rfe.getApiError().toString() );
196                         return responseBuilder.error(rfe.getApiError());
197                 }
198                 mr_clientService.removeMr_Client(id, true, apiError);
199                 if (apiError.is2xx()) {
200                         return responseBuilder.success(NO_CONTENT.getStatusCode(), null);
201                 }
202                 
203                 return responseBuilder.error(apiError);
204         }
205
206         @GET
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 )
213         })
214         @Path("/{subId}")
215         public Response test(@PathParam("subId") String id) {
216                 ApiError apiError = new ApiError();
217
218                 try {
219                         checker.required( "clientId", id);
220                 } catch ( RequiredFieldException rfe ) {
221                         logger.debug( rfe.getApiError().toString() );
222                         return responseBuilder.error(rfe.getApiError());
223                 }
224                 MR_Client nClient =  mr_clientService.getMr_Client(id, apiError);
225                 if (apiError.is2xx()) {
226                         return responseBuilder.success(nClient);
227                 }
228                 return responseBuilder.error(apiError);
229         }
230 }