f49e8cd225b00f863e0cec74c368f1113a7d56c3
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020-2021 Nordix Foundation.
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.apex.service.engine.main;
22
23 import java.util.concurrent.atomic.AtomicLong;
24 import org.onap.policy.common.utils.services.Registry;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class ApexPolicyStatisticsManager {
29     private static final Logger LOGGER = LoggerFactory.getLogger(ApexPolicyStatisticsManager.class);
30     public static final String REG_APEX_PDP_POLICY_COUNTER = "object:pdp/statistics/policy/counter";
31     private final AtomicLong policyDeployCount = new AtomicLong(0);
32     private final AtomicLong policyDeploySuccessCount = new AtomicLong(0);
33     private final AtomicLong policyDeployFailCount = new AtomicLong(0);
34     private final AtomicLong policyUndeployCount = new AtomicLong(0);
35     private final AtomicLong policyUndeploySuccessCount = new AtomicLong(0);
36     private final AtomicLong policyUndeployFailCount = new AtomicLong(0);
37     private final AtomicLong policyExecutedCount = new AtomicLong(0);
38     private final AtomicLong policyExecutedSuccessCount = new AtomicLong(0);
39     private final AtomicLong policyExecutedFailCount = new AtomicLong(0);
40
41     /**
42      * Constructs the object.
43      */
44     public ApexPolicyStatisticsManager() {
45         super();
46     }
47
48     /**
49      * To get the ApexPolicyStatisticsManager in Registry.
50      *
51      * @return ApexPolicyStatisticsManager The obj in Registry.
52      */
53     public static ApexPolicyStatisticsManager getInstanceFromRegistry() {
54         ApexPolicyStatisticsManager instance = null;
55         try {
56             instance = Registry.get(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER);
57         } catch (IllegalArgumentException e) {
58             LOGGER.debug("ApexPolicyStatisticsManager is not registered yet");
59         }
60         return instance;
61     }
62
63
64     /**
65      * Update the policy deploy count.
66      */
67     public void updatePolicyDeployCounter(final boolean isSuccessful) {
68         this.updatepPolicyDeployCount();
69         if (!isSuccessful) {
70             this.updatePolicyDeployFailCount();
71         } else {
72             this.updatePolicyDeploySuccessCount();
73         }
74     }
75
76     /**
77      * Update the policy executed count.
78      */
79     public void updatePolicyExecutedCounter(final boolean isSuccessful) {
80         this.updatePolicyExecutedCount();
81         if (isSuccessful) {
82             this.updatePolicyExecutedSuccessCount();
83         } else {
84             this.updatePolicyExecutedFailCount();
85         }
86     }
87
88
89     /**
90      * Update the policy undeploy count.
91      */
92     public void updatePolicyUndeployCounter(final boolean isSuccessful) {
93         this.policyUndeployCount.incrementAndGet();
94         if (isSuccessful) {
95             this.policyUndeploySuccessCount.incrementAndGet();
96         } else {
97             this.policyUndeployFailCount.incrementAndGet();
98         }
99     }
100
101     /**
102      * Method to update the total policy deploy count.
103      *
104      * @return the updated value of policyDeployCount
105      */
106     private long updatepPolicyDeployCount() {
107         return policyDeployCount.incrementAndGet();
108     }
109
110     /**
111      * Method to update the total policy deploy failed count.
112      *
113      * @return the updated value of totalPolicyDeployCount
114      */
115     private long updatePolicyDeployFailCount() {
116         return policyDeployFailCount.incrementAndGet();
117     }
118
119     /**
120      * Method to update the policy deploy success count.
121      *
122      * @return the updated value of policyDeploySuccessCount
123      */
124     private long updatePolicyDeploySuccessCount() {
125         return policyDeploySuccessCount.incrementAndGet();
126     }
127
128
129     /**
130      * Method to update the total policy executed count.
131      *
132      * @return the updated value of policyExecutedCount
133      */
134     private long updatePolicyExecutedCount() {
135         return policyExecutedCount.incrementAndGet();
136     }
137
138     /**
139      * Method to update the policy executed success count.
140      *
141      * @return the updated value of policyExecutedSuccessCount
142      */
143     private long updatePolicyExecutedSuccessCount() {
144         return policyExecutedSuccessCount.incrementAndGet();
145     }
146
147     /**
148      * Method to update the policy executed failure count.
149      *
150      * @return the updated value of policyExecutedFailCount
151      */
152     private long updatePolicyExecutedFailCount() {
153         return policyExecutedFailCount.incrementAndGet();
154     }
155
156     /**
157      * Reset all the statistics counts to 0.
158      */
159     public void resetAllStatistics() {
160         policyDeployCount.set(0L);
161         policyDeployFailCount.set(0L);
162         policyDeploySuccessCount.set(0L);
163         policyUndeployCount.set(0L);
164         policyUndeployFailCount.set(0L);
165         policyUndeploySuccessCount.set(0L);
166         policyExecutedCount.set(0L);
167         policyExecutedSuccessCount.set(0L);
168         policyExecutedFailCount.set(0L);
169     }
170
171     public long getPolicyDeployCount() {
172         return policyDeployCount.get();
173     }
174
175     public long getPolicyDeployFailCount() {
176         return policyDeployFailCount.get();
177     }
178
179     public long getPolicyDeploySuccessCount() {
180         return policyDeploySuccessCount.get();
181     }
182
183     public long getPolicyExecutedCount() {
184         return policyExecutedCount.get();
185     }
186
187     public long getPolicyExecutedSuccessCount() {
188         return policyExecutedSuccessCount.get();
189     }
190
191     public long getPolicyExecutedFailCount() {
192         return policyExecutedFailCount.get();
193     }
194
195     public long getPolicyUndeployCount() {
196         return policyUndeployCount.get();
197     }
198
199     public long getPolicyUndeploySuccessCount() {
200         return policyUndeploySuccessCount.get();
201     }
202
203     public long getPolicyUndeployFailCount() {
204         return policyUndeployFailCount.get();
205     }
206 }