Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / java / org / openecomp / policy / controller / DashboardController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.policy.controller;
22
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.net.MalformedURLException;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33
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;
45
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;
58
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;
65
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;
73
74 @Controller
75 @RequestMapping({"/"})
76 public class DashboardController  extends RestrictedBaseController{
77         private static final Logger logger = FlexLogger.getLogger(DashboardController.class);
78         @Autowired
79         SystemLogDbDao systemDAO;
80         
81         private int pdpCount;
82         private PDPGroupContainer pdpConatiner;
83         private ArrayList<Object> pdpStatusData;
84         private ArrayList<Object> papStatusData;
85         private ArrayList<Object> policyActivityData;
86         
87
88         
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){
91                 try{
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());
98                 }
99                 catch (Exception e){
100                         e.printStackTrace();
101                 }
102         }
103         
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){
106                 try{
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());
113                 }
114                 catch (Exception e){
115                         e.printStackTrace();
116                 }
117         }
118         
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){
121                 try{
122                         Map<String, Object> model = new HashMap<String, Object>();
123                         ObjectMapper mapper = new ObjectMapper();
124                         mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
125                         addPAPToTable();
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());
130                 }
131                 catch (Exception e){
132                         e.printStackTrace();
133                 }
134         }
135         
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){
138                 try{
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());
143                         addPDPToTable();
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());
148                 }
149                 catch (Exception e){
150                         e.printStackTrace();
151                 }
152         }
153         
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){
156                 try{
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());
161                         addPolicyToTable();
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());
166                 }
167                 catch (Exception e){
168                         e.printStackTrace();
169                 }
170         }
171         
172         /*
173          * Add the PAP information to the PAP Table
174          */
175         public void addPAPToTable(){
176                 papStatusData = new ArrayList<Object>();
177                 File folder = new File(PolicyController.getGitPath().toAbsolutePath().toString());
178                 String papStatus = null;
179                 try {
180                         Set<EcompPDPGroup> groups = PolicyController.getPapEngine().getEcompPDPGroups();
181                         if (groups == null) {
182                                 papStatus = "UNKNOWN";
183                                 throw new PAPException("PAP not running");              
184                         }else {
185                                 papStatus = "IS_OK";
186                         }
187                 } catch (PAPException | NullPointerException e1) {
188                         papStatus = "CANNOT_CONNECT";
189                         logger.error("Error getting PAP status, PAP not responding to requests");
190                 }
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);
198         }
199         
200         private int countFilesInDirectory(File directory) {
201             int count = 0;
202             for (File file : directory.listFiles()) {
203                   if (file.isFile() && file.getName().endsWith(".xml")) {
204                       count++;
205                   }
206                   if (file.isDirectory()) {
207                      count += countFilesInDirectory(file);
208                   }
209               }
210               return count;
211         }
212         /**
213          * Add PDP Information to the PDP Table
214          *  
215          */
216         public void addPDPToTable(){    
217                 pdpCount = 0;
218                 pdpStatusData = new ArrayList<Object>();
219                 long naCount;
220                 long denyCount = 0;
221                 long permitCount = 0;
222                 for (PDPGroup group : this.pdpConatiner.getGroups()){   
223                         for (PDP pdp : group.getPdps()){
224                                 naCount = -1;
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();
228                                         if (port != 0)
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");
233                                 }
234                                 if (naCount == -1){
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);
245                                 }else{  
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);
256                                 }
257                                 pdpCount++;
258                         }
259                 }
260         }
261         
262         private static String parseIPSystem(String line) {      
263                 Pattern pattern = Pattern.compile("://(.+?):");
264                 Matcher ip = pattern.matcher(line);
265                 if (ip.find())
266                 {
267                         return ip.group(1);
268                 } 
269                 return null;
270         }
271         
272         /*
273          * Contact JMX Connector Sever and return the value of the given jmxAttribute
274          */
275         @SuppressWarnings("rawtypes")
276         private long getRequestCounts(String host, int port, String jmxAttribute) {
277                 
278                 logger.debug("Create an RMI connector client and connect it to the JMX connector server");
279                 HashMap map = new HashMap();
280                 map = null;
281                 JMXConnector jmxConnection;
282                 try {
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);
288                         return (long) 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");
301                         e.printStackTrace();
302                 } catch (ReflectionException e) {
303                         logger.error("ReflectionException for JMX connection");
304                 }
305                 
306                 return -1;
307         }
308         
309         private static JMXServiceURL createConnectionURL(String host, int port) throws MalformedURLException{
310             return new JMXServiceURL("rmi", "", 0, "/jndi/rmi://" + host + ":" + port + "/jmxrmi");
311         }
312         
313         
314         /*
315          * Add the information to the Policy Table
316          */
317         private void addPolicyToTable() {
318                 policyActivityData = new ArrayList<Object>();
319                 int i = 1;
320                 String policyID = null;
321                 int policyFireCount = 0;
322                 Map<String, String> policyMap = new HashMap<>();
323                 Object policyList = null;
324                 //get list of policy 
325                 
326                 for (PDPGroup group : this.pdpConatiner.getGroups()){   
327                         for (PDPPolicy policy : group.getPolicies()){
328                                 try{
329                                         policyMap.put(policy.getPolicyId().replace(" ", ""), policy.getId());
330                                 }catch(Exception e){
331                                         logger.error(XACMLErrorConstants.ERROR_SCHEMA_INVALID+policy.getName() +e);
332                                 }
333                         }
334                         
335                         for (PDP pdp : group.getPdps()){                
336                                         // Add rows to the Policy Table
337                                 policyList = null;
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");
341                                 }
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);      
354                                                                         i++;
355                                                                 }
356                                                         }
357                                                 }
358                                 }else {
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);
365                                                 i++;
366                                         }else{
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);
372                                                 i++;
373                                         }
374                                 }                                                       
375         
376                         }
377                 }
378         }
379         
380         /*
381          * Contact JMX Connector Sever and return the list of {policy id , count}
382          */
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();
387                 map = null;
388                 JMXConnector jmxConnection;
389                 try {
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);
395                         return  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");
408                         e.printStackTrace();
409                 } catch (ReflectionException e) {
410                         logger.error("ReflectionException for JMX connection");
411                 }
412                 
413                 return null;
414         
415         }
416         
417         private static String urnPolicyID(String line){
418                 String[]  splitLine = line.toString().split("=");       
419                 String removeSpaces = splitLine[0].replaceAll("\\s+", "");
420                 return removeSpaces.replace("{", "");
421         }
422         
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);
427                 return intCount;
428         }
429         
430 }