Merge "use hibernate and breakup dbdao and papservlet"
[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,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
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.onap.policy.pap.xacml.rest.service;
22
23 import com.att.research.xacml.api.pap.PDPPolicy;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
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;
38
39 @Component
40 public class MetricService {
41     private static String errorMsg = "error";
42     private static CommonClassDao commonClassDao;
43
44     /*
45      * This is a private constructor
46      */
47     private MetricService() {
48
49     }
50
51     @Autowired
52     private MetricService(CommonClassDao commonClassDao) {
53         MetricService.commonClassDao = commonClassDao;
54     }
55
56     public static void doGetPolicyMetrics(HttpServletResponse response) {
57         Set<OnapPDPGroup> groups = new HashSet<>();
58         try {
59             // get the count of policies on the PDP
60             if (XACMLPapServlet.getPAPEngine() != null) {
61                 groups = XACMLPapServlet.getPAPEngine().getOnapPDPGroups();
62             }
63             int pdpCount = 0;
64             for (OnapPDPGroup group : groups) {
65                 Set<PDPPolicy> policies = group.getPolicies();
66                 pdpCount += policies.size();
67             }
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) {
78                 PolicyLogger.info(
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());
84                 return;
85             } else {
86                 String message =
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);
90                 return;
91             }
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);
97             return;
98         }
99     }
100
101 }