6e652a8f43d9ee72939ad87af2542407db64f418
[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.isEmpty() ) {
108                                 apiError.setCode(Status.NOT_FOUND.getStatusCode());
109                                 apiError.setFields("feedName");
110                                 return responseBuilder.error(apiError);
111                         }
112                         fnew = nfeeds.get(0);
113                 }
114                 try {
115                         checker.required( "dcaeLocationName", pub.getDcaeLocationName());
116                 } catch ( RequiredFieldException rfe ) {
117                         logger.debug( rfe.getApiError().toString() );
118                         return responseBuilder.error(rfe.getApiError());
119                 }
120
121
122                 // we may have fnew already if located by FeedName
123                 if ( fnew == null ) {
124                         fnew = feeds.getFeed(pub.getFeedId(), apiError);
125                 }
126                 if ( fnew == null ) {
127                         logger.info( "Specified feed " + pub.getFeedId() + " or " + pub.getFeedName() + " not known to Bus Controller");        
128                         return responseBuilder.error(apiError);
129                 }
130
131                 ArrayList<DR_Pub> pubs = fnew.getPubs();
132                 logger.info( "num existing pubs before = " + pubs.size() );
133                 
134                 logger.info( "update feed");
135                 pub.setNextPubId();
136                 if ( pub.getUsername() == null ) {
137                         pub.setRandomUserName();
138                 }
139                 if ( pub.getUserpwd() == null ) {
140                         pub.setRandomPassword();
141                 }
142                 pubs.add( pub );
143                 fnew.setPubs(pubs);
144                 fnew = feeds.updateFeed(fnew, apiError);
145                 
146                 if (!apiError.is2xx()) {
147                         return responseBuilder.error(apiError);
148                 }
149                 pubs = fnew.getPubs();
150                 logger.info( "num existing pubs after = " + pubs.size() );
151                 
152                 DR_Pub pnew = dr_pubService.getDr_Pub(pub.getPubId(), apiError);
153                 return responseBuilder.success(Status.CREATED.getStatusCode(), pnew);
154         }
155         
156         @PUT
157         @ApiOperation( value = "return DR_Pub details", 
158         notes = "update a DR Publisher in the specified environment.  Update a `DR_Pub` object by pubId", 
159         response = DR_Pub.class)
160         @ApiResponses( value = {
161             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
162             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
163         })
164         @Path("/{pubId}")
165         public Response updateDr_Pub(@PathParam("pubId") String name, DR_Pub pub) {
166                 logger.info( "Entry: PUT /dr_pubs");
167                 pub.setPubId(name);
168                 DR_Pub res = dr_pubService.updateDr_Pub(pub);
169                 return responseBuilder.success(res);
170         }
171         
172         @DELETE
173         @ApiOperation( value = "return DR_Pub details", 
174         notes = "delete a DR Publisher in the specified environment. Delete a `DR_Pub` object by pubId", 
175         response = DR_Pub.class)
176         @ApiResponses( value = {
177             @ApiResponse( code = 204, message = "Success", response = DR_Pub.class),
178             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
179         })
180         @Path("/{pubId}")
181         public Response deleteDr_Pub(@PathParam("pubId") String id){
182
183                 ApiError apiError = new ApiError();
184
185                 try {
186                         checker.required( "pubId", id);
187                 } catch ( RequiredFieldException rfe ) {
188                         return responseBuilder.error(rfe.getApiError());
189                 }
190
191                 DR_Pub pub =  dr_pubService.getDr_Pub(id, apiError);
192                 if ( !apiError.is2xx()) {
193                         return responseBuilder.error(apiError);
194                 }
195                 FeedService feeds = new FeedService();
196                 Feed fnew = feeds.getFeed(pub.getFeedId(), apiError);
197                 if ( fnew == null ) {
198                         logger.info( "Specified feed " + pub.getFeedId() + " not known to Bus Controller");     
199                         return responseBuilder.error(apiError);
200                 }
201                 ArrayList<DR_Pub> pubs = fnew.getPubs();
202                 if ( pubs.size() == 1 ) {
203                         apiError.setCode(Status.BAD_REQUEST.getStatusCode());
204                         apiError.setMessage( "Can't delete the last publisher of a feed");
205                         return responseBuilder.error(apiError);
206                 }
207                 
208                 for( Iterator<DR_Pub> i = pubs.iterator(); i.hasNext(); ) {
209                         DR_Pub listItem = i.next();
210                         if ( listItem.getPubId().equals(id)) {
211                                 i.remove();
212                         }
213                 }
214                 fnew.setPubs(pubs);
215                 fnew = feeds.updateFeed(fnew,apiError);
216                 if (!apiError.is2xx()) {
217                         return responseBuilder.error(apiError);
218                 }
219                 
220                 dr_pubService.removeDr_Pub(id, apiError);
221                 if (!apiError.is2xx()) {
222                         return responseBuilder.error(apiError);
223                 }
224                 return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null);
225         }
226
227         @GET
228         @ApiOperation( value = "return DR_Pub details", 
229         notes = "returns a DR Publisher in the specified environment. Gets a `DR_Pub` object by pubId", 
230         response = DR_Pub.class)
231         @ApiResponses( value = {
232             @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
233             @ApiResponse( code = 400, message = "Error", response = ApiError.class )
234         })
235         @Path("/{pubId}")
236         public Response get(@PathParam("pubId") String id) {
237                 ApiError apiError = new ApiError();
238
239                 try {
240                         checker.required( "feedId", id);
241                 } catch ( RequiredFieldException rfe ) {
242                         return responseBuilder.error(rfe.getApiError());
243                 }
244
245                 DR_Pub pub =  dr_pubService.getDr_Pub(id, apiError);
246                 if (!apiError.is2xx()) {
247                         return responseBuilder.error(apiError);
248                 }
249                 return responseBuilder.success(Status.OK.getStatusCode(), pub);
250         }
251 }