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