diff options
author | 2017-02-07 11:47:55 -0500 | |
---|---|---|
committer | 2017-02-07 12:51:32 -0500 | |
commit | 1faf201e8608dfa4d7af3460fd3d1fc7ebec398b (patch) | |
tree | 2208f76271cc6b1a81dbfb9143cd7118b071a909 /ecomp-sdk/quantum/src/main/java/org/openecomp/portalsdk/core/controller/CacheAdminController.java | |
parent | ca23569858b8cc9f60010e193b7041feb2d3227b (diff) |
Initial OpenECOMP Portal SDK commit
Change-Id: I66a3491600a4b9ea241128dc29267eed6a78ed76
Signed-off-by: talasila <talasila@research.att.com>
Diffstat (limited to 'ecomp-sdk/quantum/src/main/java/org/openecomp/portalsdk/core/controller/CacheAdminController.java')
-rw-r--r-- | ecomp-sdk/quantum/src/main/java/org/openecomp/portalsdk/core/controller/CacheAdminController.java | 252 |
1 files changed, 252 insertions, 0 deletions
diff --git a/ecomp-sdk/quantum/src/main/java/org/openecomp/portalsdk/core/controller/CacheAdminController.java b/ecomp-sdk/quantum/src/main/java/org/openecomp/portalsdk/core/controller/CacheAdminController.java new file mode 100644 index 00000000..095c41b6 --- /dev/null +++ b/ecomp-sdk/quantum/src/main/java/org/openecomp/portalsdk/core/controller/CacheAdminController.java @@ -0,0 +1,252 @@ +/*- + * ================================================================================ + * eCOMP Portal SDK + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ================================================================================ + */ +package org.openecomp.portalsdk.core.controller; + +import java.io.PrintWriter; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.jcs.JCS; +import org.apache.jcs.admin.CacheRegionInfo; +import org.apache.jcs.admin.JCSAdminBean; +import org.apache.jcs.engine.behavior.ICacheElement; +import org.json.JSONArray; +import org.json.JSONObject; +import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.openecomp.portalsdk.core.web.support.JsonMessage; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + +@Controller +@RequestMapping("/") +public class CacheAdminController extends RestrictedBaseController { + + EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CacheAdminController.class); + private JCSAdminBean jcsAdminBean = new JCSAdminBean(); + + @RequestMapping(value = {"/jcs_admin" }, method = RequestMethod.GET) + public ModelAndView cacheAdmin(HttpServletRequest request) { + Map<String, Object> model = new HashMap<String, Object>(); + + model.put("model", getRegions()); + + return new ModelAndView(getViewName(),model); + } + + @RequestMapping(value = {"/get_regions" }, method = RequestMethod.GET) + public void getRegions(HttpServletRequest request,HttpServletResponse response) { + // ObjectMapper mapper = new ObjectMapper(); + try{ + JsonMessage msg = new JsonMessage(getRegions().toString()); + JSONObject j = new JSONObject(msg); + response.getWriter().write(j.toString()); + }catch (Exception e) { + e.printStackTrace(); + } + + } + + @RequestMapping(value = {"/jcs_admin/clearRegion" }, method = RequestMethod.GET) + public void clearRegion(HttpServletRequest request,HttpServletResponse response) throws Exception { + String cacheName = (String) request.getParameter("cacheName"); + clearCacheRegion(cacheName); + + response.setContentType("application/json"); + PrintWriter out = response.getWriter(); + out.write(getRegions().toString()); + } + + @RequestMapping(value = {"/jcs_admin/clearAll" }, method = RequestMethod.GET) + public void clearAll(HttpServletRequest request,HttpServletResponse response) throws Exception { + clearAllRegions(); + + response.setContentType("application/json"); + PrintWriter out = response.getWriter(); + out.write(getRegions().toString()); + } + + @RequestMapping(value = {"/jcs_admin/clearItem" }, method = RequestMethod.GET) + public void clearItem(HttpServletRequest request,HttpServletResponse response) throws Exception { + String keyName = (String) request.getParameter("keyName"); + String cacheName = (String) request.getParameter("cacheName"); + clearCacheRegionItem(cacheName, keyName); + + response.setContentType("application/json"); + PrintWriter out = response.getWriter(); + out.write(getRegions().toString()); + } + + @RequestMapping(value = {"/jcs_admin/showItemDetails" }, method = RequestMethod.GET) + public void showItemDetails(HttpServletRequest request,HttpServletResponse response) throws Exception { + String cacheName = (String) request.getParameter("cacheName"); + String keyName = (String) request.getParameter("keyName"); + String details = null; + + try { + details = getItemDetails(cacheName, keyName); + } catch (Exception e) { + details = "There was an error retrieving the region details. Please try again."; + logger.error(EELFLoggerDelegate.errorLogger, "An error has occurred while retrieving the item details for the cache region - " + + cacheName + e.getMessage()); + + } + + response.setContentType("application/json"); + PrintWriter out = response.getWriter(); + out.write(details); + } + + @RequestMapping(value = {"/jcs_admin/showRegionDetails" }, method = RequestMethod.GET) + public void showRegionDetails(HttpServletRequest request,HttpServletResponse response) throws Exception { + String cacheName = (String) request.getParameter("cacheName"); + String details = null; + + try { + details = getRegionStats(cacheName); + } catch (Exception e) { + details = "There was an error retrieving the region details. Please try again."; + logger.error(EELFLoggerDelegate.errorLogger, "An error has occurred while retrieving the region details for the cache region - " + + cacheName + e.getMessage()); + + } + + response.setContentType("application/json"); + PrintWriter out = response.getWriter(); + out.write(details); + } + + @SuppressWarnings("unchecked") + public JSONArray getRegions(){ + LinkedList<CacheRegionInfo> regions = null; + JSONArray ja = new JSONArray(); + try { + regions = getJcsAdminBean().buildCacheInfo(); + ObjectMapper mapper = new ObjectMapper(); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); + for (CacheRegionInfo cri : regions) { + JSONObject jo = new JSONObject(); + jo.put("cacheName", cri.getCache().getCacheName()); + jo.put("size", cri.getCache().getSize()); + jo.put("byteCount", cri.getByteCount()); + jo.put("status", cri.getStatus()); + jo.put("hitCountRam", cri.getCache().getHitCountRam()); + jo.put("hitCountAux", cri.getCache().getHitCountAux()); + jo.put("missCountNotFound", cri.getCache().getMissCountNotFound()); + jo.put("missCountExpired", cri.getCache().getMissCountExpired()); + jo.put("items",new JSONArray(mapper.writeValueAsString(getRegionItems(cri.getCache().getCacheName())))); + ja.put(jo); + } + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "An error has occurred while retrieving the list of cache regions" + e.getMessage()); + + } + + return ja; + } + + private String getRegionStats(String cacheName) throws Exception { + String stats = ""; + + JCS cache = JCS.getInstance(cacheName); + stats = cache.getStats(); + + return stats; + } + + private String getItemDetails(String cacheName, String keyName) + throws Exception { + String details = ""; + + JCS cache = JCS.getInstance(cacheName); + ICacheElement element = cache.getCacheElement(keyName); + + if (element != null) { + ObjectMapper mapper = new ObjectMapper(); + mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); + details = mapper.writeValueAsString(element); + } + + return details; + } + + @SuppressWarnings("rawtypes") + private List getRegionItems(String cacheName) { + List items = null; + + try { + items = getJcsAdminBean().buildElementInfo(cacheName); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "An error has occurred while retrieving the list of items for cache region - " + + cacheName + e.getMessage()); + + } + + return items; + } + + private void clearAllRegions() { + try { + getJcsAdminBean().clearAllRegions(); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "An error has occurred while clearing all cache regions." + e.getMessage()); + + } + } + + private void clearCacheRegion(String cacheName) { + try { + getJcsAdminBean().clearRegion(cacheName); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "An error has occurred while clearing the cache region - " + + cacheName + e.getMessage()); + + } + } + + private void clearCacheRegionItem(String cacheName, String keyName) { + try { + getJcsAdminBean().removeItem(cacheName, keyName); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "An error has occurred while removing cache region item - " + + keyName + e.getMessage()); + + } + } + + public JCSAdminBean getJcsAdminBean() { + return jcsAdminBean; + } + + public void setJcsAdminBean(JCSAdminBean jcsAdminBean) { + this.jcsAdminBean = jcsAdminBean; + } +} |