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