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