2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.controller.core;
40 import com.fasterxml.jackson.core.JsonParseException;
41 import com.fasterxml.jackson.core.JsonProcessingException;
42 import java.io.IOException;
43 import java.io.PrintWriter;
44 import java.util.HashMap;
45 import java.util.LinkedList;
46 import java.util.List;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
52 import org.apache.jcs.JCS;
53 import org.apache.jcs.access.exception.CacheException;
54 import org.apache.jcs.admin.CacheRegionInfo;
55 import org.apache.jcs.admin.JCSAdminBean;
56 import org.apache.jcs.engine.behavior.ICacheElement;
57 import org.json.JSONArray;
58 import org.json.JSONObject;
59 import org.onap.portalsdk.core.controller.RestrictedBaseController;
60 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
61 import org.onap.portalsdk.core.web.support.JsonMessage;
62 import org.springframework.stereotype.Controller;
63 import org.springframework.web.bind.annotation.RequestMapping;
64 import org.springframework.web.bind.annotation.RequestMethod;
65 import org.springframework.web.servlet.ModelAndView;
67 import com.fasterxml.jackson.databind.DeserializationFeature;
68 import com.fasterxml.jackson.databind.ObjectMapper;
69 import com.fasterxml.jackson.databind.SerializationFeature;
73 public class CacheAdminController extends RestrictedBaseController {
75 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CacheAdminController.class);
76 private static final String CACHE_NAME = "cacheName";
77 private static final String APPLICATION_JSON = "application/json";
79 private JCSAdminBean jcsAdminBean = new JCSAdminBean();
81 @RequestMapping(value = { "/jcs_admin" }, method = RequestMethod.GET)
82 public ModelAndView cacheAdmin() {
83 Map<String, Object> model = new HashMap<>();
84 model.put("model", getRegions());
85 return new ModelAndView(getViewName(), model);
88 @RequestMapping(value = { "/get_regions" }, method = RequestMethod.GET)
89 public void getRegions(HttpServletResponse response) {
91 JsonMessage msg = new JsonMessage(getRegions().toString());
92 JSONObject j = new JSONObject(msg);
93 response.getWriter().write(j.toString());
94 } catch (Exception e) {
95 logger.error(EELFLoggerDelegate.errorLogger, "getRegions failed", e);
99 @RequestMapping(value = { "/jcs_admin/clearRegion" }, method = RequestMethod.GET)
100 public void clearRegion(HttpServletRequest request, HttpServletResponse response) throws IOException {
101 String cacheName = request.getParameter(CACHE_NAME);
102 clearCacheRegion(cacheName);
103 response.setContentType(APPLICATION_JSON);
104 PrintWriter out = response.getWriter();
105 out.write(getRegions().toString());
108 @RequestMapping(value = { "/jcs_admin/clearAll" }, method = RequestMethod.GET)
109 public void clearAll(HttpServletResponse response) throws IOException {
111 response.setContentType(APPLICATION_JSON);
112 PrintWriter out = response.getWriter();
113 out.write(getRegions().toString());
116 @RequestMapping(value = { "/jcs_admin/clearItem" }, method = RequestMethod.GET)
117 public void clearItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
118 String keyName = request.getParameter("keyName");
119 String cacheName = request.getParameter(CACHE_NAME);
120 clearCacheRegionItem(cacheName, keyName);
121 response.setContentType(APPLICATION_JSON);
122 PrintWriter out = response.getWriter();
123 out.write(getRegions().toString());
126 @RequestMapping(value = { "/jcs_admin/showItemDetails" }, method = RequestMethod.GET)
127 public void showItemDetails(HttpServletRequest request, HttpServletResponse response) throws IOException {
128 String cacheName = request.getParameter(CACHE_NAME);
129 String keyName = request.getParameter("keyName");
130 String details = null;
132 details = getItemDetails(cacheName, keyName);
133 } catch (Exception e) {
134 details = "There was an error retrieving the region details. Please try again.";
135 logger.error(EELFLoggerDelegate.errorLogger, "showItemDetails failed for cache name " + cacheName, e);
137 JSONObject j = new JSONObject(details);
138 response.setContentType(APPLICATION_JSON);
139 PrintWriter out = response.getWriter();
140 out.write(j.toString());
143 @RequestMapping(value = { "/jcs_admin/showRegionDetails" }, method = RequestMethod.GET)
144 public void showRegionDetails(HttpServletRequest request, HttpServletResponse response) {
145 String cacheName = request.getParameter(CACHE_NAME);
146 ObjectMapper mapper = new ObjectMapper();
148 String details = getRegionStats(cacheName);
149 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(details));
150 JSONObject j = new JSONObject(msg);
151 response.setContentType(APPLICATION_JSON);
152 PrintWriter out = response.getWriter();
153 out.write(j.toString());
154 } catch (Exception e) {
155 logger.error(EELFLoggerDelegate.errorLogger, "showRegionDetailed failed for cache name " + cacheName, e);
160 @SuppressWarnings("unchecked")
161 public JSONArray getRegions() {
162 LinkedList<CacheRegionInfo> regions = null;
163 JSONArray ja = new JSONArray();
165 regions = getJcsAdminBean().buildCacheInfo();
166 ObjectMapper mapper = new ObjectMapper();
167 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
168 mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
169 for (CacheRegionInfo cri : regions) {
170 if (cri.getCache().getCacheName() != null && !"[object Object]".equals(cri.getCache().getCacheName())) {
171 JSONObject jo = new JSONObject();
172 jo.put(CACHE_NAME, cri.getCache().getCacheName());
173 jo.put("size", cri.getCache().getSize());
174 jo.put("byteCount", cri.getByteCount());
175 jo.put("status", cri.getStatus());
176 jo.put("hitCountRam", cri.getCache().getHitCountRam());
177 jo.put("hitCountAux", cri.getCache().getHitCountAux());
178 jo.put("missCountNotFound", cri.getCache().getMissCountNotFound());
179 jo.put("missCountExpired", cri.getCache().getMissCountExpired());
181 new JSONArray(mapper.writeValueAsString(getRegionItems(cri.getCache().getCacheName()))));
185 } catch (Exception e) {
186 logger.error(EELFLoggerDelegate.errorLogger, "getRegions failed", e);
192 private String getRegionStats(String cacheName) throws CacheException {
193 JCS cache = JCS.getInstance(cacheName);
194 return cache.getStats();
197 private String getItemDetails(String cacheName, String keyName) throws CacheException,JsonProcessingException {
199 JCS cache = JCS.getInstance(cacheName);
200 ICacheElement element = cache.getCacheElement(keyName);
203 if (element != null) {
204 ObjectMapper mapper = new ObjectMapper();
205 mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
206 details = mapper.writeValueAsString(element);
212 @SuppressWarnings("rawtypes")
213 private List getRegionItems(String cacheName) {
217 items = getJcsAdminBean().buildElementInfo(cacheName);
218 } catch (Exception e) {
219 logger.error(EELFLoggerDelegate.errorLogger, "getRegionItems failed for cache name " + cacheName, e);
224 private void clearAllRegions() {
226 getJcsAdminBean().clearAllRegions();
227 } catch (Exception e) {
228 logger.error(EELFLoggerDelegate.errorLogger, "clearAllRegions faield", e);
232 private void clearCacheRegion(String cacheName) {
234 getJcsAdminBean().clearRegion(cacheName);
235 } catch (Exception e) {
236 logger.error(EELFLoggerDelegate.errorLogger, "clearCacheRegion failed for cache name " + cacheName, e);
240 private void clearCacheRegionItem(String cacheName, String keyName) {
242 getJcsAdminBean().removeItem(cacheName, keyName);
243 } catch (Exception e) {
244 logger.error(EELFLoggerDelegate.errorLogger, "clearCacheRegionItem failed for key name " + keyName, e);
248 public JCSAdminBean getJcsAdminBean() {
252 public void setJcsAdminBean(JCSAdminBean jcsAdminBean) {
253 this.jcsAdminBean = jcsAdminBean;