X-Git-Url: https://gerrit.onap.org/r/gitweb?p=policy%2Fengine.git;a=blobdiff_plain;f=POLICY-SDK-APP%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fcontroller%2FDashboardController.java;h=0ca315bf1ab1c0b55b123cc7327466d41a4de3fd;hp=d3af75dfd654a97641b4e75b630252a95afee290;hb=c2ca6ea5cb44103903e1409e8dd6db80167e61e8;hpb=44bae6729336dfc47f1aaaa2c4e88f4e32e207ce diff --git a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/DashboardController.java b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/DashboardController.java index d3af75dfd..0ca315bf1 100644 --- a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/DashboardController.java +++ b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/DashboardController.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy Engine * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,6 +29,7 @@ import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; + import java.io.IOException; import java.io.PrintWriter; import java.net.MalformedURLException; @@ -41,6 +42,7 @@ import java.util.Map; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; + import javax.management.AttributeNotFoundException; import javax.management.InstanceNotFoundException; import javax.management.MBeanException; @@ -53,12 +55,13 @@ import javax.management.remote.JMXServiceURL; import javax.script.SimpleBindings; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; + import org.json.JSONObject; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.dao.SystemLogDbDao; import org.onap.policy.model.PDPGroupContainer; -import org.onap.policy.rest.XACMLRestProperties; +import org.onap.policy.rest.XacmlRestProperties; import org.onap.policy.rest.dao.CommonClassDao; import org.onap.policy.rest.jpa.PolicyEntity; import org.onap.policy.utils.PolicyUtils; @@ -117,15 +120,14 @@ public class DashboardController extends RestrictedBaseController { return policyController != null ? getPolicyController() : new PolicyController(); } - - /** * This method is to retrieve all the data of last 30 days from PolicyEntity table as default. - * + * * @param request object * @param response object contains retrieved data */ - @RequestMapping(value = {"/get_DashboardPolicyCRUDData"}, + @RequestMapping( + value = {"/get_DashboardPolicyCRUDData"}, method = {org.springframework.web.bind.annotation.RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) public void get_DashboardPolicyCrudData(HttpServletRequest request, HttpServletResponse response) { @@ -135,9 +137,7 @@ public class DashboardController extends RestrictedBaseController { mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); addPolicyCrudInfoToTable(); model.put("papStatusCRUDData", mapper.writeValueAsString(policyStatusCrudData)); - JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); - JSONObject j = new JSONObject(msg); - response.getWriter().write(j.toString()); + response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(model))).toString()); } catch (Exception e) { policyLogger.error(exceptionOccured, e); } @@ -145,11 +145,12 @@ public class DashboardController extends RestrictedBaseController { /** * This method retrieves data based on input criteria. - * + * * @param request object * @param response object contains retrieved data */ - @RequestMapping(value = {"/dashboardController/dashboardAdvancedSearch.htm"}, + @RequestMapping( + value = {"/dashboardController/dashboardAdvancedSearch.htm"}, method = {org.springframework.web.bind.annotation.RequestMethod.POST}) public void dashboardAdvancedSearch(HttpServletRequest request, HttpServletResponse response) throws IOException { String lscope = null; @@ -185,11 +186,9 @@ public class DashboardController extends RestrictedBaseController { getPolicyData(lscope, lstage, isDelected, ttlDateAfter, ttlDateBefore); Map model = new HashMap<>(); model.put("policyStatusCRUDData", mapper.writeValueAsString(policyStatusCrudData)); - JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); - JSONObject j = new JSONObject(msg); - response.getWriter().write(j.toString()); + response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(model))).toString()); } catch (Exception e) { - response.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); PrintWriter out = response.getWriter(); out.write(PolicyUtils.CATCH_EXCEPTION); } @@ -198,11 +197,12 @@ public class DashboardController extends RestrictedBaseController { /** * This method is to retrieve data from PolicyEntity table. - * + * * @param request object * @param response object contains retrieved data */ - @RequestMapping(value = {"/get_DashboardPdpPolicyCRUDData"}, + @RequestMapping( + value = {"/get_DashboardPdpPolicyCRUDData"}, method = {org.springframework.web.bind.annotation.RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) public void get_DashboardPdpPolicyCrudData(HttpServletRequest request, HttpServletResponse response) { @@ -211,15 +211,20 @@ public class DashboardController extends RestrictedBaseController { ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); model.put("pdpStatusCRUDData", mapper.writeValueAsString(pdpStatusCrudData)); - JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); - JSONObject j = new JSONObject(msg); - response.getWriter().write(j.toString()); + response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(model))).toString()); } catch (Exception e) { policyLogger.error(exceptionOccured, e); } } - @RequestMapping(value = {"/get_DashboardLoggingData"}, + /** + * getData. + * + * @param request HttpServletRequest + * @param response HttpServletResponse + */ + @RequestMapping( + value = {"/get_DashboardLoggingData"}, method = {org.springframework.web.bind.annotation.RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) public void getData(HttpServletRequest request, HttpServletResponse response) { @@ -227,15 +232,20 @@ public class DashboardController extends RestrictedBaseController { Map model = new HashMap<>(); ObjectMapper mapper = new ObjectMapper(); model.put("availableLoggingDatas", mapper.writeValueAsString(systemDAO.getLoggingData())); - JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); - JSONObject j = new JSONObject(msg); - response.getWriter().write(j.toString()); + response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(model))).toString()); } catch (Exception e) { policyLogger.error(exceptionOccured + e); } } - @RequestMapping(value = {"/get_DashboardSystemAlertData"}, + /** + * getSystemAlertData. + * + * @param request HttpServletRequest + * @param response HttpServletResponse + */ + @RequestMapping( + value = {"/get_DashboardSystemAlertData"}, method = {org.springframework.web.bind.annotation.RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) public void getSystemAlertData(HttpServletRequest request, HttpServletResponse response) { @@ -243,15 +253,20 @@ public class DashboardController extends RestrictedBaseController { Map model = new HashMap<>(); ObjectMapper mapper = new ObjectMapper(); model.put("systemAlertsTableDatas", mapper.writeValueAsString(systemDAO.getSystemAlertData())); - JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); - JSONObject j = new JSONObject(msg); - response.getWriter().write(j.toString()); + response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(model))).toString()); } catch (Exception e) { policyLogger.error(exceptionOccured + e); } } - @RequestMapping(value = {"/get_DashboardPAPStatusData"}, + /** + * getPAPStatusData. + * + * @param request HttpServletRequest + * @param response HttpServletResponse + */ + @RequestMapping( + value = {"/get_DashboardPAPStatusData"}, method = {org.springframework.web.bind.annotation.RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) public void getPAPStatusData(HttpServletRequest request, HttpServletResponse response) { @@ -261,20 +276,25 @@ public class DashboardController extends RestrictedBaseController { mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); addPAPToTable(); model.put("papTableDatas", mapper.writeValueAsString(papStatusData)); - JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); - JSONObject j = new JSONObject(msg); - response.getWriter().write(j.toString()); + response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(model))).toString()); } catch (Exception e) { policyLogger.error(exceptionOccured + e); } } - @RequestMapping(value = {"/get_DashboardPDPStatusData"}, + /** + * getPDPStatusData. + * + * @param request HttpServletRequest + * @param response HttpServletResponse + */ + @RequestMapping( + value = {"/get_DashboardPDPStatusData"}, method = {org.springframework.web.bind.annotation.RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) public void getPDPStatusData(HttpServletRequest request, HttpServletResponse response) { try { - Map model = new HashMap<>(); + final Map model = new HashMap<>(); ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); PolicyController controller = getPolicyControllerInstance(); @@ -282,28 +302,32 @@ public class DashboardController extends RestrictedBaseController { addPDPToTable(); model.put("pdpTableDatas", mapper.writeValueAsString(pdpStatusData)); JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); - JSONObject j = new JSONObject(msg); - response.getWriter().write(j.toString()); + response.getWriter().write(new JSONObject(msg).toString()); } catch (Exception e) { policyLogger.error(exceptionOccured + e); } } - @RequestMapping(value = {"/get_DashboardPolicyActivityData"}, + /** + * getPolicyActivityData. + * + * @param request HttpServletRequest + * @param response HttpServletResponse + */ + @RequestMapping( + value = {"/get_DashboardPolicyActivityData"}, method = {org.springframework.web.bind.annotation.RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) public void getPolicyActivityData(HttpServletRequest request, HttpServletResponse response) { try { - Map model = new HashMap<>(); + final Map model = new HashMap<>(); ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); PolicyController controller = getPolicyControllerInstance(); this.pdpConatiner = new PDPGroupContainer(controller.getPapEngine()); addPolicyToTable(); model.put("policyActivityTableDatas", mapper.writeValueAsString(policyActivityData)); - JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); - JSONObject j = new JSONObject(msg); - response.getWriter().write(j.toString()); + response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(model))).toString()); } catch (Exception e) { policyLogger.error(exceptionOccured + e); } @@ -327,9 +351,9 @@ public class DashboardController extends RestrictedBaseController { papStatus = "CANNOT_CONNECT"; policyLogger.error("Error getting PAP status, PAP not responding to requests", e1); } - String papURL = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_URL); + String papUrl = XACMLProperties.getProperty(XacmlRestProperties.PROP_PAP_URL); JSONObject object = new JSONObject(); - object.put("system", papURL); + object.put("system", papUrl); object.put("status", papStatus); List data = commonClassDao.getDataByQuery("from PolicyEntity", new SimpleBindings()); object.put("noOfPolicy", data.size()); @@ -337,9 +361,8 @@ public class DashboardController extends RestrictedBaseController { papStatusData.add(0, object); } - - /* - * Add the PAP Policy information to the PAP Table + /** + * Add the PAP Policy information to the PAP Table. */ public void addPolicyCrudInfoToTable() { policyStatusCrudData = new ArrayList<>(); @@ -362,7 +385,7 @@ public class DashboardController extends RestrictedBaseController { object.put(scope, ((PolicyEntity) data).getScope()); object.put("policyName", ((PolicyEntity) data).getPolicyName()); object.put("version", ((PolicyEntity) data).getVersion()); - if (isPushedToPDP(((PolicyEntity) data).getPolicyId())) { + if (isPushedToPdp(((PolicyEntity) data).getPolicyId())) { object.put(stage, "PDP"); } else { object.put(stage, "PAP"); @@ -388,13 +411,13 @@ public class DashboardController extends RestrictedBaseController { /* * Add the PDP Policy information to the PDP Table */ - private boolean isPushedToPDP(long policyId) { + private boolean isPushedToPdp(long policyId) { try { String groupEntityquery = "from PolicyGroupEntity where policyid = :policyEntityId"; SimpleBindings geParams = new SimpleBindings(); geParams.put("policyEntityId", policyId); List groupobject = commonClassDao.getDataByQuery(groupEntityquery, geParams); - if (groupobject != null && groupobject.size() > 0) { + if (groupobject != null && ! groupobject.isEmpty()) { return true; } } catch (Exception e) { @@ -407,7 +430,8 @@ public class DashboardController extends RestrictedBaseController { /* * Add the Policy information to the policyStatusCRUDDataTable */ - private void getPolicyData(String inputScope, String inputStage, String isDeleted, String createdAfter, String createdBefore) { + private void getPolicyData(String inputScope, String inputStage, String isDeleted, String createdAfter, + String createdBefore) { policyStatusCrudData = new ArrayList<>(); List policyData = null; SimpleBindings geParams = new SimpleBindings(); @@ -479,7 +503,7 @@ public class DashboardController extends RestrictedBaseController { object.put(scope, data.getScope()); object.put("policyName", data.getPolicyName()); object.put("version", data.getVersion()); - if (isPushedToPDP(data.getPolicyId())) { + if (isPushedToPdp(data.getPolicyId())) { object.put(stage, "PDP"); } else { object.put(stage, "PAP"); @@ -503,7 +527,7 @@ public class DashboardController extends RestrictedBaseController { } /** - * Add PDP Information to the PDP Table + * Add PDP Information to the PDP Table. * */ public void addPDPToTable() { @@ -516,7 +540,7 @@ public class DashboardController extends RestrictedBaseController { for (PDP pdp : group.getPdps()) { naCount = -1; if ("UP_TO_DATE".equals(pdp.getStatus().getStatus().toString()) && ((OnapPDP) pdp).getJmxPort() != 0) { - String pdpIpAddress = parseIPSystem(pdp.getId()); + String pdpIpAddress = parseIpSystem(pdp.getId()); int port = ((OnapPDP) pdp).getJmxPort(); if (port != 0) { policyLogger.debug("Getting JMX Response Counts from " + pdpIpAddress + " at JMX port " + port); @@ -553,7 +577,7 @@ public class DashboardController extends RestrictedBaseController { } } - private static String parseIPSystem(String line) { + private static String parseIpSystem(String line) { Pattern pattern = Pattern.compile("://(.+?):"); Matcher ip = pattern.matcher(line); if (ip.find()) { @@ -570,13 +594,12 @@ public class DashboardController extends RestrictedBaseController { policyLogger.debug("Create an RMI connector client and connect it to the JMX connector server"); HashMap map = null; - try (JMXConnector jmxConnection = JMXConnectorFactory.newJMXConnector(createConnectionURL(host, port), map)) { + try (JMXConnector jmxConnection = JMXConnectorFactory.newJMXConnector(createConnectionUrl(host, port), map)) { jmxConnection.connect(); - Object o = - jmxConnection.getMBeanServerConnection().getAttribute( - new ObjectName("PdpRest:type=PdpRestMonitor"), jmxAttribute); - policyLogger.debug("pdpEvaluationNA value retreived: " + o); - return (long) o; + Object obj = jmxConnection.getMBeanServerConnection() + .getAttribute(new ObjectName("PdpRest:type=PdpRestMonitor"), jmxAttribute); + policyLogger.debug("pdpEvaluationNA value retreived: " + obj); + return (long) obj; } catch (MalformedURLException e) { policyLogger.error("MalformedURLException for JMX connection", e); } catch (IOException e) { @@ -597,11 +620,10 @@ public class DashboardController extends RestrictedBaseController { return -1; } - private static JMXServiceURL createConnectionURL(String host, int port) throws MalformedURLException { + private static JMXServiceURL createConnectionUrl(String host, int port) throws MalformedURLException { return new JMXServiceURL("rmi", "", 0, "/jndi/rmi://" + host + ":" + port + "/jmxrmi"); } - /* * Add the information to the Policy Table */ @@ -626,8 +648,8 @@ public class DashboardController extends RestrictedBaseController { // Add rows to the Policy Table policyList = null; if ("UP_TO_DATE".equals(pdp.getStatus().getStatus().toString()) && ((OnapPDP) pdp).getJmxPort() != 0) { - String pdpIpAddress = parseIPSystem(pdp.getId()); - policyList = getPolicy(pdpIpAddress, ((OnapPDP) pdp).getJmxPort(), "policyCount"); + String pdpIpAddress = parseIpSystem(pdp.getId()); + policyList = getPolicy(pdpIpAddress, ((OnapPDP) pdp).getJmxPort()); } if (policyList != null && policyList.toString().length() > 3) { String[] splitPolicy = policyList.toString().split(","); @@ -643,19 +665,15 @@ public class DashboardController extends RestrictedBaseController { } } } else { + JSONObject object = new JSONObject(); if (policyList != null) { - JSONObject object = new JSONObject(); object.put("policyId", "Unable to retrieve policy information"); - object.put("fireCount", "NA"); - object.put("system", pdp.getId()); - policyActivityData.add(object); } else { - JSONObject object = new JSONObject(); object.put("policyId", "Unable to access PDP JMX Server"); - object.put("fireCount", "NA"); - object.put("system", pdp.getId()); - policyActivityData.add(object); } + object.put("fireCount", "NA"); + object.put("system", pdp.getId()); + policyActivityData.add(object); } } } @@ -665,17 +683,16 @@ public class DashboardController extends RestrictedBaseController { * Contact JMX Connector Sever and return the list of {policy id , count} */ @SuppressWarnings({"rawtypes", "unchecked"}) - private Object getPolicy(String host, int port, String jmxAttribute) { - policyLogger.debug("Create an RMI connector client and connect it to the JMX connector server for Policy: " - + host); + private Object getPolicy(String host, int port) { + policyLogger + .debug("Create an RMI connector client and connect it to the JMX connector server for Policy: " + host); HashMap map = null; - try (JMXConnector jmxConnection = JMXConnectorFactory.newJMXConnector(createConnectionURL(host, port), map)) { + try (JMXConnector jmxConnection = JMXConnectorFactory.newJMXConnector(createConnectionUrl(host, port), map)) { jmxConnection.connect(); - Object o = - jmxConnection.getMBeanServerConnection().getAttribute( - new ObjectName("PdpRest:type=PdpRestMonitor"), "policyMap"); - policyLogger.debug("policyMap value retreived: " + o); - return o; + Object obj = jmxConnection.getMBeanServerConnection() + .getAttribute(new ObjectName("PdpRest:type=PdpRestMonitor"), "policyMap"); + policyLogger.debug("policyMap value retreived: " + obj); + return obj; } catch (MalformedURLException e) { policyLogger.error("MalformedURLException for JMX connection", e); } catch (IOException e) { @@ -705,8 +722,7 @@ public class DashboardController extends RestrictedBaseController { private static Integer countPolicyID(String line) { String[] splitLine = line.split("="); - String sCount = splitLine[1].replace("}", ""); - return Integer.parseInt(sCount); + return Integer.parseInt(splitLine[1].replace("}", "")); } }