Merge changes I3845ca4f,I7ae1ad11
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / service / MetricService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
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 package org.onap.policy.pap.xacml.rest.service;
21
22 import java.util.HashSet;
23 import java.util.Set;
24
25 import javax.persistence.EntityManager;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.json.JSONObject;
30 import org.onap.policy.common.logging.eelf.MessageCodes;
31 import org.onap.policy.common.logging.eelf.PolicyLogger;
32 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
33 import org.onap.policy.xacml.api.XACMLErrorConstants;
34 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
35
36 import com.att.research.xacml.api.pap.PDPPolicy;
37
38 public class MetricService {
39         private static String errorMsg  = "error";
40         /*
41          * This is a private constructor
42          * */
43         public MetricService(){
44                 
45         }
46         public static void doGetPolicyMetrics(HttpServletResponse response) {
47                 Set<OnapPDPGroup> groups = new HashSet<>();
48                 try {
49                         //get the count of policies on the PDP
50                         if(XACMLPapServlet.getPAPEngine()!=null){
51                                 groups = XACMLPapServlet.getPAPEngine().getOnapPDPGroups();
52                         }
53                         int pdpCount = 0;
54                         for (OnapPDPGroup group : groups) {
55                                 Set<PDPPolicy> policies = group.getPolicies();
56                                 pdpCount += policies.size();
57                         }
58                         //get the count of policies on the PAP
59                         EntityManager em = null;
60                         if(XACMLPapServlet.getEmf()!=null){
61                                 em = XACMLPapServlet.getEmf().createEntityManager();
62                         }
63                         if (em==null){
64                                 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " Error creating entity manager with persistence unit: " + XACMLPapServlet.getPersistenceUnit());
65                                 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);                                                         
66                                 response.addHeader(errorMsg, "Error creating entity manager with persistence unit");
67                                 return;
68                         }
69                         int papCount = ((Number) em.createNamedQuery("PolicyVersion.findAllCount").getSingleResult()).intValue();
70                         em.close();
71                         int totalCount = pdpCount + papCount;
72                         //create json string for API response
73                         JSONObject json = new JSONObject();
74                         json.put("papCount", papCount);
75                         json.put("pdpCount", pdpCount);
76                         json.put("totalCount", totalCount);
77                         if (pdpCount>0 && papCount>0 && totalCount>0) {
78                                 PolicyLogger.info("Metrics have been found on the Policy Engine for the number of policies on the PAP and PDP.");
79                                 response.setStatus(HttpServletResponse.SC_OK);
80                                 response.addHeader("successMapKey", "success");
81                                 response.addHeader("operation", "getMetrics");
82                                 response.addHeader("metrics", json.toString() );
83                                 return;
84                         }else{
85                                 String message = "The policy count on the PAP and PDP is 0.  Please check the database and file system to correct this error.";
86                                 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);                                                         
87                                 response.addHeader(errorMsg, message);
88                                 return;
89                         }
90                 } catch (Exception e) {
91                         String message = XACMLErrorConstants.ERROR_DATA_ISSUE + " Error Querying the Database: " + e.getMessage();
92                         PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPapServlet", " Error Querying the Database.");
93                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);                                                         
94                         response.addHeader(errorMsg, message);
95                         return;                 
96                 }
97         }
98
99 }