ApiService used only in AuthorizationFilter
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / resources / DR_PubResource.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.Iterator;
30 import java.util.List;
31
32 import javax.ws.rs.Consumes;
33 import javax.ws.rs.DELETE;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.POST;
36 import javax.ws.rs.PUT;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.PathParam;
39 import javax.ws.rs.Produces;
40 import javax.ws.rs.core.GenericEntity;
41 import javax.ws.rs.core.MediaType;
42 import javax.ws.rs.core.Response;
43 import javax.ws.rs.core.Response.Status;
44
45 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
46 import org.onap.dmaap.dbcapi.model.ApiError;
47 import org.onap.dmaap.dbcapi.model.DR_Pub;
48 import org.onap.dmaap.dbcapi.model.Feed;
49 import org.onap.dmaap.dbcapi.service.DR_PubService;
50 import org.onap.dmaap.dbcapi.service.FeedService;
51
52
53 @Path("/dr_pubs")
54 @Api( value= "dr_pubs", description = "Endpoint for a Data Router client that implements a Publisher" )
55 @Consumes(MediaType.APPLICATION_JSON)
56 @Produces(MediaType.APPLICATION_JSON)
57 @Authorization
58 public class DR_PubResource extends BaseLoggingClass {
59
60         private DR_PubService dr_pubService = new DR_PubService();
61         private ResponseBuilder responseBuilder = new ResponseBuilder();
62         private RequiredChecker checker = new RequiredChecker();
63         
64         @GET
65         @ApiOperation( value = "return DR_Pub details", 
66         notes = "Returns array of  `DR_Pub` objects.  Add filter for feedId.", 
67         response = DR_Pub.class)
68         @ApiResponses( value = {
69             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
70             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
71         })
72         public  Response getDr_Pubs() {
73                 logger.info( "Entry: GET /dr_pubs");
74                 List<DR_Pub> pubs = dr_pubService.getAllDr_Pubs();
75
76                 GenericEntity<List<DR_Pub>> list = new GenericEntity<List<DR_Pub>>(pubs) {
77         };
78         return responseBuilder.success(list);
79         }
80         
81         @POST
82         @ApiOperation( value = "return DR_Pub details", 
83         notes = "create a DR Publisher in the specified environment.", 
84         response = DR_Pub.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_Pub(DR_Pub pub) {
90                 ApiError apiError = new ApiError();
91                 FeedService feeds = new FeedService();
92                 Feed fnew = null;
93
94                 logger.info( "Entry: POST /dr_pubs");
95
96                 try {
97                         checker.required( "feedId", pub.getFeedId());
98                 } catch ( RequiredFieldException rfe ) {
99                         try {
100                                 checker.required( "feedName", pub.getFeedName());
101                         }catch ( RequiredFieldException rfe2 ) {
102                                 logger.debug( rfe2.getApiError().toString() );
103                                 return responseBuilder.error(rfe2.getApiError());
104                         }
105                         // if we found a FeedName instead of a FeedId then try to look it up.
106                         List<Feed> nfeeds =  feeds.getAllFeeds( pub.getFeedName(), pub.getFeedVersion(), "equals");
107                         if ( nfeeds.size() != 1 ) {
108                                 logger.debug( "Attempt to match "+ pub.getFeedName() + " ver="+pub.getFeedVersion() + " matched " + nfeeds.size() );
109                                 return responseBuilder.error(apiError);
110                         }
111                         fnew = nfeeds.get(0);
112                 }
113                 try {
114                         checker.required( "dcaeLocationName", pub.getDcaeLocationName());
115                 } catch ( RequiredFieldException rfe ) {
116                         logger.debug( rfe.getApiError().toString() );
117                         return responseBuilder.error(rfe.getApiError());
118                 }
119
120
121                 // we may have fnew already if located by FeedName
122                 if ( fnew == null ) {
123                         fnew = feeds.getFeed(pub.getFeedId(), apiError);
124                 }
125                 if ( fnew == null ) {
126                         logger.info( "Specified feed " + pub.getFeedId() + " or " + pub.getFeedName() + " not known to Bus Controller");        
127                         return responseBuilder.error(apiError);
128                 }
129
130                 ArrayList<DR_Pub> pubs = fnew.getPubs();
131                 logger.info( "num existing pubs before = " + pubs.size() );
132                 
133                 logger.info( "update feed");
134                 pub.setNextPubId();
135                 if ( pub.getUsername() == null ) {
136                         pub.setRandomUserName();
137                 }
138                 if ( pub.getUserpwd() == null ) {
139                         pub.setRandomPassword();
140                 }
141                 pubs.add( pub );
142                 fnew.setPubs(pubs);
143                 fnew = feeds.updateFeed(fnew, apiError);
144                 
145                 if (!apiError.is2xx()) {
146                         return responseBuilder.error(apiError);
147                 }
148                 pubs = fnew.getPubs();
149                 logger.info( "num existing pubs after = " + pubs.size() );
150                 
151                 DR_Pub pnew = dr_pubService.getDr_Pub(pub.getPubId(), apiError);
152                 return responseBuilder.success(Status.CREATED.getStatusCode(), pnew);
153         }
154         
155         @PUT
156         @ApiOperation( value = "return DR_Pub details", 
157         notes = "update a DR Publisher in the specified environment.  Update a `DR_Pub` object by pubId", 
158         response = DR_Pub.class)
159         @ApiResponses( value = {
160             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
161             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
162         })
163         @Path("/{pubId}")
164         public Response updateDr_Pub(@PathParam("pubId") String name, DR_Pub pub) {
165                 logger.info( "Entry: PUT /dr_pubs");
166                 pub.setPubId(name);
167                 DR_Pub res = dr_pubService.updateDr_Pub(pub);
168                 return responseBuilder.success(res);
169         }
170         
171         @DELETE
172         @ApiOperation( value = "return DR_Pub details", 
173         notes = "delete a DR Publisher in the specified environment. Delete a `DR_Pub` object by pubId", 
174         response = DR_Pub.class)
175         @ApiResponses( value = {
176             @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
177             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
178         })
179         @Path("/{pubId}")
180         public Response deleteDr_Pub(@PathParam("pubId") String id){
181
182                 ApiError apiError = new ApiError();
183
184                 try {
185                         checker.required( "pubId", id);
186                 } catch ( RequiredFieldException rfe ) {
187                         return responseBuilder.error(rfe.getApiError());
188                 }
189
190                 DR_Pub pub =  dr_pubService.getDr_Pub(id, apiError);
191                 if ( !apiError.is2xx()) {
192                         return responseBuilder.error(apiError);
193                 }
194                 FeedService feeds = new FeedService();
195                 Feed fnew = feeds.getFeed(pub.getFeedId(), apiError);
196                 if ( fnew == null ) {
197                         logger.info( "Specified feed " + pub.getFeedId() + " not known to Bus Controller");     
198                         return responseBuilder.error(apiError);
199                 }
200                 ArrayList<DR_Pub> pubs = fnew.getPubs();
201                 if ( pubs.size() == 1 ) {
202                         apiError.setCode(Status.BAD_REQUEST.getStatusCode());
203                         apiError.setMessage( "Can't delete the last publisher of a feed");
204                         return responseBuilder.error(apiError);
205                 }
206                 
207                 for( Iterator<DR_Pub> i = pubs.iterator(); i.hasNext(); ) {
208                         DR_Pub listItem = i.next();
209                         if ( listItem.getPubId().equals(id)) {
210                                 i.remove();
211                         }
212                 }
213                 fnew.setPubs(pubs);
214                 fnew = feeds.updateFeed(fnew,apiError);
215                 if (!apiError.is2xx()) {
216                         return responseBuilder.error(apiError);
217                 }
218                 
219                 dr_pubService.removeDr_Pub(id, apiError);
220                 if (!apiError.is2xx()) {
221                         return responseBuilder.error(apiError);
222                 }
223                 return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null);
224         }
225
226         @GET
227         @ApiOperation( value = "return DR_Pub details", 
228         notes = "returns a DR Publisher in the specified environment. Gets a `DR_Pub` object by pubId", 
229         response = DR_Pub.class)
230         @ApiResponses( value = {
231             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
232             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
233         })
234         @Path("/{pubId}")
235         public Response get(@PathParam("pubId") String id) {
236                 ApiError apiError = new ApiError();
237
238                 try {
239                         checker.required( "feedId", id);
240                 } catch ( RequiredFieldException rfe ) {
241                         return responseBuilder.error(rfe.getApiError());
242                 }
243
244                 DR_Pub pub =  dr_pubService.getDr_Pub(id, apiError);
245                 if (!apiError.is2xx()) {
246                         return responseBuilder.error(apiError);
247                 }
248                 return responseBuilder.success(Status.OK.getStatusCode(), pub);
249         }
250 }