Fix object references to show fields
[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 = "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 )
88         })
89         public Response addMr_Client( 
90                         MR_Client client
91                         ) {
92                 ApiService resp = new ApiService();
93
94                 try {
95                         resp.required( "fqtn", client.getFqtn(), "");
96                         resp.required( "dcaeLocationName", client.getDcaeLocationName(), "");
97                         resp.required( "clientRole", client.getClientRole(), "" );
98                         resp.required( "action", client.getAction(), "");
99
100                 } catch ( RequiredFieldException rfe ) {
101                         logger.debug( resp.toString() );
102                         return resp.error();    
103                 }
104                 MR_ClusterService clusters = new MR_ClusterService();
105
106                 MR_Cluster cluster = clusters.getMr_Cluster(client.getDcaeLocationName(), resp.getErr());
107                 if ( cluster == null ) {
108
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() );
113                         return resp.error();
114                 }
115                 String url = cluster.getFqdn();
116                 if ( url == null || url.isEmpty() ) {
117
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() );
122                         return resp.error();
123                 }
124                 TopicService topics = new TopicService();
125
126                 Topic t = topics.getTopic(client.getFqtn(), resp.getErr() );
127                 if ( t == null ) {
128                         return resp.error();            
129                 }
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);
135                 }
136                 else {
137                         return resp.error();                    
138                 }
139         }
140                 
141         @PUT
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 )
148         })
149         @Path("/{clientId}")
150         public Response updateMr_Client( 
151                         @PathParam("clientId") String clientId, 
152                         MR_Client client
153                         ) {
154                 ApiService resp = new ApiService();
155
156                 try {
157                         resp.required( "fqtn", client.getFqtn(), "");
158                         resp.required( "dcaeLocationName", client.getDcaeLocationName(), "");
159                         resp.required( "clientRole", client.getClientRole(), "" );
160                         resp.required( "action", client.getAction(), "");
161
162                 } catch ( RequiredFieldException rfe ) {
163                         logger.debug( resp.toString() );
164                         return resp.error();            
165                 }
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)
170                                 .build();
171                 }
172                 return Response.status(resp.getErr().getCode())
173                                 .entity( resp.getErr() )
174                                 .build();
175         }
176                 
177         @DELETE
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 )
184         })
185         @Path("/{subId}")
186         public Response deleteMr_Client( 
187                         @PathParam("subId") String id
188                         ){
189                 ApiService resp = new ApiService();
190
191                 try {
192                         resp.required( "clientId", id, "");
193                 } catch ( RequiredFieldException rfe ) {
194                         logger.debug( resp.toString() );
195                         return resp.error();    
196                 }
197                 mr_clientService.removeMr_Client(id, true, resp.getErr() );
198                 if ( resp.getErr().is2xx()) {
199                         return resp.success(Status.NO_CONTENT.getStatusCode(), null);
200                 }
201                 
202                 return resp.error();
203         }
204
205         @GET
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 )
212         })
213         @Path("/{subId}")
214         public Response test( 
215                         @PathParam("subId") String id
216                         ) {
217                 ApiService resp = new ApiService();
218
219                 try {
220                         resp.required( "clientId", id, "");
221                 } catch ( RequiredFieldException rfe ) {
222                         logger.debug( resp.toString() );
223                         return resp.error();    
224                 }
225                 MR_Client nClient =  mr_clientService.getMr_Client( id, resp.getErr() );
226                 if ( resp.getErr().is2xx()) {
227                         return resp.success(nClient);
228                 }
229                 return resp.error();    
230         }
231 }