c3f8cfc359f44fb203f5838168b5d4cc5ddd1370
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.controller.core;
39
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;
45 import java.util.Map;
46
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
49
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;
64
65 import com.fasterxml.jackson.databind.DeserializationFeature;
66 import com.fasterxml.jackson.databind.ObjectMapper;
67 import com.fasterxml.jackson.databind.SerializationFeature;
68
69 @Controller
70 @RequestMapping("/")
71 public class CacheAdminController extends RestrictedBaseController {
72
73         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CacheAdminController.class);
74
75         private JCSAdminBean jcsAdminBean = new JCSAdminBean();
76
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);
82         }
83
84         @RequestMapping(value = { "/get_regions" }, method = RequestMethod.GET)
85         public void getRegions(HttpServletResponse response) {
86                 try {
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);
92                 }
93         }
94
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());
102         }
103
104         @RequestMapping(value = { "/jcs_admin/clearAll" }, method = RequestMethod.GET)
105         public void clearAll(HttpServletResponse response) throws IOException {
106                 clearAllRegions();
107                 response.setContentType("application/json");
108                 PrintWriter out = response.getWriter();
109                 out.write(getRegions().toString());
110         }
111
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());
120         }
121
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;
127                 try {
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);
132                 }
133                 JSONObject j = new JSONObject(details);
134                 response.setContentType("application/json");
135                 PrintWriter out = response.getWriter();
136                 out.write(j.toString());
137         }
138
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();
143                 try {
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);
152                         return;
153                 }
154         }
155
156         @SuppressWarnings("unchecked")
157         public JSONArray getRegions() {
158                 LinkedList<CacheRegionInfo> regions = null;
159                 JSONArray ja = new JSONArray();
160                 try {
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());
176                                         jo.put("items",
177                                                         new JSONArray(mapper.writeValueAsString(getRegionItems(cri.getCache().getCacheName()))));
178                                         ja.put(jo);
179                                 }
180                         }
181                 } catch (Exception e) {
182                         logger.error(EELFLoggerDelegate.errorLogger, "getRegions failed", e);
183                 }
184
185                 return ja;
186         }
187
188         private String getRegionStats(String cacheName) throws CacheException {
189                 JCS cache = JCS.getInstance(cacheName);
190                 String stats = cache.getStats();
191                 return stats;
192         }
193
194         private String getItemDetails(String cacheName, String keyName) throws Exception {
195
196                 JCS cache = JCS.getInstance(cacheName);
197                 ICacheElement element = cache.getCacheElement(keyName);
198
199                 String details = "";
200                 if (element != null) {
201                         ObjectMapper mapper = new ObjectMapper();
202                         mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
203                         details = mapper.writeValueAsString(element);
204                 }
205
206                 return details;
207         }
208
209         @SuppressWarnings("rawtypes")
210         private List getRegionItems(String cacheName) {
211                 List items = null;
212
213                 try {
214                         items = getJcsAdminBean().buildElementInfo(cacheName);
215                 } catch (Exception e) {
216                         logger.error(EELFLoggerDelegate.errorLogger, "getRegionItems failed for cache name " + cacheName, e);
217                 }
218                 return items;
219         }
220
221         private void clearAllRegions() {
222                 try {
223                         getJcsAdminBean().clearAllRegions();
224                 } catch (Exception e) {
225                         logger.error(EELFLoggerDelegate.errorLogger, "clearAllRegions faield", e);
226                 }
227         }
228
229         private void clearCacheRegion(String cacheName) {
230                 try {
231                         getJcsAdminBean().clearRegion(cacheName);
232                 } catch (Exception e) {
233                         logger.error(EELFLoggerDelegate.errorLogger, "clearCacheRegion failed for cache name " + cacheName, e);
234                 }
235         }
236
237         private void clearCacheRegionItem(String cacheName, String keyName) {
238                 try {
239                         getJcsAdminBean().removeItem(cacheName, keyName);
240                 } catch (Exception e) {
241                         logger.error(EELFLoggerDelegate.errorLogger, "clearCacheRegionItem failed for key name " + keyName, e);
242                 }
243         }
244
245         public JCSAdminBean getJcsAdminBean() {
246                 return jcsAdminBean;
247         }
248
249         public void setJcsAdminBean(JCSAdminBean jcsAdminBean) {
250                 this.jcsAdminBean = jcsAdminBean;
251         }
252 }