Create ResponseBuilder class
[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 java.util.ArrayList;
26 import java.util.List;
27
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.PUT;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.PathParam;
35 import javax.ws.rs.Produces;
36 import javax.ws.rs.core.GenericEntity;
37 import javax.ws.rs.core.MediaType;
38 import javax.ws.rs.core.Response;
39 import javax.ws.rs.core.Response.Status;
40
41 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
42 import org.onap.dmaap.dbcapi.model.ApiError;
43 import org.onap.dmaap.dbcapi.model.DR_Sub;
44 import org.onap.dmaap.dbcapi.model.Feed;
45 import org.onap.dmaap.dbcapi.service.ApiService;
46 import org.onap.dmaap.dbcapi.service.DR_SubService;
47 import org.onap.dmaap.dbcapi.service.FeedService;
48
49 import io.swagger.annotations.Api;
50 import io.swagger.annotations.ApiOperation;
51 import io.swagger.annotations.ApiResponse;
52 import io.swagger.annotations.ApiResponses;
53
54 import static javax.ws.rs.core.Response.Status.CREATED;
55
56
57 @Path("/dr_subs")
58 @Api( value= "dr_subs", description = "Endpoint for a Data Router client that implements a Subscriber" )
59 @Consumes(MediaType.APPLICATION_JSON)
60 @Produces(MediaType.APPLICATION_JSON)
61 @Authorization
62 public class DR_SubResource extends BaseLoggingClass {
63
64         private ResponseBuilder responseBuilder = new ResponseBuilder();
65                 
66         @GET
67         @ApiOperation( value = "return DR_Sub details", 
68         notes = "Returns array of  `DR_Sub` objects.  Add filter for feedId.", 
69         response = DR_Sub.class)
70         @ApiResponses( value = {
71             @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
72             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
73         })
74         public Response getDr_Subs() {
75                 DR_SubService dr_subService = new DR_SubService();
76                 List<DR_Sub> subs = dr_subService.getAllDr_Subs();
77
78                 GenericEntity<List<DR_Sub>> list = new GenericEntity<List<DR_Sub>>(subs) {
79         };
80         return responseBuilder.success(list);
81         }
82                 
83         @POST
84         @ApiOperation( value = "return DR_Sub details", 
85         notes = "Create a  `DR_Sub` object.  ", 
86         response = DR_Sub.class)
87         @ApiResponses( value = {
88             @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
89             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
90         })
91         public Response addDr_Sub( 
92                         DR_Sub sub
93                         ) {
94         
95                 ApiService resp = new ApiService();
96                 FeedService feeds = new FeedService();
97                 Feed fnew = null;
98                 try {
99                         resp.required( "feedId", sub.getFeedId(), "");
100                 } catch ( RequiredFieldException rfe ) {
101                         try {
102                                 resp.required( "feedName", sub.getFeedName(), "");
103                         }catch ( RequiredFieldException rfe2 ) {
104                                 logger.debug( resp.toString() );
105                                 return responseBuilder.error(resp.getErr());
106                         }
107                         // if we found a FeedName instead of a FeedId then try to look it up.
108                         List<Feed> nfeeds =  feeds.getAllFeeds( sub.getFeedName(), sub.getFeedVersion(), "equals");
109                         if ( nfeeds.size() != 1 ) {
110                                 logger.debug( "Attempt to match "+ sub.getFeedName() + " ver="+sub.getFeedVersion() + " matched " + nfeeds.size() );
111                                 return responseBuilder.error(resp.getErr());
112                         }
113                         fnew = nfeeds.get(0);
114                 }
115                         
116                 try {
117                         resp.required( "dcaeLocationName", sub.getDcaeLocationName(), "");
118                 } catch ( RequiredFieldException rfe ) {
119                         logger.debug( resp.toString() );
120                         return responseBuilder.error(resp.getErr());
121                 }
122                 // we may have fnew already if located by FeedName
123                 if ( fnew == null ) {
124                         fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
125                 }
126                 if ( fnew == null ) {
127                         logger.warn( "Specified feed " + sub.getFeedId() + " or " + sub.getFeedName() + " not known to Bus Controller");
128                         resp.setCode(Status.NOT_FOUND.getStatusCode());
129                         return responseBuilder.error(resp.getErr());
130                 }
131                 DR_SubService dr_subService = new DR_SubService( fnew.getSubscribeURL());
132                 ArrayList<DR_Sub> subs = fnew.getSubs();
133                 logger.info( "num existing subs before = " + subs.size() );
134                 DR_Sub snew = dr_subService.addDr_Sub(sub, resp.getErr() );
135                 if ( ! resp.getErr().is2xx() ) {
136                         return responseBuilder.error(resp.getErr());
137                 }
138                 subs.add( snew );
139                 logger.info( "num existing subs after = " + subs.size() );
140                 
141                 fnew.setSubs(subs);
142                 logger.info( "update feed");
143                 return responseBuilder.success(CREATED.getStatusCode(), snew);
144
145         }
146                 
147         @PUT
148         @ApiOperation( value = "return DR_Sub details", 
149         notes = "Update a  `DR_Sub` object, selected by subId", 
150         response = DR_Sub.class)
151         @ApiResponses( value = {
152             @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
153             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
154         })
155         @Path("/{subId}")
156         public Response updateDr_Sub( 
157                         @PathParam("subId") String name, 
158                         DR_Sub sub
159                         ) {
160
161                 ApiService resp = new ApiService();
162
163                 try {
164                         resp.required( "subId", name, "");
165                         resp.required( "feedId", sub.getFeedId(), "");
166                         resp.required( "dcaeLocationName", sub.getDcaeLocationName(), "");
167         
168                 } catch ( RequiredFieldException rfe ) {
169                         logger.debug( resp.toString() );
170                         return responseBuilder.error(resp.getErr());
171                 }
172                 FeedService feeds = new FeedService();
173                 Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
174                 if ( fnew == null ) {
175                         logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
176                         return responseBuilder.error(resp.getErr());
177                 }
178
179                 DR_SubService dr_subService = new DR_SubService();
180                 sub.setSubId(name);
181                 DR_Sub nsub = dr_subService.updateDr_Sub(sub, resp.getErr() );
182                 if ( nsub != null && nsub.isStatusValid() ) {
183                         return responseBuilder.success(nsub);
184                 }
185                 return responseBuilder.error(resp.getErr());
186         }
187                 
188         @DELETE
189         @ApiOperation( value = "return DR_Sub details", 
190         notes = "Delete a  `DR_Sub` object, selected by subId", 
191         response = DR_Sub.class)
192         @ApiResponses( value = {
193             @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
194             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
195         })
196         @Path("/{subId}")
197         public Response deleteDr_Sub( 
198                         @PathParam("subId") String id
199                         ){
200
201                 ApiService resp = new ApiService();
202
203                 try {
204                         resp.required( "subId", id, "");
205                 } catch ( RequiredFieldException rfe ) {
206                         logger.debug( resp.toString() );
207                         return responseBuilder.error(resp.getErr());
208                 }
209                 DR_SubService dr_subService = new DR_SubService();
210                 dr_subService.removeDr_Sub(id, resp.getErr() );
211                 if ( ! resp.getErr().is2xx() ) {
212                         return responseBuilder.error(resp.getErr());
213                 }
214                 return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null );
215         }
216
217         @GET
218         @ApiOperation( value = "return DR_Sub details", 
219         notes = "Retrieve a  `DR_Sub` object, selected by subId", 
220         response = DR_Sub.class)
221         @ApiResponses( value = {
222             @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
223             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
224         })
225         @Path("/{subId}")
226         public Response get( 
227                         @PathParam("subId") String id
228                         ) {
229                 ApiService resp = new ApiService();
230
231                 try {
232                         resp.required( "subId", id, "");
233                 } catch ( RequiredFieldException rfe ) {
234                         logger.debug( resp.toString() );
235                         return responseBuilder.error(resp.getErr());
236                 }
237                 DR_SubService dr_subService = new DR_SubService();
238                 DR_Sub sub =  dr_subService.getDr_Sub( id, resp.getErr() );
239                 if ( sub != null && sub.isStatusValid() ) {
240                         return responseBuilder.success(sub);
241                 }
242                 return responseBuilder.error(resp.getErr());
243         }
244 }