Patch set 2: changes to MR_Client
[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.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;
52
53
54 @Path("/mr_clients")
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)
58 @Authorization
59 public class MR_ClientResource extends BaseLoggingClass {
60
61         private MR_ClientService mr_clientService = new MR_ClientService();
62                 
63         @GET
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 )
70         })
71         public Response getMr_Clients() {
72                 ApiService resp = new ApiService();
73
74                 List<MR_Client> clients = mr_clientService.getAllMr_Clients();
75
76                 GenericEntity<List<MR_Client>> list = new GenericEntity<List<MR_Client>>(clients) {
77         };
78         return resp.success(list);              
79         }
80                 
81         @POST
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 )
91         })
92         public Response addMr_Client( 
93                         MR_Client client
94                         ) {
95                 ApiService resp = new ApiService();
96
97                 try {
98                         resp.required( "fqtn", client.getFqtn(), "");
99                         resp.required( "dcaeLocationName", client.getDcaeLocationName(), "");
100                         String s = client.getClientRole();
101                         if ( s == null ) {
102                                 s = client.getClientIdentity();
103                         }
104                         resp.required( "clientRole or clientIdentity", s, "" );
105                         resp.required( "action", client.getAction(), "");
106
107                 } catch ( RequiredFieldException rfe ) {
108                         logger.debug( resp.toString() );
109                         return resp.error();    
110                 }
111                 MR_ClusterService clusters = new MR_ClusterService();
112
113                 MR_Cluster cluster = clusters.getMr_Cluster(client.getDcaeLocationName(), resp.getErr());
114                 if ( cluster == null ) {
115
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() );
120                         return resp.error();
121                 }
122                 String url = cluster.getFqdn();
123                 if ( url == null || url.isEmpty() ) {
124
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() );
129                         return resp.error();
130                 }
131                 TopicService topics = new TopicService();
132
133                 Topic t = topics.getTopic(client.getFqtn(), resp.getErr() );
134                 if ( t == null ) {
135                         return resp.error();            
136                 }
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);
142                 }
143                 else {
144                         return resp.error();                    
145                 }
146         }
147                 
148         @PUT
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 )
155         })
156         @Path("/{clientId}")
157         public Response updateMr_Client( 
158                         @PathParam("clientId") String clientId, 
159                         MR_Client client
160                         ) {
161                 ApiService resp = new ApiService();
162
163                 try {
164                         resp.required( "fqtn", client.getFqtn(), "");
165                         resp.required( "dcaeLocationName", client.getDcaeLocationName(), "");
166                         resp.required( "clientRole", client.getClientRole(), "" );
167                         resp.required( "action", client.getAction(), "");
168
169                 } catch ( RequiredFieldException rfe ) {
170                         logger.debug( resp.toString() );
171                         return resp.error();            
172                 }
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)
177                                 .build();
178                 }
179                 return Response.status(resp.getErr().getCode())
180                                 .entity( resp.getErr() )
181                                 .build();
182         }
183                 
184         @DELETE
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 )
191         })
192         @Path("/{subId}")
193         public Response deleteMr_Client( 
194                         @PathParam("subId") String id
195                         ){
196                 ApiService resp = new ApiService();
197
198                 try {
199                         resp.required( "clientId", id, "");
200                 } catch ( RequiredFieldException rfe ) {
201                         logger.debug( resp.toString() );
202                         return resp.error();    
203                 }
204                 mr_clientService.removeMr_Client(id, true, resp.getErr() );
205                 if ( resp.getErr().is2xx()) {
206                         return resp.success(Status.NO_CONTENT.getStatusCode(), null);
207                 }
208                 
209                 return resp.error();
210         }
211
212         @GET
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 )
219         })
220         @Path("/{subId}")
221         public Response test( 
222                         @PathParam("subId") String id
223                         ) {
224                 ApiService resp = new ApiService();
225
226                 try {
227                         resp.required( "clientId", id, "");
228                 } catch ( RequiredFieldException rfe ) {
229                         logger.debug( resp.toString() );
230                         return resp.error();    
231                 }
232                 MR_Client nClient =  mr_clientService.getMr_Client( id, resp.getErr() );
233                 if ( resp.getErr().is2xx()) {
234                         return resp.success(nClient);
235                 }
236                 return resp.error();    
237         }
238 }