Fix Resources Jersey tests
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / resources / DR_SubResource.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  *
7  * Modifications Copyright (C) 2019 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.dmaap.dbcapi.resources;
24
25 import com.google.common.collect.Iterables;
26 import java.util.ArrayList;
27 import java.util.Collections;
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_Sub;
46 import org.onap.dmaap.dbcapi.model.Feed;
47 import org.onap.dmaap.dbcapi.service.ApiService;
48 import org.onap.dmaap.dbcapi.service.DR_SubService;
49 import org.onap.dmaap.dbcapi.service.FeedService;
50
51 import io.swagger.annotations.Api;
52 import io.swagger.annotations.ApiOperation;
53 import io.swagger.annotations.ApiResponse;
54 import io.swagger.annotations.ApiResponses;
55
56 import static javax.ws.rs.core.Response.Status.CREATED;
57
58
59 @Path("/dr_subs")
60 @Api( value= "dr_subs", description = "Endpoint for a Data Router client that implements a Subscriber" )
61 @Consumes(MediaType.APPLICATION_JSON)
62 @Produces(MediaType.APPLICATION_JSON)
63 @Authorization
64 public class DR_SubResource extends BaseLoggingClass {
65
66         private ResponseBuilder responseBuilder = new ResponseBuilder();
67         private RequiredChecker checker = new RequiredChecker();
68                 
69         @GET
70         @ApiOperation( value = "return DR_Sub details", 
71         notes = "Returns array of  `DR_Sub` objects.  Add filter for feedId.", 
72         response = DR_Sub.class)
73         @ApiResponses( value = {
74             @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
75             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
76         })
77         public Response getDr_Subs() {
78                 DR_SubService dr_subService = new DR_SubService();
79                 List<DR_Sub> subs = dr_subService.getAllDr_Subs();
80
81                 GenericEntity<List<DR_Sub>> list = new GenericEntity<List<DR_Sub>>(subs) {
82         };
83         return responseBuilder.success(list);
84         }
85                 
86         @POST
87         @ApiOperation( value = "return DR_Sub details", 
88         notes = "Create a  `DR_Sub` object.  ", 
89         response = DR_Sub.class)
90         @ApiResponses( value = {
91             @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
92             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
93         })
94         public Response addDr_Sub( 
95                         DR_Sub sub
96                         ) {
97         
98                 ApiService resp = new ApiService();
99                 FeedService feeds = new FeedService();
100                 Feed fnew = null;
101                 try {
102                         checker.required( "feedId", sub.getFeedId());
103                 } catch ( RequiredFieldException rfe ) {
104                         try {
105                                 checker.required( "feedName", sub.getFeedName());
106                         }catch ( RequiredFieldException rfe2 ) {
107                                 logger.debug( rfe2.getApiError().toString() );
108                                 return responseBuilder.error(rfe2.getApiError());
109                         }
110                         // if we found a FeedName instead of a FeedId then try to look it up.
111                         List<Feed> nfeeds =  feeds.getAllFeeds( sub.getFeedName(), sub.getFeedVersion(), "equals");
112                         if ( nfeeds.isEmpty() ) {
113                                 resp.setCode(Status.NOT_FOUND.getStatusCode());
114                                 resp.setFields("feedName");
115                                 return responseBuilder.error(resp.getErr());
116                         } else if (nfeeds.size() > 1) {
117                                 logger.debug( "Attempt to match "+ sub.getFeedName() + " ver="+sub.getFeedVersion() + " matched " + nfeeds.size() );
118                                 resp.setCode(Status.CONFLICT.getStatusCode());
119                                 resp.setFields("feedName");
120                                 return responseBuilder.error(resp.getErr());
121                         }
122                         fnew = Iterables.getOnlyElement(nfeeds);
123                 }
124                         
125                 try {
126                         checker.required( "dcaeLocationName", sub.getDcaeLocationName());
127                 } catch ( RequiredFieldException rfe ) {
128                         logger.debug( rfe.getApiError().toString() );
129                         return responseBuilder.error(rfe.getApiError());
130                 }
131                 // we may have fnew already if located by FeedName
132                 if ( fnew == null ) {
133                         fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
134                 }
135                 if ( fnew == null ) {
136                         logger.warn( "Specified feed " + sub.getFeedId() + " or " + sub.getFeedName() + " not known to Bus Controller");
137                         resp.setCode(Status.NOT_FOUND.getStatusCode());
138                         return responseBuilder.error(resp.getErr());
139                 }
140                 DR_SubService dr_subService = new DR_SubService( fnew.getSubscribeURL());
141                 ArrayList<DR_Sub> subs = fnew.getSubs();
142                 logger.info( "num existing subs before = " + subs.size() );
143                 DR_Sub snew = dr_subService.addDr_Sub(sub, resp.getErr() );
144                 if ( ! resp.getErr().is2xx() ) {
145                         return responseBuilder.error(resp.getErr());
146                 }
147                 subs.add( snew );
148                 logger.info( "num existing subs after = " + subs.size() );
149                 
150                 fnew.setSubs(subs);
151                 logger.info( "update feed");
152                 return responseBuilder.success(CREATED.getStatusCode(), snew);
153
154         }
155                 
156         @PUT
157         @ApiOperation( value = "return DR_Sub details", 
158         notes = "Update a  `DR_Sub` object, selected by subId", 
159         response = DR_Sub.class)
160         @ApiResponses( value = {
161             @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
162             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
163         })
164         @Path("/{subId}")
165         public Response updateDr_Sub( 
166                         @PathParam("subId") String name, 
167                         DR_Sub sub
168                         ) {
169
170                 ApiService resp = new ApiService();
171
172                 try {
173                         checker.required( "subId", name);
174                         checker.required( "feedId", sub.getFeedId());
175                         checker.required( "dcaeLocationName", sub.getDcaeLocationName());
176         
177                 } catch ( RequiredFieldException rfe ) {
178                         logger.debug( rfe.getApiError().toString() );
179                         return responseBuilder.error(rfe.getApiError());
180                 }
181                 FeedService feeds = new FeedService();
182                 Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
183                 if ( fnew == null ) {
184                         logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
185                         return responseBuilder.error(resp.getErr());
186                 }
187
188                 DR_SubService dr_subService = new DR_SubService();
189                 sub.setSubId(name);
190                 DR_Sub nsub = dr_subService.updateDr_Sub(sub, resp.getErr() );
191                 if ( nsub != null && nsub.isStatusValid() ) {
192                         return responseBuilder.success(nsub);
193                 }
194                 return responseBuilder.error(resp.getErr());
195         }
196                 
197         @DELETE
198         @ApiOperation( value = "return DR_Sub details", 
199         notes = "Delete a  `DR_Sub` object, selected by subId", 
200         response = DR_Sub.class)
201         @ApiResponses( value = {
202             @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
203             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
204         })
205         @Path("/{subId}")
206         public Response deleteDr_Sub( 
207                         @PathParam("subId") String id
208                         ){
209
210                 ApiService resp = new ApiService();
211
212                 try {
213                         checker.required( "subId", id);
214                 } catch ( RequiredFieldException rfe ) {
215                         logger.debug( rfe.getApiError().toString() );
216                         return responseBuilder.error(rfe.getApiError());
217                 }
218                 DR_SubService dr_subService = new DR_SubService();
219                 dr_subService.removeDr_Sub(id, resp.getErr() );
220                 if ( ! resp.getErr().is2xx() ) {
221                         return responseBuilder.error(resp.getErr());
222                 }
223                 return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null );
224         }
225
226         @GET
227         @ApiOperation( value = "return DR_Sub details", 
228         notes = "Retrieve a  `DR_Sub` object, selected by subId", 
229         response = DR_Sub.class)
230         @ApiResponses( value = {
231             @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
232             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
233         })
234         @Path("/{subId}")
235         public Response get( 
236                         @PathParam("subId") String id
237                         ) {
238                 ApiService resp = new ApiService();
239
240                 try {
241                         checker.required( "subId", id);
242                 } catch ( RequiredFieldException rfe ) {
243                         logger.debug( rfe.getApiError().toString() );
244                         return responseBuilder.error(rfe.getApiError());
245                 }
246                 DR_SubService dr_subService = new DR_SubService();
247                 DR_Sub sub =  dr_subService.getDr_Sub( id, resp.getErr() );
248                 if ( sub != null && sub.isStatusValid() ) {
249                         return responseBuilder.success(sub);
250                 }
251                 return responseBuilder.error(resp.getErr());
252         }
253 }