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.PrintWriter;
41 import java.util.HashMap;
42 import java.util.LinkedList;
43 import java.util.List;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
49 import org.apache.jcs.JCS;
50 import org.apache.jcs.admin.CacheRegionInfo;
51 import org.apache.jcs.admin.JCSAdminBean;
52 import org.apache.jcs.engine.behavior.ICacheElement;
53 import org.json.JSONArray;
54 import org.json.JSONObject;
55 import org.onap.portalsdk.core.controller.RestrictedBaseController;
56 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
57 import org.onap.portalsdk.core.web.support.JsonMessage;
58 import org.springframework.stereotype.Controller;
59 import org.springframework.web.bind.annotation.RequestMapping;
60 import org.springframework.web.bind.annotation.RequestMethod;
61 import org.springframework.web.servlet.ModelAndView;
63 import com.fasterxml.jackson.databind.DeserializationFeature;
64 import com.fasterxml.jackson.databind.ObjectMapper;
65 import com.fasterxml.jackson.databind.SerializationFeature;
69 public class CacheAdminController extends RestrictedBaseController {
71 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CacheAdminController.class);
73 private JCSAdminBean jcsAdminBean = new JCSAdminBean();
75 @RequestMapping(value = { "/jcs_admin" }, method = RequestMethod.GET)
76 public ModelAndView cacheAdmin(HttpServletRequest request) {
77 Map<String, Object> model = new HashMap<String, Object>();
79 model.put("model", getRegions());
81 return new ModelAndView(getViewName(), model);
84 @RequestMapping(value = { "/get_regions" }, method = RequestMethod.GET)
85 public void getRegions(HttpServletRequest request, 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 Exception {
97 String cacheName = (String) request.getParameter("cacheName");
98 clearCacheRegion(cacheName);
100 response.setContentType("application/json");
101 PrintWriter out = response.getWriter();
102 out.write(getRegions().toString());
105 @RequestMapping(value = { "/jcs_admin/clearAll" }, method = RequestMethod.GET)
106 public void clearAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
109 response.setContentType("application/json");
110 PrintWriter out = response.getWriter();
111 out.write(getRegions().toString());
114 @RequestMapping(value = { "/jcs_admin/clearItem" }, method = RequestMethod.GET)
115 public void clearItem(HttpServletRequest request, HttpServletResponse response) throws Exception {
116 String keyName = (String) request.getParameter("keyName");
117 String cacheName = (String) request.getParameter("cacheName");
118 clearCacheRegionItem(cacheName, keyName);
120 response.setContentType("application/json");
121 PrintWriter out = response.getWriter();
122 out.write(getRegions().toString());
125 @RequestMapping(value = { "/jcs_admin/showItemDetails" }, method = RequestMethod.GET)
126 public void showItemDetails(HttpServletRequest request, HttpServletResponse response) throws Exception {
127 String cacheName = (String) request.getParameter("cacheName");
128 String keyName = (String) request.getParameter("keyName");
129 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 // response.setContentType("text/plain");
140 PrintWriter out = response.getWriter();
141 out.write(j.toString());
144 @RequestMapping(value = { "/jcs_admin/showRegionDetails" }, method = RequestMethod.GET)
145 public void showRegionDetails(HttpServletRequest request, HttpServletResponse response) throws Exception {
146 String cacheName = (String) request.getParameter("cacheName");
147 ObjectMapper mapper = new ObjectMapper();
149 String details = getRegionStats(cacheName);
150 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(details));
151 JSONObject j = new JSONObject(msg);
152 response.setContentType("application/json");
153 PrintWriter out = response.getWriter();
154 out.write(j.toString());
155 } catch (Exception e) {
156 logger.error(EELFLoggerDelegate.errorLogger, "showRegionDetailed failed for cache name " + cacheName, e);
161 @SuppressWarnings("unchecked")
162 public JSONArray getRegions() {
163 LinkedList<CacheRegionInfo> regions = null;
164 JSONArray ja = new JSONArray();
166 regions = getJcsAdminBean().buildCacheInfo();
167 ObjectMapper mapper = new ObjectMapper();
168 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
169 mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
170 for (CacheRegionInfo cri : regions) {
171 if (cri.getCache().getCacheName() != null && !cri.getCache().getCacheName().equals("[object Object]")) {
172 JSONObject jo = new JSONObject();
173 jo.put("cacheName", cri.getCache().getCacheName());
174 jo.put("size", cri.getCache().getSize());
175 jo.put("byteCount", cri.getByteCount());
176 jo.put("status", cri.getStatus());
177 jo.put("hitCountRam", cri.getCache().getHitCountRam());
178 jo.put("hitCountAux", cri.getCache().getHitCountAux());
179 jo.put("missCountNotFound", cri.getCache().getMissCountNotFound());
180 jo.put("missCountExpired", cri.getCache().getMissCountExpired());
182 new JSONArray(mapper.writeValueAsString(getRegionItems(cri.getCache().getCacheName()))));
186 } catch (Exception e) {
187 logger.error(EELFLoggerDelegate.errorLogger, "getRegions failed", e);
193 private String getRegionStats(String cacheName) throws Exception {
196 JCS cache = JCS.getInstance(cacheName);
197 stats = cache.getStats();
202 private String getItemDetails(String cacheName, String keyName) throws Exception {
205 JCS cache = JCS.getInstance(cacheName);
206 ICacheElement element = cache.getCacheElement(keyName);
208 if (element != null) {
209 ObjectMapper mapper = new ObjectMapper();
210 mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
211 details = mapper.writeValueAsString(element);
217 @SuppressWarnings("rawtypes")
218 private List getRegionItems(String cacheName) {
222 items = getJcsAdminBean().buildElementInfo(cacheName);
223 } catch (Exception e) {
224 logger.error(EELFLoggerDelegate.errorLogger, "getRegionItems failed for cache name " + cacheName, e);
229 private void clearAllRegions() {
231 getJcsAdminBean().clearAllRegions();
232 } catch (Exception e) {
233 logger.error(EELFLoggerDelegate.errorLogger, "clearAllRegions faield", e);
237 private void clearCacheRegion(String cacheName) {
239 getJcsAdminBean().clearRegion(cacheName);
240 } catch (Exception e) {
241 logger.error(EELFLoggerDelegate.errorLogger, "clearCacheRegion failed for cache name " + cacheName, e);
245 private void clearCacheRegionItem(String cacheName, String keyName) {
247 getJcsAdminBean().removeItem(cacheName, keyName);
248 } catch (Exception e) {
249 logger.error(EELFLoggerDelegate.errorLogger, "clearCacheRegionItem failed for key name " + keyName, e);
253 public JCSAdminBean getJcsAdminBean() {
257 public void setJcsAdminBean(JCSAdminBean jcsAdminBean) {
258 this.jcsAdminBean = jcsAdminBean;