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