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=========================================================
20 package org.onap.policy.pap.xacml.rest.service;
22 import java.util.HashSet;
25 import javax.persistence.EntityManager;
26 import javax.servlet.http.HttpServletResponse;
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;
35 import com.att.research.xacml.api.pap.PDPPolicy;
37 public class MetricService {
38 private static String errorMsg = "error";
41 * This is a private constructor
43 private MetricService() {
47 public static void doGetPolicyMetrics(HttpServletResponse response) {
48 Set<OnapPDPGroup> groups = new HashSet<>();
50 //get the count of policies on the PDP
51 if (XACMLPapServlet.getPAPEngine() != null) {
52 groups = XACMLPapServlet.getPAPEngine().getOnapPDPGroups();
55 for (OnapPDPGroup group : groups) {
56 Set<PDPPolicy> policies = group.getPolicies();
57 pdpCount += policies.size();
59 //get the count of policies on the PAP
60 EntityManager em = null;
61 if (XACMLPapServlet.getEmf() != null) {
62 em = XACMLPapServlet.getEmf().createEntityManager();
65 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE +
66 " Error creating entity manager with persistence unit: " +
67 XACMLPapServlet.getPersistenceUnit());
68 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
69 response.addHeader(errorMsg, "Error creating entity manager with persistence unit");
72 int papCount = ((Number) em.createNamedQuery("PolicyVersion.findAllCount").getSingleResult()).intValue();
74 int totalCount = pdpCount + papCount;
75 //create json string for API response
76 JSONObject json = new JSONObject();
77 json.put("papCount", papCount);
78 json.put("pdpCount", pdpCount);
79 json.put("totalCount", totalCount);
80 if (pdpCount > 0 && papCount > 0 && totalCount > 0) {
82 .info("Metrics have been found on the Policy Engine for the number of policies on the PAP and" +
84 response.setStatus(HttpServletResponse.SC_OK);
85 response.addHeader("successMapKey", "success");
86 response.addHeader("operation", "getMetrics");
87 response.addHeader("metrics", json.toString());
90 "The policy count on the PAP and PDP is 0. Please check the database and file system to " +
91 "correct this error.";
92 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
93 response.addHeader(errorMsg, message);
95 } catch (Exception e) {
96 String message = XACMLErrorConstants.ERROR_DATA_ISSUE + " Error Querying the Database: " + e.getMessage();
97 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPapServlet", " Error Querying the Database.");
98 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
99 response.addHeader(errorMsg, message);