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.dao.SystemLogDbDao;
49 import org.onap.policy.model.PDPGroupContainer;
50 import org.onap.policy.rest.XACMLRestProperties;
51 import org.onap.policy.rest.dao.CommonClassDao;
52 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
53 import org.openecomp.portalsdk.core.web.support.JsonMessage;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.http.MediaType;
56 import org.springframework.stereotype.Controller;
57 import org.springframework.web.bind.annotation.RequestMapping;
59 import org.onap.policy.common.logging.flexlogger.FlexLogger;
60 import org.onap.policy.common.logging.flexlogger.Logger;
61 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
62 import org.onap.policy.xacml.api.XACMLErrorConstants;
63 import org.onap.policy.xacml.api.pap.OnapPDP;
64 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
66 import com.att.research.xacml.api.pap.PAPException;
67 import com.att.research.xacml.api.pap.PDP;
68 import com.att.research.xacml.api.pap.PDPGroup;
69 import com.att.research.xacml.api.pap.PDPPolicy;
70 import com.att.research.xacml.util.XACMLProperties;
71 import com.fasterxml.jackson.annotation.PropertyAccessor;
72 import com.fasterxml.jackson.databind.ObjectMapper;
75 @RequestMapping({"/"})
76 public class DashboardController extends RestrictedBaseController{
77 private static final Logger policyLogger = FlexLogger.getLogger(DashboardController.class);
79 SystemLogDbDao systemDAO;
82 CommonClassDao commonClassDao;
85 private PDPGroupContainer pdpConatiner;
86 private ArrayList<Object> pdpStatusData;
87 private ArrayList<Object> papStatusData;
88 private ArrayList<Object> policyActivityData;
90 private PolicyController policyController;
91 public PolicyController getPolicyController() {
92 return policyController;
95 public void setPolicyController(PolicyController policyController) {
96 this.policyController = policyController;
99 private PolicyController getPolicyControllerInstance(){
100 return policyController != null ? getPolicyController() : new PolicyController();
103 @RequestMapping(value={"/get_DashboardLoggingData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
104 public void getData(HttpServletRequest request, HttpServletResponse response){
106 Map<String, Object> model = new HashMap<>();
107 ObjectMapper mapper = new ObjectMapper();
108 model.put("availableLoggingDatas", mapper.writeValueAsString(systemDAO.getLoggingData()));
109 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
110 JSONObject j = new JSONObject(msg);
111 response.getWriter().write(j.toString());
114 policyLogger.error("Exception Occured"+e);
118 @RequestMapping(value={"/get_DashboardSystemAlertData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
119 public void getSystemAlertData(HttpServletRequest request, HttpServletResponse response){
121 Map<String, Object> model = new HashMap<>();
122 ObjectMapper mapper = new ObjectMapper();
123 model.put("systemAlertsTableDatas", mapper.writeValueAsString(systemDAO.getSystemAlertData()));
124 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
125 JSONObject j = new JSONObject(msg);
126 response.getWriter().write(j.toString());
129 policyLogger.error("Exception Occured"+e);
133 @RequestMapping(value={"/get_DashboardPAPStatusData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
134 public void getPAPStatusData(HttpServletRequest request, HttpServletResponse response){
136 Map<String, Object> model = new HashMap<>();
137 ObjectMapper mapper = new ObjectMapper();
138 mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
140 model.put("papTableDatas", mapper.writeValueAsString(papStatusData));
141 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
142 JSONObject j = new JSONObject(msg);
143 response.getWriter().write(j.toString());
146 policyLogger.error("Exception Occured"+e);
150 @RequestMapping(value={"/get_DashboardPDPStatusData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
151 public void getPDPStatusData(HttpServletRequest request, HttpServletResponse response){
153 Map<String, Object> model = new HashMap<>();
154 ObjectMapper mapper = new ObjectMapper();
155 mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
156 PolicyController controller = getPolicyControllerInstance();
157 this.pdpConatiner = new PDPGroupContainer(controller.getPapEngine());
159 model.put("pdpTableDatas", mapper.writeValueAsString(pdpStatusData));
160 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
161 JSONObject j = new JSONObject(msg);
162 response.getWriter().write(j.toString());
165 policyLogger.error("Exception Occured"+e);
169 @RequestMapping(value={"/get_DashboardPolicyActivityData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
170 public void getPolicyActivityData(HttpServletRequest request, HttpServletResponse response){
172 Map<String, Object> model = new HashMap<>();
173 ObjectMapper mapper = new ObjectMapper();
174 mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
175 PolicyController controller = getPolicyControllerInstance();
176 this.pdpConatiner = new PDPGroupContainer(controller.getPapEngine());
178 model.put("policyActivityTableDatas", mapper.writeValueAsString(policyActivityData));
179 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
180 JSONObject j = new JSONObject(msg);
181 response.getWriter().write(j.toString());
184 policyLogger.error("Exception Occured"+e);
189 * Add the PAP information to the PAP Table
191 public void addPAPToTable(){
192 papStatusData = new ArrayList<>();
193 String papStatus = null;
195 PolicyController controller = getPolicyControllerInstance();
196 Set<OnapPDPGroup> groups = controller.getPapEngine().getOnapPDPGroups();
197 if (groups == null) {
198 papStatus = "UNKNOWN";
199 throw new PAPException("PAP not running");
203 } catch (PAPException | NullPointerException e1) {
204 papStatus = "CANNOT_CONNECT";
205 policyLogger.error("Error getting PAP status, PAP not responding to requests", e1);
207 String papURL = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_URL);
208 JSONObject object = new JSONObject();
209 object.put("system", papURL);
210 object.put("status", papStatus);
211 List<Object> data = commonClassDao.getDataByQuery("from PolicyEntity", new SimpleBindings());
212 object.put("noOfPolicy", data.size());
213 object.put("noOfConnectedTrap", pdpCount);
214 papStatusData.add(0, object);
218 * Add PDP Information to the PDP Table
221 public void addPDPToTable(){
223 pdpStatusData = new ArrayList<>();
226 long permitCount = 0;
227 for (PDPGroup group : this.pdpConatiner.getGroups()){
228 for (PDP pdp : group.getPdps()){
230 if ("UP_TO_DATE".equals(pdp.getStatus().getStatus().toString()) && ((OnapPDP) pdp).getJmxPort() != 0){
231 String pdpIpAddress = parseIPSystem(pdp.getId());
232 int port = ((OnapPDP) pdp).getJmxPort();
234 policyLogger.debug("Getting JMX Response Counts from " + pdpIpAddress + " at JMX port " + port);
235 naCount = getRequestCounts(pdpIpAddress, port, "pdpEvaluationNA");
236 permitCount = getRequestCounts(pdpIpAddress, port, "PdpEvaluationPermit");
237 denyCount = getRequestCounts(pdpIpAddress, port, "PdpEvaluationDeny");
241 JSONObject object = new JSONObject();
242 object.put("id", pdp.getId());
243 object.put("name", pdp.getName());
244 object.put("groupname", group.getName());
245 object.put("status", pdp.getStatus().getStatus().toString());
246 object.put("description", pdp.getDescription());
247 object.put("permitCount", "NA");
248 object.put("denyCount", "NA");
249 object.put("naCount", "NA");
250 pdpStatusData.add(object);
252 JSONObject object = new JSONObject();
253 object.put("id", pdp.getId());
254 object.put("name", pdp.getName());
255 object.put("groupname", group.getName());
256 object.put("status", pdp.getStatus().getStatus().toString());
257 object.put("description", pdp.getDescription());
258 object.put("permitCount", permitCount);
259 object.put("denyCount", denyCount);
260 object.put("naCount", naCount);
261 pdpStatusData.add(object);
268 private static String parseIPSystem(String line) {
269 Pattern pattern = Pattern.compile("://(.+?):");
270 Matcher ip = pattern.matcher(line);
279 * Contact JMX Connector Sever and return the value of the given jmxAttribute
281 @SuppressWarnings({ "rawtypes", "unchecked" })
282 private long getRequestCounts(String host, int port, String jmxAttribute) {
284 policyLogger.debug("Create an RMI connector client and connect it to the JMX connector server");
285 HashMap map = new HashMap();
287 JMXConnector jmxConnection;
289 jmxConnection = JMXConnectorFactory.newJMXConnector(createConnectionURL(host, port), map);
290 jmxConnection.connect();
291 Object o = jmxConnection.getMBeanServerConnection().getAttribute(new ObjectName("PdpRest:type=PdpRestMonitor"), jmxAttribute);
292 jmxConnection.close();
293 policyLogger.debug("pdpEvaluationNA value retreived: " + o);
295 } catch (MalformedURLException e) {
296 policyLogger.error("MalformedURLException for JMX connection" , e);
297 } catch (IOException e) {
298 policyLogger.error("Error in reteriving" + jmxAttribute + " from JMX connection", e);
299 } catch (AttributeNotFoundException e) {
300 policyLogger.error("AttributeNotFoundException " + jmxAttribute + " for JMX connection", e);
301 } catch (InstanceNotFoundException e) {
302 policyLogger.error("InstanceNotFoundException " + host + " for JMX connection", e);
303 } catch (MalformedObjectNameException e) {
304 policyLogger.error("MalformedObjectNameException for JMX connection", e);
305 } catch (MBeanException e) {
306 policyLogger.error("MBeanException for JMX connection");
307 policyLogger.error("Exception Occured"+e);
308 } catch (ReflectionException e) {
309 policyLogger.error("ReflectionException for JMX connection", e);
315 private static JMXServiceURL createConnectionURL(String host, int port) throws MalformedURLException{
316 return new JMXServiceURL("rmi", "", 0, "/jndi/rmi://" + host + ":" + port + "/jmxrmi");
321 * Add the information to the Policy Table
323 private void addPolicyToTable() {
324 policyActivityData = new ArrayList<>();
325 String policyID = null;
326 int policyFireCount = 0;
327 Map<String, String> policyMap = new HashMap<>();
328 Object policyList = null;
331 for (PDPGroup group : this.pdpConatiner.getGroups()){
332 for (PDPPolicy policy : group.getPolicies()){
334 policyMap.put(policy.getPolicyId().replace(" ", ""), policy.getId());
336 policyLogger.error(XACMLErrorConstants.ERROR_SCHEMA_INVALID+policy.getName() +e);
340 for (PDP pdp : group.getPdps()){
341 // Add rows to the Policy Table
343 if ("UP_TO_DATE".equals(pdp.getStatus().getStatus().toString()) && ((OnapPDP) pdp).getJmxPort() != 0){
344 String pdpIpAddress = parseIPSystem(pdp.getId());
345 policyList = getPolicy(pdpIpAddress, ((OnapPDP) pdp).getJmxPort(), "policyCount");
347 if (policyList != null && policyList.toString().length() > 3){
348 String[] splitPolicy = policyList.toString().split(",");
349 for (String policyKeyValue : splitPolicy){
350 policyID = urnPolicyID(policyKeyValue);
351 policyFireCount = countPolicyID(policyKeyValue);
352 if (policyID != null ){
353 if (policyMap.containsKey(policyID)){
354 JSONObject object = new JSONObject();
355 object.put("policyId", policyMap.get(policyID));
356 object.put("fireCount", policyFireCount);
357 object.put("system", pdp.getId());
358 policyActivityData.add(object);
363 if (policyList != null){
364 JSONObject object = new JSONObject();
365 object.put("policyId", "Unable to retrieve policy information");
366 object.put("fireCount", "NA");
367 object.put("system", pdp.getId());
368 policyActivityData.add(object);
370 JSONObject object = new JSONObject();
371 object.put("policyId", "Unable to access PDP JMX Server");
372 object.put("fireCount", "NA");
373 object.put("system", pdp.getId());
374 policyActivityData.add(object);
382 * Contact JMX Connector Sever and return the list of {policy id , count}
384 @SuppressWarnings({ "rawtypes", "unchecked" })
385 private Object getPolicy(String host, int port, String jmxAttribute){
386 policyLogger.debug("Create an RMI connector client and connect it to the JMX connector server for Policy: " + host);
387 HashMap map = new HashMap();
389 JMXConnector jmxConnection;
391 jmxConnection = JMXConnectorFactory.newJMXConnector(createConnectionURL(host, port), map);
392 jmxConnection.connect();
393 Object o = jmxConnection.getMBeanServerConnection().getAttribute(new ObjectName("PdpRest:type=PdpRestMonitor"), "policyMap");
394 jmxConnection.close();
395 policyLogger.debug("policyMap value retreived: " + o);
397 } catch (MalformedURLException e) {
398 policyLogger.error("MalformedURLException for JMX connection" , e);
399 } catch (IOException e) {
400 policyLogger.error("AttributeNotFoundException for policyMap" , e);
401 } catch (AttributeNotFoundException e) {
402 policyLogger.error("AttributeNotFoundException for JMX connection", e);
403 } catch (InstanceNotFoundException e) {
404 policyLogger.error("InstanceNotFoundException " + host + " for JMX connection", e);
405 } catch (MalformedObjectNameException e) {
406 policyLogger.error("MalformedObjectNameException for JMX connection", e);
407 } catch (MBeanException e) {
408 policyLogger.error("MBeanException for JMX connection", e);
409 policyLogger.error("Exception Occured"+e);
410 } catch (ReflectionException e) {
411 policyLogger.error("ReflectionException for JMX connection", e);
418 private static String urnPolicyID(String line){
419 String[] splitLine = line.toString().split("=");
420 String removeSpaces = splitLine[0].replaceAll("\\s+", "");
421 return removeSpaces.replace("{", "");
424 private static Integer countPolicyID(String line){
425 String[] splitLine = line.toString().split("=");
426 String sCount = splitLine[1].replace("}", "");
427 int intCount = Integer.parseInt(sCount);