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