277b23f067dd2b0854930e057c2d10487b2518c9
[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.services.Registry;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 @NoArgsConstructor
33 public class ApexPolicyStatisticsManager {
34     private static final Logger LOGGER = LoggerFactory.getLogger(ApexPolicyStatisticsManager.class);
35
36     static final Counter POLICY_DEPLOY_REQUESTS_COUNTER = Counter.build()
37             .name("policies_deploy_requests_total")
38             .help("Total number of TOSCA policies deploy requests.").register();
39     static final Counter POLICY_DEPLOY_REQUESTS_SUCCESS_COUNTER = Counter.build()
40             .name("policies_deploy_requests_success")
41             .help("Total number of TOSCA policies deploy requests that succeeded.").register();
42     static final Counter POLICY_DEPLOY_REQUESTS_FAILED_COUNTER = Counter.build()
43             .name("policies_deploy_requests_failed")
44             .help("Total number of TOSCA policies deploy requests that failed.").register();
45     static final Counter POLICY_UNDEPLOY_REQUESTS_COUNTER = Counter.build()
46             .name("policies_undeploy_requests_total").help("Total number of TOSCA policies undeploy requests.")
47             .register();
48     static final Counter POLICY_UNDEPLOY_REQUESTS_SUCCESS_COUNTER = Counter.build()
49             .name("policies_undeploy_requests_success")
50             .help("Total number of TOSCA policies undeploy requests that succeeded.").register();
51     static final Counter POLICY_UNDEPLOY_REQUESTS_FAILED_COUNTER = Counter.build()
52             .name("policies_undeploy_requests_failed")
53             .help("Total number of TOSCA policies undeploy requests that failed.").register();
54
55     public static final String REG_APEX_PDP_POLICY_COUNTER = "object:pdp/statistics/policy/counter";
56
57     private final AtomicLong policyDeployCount = new AtomicLong(0);
58     private final AtomicLong policyDeploySuccessCount = new AtomicLong(0);
59     private final AtomicLong policyDeployFailCount = new AtomicLong(0);
60     private final AtomicLong policyUndeployCount = new AtomicLong(0);
61     private final AtomicLong policyUndeploySuccessCount = new AtomicLong(0);
62     private final AtomicLong policyUndeployFailCount = new AtomicLong(0);
63     private final AtomicLong policyExecutedCount = new AtomicLong(0);
64     private final AtomicLong policyExecutedSuccessCount = new AtomicLong(0);
65     private final AtomicLong policyExecutedFailCount = new AtomicLong(0);
66
67     /**
68      * To get the ApexPolicyStatisticsManager in Registry.
69      *
70      * @return ApexPolicyStatisticsManager The obj in Registry.
71      */
72     public static ApexPolicyStatisticsManager getInstanceFromRegistry() {
73         ApexPolicyStatisticsManager instance = null;
74         try {
75             instance = Registry.get(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER);
76         } catch (IllegalArgumentException e) {
77             LOGGER.debug("ApexPolicyStatisticsManager is not registered yet");
78         }
79         return instance;
80     }
81
82
83     /**
84      * Update the policy deploy count.
85      */
86     public void updatePolicyDeployCounter(final boolean isSuccessful) {
87         this.policyDeployCount.incrementAndGet();
88         POLICY_DEPLOY_REQUESTS_COUNTER.inc();
89         if (!isSuccessful) {
90             this.policyDeployFailCount.incrementAndGet();
91             POLICY_DEPLOY_REQUESTS_FAILED_COUNTER.inc();
92         } else {
93             this.policyDeploySuccessCount.incrementAndGet();
94             POLICY_DEPLOY_REQUESTS_SUCCESS_COUNTER.inc();
95         }
96     }
97
98     /**
99      * Update the policy executed count.
100      */
101     public void updatePolicyExecutedCounter(final boolean isSuccessful) {
102         this.policyExecutedCount.incrementAndGet();
103         if (isSuccessful) {
104             this.policyExecutedSuccessCount.incrementAndGet();
105         } else {
106             this.policyExecutedFailCount.incrementAndGet();
107         }
108     }
109
110
111     /**
112      * Update the policy undeploy count.
113      */
114     public void updatePolicyUndeployCounter(final boolean isSuccessful) {
115         this.policyUndeployCount.incrementAndGet();
116         POLICY_UNDEPLOY_REQUESTS_COUNTER.inc();
117         if (isSuccessful) {
118             this.policyUndeploySuccessCount.incrementAndGet();
119             POLICY_UNDEPLOY_REQUESTS_SUCCESS_COUNTER.inc();
120         } else {
121             this.policyUndeployFailCount.incrementAndGet();
122             POLICY_UNDEPLOY_REQUESTS_FAILED_COUNTER.inc();
123         }
124     }
125
126     public long getPolicyDeployCount() {
127         return Double.valueOf(POLICY_DEPLOY_REQUESTS_COUNTER.get()).longValue();
128     }
129
130     public long getPolicyDeployFailCount() {
131         return Double.valueOf(POLICY_DEPLOY_REQUESTS_FAILED_COUNTER.get()).longValue();
132     }
133
134     public long getPolicyDeploySuccessCount() {
135         return Double.valueOf(POLICY_DEPLOY_REQUESTS_SUCCESS_COUNTER.get()).longValue();
136     }
137
138     public long getPolicyExecutedCount() {
139         return policyExecutedCount.get();
140     }
141
142     public long getPolicyExecutedSuccessCount() {
143         return policyExecutedSuccessCount.get();
144     }
145
146     public long getPolicyExecutedFailCount() {
147         return policyExecutedFailCount.get();
148     }
149
150     public long getPolicyUndeployCount() {
151         return Double.valueOf(POLICY_UNDEPLOY_REQUESTS_COUNTER.get()).longValue();
152     }
153
154     public long getPolicyUndeploySuccessCount() {
155         return Double.valueOf(POLICY_UNDEPLOY_REQUESTS_SUCCESS_COUNTER.get()).longValue();
156     }
157
158     public long getPolicyUndeployFailCount() {
159         return Double.valueOf(POLICY_UNDEPLOY_REQUESTS_FAILED_COUNTER.get()).longValue();
160     }
161 }