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