2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controller;
24 import java.io.IOException;
25 import java.net.MalformedURLException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
34 import javax.management.AttributeNotFoundException;
35 import javax.management.InstanceNotFoundException;
36 import javax.management.MBeanException;
37 import javax.management.MalformedObjectNameException;
38 import javax.management.ObjectName;
39 import javax.management.ReflectionException;
40 import javax.management.remote.JMXConnector;
41 import javax.management.remote.JMXConnectorFactory;
42 import javax.management.remote.JMXServiceURL;
43 import javax.script.SimpleBindings;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
47 import org.json.JSONObject;
48 import org.onap.policy.common.logging.flexlogger.FlexLogger;
49 import org.onap.policy.common.logging.flexlogger.Logger;
50 import org.onap.policy.dao.SystemLogDbDao;
51 import org.onap.policy.model.PDPGroupContainer;
52 import org.onap.policy.rest.XACMLRestProperties;
53 import org.onap.policy.rest.dao.CommonClassDao;
54 import org.onap.policy.xacml.api.XACMLErrorConstants;
55 import org.onap.policy.xacml.api.pap.OnapPDP;
56 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
57 import org.onap.portalsdk.core.controller.RestrictedBaseController;
58 import org.onap.portalsdk.core.web.support.JsonMessage;
59 import org.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.http.MediaType;
61 import org.springframework.stereotype.Controller;
62 import org.springframework.web.bind.annotation.RequestMapping;
64 import com.att.research.xacml.api.pap.PAPException;
65 import com.att.research.xacml.api.pap.PDP;
66 import com.att.research.xacml.api.pap.PDPGroup;
67 import com.att.research.xacml.api.pap.PDPPolicy;
68 import com.att.research.xacml.util.XACMLProperties;
69 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
70 import com.fasterxml.jackson.annotation.PropertyAccessor;
71 import com.fasterxml.jackson.databind.ObjectMapper;
74 @RequestMapping({"/"})
75 public class DashboardController extends RestrictedBaseController{
76 private static final Logger policyLogger = FlexLogger.getLogger(DashboardController.class);
78 SystemLogDbDao systemDAO;
81 CommonClassDao commonClassDao;
84 private PDPGroupContainer pdpConatiner;
85 private ArrayList<Object> pdpStatusData;
86 private ArrayList<Object> papStatusData;
87 private ArrayList<Object> policyActivityData;
89 private PolicyController policyController;
90 public PolicyController getPolicyController() {
91 return policyController;
94 public void setPolicyController(PolicyController policyController) {
95 this.policyController = policyController;
98 private PolicyController getPolicyControllerInstance(){
99 return policyController != null ? getPolicyController() : new PolicyController();
102 @RequestMapping(value={"/get_DashboardLoggingData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
103 public void getData(HttpServletRequest request, HttpServletResponse response){
105 Map<String, Object> model = new HashMap<>();
106 ObjectMapper mapper = new ObjectMapper();
107 model.put("availableLoggingDatas", mapper.writeValueAsString(systemDAO.getLoggingData()));
108 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
109 JSONObject j = new JSONObject(msg);
110 response.getWriter().write(j.toString());
113 policyLogger.error("Exception Occured"+e);
117 @RequestMapping(value={"/get_DashboardSystemAlertData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
118 public void getSystemAlertData(HttpServletRequest request, HttpServletResponse response){
120 Map<String, Object> model = new HashMap<>();
121 ObjectMapper mapper = new ObjectMapper();
122 model.put("systemAlertsTableDatas", mapper.writeValueAsString(systemDAO.getSystemAlertData()));
123 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
124 JSONObject j = new JSONObject(msg);
125 response.getWriter().write(j.toString());
128 policyLogger.error("Exception Occured"+e);
132 @RequestMapping(value={"/get_DashboardPAPStatusData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
133 public void getPAPStatusData(HttpServletRequest request, HttpServletResponse response){
135 Map<String, Object> model = new HashMap<>();
136 ObjectMapper mapper = new ObjectMapper();
137 mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
139 model.put("papTableDatas", mapper.writeValueAsString(papStatusData));
140 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
141 JSONObject j = new JSONObject(msg);
142 response.getWriter().write(j.toString());
145 policyLogger.error("Exception Occured"+e);
149 @RequestMapping(value={"/get_DashboardPDPStatusData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
150 public void getPDPStatusData(HttpServletRequest request, HttpServletResponse response){
152 Map<String, Object> model = new HashMap<>();
153 ObjectMapper mapper = new ObjectMapper();
154 mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
155 PolicyController controller = getPolicyControllerInstance();
156 this.pdpConatiner = new PDPGroupContainer(controller.getPapEngine());
158 model.put("pdpTableDatas", mapper.writeValueAsString(pdpStatusData));
159 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
160 JSONObject j = new JSONObject(msg);
161 response.getWriter().write(j.toString());
164 policyLogger.error("Exception Occured"+e);
168 @RequestMapping(value={"/get_DashboardPolicyActivityData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
169 public void getPolicyActivityData(HttpServletRequest request, HttpServletResponse response){
171 Map<String, Object> model = new HashMap<>();
172 ObjectMapper mapper = new ObjectMapper();
173 mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
174 PolicyController controller = getPolicyControllerInstance();
175 this.pdpConatiner = new PDPGroupContainer(controller.getPapEngine());
177 model.put("policyActivityTableDatas", mapper.writeValueAsString(policyActivityData));
178 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
179 JSONObject j = new JSONObject(msg);
180 response.getWriter().write(j.toString());
183 policyLogger.error("Exception Occured"+e);
188 * Add the PAP information to the PAP Table
190 public void addPAPToTable(){
191 papStatusData = new ArrayList<>();
192 String papStatus = null;
194 PolicyController controller = getPolicyControllerInstance();
195 Set<OnapPDPGroup> groups = controller.getPapEngine().getOnapPDPGroups();
196 if (groups == null) {
197 papStatus = "UNKNOWN";
198 throw new PAPException("PAP not running");
202 } catch (PAPException | NullPointerException e1) {
203 papStatus = "CANNOT_CONNECT";
204 policyLogger.error("Error getting PAP status, PAP not responding to requests", e1);
206 String papURL = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_URL);
207 JSONObject object = new JSONObject();
208 object.put("system", papURL);
209 object.put("status", papStatus);
210 List<Object> data = commonClassDao.getDataByQuery("from PolicyEntity", new SimpleBindings());
211 object.put("noOfPolicy", data.size());
212 object.put("noOfConnectedTrap", pdpCount);
213 papStatusData.add(0, object);
217 * Add PDP Information to the PDP Table
220 public void addPDPToTable(){
222 pdpStatusData = new ArrayList<>();
225 long permitCount = 0;
226 for (PDPGroup group : this.pdpConatiner.getGroups()){
227 for (PDP pdp : group.getPdps()){
229 if ("UP_TO_DATE".equals(pdp.getStatus().getStatus().toString()) && ((OnapPDP) pdp).getJmxPort() != 0){
230 String pdpIpAddress = parseIPSystem(pdp.getId());
231 int port = ((OnapPDP) pdp).getJmxPort();
233 policyLogger.debug("Getting JMX Response Counts from " + pdpIpAddress + " at JMX port " + port);
234 naCount = getRequestCounts(pdpIpAddress, port, "pdpEvaluationNA");
235 permitCount = getRequestCounts(pdpIpAddress, port, "PdpEvaluationPermit");
236 denyCount = getRequestCounts(pdpIpAddress, port, "PdpEvaluationDeny");
240 JSONObject object = new JSONObject();
241 object.put("id", pdp.getId());
242 object.put("name", pdp.getName());
243 object.put("groupname", group.getName());
244 object.put("status", pdp.getStatus().getStatus().toString());
245 object.put("description", pdp.getDescription());
246 object.put("permitCount", "NA");
247 object.put("denyCount", "NA");
248 object.put("naCount", "NA");
249 pdpStatusData.add(object);
251 JSONObject object = new JSONObject();
252 object.put("id", pdp.getId());
253 object.put("name", pdp.getName());
254 object.put("groupname", group.getName());
255 object.put("status", pdp.getStatus().getStatus().toString());
256 object.put("description", pdp.getDescription());
257 object.put("permitCount", permitCount);
258 object.put("denyCount", denyCount);
259 object.put("naCount", naCount);
260 pdpStatusData.add(object);
267 private static String parseIPSystem(String line) {
268 Pattern pattern = Pattern.compile("://(.+?):");
269 Matcher ip = pattern.matcher(line);
278 * Contact JMX Connector Sever and return the value of the given jmxAttribute
280 @SuppressWarnings({ "rawtypes", "unchecked" })
281 private long getRequestCounts(String host, int port, String jmxAttribute) {
283 policyLogger.debug("Create an RMI connector client and connect it to the JMX connector server");
284 HashMap map = new HashMap();
286 JMXConnector jmxConnection;
288 jmxConnection = JMXConnectorFactory.newJMXConnector(createConnectionURL(host, port), map);
289 jmxConnection.connect();
290 Object o = jmxConnection.getMBeanServerConnection().getAttribute(new ObjectName("PdpRest:type=PdpRestMonitor"), jmxAttribute);
291 jmxConnection.close();
292 policyLogger.debug("pdpEvaluationNA value retreived: " + o);
294 } catch (MalformedURLException e) {
295 policyLogger.error("MalformedURLException for JMX connection" , e);
296 } catch (IOException e) {
297 policyLogger.error("Error in reteriving" + jmxAttribute + " from JMX connection", e);
298 } catch (AttributeNotFoundException e) {
299 policyLogger.error("AttributeNotFoundException " + jmxAttribute + " for JMX connection", e);
300 } catch (InstanceNotFoundException e) {
301 policyLogger.error("InstanceNotFoundException " + host + " for JMX connection", e);
302 } catch (MalformedObjectNameException e) {
303 policyLogger.error("MalformedObjectNameException for JMX connection", e);
304 } catch (MBeanException e) {
305 policyLogger.error("MBeanException for JMX connection");
306 policyLogger.error("Exception Occured"+e);
307 } catch (ReflectionException e) {
308 policyLogger.error("ReflectionException for JMX connection", e);
314 private static JMXServiceURL createConnectionURL(String host, int port) throws MalformedURLException{
315 return new JMXServiceURL("rmi", "", 0, "/jndi/rmi://" + host + ":" + port + "/jmxrmi");
320 * Add the information to the Policy Table
322 private void addPolicyToTable() {
323 policyActivityData = new ArrayList<>();
324 String policyID = null;
325 int policyFireCount = 0;
326 Map<String, String> policyMap = new HashMap<>();
327 Object policyList = null;
330 for (PDPGroup group : this.pdpConatiner.getGroups()){
331 for (PDPPolicy policy : group.getPolicies()){
333 policyMap.put(policy.getPolicyId().replace(" ", ""), policy.getId());
335 policyLogger.error(XACMLErrorConstants.ERROR_SCHEMA_INVALID+policy.getName() +e);
339 for (PDP pdp : group.getPdps()){
340 // Add rows to the Policy Table
342 if ("UP_TO_DATE".equals(pdp.getStatus().getStatus().toString()) && ((OnapPDP) pdp).getJmxPort() != 0){
343 String pdpIpAddress = parseIPSystem(pdp.getId());
344 policyList = getPolicy(pdpIpAddress, ((OnapPDP) pdp).getJmxPort(), "policyCount");
346 if (policyList != null && policyList.toString().length() > 3){
347 String[] splitPolicy = policyList.toString().split(",");
348 for (String policyKeyValue : splitPolicy){
349 policyID = urnPolicyID(policyKeyValue);
350 policyFireCount = countPolicyID(policyKeyValue);
351 if (policyID != null ){
352 if (policyMap.containsKey(policyID)){
353 JSONObject object = new JSONObject();
354 object.put("policyId", policyMap.get(policyID));
355 object.put("fireCount", policyFireCount);
356 object.put("system", pdp.getId());
357 policyActivityData.add(object);
362 if (policyList != null){
363 JSONObject object = new JSONObject();
364 object.put("policyId", "Unable to retrieve policy information");
365 object.put("fireCount", "NA");
366 object.put("system", pdp.getId());
367 policyActivityData.add(object);
369 JSONObject object = new JSONObject();
370 object.put("policyId", "Unable to access PDP JMX Server");
371 object.put("fireCount", "NA");
372 object.put("system", pdp.getId());
373 policyActivityData.add(object);
381 * Contact JMX Connector Sever and return the list of {policy id , count}
383 @SuppressWarnings({ "rawtypes", "unchecked" })
384 private Object getPolicy(String host, int port, String jmxAttribute){
385 policyLogger.debug("Create an RMI connector client and connect it to the JMX connector server for Policy: " + host);
386 HashMap map = new HashMap();
388 JMXConnector jmxConnection;
390 jmxConnection = JMXConnectorFactory.newJMXConnector(createConnectionURL(host, port), map);
391 jmxConnection.connect();
392 Object o = jmxConnection.getMBeanServerConnection().getAttribute(new ObjectName("PdpRest:type=PdpRestMonitor"), "policyMap");
393 jmxConnection.close();
394 policyLogger.debug("policyMap value retreived: " + o);
396 } catch (MalformedURLException e) {
397 policyLogger.error("MalformedURLException for JMX connection" , e);
398 } catch (IOException e) {
399 policyLogger.error("AttributeNotFoundException for policyMap" , e);
400 } catch (AttributeNotFoundException e) {
401 policyLogger.error("AttributeNotFoundException for JMX connection", e);
402 } catch (InstanceNotFoundException e) {
403 policyLogger.error("InstanceNotFoundException " + host + " for JMX connection", e);
404 } catch (MalformedObjectNameException e) {
405 policyLogger.error("MalformedObjectNameException for JMX connection", e);
406 } catch (MBeanException e) {
407 policyLogger.error("MBeanException for JMX connection", e);
408 policyLogger.error("Exception Occured"+e);
409 } catch (ReflectionException e) {
410 policyLogger.error("ReflectionException for JMX connection", e);
417 private static String urnPolicyID(String line){
418 String[] splitLine = line.toString().split("=");
419 String removeSpaces = splitLine[0].replaceAll("\\s+", "");
420 return removeSpaces.replace("{", "");
423 private static Integer countPolicyID(String line){
424 String[] splitLine = line.toString().split("=");
425 String sCount = splitLine[1].replace("}", "");
426 int intCount = Integer.parseInt(sCount);