79866f6c54f69fcf001af435d4d26c2db02ca3ab
[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.PrintWriter;
41 import java.util.HashMap;
42 import java.util.LinkedList;
43 import java.util.List;
44 import java.util.Map;
45
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48
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;
62
63 import com.fasterxml.jackson.databind.DeserializationFeature;
64 import com.fasterxml.jackson.databind.ObjectMapper;
65 import com.fasterxml.jackson.databind.SerializationFeature;
66
67 @Controller
68 @RequestMapping("/")
69 public class CacheAdminController extends RestrictedBaseController {
70
71         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CacheAdminController.class);
72
73         private JCSAdminBean jcsAdminBean = new JCSAdminBean();
74
75         @RequestMapping(value = { "/jcs_admin" }, method = RequestMethod.GET)
76         public ModelAndView cacheAdmin(HttpServletRequest request) {
77                 Map<String, Object> model = new HashMap<String, Object>();
78
79                 model.put("model", getRegions());
80
81                 return new ModelAndView(getViewName(), model);
82         }
83
84         @RequestMapping(value = { "/get_regions" }, method = RequestMethod.GET)
85         public void getRegions(HttpServletRequest request, 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 Exception {
97                 String cacheName = (String) request.getParameter("cacheName");
98                 clearCacheRegion(cacheName);
99
100                 response.setContentType("application/json");
101                 PrintWriter out = response.getWriter();
102                 out.write(getRegions().toString());
103         }
104
105         @RequestMapping(value = { "/jcs_admin/clearAll" }, method = RequestMethod.GET)
106         public void clearAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
107                 clearAllRegions();
108
109                 response.setContentType("application/json");
110                 PrintWriter out = response.getWriter();
111                 out.write(getRegions().toString());
112         }
113
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);
119
120                 response.setContentType("application/json");
121                 PrintWriter out = response.getWriter();
122                 out.write(getRegions().toString());
123         }
124
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;
130
131                 try {
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);
136                 }
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());
142         }
143
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();
148                 try {
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);
157                         return;
158                 }
159         }
160
161         @SuppressWarnings("unchecked")
162         public JSONArray getRegions() {
163                 LinkedList<CacheRegionInfo> regions = null;
164                 JSONArray ja = new JSONArray();
165                 try {
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());
181                                         jo.put("items",
182                                                         new JSONArray(mapper.writeValueAsString(getRegionItems(cri.getCache().getCacheName()))));
183                                         ja.put(jo);
184                                 }
185                         }
186                 } catch (Exception e) {
187                         logger.error(EELFLoggerDelegate.errorLogger, "getRegions failed", e);
188                 }
189
190                 return ja;
191         }
192
193         private String getRegionStats(String cacheName) throws Exception {
194                 String stats = "";
195
196                 JCS cache = JCS.getInstance(cacheName);
197                 stats = cache.getStats();
198
199                 return stats;
200         }
201
202         private String getItemDetails(String cacheName, String keyName) throws Exception {
203                 String details = "";
204
205                 JCS cache = JCS.getInstance(cacheName);
206                 ICacheElement element = cache.getCacheElement(keyName);
207
208                 if (element != null) {
209                         ObjectMapper mapper = new ObjectMapper();
210                         mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
211                         details = mapper.writeValueAsString(element);
212                 }
213
214                 return details;
215         }
216
217         @SuppressWarnings("rawtypes")
218         private List getRegionItems(String cacheName) {
219                 List items = null;
220
221                 try {
222                         items = getJcsAdminBean().buildElementInfo(cacheName);
223                 } catch (Exception e) {
224                         logger.error(EELFLoggerDelegate.errorLogger, "getRegionItems failed for cache name " + cacheName, e);
225                 }
226                 return items;
227         }
228
229         private void clearAllRegions() {
230                 try {
231                         getJcsAdminBean().clearAllRegions();
232                 } catch (Exception e) {
233                         logger.error(EELFLoggerDelegate.errorLogger, "clearAllRegions faield", e);
234                 }
235         }
236
237         private void clearCacheRegion(String cacheName) {
238                 try {
239                         getJcsAdminBean().clearRegion(cacheName);
240                 } catch (Exception e) {
241                         logger.error(EELFLoggerDelegate.errorLogger, "clearCacheRegion failed for cache name " + cacheName, e);
242                 }
243         }
244
245         private void clearCacheRegionItem(String cacheName, String keyName) {
246                 try {
247                         getJcsAdminBean().removeItem(cacheName, keyName);
248                 } catch (Exception e) {
249                         logger.error(EELFLoggerDelegate.errorLogger, "clearCacheRegionItem failed for key name " + keyName, e);
250                 }
251         }
252
253         public JCSAdminBean getJcsAdminBean() {
254                 return jcsAdminBean;
255         }
256
257         public void setJcsAdminBean(JCSAdminBean jcsAdminBean) {
258                 this.jcsAdminBean = jcsAdminBean;
259         }
260 }