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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalapp.controller.core;
40 import java.io.IOException;
41 import java.io.PrintWriter;
42 import java.util.HashMap;
43 import java.util.LinkedList;
44 import java.util.List;
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
50 import org.apache.jcs.JCS;
51 import org.apache.jcs.access.exception.CacheException;
52 import org.apache.jcs.admin.CacheRegionInfo;
53 import org.apache.jcs.admin.JCSAdminBean;
54 import org.apache.jcs.engine.behavior.ICacheElement;
55 import org.json.JSONArray;
56 import org.json.JSONObject;
57 import org.onap.portalsdk.core.controller.RestrictedBaseController;
58 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
59 import org.onap.portalsdk.core.web.support.JsonMessage;
60 import org.springframework.stereotype.Controller;
61 import org.springframework.web.bind.annotation.RequestMapping;
62 import org.springframework.web.bind.annotation.RequestMethod;
63 import org.springframework.web.servlet.ModelAndView;
65 import com.fasterxml.jackson.databind.DeserializationFeature;
66 import com.fasterxml.jackson.databind.ObjectMapper;
67 import com.fasterxml.jackson.databind.SerializationFeature;
71 public class CacheAdminController extends RestrictedBaseController {
73 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CacheAdminController.class);
75 private JCSAdminBean jcsAdminBean = new JCSAdminBean();
77 @RequestMapping(value = { "/jcs_admin" }, method = RequestMethod.GET)
78 public ModelAndView cacheAdmin() {
79 Map<String, Object> model = new HashMap<>();
80 model.put("model", getRegions());
81 return new ModelAndView(getViewName(), model);
84 @RequestMapping(value = { "/get_regions" }, method = RequestMethod.GET)
85 public void getRegions(HttpServletResponse response) {
87 JsonMessage msg = new JsonMessage(getRegions().toString());
88 JSONObject j = new JSONObject(msg);
89 response.getWriter().write(j.toString());
90 } catch (Exception e) {
91 logger.error(EELFLoggerDelegate.errorLogger, "getRegions failed", e);
95 @RequestMapping(value = { "/jcs_admin/clearRegion" }, method = RequestMethod.GET)
96 public void clearRegion(HttpServletRequest request, HttpServletResponse response) throws IOException {
97 String cacheName = request.getParameter("cacheName");
98 clearCacheRegion(cacheName);
99 response.setContentType("application/json");
100 PrintWriter out = response.getWriter();
101 out.write(getRegions().toString());
104 @RequestMapping(value = { "/jcs_admin/clearAll" }, method = RequestMethod.GET)
105 public void clearAll(HttpServletResponse response) throws IOException {
107 response.setContentType("application/json");
108 PrintWriter out = response.getWriter();
109 out.write(getRegions().toString());
112 @RequestMapping(value = { "/jcs_admin/clearItem" }, method = RequestMethod.GET)
113 public void clearItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
114 String keyName = request.getParameter("keyName");
115 String cacheName = request.getParameter("cacheName");
116 clearCacheRegionItem(cacheName, keyName);
117 response.setContentType("application/json");
118 PrintWriter out = response.getWriter();
119 out.write(getRegions().toString());
122 @RequestMapping(value = { "/jcs_admin/showItemDetails" }, method = RequestMethod.GET)
123 public void showItemDetails(HttpServletRequest request, HttpServletResponse response) throws IOException {
124 String cacheName = request.getParameter("cacheName");
125 String keyName = request.getParameter("keyName");
126 String details = null;
128 details = getItemDetails(cacheName, keyName);
129 } catch (Exception e) {
130 details = "There was an error retrieving the region details. Please try again.";
131 logger.error(EELFLoggerDelegate.errorLogger, "showItemDetails failed for cache name " + cacheName, e);
133 JSONObject j = new JSONObject(details);
134 response.setContentType("application/json");
135 PrintWriter out = response.getWriter();
136 out.write(j.toString());
139 @RequestMapping(value = { "/jcs_admin/showRegionDetails" }, method = RequestMethod.GET)
140 public void showRegionDetails(HttpServletRequest request, HttpServletResponse response) {
141 String cacheName = request.getParameter("cacheName");
142 ObjectMapper mapper = new ObjectMapper();
144 String details = getRegionStats(cacheName);
145 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(details));
146 JSONObject j = new JSONObject(msg);
147 response.setContentType("application/json");
148 PrintWriter out = response.getWriter();
149 out.write(j.toString());
150 } catch (Exception e) {
151 logger.error(EELFLoggerDelegate.errorLogger, "showRegionDetailed failed for cache name " + cacheName, e);
156 @SuppressWarnings("unchecked")
157 public JSONArray getRegions() {
158 LinkedList<CacheRegionInfo> regions = null;
159 JSONArray ja = new JSONArray();
161 regions = getJcsAdminBean().buildCacheInfo();
162 ObjectMapper mapper = new ObjectMapper();
163 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
164 mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
165 for (CacheRegionInfo cri : regions) {
166 if (cri.getCache().getCacheName() != null && !cri.getCache().getCacheName().equals("[object Object]")) {
167 JSONObject jo = new JSONObject();
168 jo.put("cacheName", cri.getCache().getCacheName());
169 jo.put("size", cri.getCache().getSize());
170 jo.put("byteCount", cri.getByteCount());
171 jo.put("status", cri.getStatus());
172 jo.put("hitCountRam", cri.getCache().getHitCountRam());
173 jo.put("hitCountAux", cri.getCache().getHitCountAux());
174 jo.put("missCountNotFound", cri.getCache().getMissCountNotFound());
175 jo.put("missCountExpired", cri.getCache().getMissCountExpired());
177 new JSONArray(mapper.writeValueAsString(getRegionItems(cri.getCache().getCacheName()))));
181 } catch (Exception e) {
182 logger.error(EELFLoggerDelegate.errorLogger, "getRegions failed", e);
188 private String getRegionStats(String cacheName) throws CacheException {
189 JCS cache = JCS.getInstance(cacheName);
190 String stats = cache.getStats();
194 private String getItemDetails(String cacheName, String keyName) throws Exception {
196 JCS cache = JCS.getInstance(cacheName);
197 ICacheElement element = cache.getCacheElement(keyName);
200 if (element != null) {
201 ObjectMapper mapper = new ObjectMapper();
202 mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
203 details = mapper.writeValueAsString(element);
209 @SuppressWarnings("rawtypes")
210 private List getRegionItems(String cacheName) {
214 items = getJcsAdminBean().buildElementInfo(cacheName);
215 } catch (Exception e) {
216 logger.error(EELFLoggerDelegate.errorLogger, "getRegionItems failed for cache name " + cacheName, e);
221 private void clearAllRegions() {
223 getJcsAdminBean().clearAllRegions();
224 } catch (Exception e) {
225 logger.error(EELFLoggerDelegate.errorLogger, "clearAllRegions faield", e);
229 private void clearCacheRegion(String cacheName) {
231 getJcsAdminBean().clearRegion(cacheName);
232 } catch (Exception e) {
233 logger.error(EELFLoggerDelegate.errorLogger, "clearCacheRegion failed for cache name " + cacheName, e);
237 private void clearCacheRegionItem(String cacheName, String keyName) {
239 getJcsAdminBean().removeItem(cacheName, keyName);
240 } catch (Exception e) {
241 logger.error(EELFLoggerDelegate.errorLogger, "clearCacheRegionItem failed for key name " + keyName, e);
245 public JCSAdminBean getJcsAdminBean() {
249 public void setJcsAdminBean(JCSAdminBean jcsAdminBean) {
250 this.jcsAdminBean = jcsAdminBean;