[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / 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.List;
28
29 import javax.ws.rs.Consumes;
30 import javax.ws.rs.DELETE;
31 import javax.ws.rs.GET;
32 import javax.ws.rs.POST;
33 import javax.ws.rs.PUT;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.core.GenericEntity;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.Response;
40 import javax.ws.rs.core.Response.Status;
41
42 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
43 import org.onap.dmaap.dbcapi.model.ApiError;
44 import org.onap.dmaap.dbcapi.model.DR_Sub;
45 import org.onap.dmaap.dbcapi.model.Feed;
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(DR_Sub sub) {
93
94                 ApiError apiError = new ApiError();
95                 FeedService feeds = new FeedService();
96                 Feed fnew = null;
97                 try {
98                         checker.required( "feedId", sub.getFeedId());
99                 } catch ( RequiredFieldException rfe ) {
100                         try {
101                                 checker.required( "feedName", sub.getFeedName());
102                         }catch ( RequiredFieldException rfe2 ) {
103                                 logger.debug( rfe2.getApiError().toString() );
104                                 return responseBuilder.error(rfe2.getApiError());
105                         }
106                         // if we found a FeedName instead of a FeedId then try to look it up.
107                         List<Feed> nfeeds =  feeds.getAllFeeds( sub.getFeedName(), sub.getFeedVersion(), "equals");
108                         if ( nfeeds.isEmpty() ) {
109                                 apiError.setCode(Status.NOT_FOUND.getStatusCode());
110                                 apiError.setFields("feedName");
111                                 return responseBuilder.error(apiError);
112                         } else if (nfeeds.size() > 1) {
113                                 logger.debug( "Attempt to match "+ sub.getFeedName() + " ver="+sub.getFeedVersion() + " matched " + nfeeds.size() );
114                                 apiError.setCode(Status.CONFLICT.getStatusCode());
115                                 apiError.setFields("feedName");
116                                 return responseBuilder.error(apiError);
117                         }
118                         fnew = Iterables.getOnlyElement(nfeeds);
119                 }
120                         
121                 try {
122                         checker.required( "dcaeLocationName", sub.getDcaeLocationName());
123                 } catch ( RequiredFieldException rfe ) {
124                         logger.debug( rfe.getApiError().toString() );
125                         return responseBuilder.error(rfe.getApiError());
126                 }
127                 // we may have fnew already if located by FeedName
128                 if ( fnew == null ) {
129                         fnew = feeds.getFeed( sub.getFeedId(), apiError);
130                 }
131                 if ( fnew == null ) {
132                         logger.warn( "Specified feed " + sub.getFeedId() + " or " + sub.getFeedName() + " not known to Bus Controller");
133                         apiError.setCode(Status.NOT_FOUND.getStatusCode());
134                         return responseBuilder.error(apiError);
135                 }
136                 DR_SubService dr_subService = new DR_SubService( fnew.getSubscribeURL());
137                 ArrayList<DR_Sub> subs = fnew.getSubs();
138                 logger.info( "num existing subs before = " + subs.size() );
139                 DR_Sub snew = dr_subService.addDr_Sub(sub, apiError);
140                 if (!apiError.is2xx()) {
141                         return responseBuilder.error(apiError);
142                 }
143                 subs.add( snew );
144                 logger.info( "num existing subs after = " + subs.size() );
145                 
146                 fnew.setSubs(subs);
147                 logger.info( "update feed");
148                 return responseBuilder.success(CREATED.getStatusCode(), snew);
149
150         }
151                 
152         @PUT
153         @ApiOperation( value = "return DR_Sub details", 
154         notes = "Update a  `DR_Sub` object, selected by subId", 
155         response = DR_Sub.class)
156         @ApiResponses( value = {
157             @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
158             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
159         })
160         @Path("/{subId}")
161         public Response updateDr_Sub(@PathParam("subId") String name, DR_Sub sub) {
162
163                 ApiError apiError = new ApiError();
164
165                 try {
166                         checker.required( "subId", name);
167                         checker.required( "feedId", sub.getFeedId());
168                         checker.required( "dcaeLocationName", sub.getDcaeLocationName());
169         
170                 } catch ( RequiredFieldException rfe ) {
171                         logger.debug( rfe.getApiError().toString() );
172                         return responseBuilder.error(rfe.getApiError());
173                 }
174                 FeedService feeds = new FeedService();
175                 Feed fnew = feeds.getFeed(sub.getFeedId(), apiError);
176                 if ( fnew == null ) {
177                         logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
178                         return responseBuilder.error(apiError);
179                 }
180
181                 DR_SubService dr_subService = new DR_SubService();
182                 sub.setSubId(name);
183                 DR_Sub nsub = dr_subService.updateDr_Sub(sub, apiError);
184                 if ( nsub != null && nsub.isStatusValid() ) {
185                         return responseBuilder.success(nsub);
186                 }
187                 return responseBuilder.error(apiError);
188         }
189                 
190         @DELETE
191         @ApiOperation( value = "return DR_Sub details", 
192         notes = "Delete a  `DR_Sub` object, selected by subId", 
193         response = DR_Sub.class)
194         @ApiResponses( value = {
195             @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
196             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
197         })
198         @Path("/{subId}")
199         public Response deleteDr_Sub(@PathParam("subId") String id){
200
201                 ApiError apiError = new ApiError();
202
203                 try {
204                         checker.required( "subId", id);
205                 } catch ( RequiredFieldException rfe ) {
206                         logger.debug( rfe.getApiError().toString() );
207                         return responseBuilder.error(rfe.getApiError());
208                 }
209                 DR_SubService dr_subService = new DR_SubService();
210                 dr_subService.removeDr_Sub(id, apiError);
211                 if (!apiError.is2xx() ) {
212                         return responseBuilder.error(apiError);
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(@PathParam("subId") String id) {
227
228                 ApiError apiError = new ApiError();
229
230                 try {
231                         checker.required( "subId", id);
232                 } catch ( RequiredFieldException rfe ) {
233                         logger.debug( rfe.getApiError().toString() );
234                         return responseBuilder.error(rfe.getApiError());
235                 }
236                 DR_SubService dr_subService = new DR_SubService();
237                 DR_Sub sub =  dr_subService.getDr_Sub(id, apiError);
238                 if ( sub != null && sub.isStatusValid() ) {
239                         return responseBuilder.success(sub);
240                 }
241                 return responseBuilder.error(apiError);
242         }
243 }