f30ab9b32bdf2243240e6623de37cd7b5ddafc3b
[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  * 
37  */
38 package org.onap.portalapp.controller.core;
39
40 import com.fasterxml.jackson.core.JsonProcessingException;
41 import java.io.IOException;
42 import java.io.PrintWriter;
43 import java.util.HashMap;
44 import java.util.LinkedList;
45 import java.util.List;
46 import java.util.Map;
47
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
50
51 import org.apache.jcs.JCS;
52 import org.apache.jcs.access.exception.CacheException;
53 import org.apache.jcs.admin.CacheRegionInfo;
54 import org.apache.jcs.admin.JCSAdminBean;
55 import org.apache.jcs.engine.behavior.ICacheElement;
56 import org.json.JSONArray;
57 import org.json.JSONObject;
58 import org.onap.portalsdk.core.controller.RestrictedBaseController;
59 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
60 import org.onap.portalsdk.core.web.support.JsonMessage;
61 import org.springframework.stereotype.Controller;
62 import org.springframework.web.bind.annotation.RequestMapping;
63 import org.springframework.web.bind.annotation.RequestMethod;
64 import org.springframework.web.servlet.ModelAndView;
65
66 import com.fasterxml.jackson.databind.DeserializationFeature;
67 import com.fasterxml.jackson.databind.ObjectMapper;
68 import com.fasterxml.jackson.databind.SerializationFeature;
69
70 @Controller
71 @RequestMapping("/")
72 public class CacheAdminController extends RestrictedBaseController {
73
74         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CacheAdminController.class);
75         private static final String CACHE_NAME = "cacheName";
76         private static final String APPLICATION_JSON = "application/json";
77
78         private JCSAdminBean jcsAdminBean = new JCSAdminBean();
79
80         @RequestMapping(value = { "/jcs_admin" }, method = RequestMethod.GET)
81         public ModelAndView cacheAdmin() {
82                 Map<String, Object> model = new HashMap<>();
83                 model.put("model", getRegions());
84                 return new ModelAndView(getViewName(), model);
85         }
86
87         @RequestMapping(value = { "/get_regions" }, method = RequestMethod.GET)
88         public void getRegions(HttpServletResponse response) {
89                 try {
90                         JsonMessage msg = new JsonMessage(getRegions().toString());
91                         JSONObject j = new JSONObject(msg);
92                         response.getWriter().write(j.toString());
93                 } catch (Exception e) {
94                         logger.error(EELFLoggerDelegate.errorLogger, "getRegions failed", e);
95                 }
96         }
97
98         @RequestMapping(value = { "/jcs_admin/clearRegion" }, method = RequestMethod.GET)
99         public void clearRegion(HttpServletRequest request, HttpServletResponse response) throws IOException {
100                 String cacheName = request.getParameter(CACHE_NAME);
101                 clearCacheRegion(cacheName);
102                 response.setContentType(APPLICATION_JSON);
103                 PrintWriter out = response.getWriter();
104                 out.write(getRegions().toString());
105         }
106
107         @RequestMapping(value = { "/jcs_admin/clearAll" }, method = RequestMethod.GET)
108         public void clearAll(HttpServletResponse response) throws IOException {
109                 clearAllRegions();
110                 response.setContentType(APPLICATION_JSON);
111                 PrintWriter out = response.getWriter();
112                 out.write(getRegions().toString());
113         }
114
115         @RequestMapping(value = { "/jcs_admin/clearItem" }, method = RequestMethod.GET)
116         public void clearItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
117                 String keyName = request.getParameter("keyName");
118                 String cacheName = request.getParameter(CACHE_NAME);
119                 clearCacheRegionItem(cacheName, keyName);
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 IOException {
127                 String cacheName = request.getParameter(CACHE_NAME);
128                 String keyName = request.getParameter("keyName");
129                 String details = null;
130                 try {
131                         details = getItemDetails(cacheName, keyName);
132                 } catch (Exception e) {
133                         details = "There was an error retrieving the region details. Please try again.";
134                         logger.error(EELFLoggerDelegate.errorLogger, "showItemDetails failed for cache name " + cacheName, e);
135                 }
136                 JSONObject j = new JSONObject(details);
137                 response.setContentType(APPLICATION_JSON);
138                 PrintWriter out = response.getWriter();
139                 out.write(j.toString());
140         }
141
142         @RequestMapping(value = { "/jcs_admin/showRegionDetails" }, method = RequestMethod.GET)
143         public void showRegionDetails(HttpServletRequest request, HttpServletResponse response) {
144                 String cacheName = request.getParameter(CACHE_NAME);
145                 ObjectMapper mapper = new ObjectMapper();
146                 try {
147                         String details = getRegionStats(cacheName);
148                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(details));
149                         JSONObject j = new JSONObject(msg);
150                         response.setContentType(APPLICATION_JSON);
151                         PrintWriter out = response.getWriter();
152                         out.write(j.toString());
153                 } catch (Exception e) {
154                         logger.error(EELFLoggerDelegate.errorLogger, "showRegionDetailed failed for cache name " + cacheName, e);
155                         return;
156                 }
157         }
158
159         @SuppressWarnings("unchecked")
160         public JSONArray getRegions() {
161                 LinkedList<CacheRegionInfo> regions = null;
162                 JSONArray ja = new JSONArray();
163                 try {
164                         regions = getJcsAdminBean().buildCacheInfo();
165                         ObjectMapper mapper = new ObjectMapper();
166                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
167                         mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
168                         for (CacheRegionInfo cri : regions) {
169                                 if (cri.getCache().getCacheName() != null && !"[object Object]".equals(cri.getCache().getCacheName())) {
170                                         JSONObject jo = new JSONObject();
171                                         jo.put(CACHE_NAME, cri.getCache().getCacheName());
172                                         jo.put("size", cri.getCache().getSize());
173                                         jo.put("byteCount", cri.getByteCount());
174                                         jo.put("status", cri.getStatus());
175                                         jo.put("hitCountRam", cri.getCache().getHitCountRam());
176                                         jo.put("hitCountAux", cri.getCache().getHitCountAux());
177                                         jo.put("missCountNotFound", cri.getCache().getMissCountNotFound());
178                                         jo.put("missCountExpired", cri.getCache().getMissCountExpired());
179                                         jo.put("items",
180                                                         new JSONArray(mapper.writeValueAsString(getRegionItems(cri.getCache().getCacheName()))));
181                                         ja.put(jo);
182                                 }
183                         }
184                 } catch (Exception e) {
185                         logger.error(EELFLoggerDelegate.errorLogger, "getRegions failed", e);
186                 }
187
188                 return ja;
189         }
190
191         private String getRegionStats(String cacheName) throws CacheException {
192                 JCS cache = JCS.getInstance(cacheName);
193                 return cache.getStats();
194         }
195
196         private String getItemDetails(String cacheName, String keyName) throws CacheException,JsonProcessingException {
197
198                 JCS cache = JCS.getInstance(cacheName);
199                 ICacheElement element = cache.getCacheElement(keyName);
200
201                 String details = "";
202                 if (element != null) {
203                         ObjectMapper mapper = new ObjectMapper();
204                         mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
205                         details = mapper.writeValueAsString(element);
206                 }
207
208                 return details;
209         }
210
211         @SuppressWarnings("rawtypes")
212         private List getRegionItems(String cacheName) {
213                 List items = null;
214
215                 try {
216                         items = getJcsAdminBean().buildElementInfo(cacheName);
217                 } catch (Exception e) {
218                         logger.error(EELFLoggerDelegate.errorLogger, "getRegionItems failed for cache name " + cacheName, e);
219                 }
220                 return items;
221         }
222
223         private void clearAllRegions() {
224                 try {
225                         getJcsAdminBean().clearAllRegions();
226                 } catch (Exception e) {
227                         logger.error(EELFLoggerDelegate.errorLogger, "clearAllRegions faield", e);
228                 }
229         }
230
231         private void clearCacheRegion(String cacheName) {
232                 try {
233                         getJcsAdminBean().clearRegion(cacheName);
234                 } catch (Exception e) {
235                         logger.error(EELFLoggerDelegate.errorLogger, "clearCacheRegion failed for cache name " + cacheName, e);
236                 }
237         }
238
239         private void clearCacheRegionItem(String cacheName, String keyName) {
240                 try {
241                         getJcsAdminBean().removeItem(cacheName, keyName);
242                 } catch (Exception e) {
243                         logger.error(EELFLoggerDelegate.errorLogger, "clearCacheRegionItem failed for key name " + keyName, e);
244                 }
245         }
246
247         public JCSAdminBean getJcsAdminBean() {
248                 return jcsAdminBean;
249         }
250
251         public void setJcsAdminBean(JCSAdminBean jcsAdminBean) {
252                 this.jcsAdminBean = jcsAdminBean;
253         }
254 }