Extending Prometheus counts to include permit / deny / indeterminant
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / rest / XacmlPdpStatisticsManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019, 2021-2022 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main.rest;
22
23 import io.prometheus.client.Counter;
24 import lombok.Getter;
25 import lombok.Setter;
26 import lombok.Synchronized;
27 import org.onap.policy.common.utils.resources.PrometheusUtils;
28 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
29
30 /**
31  * Class to hold statistical data for xacmlPdp component.
32  */
33 @Getter(onMethod_ = @Synchronized)
34 public class XacmlPdpStatisticsManager {
35     @Getter
36     @Setter
37     private static XacmlPdpStatisticsManager current = null;
38     protected static final String PROMETHEUS_NAMESPACE = "pdpx";
39     protected static final String POLICY_DECISIONS_METRIC = "policy_decisions";
40     public static final String POLICY_DECISIONS_HELP = "The total number of policy decisions.";
41     public static final String PERMIT_OPERATION = "permit";
42     public static final String DENY_OPERATION = "deny";
43     public static final String INDETERMINANT_OPERATION = "indeterminant";
44     public static final String NOT_APPLICABLE_OPERATION = "not_applicable";
45
46     protected static final Counter deploymentsCounter =
47         Counter.build().namespace(PROMETHEUS_NAMESPACE).name(PrometheusUtils.POLICY_DEPLOYMENTS_METRIC)
48             .labelNames(PrometheusUtils.OPERATION_METRIC_LABEL,
49                 PrometheusUtils.STATUS_METRIC_LABEL)
50             .help(PrometheusUtils.POLICY_DEPLOYMENT_HELP)
51             .register();
52
53     protected static final Counter decisionsCounter =
54         Counter.build().namespace(PROMETHEUS_NAMESPACE).name(POLICY_DECISIONS_METRIC)
55             .labelNames(PrometheusUtils.STATUS_METRIC_LABEL)
56             .help(POLICY_DECISIONS_HELP)
57             .register();
58
59     private long totalPolicyTypesCount;
60     private long totalPoliciesCount;
61     private long errorCount;
62     private long permitDecisionsCount;
63     private long denyDecisionsCount;
64     private long deploySuccessCount;
65     private long deployFailureCount;
66     private long undeploySuccessCount;
67     private long undeployFailureCount;
68     private long indeterminantDecisionsCount;
69     private long notApplicableDecisionsCount;
70
71     /**
72      * Method to set the xacml pdp total policy types count. This
73      * doesn't really increment, it depends on the applications
74      * that are loaded. Which can be dynamic.
75      *
76      * @return the total
77      */
78     @Synchronized
79     public long setTotalPolicyTypesCount(long newCount) {
80         totalPolicyTypesCount = newCount;
81         return totalPolicyTypesCount;
82     }
83
84     /**
85      * Method to set the xacml pdp total policies count. This
86      * doesn't really increment, it depends on the applications
87      * that are loaded. Which can be dynamic.
88      *
89      * @return the total
90      */
91     @Synchronized
92     public long setTotalPolicyCount(long newCount) {
93         totalPoliciesCount = newCount;
94         return totalPoliciesCount;
95     }
96
97     /**
98      * Method to update the number of error decisions.
99      *
100      * @return the errorDecisionsCount
101      */
102     @Synchronized
103     public long updateErrorCount() {
104         return ++errorCount;
105     }
106
107     /**
108      * Method to update the number of permit decisions.
109      *
110      * @return the permitDecisionsCount
111      */
112     @Synchronized
113     public long updatePermitDecisionsCount() {
114         decisionsCounter.labels(PERMIT_OPERATION).inc();
115         return ++permitDecisionsCount;
116     }
117
118     /**
119      * Method to update the number of deny decisions.
120      *
121      * @return the denyDecisionsCount
122      */
123     @Synchronized
124     public long updateDenyDecisionsCount() {
125         decisionsCounter.labels(DENY_OPERATION).inc();
126         return ++denyDecisionsCount;
127     }
128
129     /**
130      * Method to update the number of successful deploys.
131      *
132      * @return the deploySuccessCount
133      */
134     @Synchronized
135     public long updateDeploySuccessCount() {
136         deploymentsCounter.labels(PrometheusUtils.DEPLOY_OPERATION,
137             PdpResponseStatus.SUCCESS.name()).inc();
138         return ++deploySuccessCount;
139     }
140
141     /**
142      * Method to update the number of failed deploys.
143      *
144      * @return the deployFailureCount
145      */
146     @Synchronized
147     public long updateDeployFailureCount() {
148         deploymentsCounter.labels(PrometheusUtils.DEPLOY_OPERATION,
149             PdpResponseStatus.FAIL.name()).inc();
150         return ++deployFailureCount;
151     }
152
153     /**
154      * Method to update the number of successful undeploys.
155      *
156      * @return the undeploySuccessCount
157      */
158     @Synchronized
159     public long updateUndeploySuccessCount() {
160         deploymentsCounter.labels(PrometheusUtils.UNDEPLOY_OPERATION,
161             PdpResponseStatus.SUCCESS.name()).inc();
162         return ++undeploySuccessCount;
163     }
164
165     /**
166      * Method to update the number of failed undeploys.
167      *
168      * @return the undeployFailureCount
169      */
170     @Synchronized
171     public long updateUndeployFailureCount() {
172         deploymentsCounter.labels(PrometheusUtils.UNDEPLOY_OPERATION,
173             PdpResponseStatus.FAIL.name()).inc();
174         return ++undeployFailureCount;
175     }
176
177     /**
178      * Method to update the number of indeterminant decisions.
179      *
180      * @return the indeterminantDecisionsCount
181      */
182     @Synchronized
183     public long updateIndeterminantDecisionsCount() {
184         decisionsCounter.labels(INDETERMINANT_OPERATION).inc();
185         return ++indeterminantDecisionsCount;
186     }
187
188     /**
189      * Method to update the number of not applicable decisions.
190      *
191      * @return the notApplicableDecisionsCount
192      */
193     @Synchronized
194     public long updateNotApplicableDecisionsCount() {
195         decisionsCounter.labels(NOT_APPLICABLE_OPERATION).inc();
196         return ++notApplicableDecisionsCount;
197     }
198
199     /**
200      * Reset all the statistics counts to 0.
201      */
202     @Synchronized
203     public void resetAllStatistics() {
204         totalPolicyTypesCount = 0L;
205         totalPoliciesCount = 0L;
206         errorCount = 0L;
207         permitDecisionsCount = 0L;
208         denyDecisionsCount = 0L;
209         deploySuccessCount = 0L;
210         deployFailureCount = 0L;
211         undeploySuccessCount = 0L;
212         undeployFailureCount = 0L;
213         indeterminantDecisionsCount = 0L;
214         notApplicableDecisionsCount = 0L;
215     }
216 }