DMAAP-83 Initial code import
[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.DR_Pub;
46 import org.onap.dmaap.dbcapi.model.MR_Client;
47 import org.onap.dmaap.dbcapi.model.MR_Cluster;
48 import org.onap.dmaap.dbcapi.model.Topic;
49 import org.onap.dmaap.dbcapi.service.ApiService;
50 import org.onap.dmaap.dbcapi.service.MR_ClientService;
51 import org.onap.dmaap.dbcapi.service.MR_ClusterService;
52 import org.onap.dmaap.dbcapi.service.TopicService;
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                 
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 = DR_Pub.class),
70             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
71         })
72         public Response getMr_Clients() {
73                 ApiService resp = new ApiService();
74
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 resp.success(list);              
80         }
81                 
82         @POST
83         @ApiOperation( value = "return MR_Client details", 
84         notes = "Create a  `MR_Client` object.", 
85         response = MR_Client.class)
86         @ApiResponses( value = {
87             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
88             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
89         })
90         public Response addMr_Client( 
91                         MR_Client client
92                         ) {
93                 ApiService resp = new ApiService();
94
95                 try {
96                         resp.required( "fqtn", client.getFqtn(), "");
97                         resp.required( "dcaeLocationName", client.getDcaeLocationName(), "");
98                         resp.required( "clientRole", client.getClientRole(), "" );
99                         resp.required( "action", client.getAction(), "");
100
101                 } catch ( RequiredFieldException rfe ) {
102                         logger.debug( resp.toString() );
103                         return resp.error();    
104                 }
105                 MR_ClusterService clusters = new MR_ClusterService();
106
107                 MR_Cluster cluster = clusters.getMr_Cluster(client.getDcaeLocationName(), resp.getErr());
108                 if ( cluster == null ) {
109
110                         resp.setCode(Status.BAD_REQUEST.getStatusCode());
111                         resp.setMessage( "MR_Cluster alias not found for dcaeLocation: " + client.getDcaeLocationName());
112                         resp.setFields("dcaeLocationName");
113                         logger.warn( resp.toString() );
114                         return resp.error();
115                 }
116                 String url = cluster.getFqdn();
117                 if ( url == null || url.isEmpty() ) {
118
119                         resp.setCode(Status.BAD_REQUEST.getStatusCode());
120                         resp.setMessage("FQDN not set for dcaeLocation " + client.getDcaeLocationName() );
121                         resp.setFields("fqdn");
122                         logger.warn( resp.toString() );
123                         return resp.error();
124                 }
125                 TopicService topics = new TopicService();
126
127                 Topic t = topics.getTopic(client.getFqtn(), resp.getErr() );
128                 if ( t == null ) {
129                         return resp.error();            
130                 }
131                 MR_Client nClient =  mr_clientService.addMr_Client(client, t, resp.getErr());
132                 if ( resp.getErr().is2xx()) {
133                         t = topics.getTopic(client.getFqtn(),  resp.getErr());
134                         topics.checkForBridge(t, resp.getErr());
135                         return resp.success(nClient);
136                 }
137                 else {
138                         return resp.error();                    
139                 }
140         }
141                 
142         @PUT
143         @ApiOperation( value = "return MR_Client details", 
144         notes = "Update a  `MR_Client` object, specified by clientId", 
145         response = MR_Client.class)
146         @ApiResponses( value = {
147             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
148             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
149         })
150         @Path("/{clientId}")
151         public Response updateMr_Client( 
152                         @PathParam("clientId") String clientId, 
153                         MR_Client client
154                         ) {
155                 ApiService resp = new ApiService();
156
157                 try {
158                         resp.required( "fqtn", client.getFqtn(), "");
159                         resp.required( "dcaeLocationName", client.getDcaeLocationName(), "");
160                         resp.required( "clientRole", client.getClientRole(), "" );
161                         resp.required( "action", client.getAction(), "");
162
163                 } catch ( RequiredFieldException rfe ) {
164                         logger.debug( resp.toString() );
165                         return resp.error();            
166                 }
167                 client.setMrClientId(clientId);
168                 MR_Client nClient = mr_clientService.updateMr_Client(client, resp.getErr() );
169                 if ( resp.getErr().is2xx()) {
170                         return Response.ok(nClient)
171                                 .build();
172                 }
173                 return Response.status(resp.getErr().getCode())
174                                 .entity( resp.getErr() )
175                                 .build();
176         }
177                 
178         @DELETE
179         @ApiOperation( value = "return MR_Client details", 
180         notes = "Delete a  `MR_Client` object, specified by clientId", 
181         response = MR_Client.class)
182         @ApiResponses( value = {
183             @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
184             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
185         })
186         @Path("/{subId}")
187         public Response deleteMr_Client( 
188                         @PathParam("subId") String id
189                         ){
190                 ApiService resp = new ApiService();
191
192                 try {
193                         resp.required( "clientId", id, "");
194                 } catch ( RequiredFieldException rfe ) {
195                         logger.debug( resp.toString() );
196                         return resp.error();    
197                 }
198                 mr_clientService.removeMr_Client(id, true, resp.getErr() );
199                 if ( resp.getErr().is2xx()) {
200                         return resp.success(Status.NO_CONTENT.getStatusCode(), null);
201                 }
202                 
203                 return resp.error();
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 = DR_Pub.class),
212             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
213         })
214         @Path("/{subId}")
215         public Response test( 
216                         @PathParam("subId") String id
217                         ) {
218                 ApiService resp = new ApiService();
219
220                 try {
221                         resp.required( "clientId", id, "");
222                 } catch ( RequiredFieldException rfe ) {
223                         logger.debug( resp.toString() );
224                         return resp.error();    
225                 }
226                 MR_Client nClient =  mr_clientService.getMr_Client( id, resp.getErr() );
227                 if ( resp.getErr().is2xx()) {
228                         return resp.success(nClient);
229                 }
230                 return resp.error();    
231         }
232 }