1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.service;
24 import java.io.IOException;
25 import java.util.Enumeration;
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;
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;
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;
64 public class AdminRestService {
69 //private static final Logger LOGGER = Logger
70 // .getLogger(AdminRestService.class);
71 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AdminRestService.class);
76 @Qualifier("configurationReader")
77 private ConfigurationReader configReader;
80 * HttpServletRequest obj
83 private HttpServletRequest request;
85 * HttpServletResponse obj
88 private HttpServletResponse response;
93 private AdminService adminService;
96 * Fetches a list of all the registered consumers along with their created
97 * time and last accessed details
99 * @return consumer list in json string format
100 * @throws CambriaApiException
101 * @throws AccessDeniedException
102 * @throws IOException
105 @Path("/consumerCache")
106 //@Produces(MediaType.TEXT_PLAIN)
107 public void getConsumerCache() throws CambriaApiException, AccessDeniedException {
108 LOGGER.info("Fetching list of registered consumers.");
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);
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);
126 * Clears consumer cache
127 * @throws CambriaApiException ex
128 * @throws AccessDeniedException
130 * @throws IOException ex
131 * @throws JSONException ex
134 @Path("/dropConsumerCache")
135 //@Produces(MediaType.TEXT_PLAIN)
136 public void dropConsumerCache() throws CambriaApiException, AccessDeniedException {
137 LOGGER.info("Dropping consumer cache");
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);
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);
151 } catch (JSONException | IOException e) {
153 "Error while Dropping consumer cache : " + e.getMessage(),
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);
164 * Get list of blacklisted ips
165 * @throws CambriaApiException excp
169 //@Produces(MediaType.TEXT_PLAIN)
170 public void getBlacklist() throws CambriaApiException {
171 LOGGER.info("Fetching list of blacklist ips.");
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);
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);
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);
192 } catch ( IOException excp) {
193 LOGGER.error("Error while Fetching list of blacklist ips : "
194 + excp.getMessage(), excp);
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);
207 * Add ip to list of blacklist ips
209 * @throws CambriaApiException excp
212 @Path("/blacklist/{ip}")
213 //@Produces(MediaType.TEXT_PLAIN)
214 public void addToBlacklist (@PathParam("ip") String ip ) throws CambriaApiException
216 LOGGER.info("Adding ip to list of blacklist ips.");
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);
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);
230 } catch (IOException | ConfigDbException excp) {
231 LOGGER.error("Error while adding ip to list of blacklist ips : "
232 + excp.getMessage(), excp);
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);
244 * Remove ip from blacklist
246 * @throws CambriaApiException excp
247 * @throws AccessDeniedException excp
248 * @throws ConfigDbException excp
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.");
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);
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);
268 } catch (IOException | ConfigDbException excp) {
269 LOGGER.error("Error while removing ip from list of blacklist ips : "
270 + excp.getMessage(), excp);
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);