28bfdc588c6c29bbaf5b9e87ec1a8a73d635d264
[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
96                 try {
97                         resp.required( "feedId", sub.getFeedId(), "");
98                         resp.required( "dcaeLocationName", sub.getDcaeLocationName(), "");
99         
100                 } catch ( RequiredFieldException rfe ) {
101                         logger.debug( resp.toString() );
102                         return resp.error();    
103                 }
104                 
105                 FeedService feeds = new FeedService();
106                 Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
107                 if ( fnew == null ) {
108                         logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
109                         resp.setCode(Status.NOT_FOUND.getStatusCode());
110                         return resp.error();
111                 }
112
113                 DR_SubService dr_subService = new DR_SubService( fnew.getSubscribeURL());
114                 ArrayList<DR_Sub> subs = fnew.getSubs();
115                 logger.info( "num existing subs before = " + subs.size() );
116                 DR_Sub snew = dr_subService.addDr_Sub(sub, resp.getErr() );
117                 if ( ! resp.getErr().is2xx() ) {
118                         return resp.error();
119                 }
120                 subs.add( snew );
121                 logger.info( "num existing subs after = " + subs.size() );
122                 
123                 fnew.setSubs(subs);
124                 logger.info( "update feed");
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 }