2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017,2019 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.onap.policy.pap.xacml.rest.service;
23 import com.att.research.xacml.api.pap.PDPPolicy;
24 import java.util.HashSet;
25 import java.util.List;
27 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.rest.dao.CommonClassDao;
33 import org.onap.policy.rest.jpa.PolicyVersion;
34 import org.onap.policy.xacml.api.XACMLErrorConstants;
35 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
40 public class MetricService {
41 private static String errorMsg = "error";
42 private static CommonClassDao commonClassDao;
45 * This is a private constructor
47 private MetricService() {
52 private MetricService(CommonClassDao commonClassDao) {
53 MetricService.commonClassDao = commonClassDao;
56 public static void doGetPolicyMetrics(HttpServletResponse response) {
57 Set<OnapPDPGroup> groups = new HashSet<>();
59 // get the count of policies on the PDP
60 if (XACMLPapServlet.getPAPEngine() != null) {
61 groups = XACMLPapServlet.getPAPEngine().getOnapPDPGroups();
64 for (OnapPDPGroup group : groups) {
65 Set<PDPPolicy> policies = group.getPolicies();
66 pdpCount += policies.size();
68 // get the count of policies on the PAP
69 List<Object> dataList = commonClassDao.getData(PolicyVersion.class);
70 int papCount = dataList.size();
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) {
79 "Metrics have been found on the Policy Engine for the number of policies on the PAP and PDP.");
80 response.setStatus(HttpServletResponse.SC_OK);
81 response.addHeader("successMapKey", "success");
82 response.addHeader("operation", "getMetrics");
83 response.addHeader("metrics", json.toString());
87 "The policy count on the PAP and PDP is 0. Please check the database and file system to correct this error.";
88 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
89 response.addHeader(errorMsg, message);
92 } catch (Exception e) {
93 String message = XACMLErrorConstants.ERROR_DATA_ISSUE + " Error Querying the Database: " + e.getMessage();
94 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPapServlet", " Error Querying the Database.");
95 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
96 response.addHeader(errorMsg, message);