d2bf081677bd53ceab0468725ab3275a041a61a8
[dmaap/messagerouter/messageservice.git] / src / main / java / com / att / nsa / dmaap / service / AdminRestService.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 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  *        http://www.apache.org/licenses/LICENSE-2.0
11 *  
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  *  
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package com.att.nsa.dmaap.service;
23
24 import java.io.IOException;
25 import java.util.Enumeration;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.Path;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.core.Context;
36 import javax.ws.rs.core.MediaType;
37
38 import org.apache.http.HttpStatus;
39 //import org.apache.log4j.Logger;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
42 import org.json.JSONException;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.beans.factory.annotation.Qualifier;
45 import org.springframework.stereotype.Component;
46
47 import org.onap.dmaap.dmf.mr.CambriaApiException;
48 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
49 import org.onap.dmaap.dmf.mr.exception.DMaaPResponseCode;
50 import org.onap.dmaap.dmf.mr.exception.ErrorResponse;
51 import org.onap.dmaap.dmf.mr.service.AdminService;
52 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
53 import com.att.nsa.configs.ConfigDbException;
54 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
55
56 /**
57  * Rest Service class
58  * for Admin Services
59  * @author Ramkumar
60  *
61  */
62 @Component
63 @Path("/")
64 public class AdminRestService {
65
66         /**
67          * Logger obj
68          */
69         //private static final Logger LOGGER = Logger
70                 //      .getLogger(AdminRestService.class);
71         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AdminRestService.class);
72         /**
73          * Config Reader
74          */
75         @Autowired
76         @Qualifier("configurationReader")
77         private ConfigurationReader configReader;
78
79         /**
80          * HttpServletRequest obj
81          */
82         @Context
83         private HttpServletRequest request;
84         /**
85          * HttpServletResponse obj
86          */
87         @Context
88         private HttpServletResponse response;
89         /**
90          * AdminService obj
91          */
92         @Autowired
93         private AdminService adminService;
94
95         /**
96          * Fetches a list of all the registered consumers along with their created
97          * time and last accessed details
98          * 
99          * @return consumer list in json string format
100          * @throws CambriaApiException 
101          * @throws AccessDeniedException 
102          * @throws IOException
103          * */
104         @GET
105         @Path("/consumerCache")
106         //@Produces(MediaType.TEXT_PLAIN)
107         public void getConsumerCache() throws CambriaApiException, AccessDeniedException {
108                 LOGGER.info("Fetching list of registered consumers.");
109                 try {
110                         adminService.showConsumerCache(ServiceUtil.getDMaaPContext(configReader, request, response));
111                         LOGGER.info("Fetching Consumer Cache Successfully");
112                 } catch (IOException e) {
113                         LOGGER.error("Error while Fetching list of registered consumers : "
114                                         + e.getMessage(), e);
115                         
116                                         
117                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND, 
118                                         DMaaPResponseCode.GET_CONSUMER_CACHE.getResponseCode(), 
119                                         "Error while Fetching list of registered consumers " + e.getMessage());
120                         LOGGER.info(errRes.toString());
121                         throw new CambriaApiException(errRes);
122                 }
123         }
124
125         /**
126          * Clears consumer cache
127          * @throws CambriaApiException ex
128          * @throws AccessDeniedException 
129          * 
130          * @throws IOException ex
131          * @throws JSONException ex
132          * */
133         @POST
134         @Path("/dropConsumerCache")
135         //@Produces(MediaType.TEXT_PLAIN)
136         public void dropConsumerCache() throws CambriaApiException, AccessDeniedException {
137                 LOGGER.info("Dropping consumer cache");
138                 try {
139                         adminService.dropConsumerCache(ServiceUtil.getDMaaPContext(configReader, request, response));
140                         LOGGER.info("Dropping Consumer Cache successfully");
141                 } catch ( AccessDeniedException   excp) {
142                         LOGGER.error("Error while dropConsumerCache : "
143                                         + excp.getMessage(), excp);
144                 
145                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED, 
146                                         DMaaPResponseCode.GET_BLACKLIST.getResponseCode(), 
147                                         "Error while Fetching list of blacklist ips " + excp.getMessage());
148                         LOGGER.info(errRes.toString());
149                         throw new CambriaApiException(errRes);
150                         
151                 } catch (JSONException | IOException e) {
152                         LOGGER.error(
153                                         "Error while Dropping consumer cache : " + e.getMessage(),
154                                         e);
155                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND, 
156                                         DMaaPResponseCode.DROP_CONSUMER_CACHE.getResponseCode(), 
157                                         "Error while Dropping consumer cache " + e.getMessage());
158                         LOGGER.info(errRes.toString());
159                         throw new CambriaApiException(errRes);
160                 }
161         }
162         
163         /**
164          * Get list of blacklisted ips
165          * @throws CambriaApiException excp
166          */
167         @GET
168         @Path("/blacklist")
169         //@Produces(MediaType.TEXT_PLAIN)
170         public void getBlacklist() throws CambriaApiException {
171                 LOGGER.info("Fetching list of blacklist ips.");
172                 try {
173                         Enumeration headerNames =ServiceUtil.getDMaaPContext(configReader, request, response).getRequest().getHeaderNames();
174                         while (headerNames.hasMoreElements()) {
175                                 String key = (String) headerNames.nextElement();
176                                 String value = request.getHeader(key);
177                         
178                         }
179                         
180                         adminService.getBlacklist(ServiceUtil.getDMaaPContext(configReader, request, response));
181                         LOGGER.info("Fetching list of blacklist ips Successfully");
182                 }catch ( AccessDeniedException   excp) {
183                         LOGGER.error("Error while Fetching list  of blacklist ips : "
184                                         + excp.getMessage(), excp);
185                 
186                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED, 
187                                         DMaaPResponseCode.GET_BLACKLIST.getResponseCode(), 
188                                         "Error while Fetching list of blacklist ips " + excp.getMessage());
189                         LOGGER.info(errRes.toString());
190                         throw new CambriaApiException(errRes);
191                         
192                 } catch ( IOException excp) {
193                         LOGGER.error("Error while Fetching list  of blacklist ips : "
194                                         + excp.getMessage(), excp);
195                 
196                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND, 
197                                         DMaaPResponseCode.GET_BLACKLIST.getResponseCode(), 
198                                         "Error while Fetching list of blacklist ips " + excp.getMessage());
199                         LOGGER.info(errRes.toString());
200                         throw new CambriaApiException(errRes);
201                         
202                 }
203                         
204         }
205
206         /**
207          * Add ip to list of blacklist ips
208          * @param ip ip
209          * @throws CambriaApiException excp
210          */
211         @POST
212         @Path("/blacklist/{ip}")
213         //@Produces(MediaType.TEXT_PLAIN)
214         public void addToBlacklist (@PathParam("ip") String ip ) throws CambriaApiException
215         {
216                 LOGGER.info("Adding ip to list of blacklist ips.");
217                 try {
218                         adminService.addToBlacklist(ServiceUtil.getDMaaPContext(configReader, request, response), ip);
219                         LOGGER.info("Fetching list of blacklist ips Successfully");
220                 } catch ( AccessDeniedException   excp) {
221                         LOGGER.error("Error while blacklist : "
222                                         + excp.getMessage(), excp);
223                 
224                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED, 
225                                         DMaaPResponseCode.GET_BLACKLIST.getResponseCode(), 
226                                         "Error while Fetching list of blacklist ips " + excp.getMessage());
227                         LOGGER.info(errRes.toString());
228                         throw new CambriaApiException(errRes);
229                         
230                 } catch (IOException |  ConfigDbException excp) {
231                         LOGGER.error("Error while adding ip to list of blacklist ips : "
232                                         + excp.getMessage(), excp);
233                 
234                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND, 
235                                         DMaaPResponseCode.ADD_BLACKLIST.getResponseCode(), 
236                                         "Error while adding ip to list of blacklist ips " + excp.getMessage());
237                         LOGGER.info(errRes.toString());
238                         throw new CambriaApiException(errRes);
239                         
240                 }
241                 
242         }
243         /**
244          * Remove ip from blacklist
245          * @param ip ip
246          * @throws CambriaApiException excp
247          * @throws AccessDeniedException excp
248          * @throws ConfigDbException excp
249          */
250         @DELETE
251         @Path("/blacklist/{ip}")
252         //@Produces(MediaType.TEXT_PLAIN)
253         public void removeFromBlacklist(@PathParam("ip") String ip) throws CambriaApiException, AccessDeniedException, ConfigDbException {
254                 LOGGER.info("Fetching list of blacklist ips.");
255                 try {
256                         adminService.removeFromBlacklist(ServiceUtil.getDMaaPContext(configReader, request, response), ip);
257                         LOGGER.info("Fetching list of blacklist ips Successfully");
258                 }catch ( AccessDeniedException   excp) {
259                         LOGGER.error("Error while blacklist : "
260                                         + excp.getMessage(), excp);
261                 
262                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED, 
263                                         DMaaPResponseCode.GET_BLACKLIST.getResponseCode(), 
264                                         "Error while removeFromBlacklist list of blacklist ips " + excp.getMessage());
265                         LOGGER.info(errRes.toString());
266                         throw new CambriaApiException(errRes);
267                         
268                 }  catch (IOException |  ConfigDbException excp) {
269                         LOGGER.error("Error while removing ip from list of blacklist ips : "
270                                         + excp.getMessage(), excp);
271                 
272                         ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_NOT_FOUND, 
273                                         DMaaPResponseCode.REMOVE_BLACKLIST.getResponseCode(), 
274                                         "Error while removing ip from list of blacklist ips " + excp.getMessage());
275                         LOGGER.info(errRes.toString());
276                         throw new CambriaApiException(errRes);
277                         
278                 }
279         }
280
281
282 }