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.openecomp.policy.controller;
25 import java.io.IOException;
26 import java.net.MalformedURLException;
27 import java.util.ArrayList;
28 import java.util.HashMap;
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.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
46 //import org.apache.commons.logging.Log;
47 //import org.apache.commons.logging.LogFactory;
48 import org.json.JSONObject;
49 import org.openecomp.policy.dao.SystemLogDbDao;
50 import org.openecomp.policy.model.PDPGroupContainer;
51 import org.openecomp.policy.rest.XACMLRestProperties;
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.openecomp.policy.common.logging.flexlogger.FlexLogger;
60 import org.openecomp.policy.common.logging.flexlogger.Logger;
61 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
62 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
63 import org.openecomp.policy.xacml.api.pap.EcompPDP;
64 import org.openecomp.policy.xacml.api.pap.EcompPDPGroup;
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 logger = FlexLogger.getLogger(DashboardController.class);
79 SystemLogDbDao systemDAO;
82 private PDPGroupContainer pdpConatiner;
83 private ArrayList<Object> pdpStatusData;
84 private ArrayList<Object> papStatusData;
85 private ArrayList<Object> policyActivityData;
89 @RequestMapping(value={"/get_DashboardLoggingData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
90 public void getData(HttpServletRequest request, HttpServletResponse response){
92 Map<String, Object> model = new HashMap<String, Object>();
93 ObjectMapper mapper = new ObjectMapper();
94 model.put("availableLoggingDatas", mapper.writeValueAsString(systemDAO.getLoggingData()));
95 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
96 JSONObject j = new JSONObject(msg);
97 response.getWriter().write(j.toString());
104 @RequestMapping(value={"/get_DashboardSystemAlertData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
105 public void getSystemAlertData(HttpServletRequest request, HttpServletResponse response){
107 Map<String, Object> model = new HashMap<String, Object>();
108 ObjectMapper mapper = new ObjectMapper();
109 model.put("systemAlertsTableDatas", mapper.writeValueAsString(systemDAO.getSystemAlertData()));
110 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
111 JSONObject j = new JSONObject(msg);
112 response.getWriter().write(j.toString());
119 @RequestMapping(value={"/get_DashboardPAPStatusData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
120 public void getPAPStatusData(HttpServletRequest request, HttpServletResponse response){
122 Map<String, Object> model = new HashMap<String, Object>();
123 ObjectMapper mapper = new ObjectMapper();
124 mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
126 model.put("papTableDatas", mapper.writeValueAsString(papStatusData));
127 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
128 JSONObject j = new JSONObject(msg);
129 response.getWriter().write(j.toString());
136 @RequestMapping(value={"/get_DashboardPDPStatusData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
137 public void getPDPStatusData(HttpServletRequest request, HttpServletResponse response){
139 Map<String, Object> model = new HashMap<String, Object>();
140 ObjectMapper mapper = new ObjectMapper();
141 mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
142 this.pdpConatiner = new PDPGroupContainer(PolicyController.getPapEngine());
144 model.put("pdpTableDatas", mapper.writeValueAsString(pdpStatusData));
145 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
146 JSONObject j = new JSONObject(msg);
147 response.getWriter().write(j.toString());
154 @RequestMapping(value={"/get_DashboardPolicyActivityData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
155 public void getPolicyActivityData(HttpServletRequest request, HttpServletResponse response){
157 Map<String, Object> model = new HashMap<String, Object>();
158 ObjectMapper mapper = new ObjectMapper();
159 mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
160 this.pdpConatiner = new PDPGroupContainer(PolicyController.getPapEngine());
162 model.put("policyActivityTableDatas", mapper.writeValueAsString(policyActivityData));
163 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
164 JSONObject j = new JSONObject(msg);
165 response.getWriter().write(j.toString());
173 * Add the PAP information to the PAP Table
175 public void addPAPToTable(){
176 papStatusData = new ArrayList<Object>();
177 File folder = new File(PolicyController.getGitPath().toAbsolutePath().toString());
178 String papStatus = null;
180 Set<EcompPDPGroup> groups = PolicyController.getPapEngine().getEcompPDPGroups();
181 if (groups == null) {
182 papStatus = "UNKNOWN";
183 throw new PAPException("PAP not running");
187 } catch (PAPException | NullPointerException e1) {
188 papStatus = "CANNOT_CONNECT";
189 logger.error("Error getting PAP status, PAP not responding to requests");
191 String papURL = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_URL);
192 JSONObject object = new JSONObject();
193 object.put("system", papURL);
194 object.put("status", papStatus);
195 object.put("noOfPolicy", countFilesInDirectory(folder));
196 object.put("noOfConnectedTrap", pdpCount);
197 papStatusData.add(0, object);
200 private int countFilesInDirectory(File directory) {
202 for (File file : directory.listFiles()) {
203 if (file.isFile() && file.getName().endsWith(".xml")) {
206 if (file.isDirectory()) {
207 count += countFilesInDirectory(file);
213 * Add PDP Information to the PDP Table
216 public void addPDPToTable(){
218 pdpStatusData = new ArrayList<Object>();
221 long permitCount = 0;
222 for (PDPGroup group : this.pdpConatiner.getGroups()){
223 for (PDP pdp : group.getPdps()){
225 if (pdp.getStatus().getStatus().toString() == "UP_TO_DATE" && ((EcompPDP) pdp).getJmxPort() != 0){
226 String pdpIpAddress = parseIPSystem(pdp.getId());
227 int port = ((EcompPDP) pdp).getJmxPort();
229 logger.debug("Getting JMX Response Counts from " + pdpIpAddress + " at JMX port " + port);
230 naCount = getRequestCounts(pdpIpAddress, port, "pdpEvaluationNA");
231 permitCount = getRequestCounts(pdpIpAddress, port, "PdpEvaluationPermit");
232 denyCount = getRequestCounts(pdpIpAddress, port, "PdpEvaluationDeny");
235 JSONObject object = new JSONObject();
236 object.put("id", pdp.getId());
237 object.put("name", pdp.getName());
238 object.put("groupname", group.getName());
239 object.put("status", pdp.getStatus().getStatus().toString());
240 object.put("description", pdp.getDescription());
241 object.put("permitCount", "NA");
242 object.put("denyCount", "NA");
243 object.put("naCount", "NA");
244 pdpStatusData.add(object);
246 JSONObject object = new JSONObject();
247 object.put("id", pdp.getId());
248 object.put("name", pdp.getName());
249 object.put("groupname", group.getName());
250 object.put("status", pdp.getStatus().getStatus().toString());
251 object.put("description", pdp.getDescription());
252 object.put("permitCount", permitCount);
253 object.put("denyCount", denyCount);
254 object.put("naCount", naCount);
255 pdpStatusData.add(object);
262 private static String parseIPSystem(String line) {
263 Pattern pattern = Pattern.compile("://(.+?):");
264 Matcher ip = pattern.matcher(line);
273 * Contact JMX Connector Sever and return the value of the given jmxAttribute
275 @SuppressWarnings("rawtypes")
276 private long getRequestCounts(String host, int port, String jmxAttribute) {
278 logger.debug("Create an RMI connector client and connect it to the JMX connector server");
279 HashMap map = new HashMap();
281 JMXConnector jmxConnection;
283 jmxConnection = JMXConnectorFactory.newJMXConnector(createConnectionURL(host, port), map);
284 jmxConnection.connect();
285 Object o = jmxConnection.getMBeanServerConnection().getAttribute(new ObjectName("PdpRest:type=PdpRestMonitor"), jmxAttribute);
286 jmxConnection.close();
287 logger.debug("pdpEvaluationNA value retreived: " + o);
289 } catch (MalformedURLException e) {
290 logger.error("MalformedURLException for JMX connection");
291 } catch (IOException e) {
292 logger.error("Error in reteriving" + jmxAttribute + " from JMX connection");
293 } catch (AttributeNotFoundException e) {
294 logger.error("AttributeNotFoundException " + jmxAttribute + " for JMX connection");
295 } catch (InstanceNotFoundException e) {
296 logger.error("InstanceNotFoundException " + host + " for JMX connection");
297 } catch (MalformedObjectNameException e) {
298 logger.error("MalformedObjectNameException for JMX connection");
299 } catch (MBeanException e) {
300 logger.error("MBeanException for JMX connection");
302 } catch (ReflectionException e) {
303 logger.error("ReflectionException for JMX connection");
309 private static JMXServiceURL createConnectionURL(String host, int port) throws MalformedURLException{
310 return new JMXServiceURL("rmi", "", 0, "/jndi/rmi://" + host + ":" + port + "/jmxrmi");
315 * Add the information to the Policy Table
317 private void addPolicyToTable() {
318 policyActivityData = new ArrayList<Object>();
320 String policyID = null;
321 int policyFireCount = 0;
322 Map<String, String> policyMap = new HashMap<>();
323 Object policyList = null;
326 for (PDPGroup group : this.pdpConatiner.getGroups()){
327 for (PDPPolicy policy : group.getPolicies()){
329 policyMap.put(policy.getPolicyId().replace(" ", ""), policy.getId());
331 logger.error(XACMLErrorConstants.ERROR_SCHEMA_INVALID+policy.getName() +e);
335 for (PDP pdp : group.getPdps()){
336 // Add rows to the Policy Table
338 if (pdp.getStatus().getStatus().toString() == "UP_TO_DATE" && ((EcompPDP) pdp).getJmxPort() != 0){
339 String pdpIpAddress = parseIPSystem(pdp.getId());
340 policyList = getPolicy(pdpIpAddress, ((EcompPDP) pdp).getJmxPort(), "policyCount");
342 if (policyList != null && policyList.toString().length() > 3){
343 String[] splitPolicy = policyList.toString().split(",");
344 for (String policyKeyValue : splitPolicy){
345 policyID = urnPolicyID(policyKeyValue);
346 policyFireCount = countPolicyID(policyKeyValue);
347 if (policyID != null ){
348 if (policyMap.containsKey(policyID)){
349 JSONObject object = new JSONObject();
350 object.put("policyId", policyMap.get(policyID));
351 object.put("fireCount", policyFireCount);
352 object.put("system", pdp.getId());
353 policyActivityData.add(i, object);
359 if (policyList != null){
360 JSONObject object = new JSONObject();
361 object.put("policyId", "Unable to retrieve policy information");
362 object.put("fireCount", "NA");
363 object.put("system", pdp.getId());
364 policyActivityData.add(i, object);
367 JSONObject object = new JSONObject();
368 object.put("policyId", "Unable to access PDP JMX Server");
369 object.put("fireCount", "NA");
370 object.put("system", pdp.getId());
371 policyActivityData.add(i, object);
381 * Contact JMX Connector Sever and return the list of {policy id , count}
383 @SuppressWarnings("rawtypes")
384 private Object getPolicy(String host, int port, String jmxAttribute){
385 logger.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 logger.debug("policyMap value retreived: " + o);
396 } catch (MalformedURLException e) {
397 logger.error("MalformedURLException for JMX connection");
398 } catch (IOException e) {
399 logger.error("AttributeNotFoundException for policyMap" );
400 } catch (AttributeNotFoundException e) {
401 logger.error("AttributeNotFoundException for JMX connection");
402 } catch (InstanceNotFoundException e) {
403 logger.error("InstanceNotFoundException " + host + " for JMX connection");
404 } catch (MalformedObjectNameException e) {
405 logger.error("MalformedObjectNameException for JMX connection");
406 } catch (MBeanException e) {
407 logger.error("MBeanException for JMX connection");
409 } catch (ReflectionException e) {
410 logger.error("ReflectionException for JMX connection");
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);