d0be6492edccecbcf5eb2712f3c1359b22d549c3
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020-2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2021 Bell Canada 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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.service.engine.main;
24
25 import io.prometheus.client.Counter;
26 import java.util.concurrent.atomic.AtomicLong;
27 import lombok.NoArgsConstructor;
28 import org.onap.policy.common.utils.resources.PrometheusUtils;
29 import org.onap.policy.common.utils.services.Registry;
30 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 @NoArgsConstructor
35 public class ApexPolicyStatisticsManager {
36     private static final Logger LOGGER = LoggerFactory.getLogger(ApexPolicyStatisticsManager.class);
37
38     static final Counter POLICY_DEPLOYMENTS_COUNTER =
39         Counter.build().namespace(PrometheusUtils.PdpType.PDPA.getNamespace())
40             .name(PrometheusUtils.POLICY_DEPLOYMENTS_METRIC)
41             .labelNames(PrometheusUtils.OPERATION_METRIC_LABEL, PrometheusUtils.STATUS_METRIC_LABEL)
42             .help(PrometheusUtils.POLICY_DEPLOYMENT_HELP).register();
43
44     public static final String REG_APEX_PDP_POLICY_COUNTER = "object:pdp/statistics/policy/counter";
45     private static final String PROMETHEUS_TOTAL_LABEL_VALUE = "TOTAL";
46
47     private final AtomicLong policyDeployCount = new AtomicLong(0);
48     private final AtomicLong policyDeploySuccessCount = new AtomicLong(0);
49     private final AtomicLong policyDeployFailCount = new AtomicLong(0);
50     private final AtomicLong policyUndeployCount = new AtomicLong(0);
51     private final AtomicLong policyUndeploySuccessCount = new AtomicLong(0);
52     private final AtomicLong policyUndeployFailCount = new AtomicLong(0);
53     private final AtomicLong policyExecutedCount = new AtomicLong(0);
54     private final AtomicLong policyExecutedSuccessCount = new AtomicLong(0);
55     private final AtomicLong policyExecutedFailCount = new AtomicLong(0);
56
57     /**
58      * To get the ApexPolicyStatisticsManager in Registry.
59      *
60      * @return ApexPolicyStatisticsManager The obj in Registry.
61      */
62     public static ApexPolicyStatisticsManager getInstanceFromRegistry() {
63         ApexPolicyStatisticsManager instance = null;
64         try {
65             instance = Registry.get(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER);
66         } catch (IllegalArgumentException e) {
67             LOGGER.debug("ApexPolicyStatisticsManager is not registered yet");
68         }
69         return instance;
70     }
71
72     /**
73      * Update the policy deploy count.
74      */
75     public void updatePolicyDeployCounter(final boolean isSuccessful) {
76         POLICY_DEPLOYMENTS_COUNTER.labels(PrometheusUtils.DEPLOY_OPERATION, PROMETHEUS_TOTAL_LABEL_VALUE).inc();
77         this.policyDeployCount.incrementAndGet();
78         if (!isSuccessful) {
79             POLICY_DEPLOYMENTS_COUNTER.labels(PrometheusUtils.DEPLOY_OPERATION, PdpResponseStatus.FAIL.name()).inc();
80             this.policyDeployFailCount.incrementAndGet();
81         } else {
82             POLICY_DEPLOYMENTS_COUNTER.labels(PrometheusUtils.DEPLOY_OPERATION, PdpResponseStatus.SUCCESS.name()).inc();
83             this.policyDeploySuccessCount.incrementAndGet();
84         }
85     }
86
87     /**
88      * Update the policy executed count.
89      */
90     public void updatePolicyExecutedCounter(final boolean isSuccessful) {
91         this.policyExecutedCount.incrementAndGet();
92         if (isSuccessful) {
93             this.policyExecutedSuccessCount.incrementAndGet();
94         } else {
95             this.policyExecutedFailCount.incrementAndGet();
96         }
97     }
98
99     /**
100      * Update the policy undeploy count.
101      */
102     public void updatePolicyUndeployCounter(final boolean isSuccessful) {
103         POLICY_DEPLOYMENTS_COUNTER.labels(PrometheusUtils.UNDEPLOY_OPERATION, PROMETHEUS_TOTAL_LABEL_VALUE).inc();
104         this.policyUndeployCount.incrementAndGet();
105         if (isSuccessful) {
106             POLICY_DEPLOYMENTS_COUNTER.labels(PrometheusUtils.UNDEPLOY_OPERATION, PdpResponseStatus.SUCCESS.name())
107                 .inc();
108             this.policyUndeploySuccessCount.incrementAndGet();
109         } else {
110             POLICY_DEPLOYMENTS_COUNTER.labels(PrometheusUtils.UNDEPLOY_OPERATION, PdpResponseStatus.FAIL.name()).inc();
111             this.policyUndeployFailCount.incrementAndGet();
112         }
113     }
114
115     public long getPolicyDeployCount() {
116         return policyDeployCount.get();
117     }
118
119     public long getPolicyDeployFailCount() {
120         return policyDeployFailCount.get();
121     }
122
123     public long getPolicyDeploySuccessCount() {
124         return policyDeploySuccessCount.get();
125     }
126
127     public long getPolicyExecutedCount() {
128         return policyExecutedCount.get();
129     }
130
131     public long getPolicyExecutedSuccessCount() {
132         return policyExecutedSuccessCount.get();
133     }
134
135     public long getPolicyExecutedFailCount() {
136         return policyExecutedFailCount.get();
137     }
138
139     public long getPolicyUndeployCount() {
140         return policyUndeployCount.get();
141     }
142
143     public long getPolicyUndeploySuccessCount() {
144         return policyUndeploySuccessCount.get();
145     }
146
147     public long getPolicyUndeployFailCount() {
148         return policyUndeployFailCount.get();
149     }
150 }