Fix reference to DR_Sub
[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  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.resources;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.DELETE;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.POST;
30 import javax.ws.rs.PUT;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.PathParam;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.core.GenericEntity;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.Response;
37 import javax.ws.rs.core.Response.Status;
38
39 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
40 import org.onap.dmaap.dbcapi.model.ApiError;
41 import org.onap.dmaap.dbcapi.model.DR_Sub;
42 import org.onap.dmaap.dbcapi.model.Feed;
43 import org.onap.dmaap.dbcapi.service.ApiService;
44 import org.onap.dmaap.dbcapi.service.DR_SubService;
45 import org.onap.dmaap.dbcapi.service.FeedService;
46
47 import io.swagger.annotations.Api;
48 import io.swagger.annotations.ApiOperation;
49 import io.swagger.annotations.ApiResponse;
50 import io.swagger.annotations.ApiResponses;
51
52
53 @Path("/dr_subs")
54 @Api( value= "dr_subs", description = "Endpoint for a Data Router client that implements a Subscriber" )
55 @Consumes(MediaType.APPLICATION_JSON)
56 @Produces(MediaType.APPLICATION_JSON)
57 @Authorization
58 public class DR_SubResource extends BaseLoggingClass {
59                 
60         @GET
61         @ApiOperation( value = "return DR_Sub details", 
62         notes = "Returns array of  `DR_Sub` objects.  Add filter for feedId.", 
63         response = DR_Sub.class)
64         @ApiResponses( value = {
65             @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
66             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
67         })
68         public Response getDr_Subs() {
69
70                 ApiService resp = new ApiService();
71
72                 DR_SubService dr_subService = new DR_SubService();
73                 List<DR_Sub> subs = dr_subService.getAllDr_Subs();
74
75                 GenericEntity<List<DR_Sub>> list = new GenericEntity<List<DR_Sub>>(subs) {
76         };
77         return resp.success(list);
78         }
79                 
80         @POST
81         @ApiOperation( value = "return DR_Sub details", 
82         notes = "Create a  `DR_Sub` object.  ", 
83         response = DR_Sub.class)
84         @ApiResponses( value = {
85             @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
86             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
87         })
88         public Response addDr_Sub( 
89                         DR_Sub sub
90                         ) {
91         
92                 ApiService resp = new ApiService();
93
94                 try {
95                         resp.required( "feedId", sub.getFeedId(), "");
96                         resp.required( "dcaeLocationName", sub.getDcaeLocationName(), "");
97         
98                 } catch ( RequiredFieldException rfe ) {
99                         logger.debug( resp.toString() );
100                         return resp.error();    
101                 }
102                 
103                 FeedService feeds = new FeedService();
104                 Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
105                 if ( fnew == null ) {
106                         logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
107                         resp.setCode(Status.NOT_FOUND.getStatusCode());
108                         return resp.error();
109                 }
110
111                 DR_SubService dr_subService = new DR_SubService( fnew.getSubscribeURL());
112                 ArrayList<DR_Sub> subs = fnew.getSubs();
113                 logger.info( "num existing subs before = " + subs.size() );
114                 DR_Sub snew = dr_subService.addDr_Sub(sub, resp.getErr() );
115                 if ( ! resp.getErr().is2xx() ) {
116                         return resp.error();
117                 }
118                 subs.add( snew );
119                 logger.info( "num existing subs after = " + subs.size() );
120                 
121                 fnew.setSubs(subs);
122                 logger.info( "update feed");
123                 //feeds.updateFeed( fnew, err );                        
124                 
125                 return resp.success(Status.CREATED.getStatusCode(), snew);
126
127         }
128                 
129         @PUT
130         @ApiOperation( value = "return DR_Sub details", 
131         notes = "Update a  `DR_Sub` object, selected by subId", 
132         response = DR_Sub.class)
133         @ApiResponses( value = {
134             @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
135             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
136         })
137         @Path("/{subId}")
138         public Response updateDr_Sub( 
139                         @PathParam("subId") String name, 
140                         DR_Sub sub
141                         ) {
142
143                 ApiService resp = new ApiService();
144
145                 try {
146                         resp.required( "subId", name, "");
147                         resp.required( "feedId", sub.getFeedId(), "");
148                         resp.required( "dcaeLocationName", sub.getDcaeLocationName(), "");
149         
150                 } catch ( RequiredFieldException rfe ) {
151                         logger.debug( resp.toString() );
152                         return resp.error();
153                 }
154                 FeedService feeds = new FeedService();
155                 Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
156                 if ( fnew == null ) {
157                         logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
158                         return resp.error();
159                 }
160                 
161                 DR_SubService dr_subService = new DR_SubService();
162                 sub.setSubId(name);
163                 DR_Sub nsub = dr_subService.updateDr_Sub(sub, resp.getErr() );
164                 if ( nsub != null && nsub.isStatusValid() ) {
165                         return resp.success(nsub);
166                 }
167                 return resp.error();
168         }
169                 
170         @DELETE
171         @ApiOperation( value = "return DR_Sub details", 
172         notes = "Delete a  `DR_Sub` object, selected by subId", 
173         response = DR_Sub.class)
174         @ApiResponses( value = {
175             @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
176             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
177         })
178         @Path("/{subId}")
179         public Response deleteDr_Sub( 
180                         @PathParam("subId") String id
181                         ){
182
183                 ApiService resp = new ApiService();
184
185                 try {
186                         resp.required( "subId", id, "");
187                 } catch ( RequiredFieldException rfe ) {
188                         logger.debug( resp.toString() );
189                         return resp.error();    
190                 }
191                 DR_SubService dr_subService = new DR_SubService();
192                 dr_subService.removeDr_Sub(id, resp.getErr() );
193                 if ( ! resp.getErr().is2xx() ) {
194                         return resp.error();
195                 }
196                 return resp.success(Status.NO_CONTENT.getStatusCode(), null );
197         }
198
199         @GET
200         @ApiOperation( value = "return DR_Sub details", 
201         notes = "Retrieve a  `DR_Sub` object, selected by subId", 
202         response = DR_Sub.class)
203         @ApiResponses( value = {
204             @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
205             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
206         })
207         @Path("/{subId}")
208         public Response get( 
209                         @PathParam("subId") String id
210                         ) {
211                 ApiService resp = new ApiService();
212
213                 try {
214                         resp.required( "subId", id, "");
215                 } catch ( RequiredFieldException rfe ) {
216                         logger.debug( resp.toString() );
217                         return resp.error();
218                 }
219                 DR_SubService dr_subService = new DR_SubService();
220                 DR_Sub sub =  dr_subService.getDr_Sub( id, resp.getErr() );
221                 if ( sub != null && sub.isStatusValid() ) {
222                         return resp.success(sub);
223                 }
224                 return resp.error();
225         }
226 }