609da8f675c1abd4a542cfa768d02a04a84ff87d
[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
40     protected static final Counter deploymentsCounter =
41         Counter.build().namespace(PROMETHEUS_NAMESPACE).name(PrometheusUtils.POLICY_DEPLOYMENTS_METRIC)
42             .labelNames(PrometheusUtils.OPERATION_METRIC_LABEL,
43                 PrometheusUtils.STATUS_METRIC_LABEL)
44             .help(PrometheusUtils.POLICY_DEPLOYMENT_HELP)
45             .register();
46
47     private long totalPolicyTypesCount;
48     private long totalPoliciesCount;
49     private long errorCount;
50     private long permitDecisionsCount;
51     private long denyDecisionsCount;
52     private long deploySuccessCount;
53     private long deployFailureCount;
54     private long undeploySuccessCount;
55     private long undeployFailureCount;
56     private long indeterminantDecisionsCount;
57     private long notApplicableDecisionsCount;
58
59     /**
60      * Method to set the xacml pdp total policy types count. This
61      * doesn't really increment, it depends on the applications
62      * that are loaded. Which can be dynamic.
63      *
64      * @return the total
65      */
66     @Synchronized
67     public long setTotalPolicyTypesCount(long newCount) {
68         totalPolicyTypesCount = newCount;
69         return totalPolicyTypesCount;
70     }
71
72     /**
73      * Method to set the xacml pdp total policies count. This
74      * doesn't really increment, it depends on the applications
75      * that are loaded. Which can be dynamic.
76      *
77      * @return the total
78      */
79     @Synchronized
80     public long setTotalPolicyCount(long newCount) {
81         totalPoliciesCount = newCount;
82         return totalPoliciesCount;
83     }
84
85     /**
86      * Method to update the number of error decisions.
87      *
88      * @return the errorDecisionsCount
89      */
90     @Synchronized
91     public long updateErrorCount() {
92         return ++errorCount;
93     }
94
95     /**
96      * Method to update the number of permit decisions.
97      *
98      * @return the permitDecisionsCount
99      */
100     @Synchronized
101     public long updatePermitDecisionsCount() {
102         return ++permitDecisionsCount;
103     }
104
105     /**
106      * Method to update the number of deny decisions.
107      *
108      * @return the denyDecisionsCount
109      */
110     @Synchronized
111     public long updateDenyDecisionsCount() {
112         return ++denyDecisionsCount;
113     }
114
115     /**
116      * Method to update the number of successful deploys.
117      *
118      * @return the deploySuccessCount
119      */
120     @Synchronized
121     public long updateDeploySuccessCount() {
122         deploymentsCounter.labels(PrometheusUtils.DEPLOY_OPERATION,
123             PdpResponseStatus.SUCCESS.name()).inc();
124         return ++deploySuccessCount;
125     }
126
127     /**
128      * Method to update the number of failed deploys.
129      *
130      * @return the deployFailureCount
131      */
132     @Synchronized
133     public long updateDeployFailureCount() {
134         deploymentsCounter.labels(PrometheusUtils.DEPLOY_OPERATION,
135             PdpResponseStatus.FAIL.name()).inc();
136         return ++deployFailureCount;
137     }
138
139     /**
140      * Method to update the number of successful undeploys.
141      *
142      * @return the undeploySuccessCount
143      */
144     @Synchronized
145     public long updateUndeploySuccessCount() {
146         deploymentsCounter.labels(PrometheusUtils.UNDEPLOY_OPERATION,
147             PdpResponseStatus.SUCCESS.name()).inc();
148         return ++undeploySuccessCount;
149     }
150
151     /**
152      * Method to update the number of failed undeploys.
153      *
154      * @return the undeployFailureCount
155      */
156     @Synchronized
157     public long updateUndeployFailureCount() {
158         deploymentsCounter.labels(PrometheusUtils.UNDEPLOY_OPERATION,
159             PdpResponseStatus.FAIL.name()).inc();
160         return ++undeployFailureCount;
161     }
162
163     /**
164      * Method to update the number of indeterminant decisions.
165      *
166      * @return the indeterminantDecisionsCount
167      */
168     @Synchronized
169     public long updateIndeterminantDecisionsCount() {
170         return ++indeterminantDecisionsCount;
171     }
172
173     /**
174      * Method to update the number of not applicable decisions.
175      *
176      * @return the notApplicableDecisionsCount
177      */
178     @Synchronized
179     public long updateNotApplicableDecisionsCount() {
180         return ++notApplicableDecisionsCount;
181     }
182
183     /**
184      * Reset all the statistics counts to 0.
185      */
186     @Synchronized
187     public void resetAllStatistics() {
188         totalPolicyTypesCount = 0L;
189         totalPoliciesCount = 0L;
190         errorCount = 0L;
191         permitDecisionsCount = 0L;
192         denyDecisionsCount = 0L;
193         deploySuccessCount = 0L;
194         deployFailureCount = 0L;
195         undeploySuccessCount = 0L;
196         undeployFailureCount = 0L;
197         indeterminantDecisionsCount = 0L;
198         notApplicableDecisionsCount = 0L;
199     }
200 }