Consolidate PolicyRestAdapter setup
[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
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28
29 import javax.servlet.http.HttpServletResponse;
30
31 import org.json.JSONObject;
32 import org.onap.policy.common.logging.eelf.MessageCodes;
33 import org.onap.policy.common.logging.eelf.PolicyLogger;
34 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
35 import org.onap.policy.rest.dao.CommonClassDao;
36 import org.onap.policy.rest.jpa.PolicyVersion;
37 import org.onap.policy.xacml.api.XACMLErrorConstants;
38 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
41
42 @Component
43 public class MetricService {
44     private static String errorMsg = "error";
45     private static CommonClassDao commonClassDao;
46
47     /*
48      * This is a private constructor
49      */
50     private MetricService() {
51
52     }
53
54     @Autowired
55     private MetricService(CommonClassDao commonClassDao) {
56         MetricService.commonClassDao = commonClassDao;
57     }
58
59     public static void doGetPolicyMetrics(HttpServletResponse response) {
60         Set<OnapPDPGroup> groups = new HashSet<>();
61         try {
62             // get the count of policies on the PDP
63             if (XACMLPapServlet.getPAPEngine() != null) {
64                 groups = XACMLPapServlet.getPAPEngine().getOnapPDPGroups();
65             }
66             int pdpCount = 0;
67             for (OnapPDPGroup group : groups) {
68                 Set<PDPPolicy> policies = group.getPolicies();
69                 pdpCount += policies.size();
70             }
71             // get the count of policies on the PAP
72             List<Object> dataList = commonClassDao.getData(PolicyVersion.class);
73             int papCount = dataList.size();
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) {
81                 PolicyLogger.info(
82                         "Metrics have been found on the Policy Engine for the number of policies on the PAP and PDP.");
83                 response.setStatus(HttpServletResponse.SC_OK);
84                 response.addHeader("successMapKey", "success");
85                 response.addHeader("operation", "getMetrics");
86                 response.addHeader("metrics", json.toString());
87                 return;
88             } else {
89                 String message =
90                         "The policy count on the PAP and PDP is 0.  Please check the database and file system to correct this error.";
91                 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
92                 response.addHeader(errorMsg, message);
93                 return;
94             }
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);
100             return;
101         }
102     }
103
104 }