0200af37d6b0ebfc362b9d8cfe0d7fe0b1c7cc3a
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 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 policyExecutedCount = new AtomicLong(0);
35     private final AtomicLong policyExecutedSuccessCount = new AtomicLong(0);
36     private final AtomicLong policyExecutedFailCount = new AtomicLong(0);
37
38     /**
39      * Constructs the object.
40      */
41     public ApexPolicyStatisticsManager() {
42         super();
43     }
44
45     /**
46      * To get the ApexPolicyStatisticsManager in Registry.
47      *
48      * @return ApexPolicyStatisticsManager The obj in Registry.
49      */
50     public static ApexPolicyStatisticsManager getInstanceFromRegistry() {
51         ApexPolicyStatisticsManager instance = null;
52         try {
53             instance = Registry.get(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER);
54         } catch (IllegalArgumentException e) {
55             LOGGER.debug("ApexPolicyStatisticsManager is not registered yet");
56         }
57         return instance;
58     }
59
60
61     /**
62      * Update the policy deploy count.
63      */
64     public void updatePolicyDeployCounter(final boolean isSuccessful) {
65         this.updatepPolicyDeployCount();
66         if (!isSuccessful) {
67             this.updatePolicyDeployFailCount();
68         } else {
69             this.updatePolicyDeploySuccessCount();
70         }
71     }
72
73     /**
74      * Update the policy executed count.
75      */
76     public void updatePolicyExecutedCounter(final boolean isSuccessful) {
77         this.updatePolicyExecutedCount();
78         if (isSuccessful) {
79             this.updatePolicyExecutedSuccessCount();
80         } else {
81             this.updatePolicyExecutedFailCount();
82         }
83     }
84
85     /**
86      * Method to update the total policy deploy count.
87      *
88      * @return the updated value of policyDeployCount
89      */
90     private long updatepPolicyDeployCount() {
91         return policyDeployCount.incrementAndGet();
92     }
93
94     /**
95      * Method to update the total policy deploy failed count.
96      *
97      * @return the updated value of totalPolicyDeployCount
98      */
99     private long updatePolicyDeployFailCount() {
100         return policyDeployFailCount.incrementAndGet();
101     }
102
103     /**
104      * Method to update the policy deploy success count.
105      *
106      * @return the updated value of policyDeploySuccessCount
107      */
108     private long updatePolicyDeploySuccessCount() {
109         return policyDeploySuccessCount.incrementAndGet();
110     }
111
112
113     /**
114      * Method to update the total policy executed count.
115      *
116      * @return the updated value of policyExecutedCount
117      */
118     private long updatePolicyExecutedCount() {
119         return policyExecutedCount.incrementAndGet();
120     }
121
122     /**
123      * Method to update the policy executed success count.
124      *
125      * @return the updated value of policyExecutedSuccessCount
126      */
127     private long updatePolicyExecutedSuccessCount() {
128         return policyExecutedSuccessCount.incrementAndGet();
129     }
130
131     /**
132      * Method to update the policy executed failure count.
133      *
134      * @return the updated value of policyExecutedFailCount
135      */
136     private long updatePolicyExecutedFailCount() {
137         return policyExecutedFailCount.incrementAndGet();
138     }
139
140     /**
141      * Reset all the statistics counts to 0.
142      */
143     public void resetAllStatistics() {
144         policyDeployCount.set(0L);
145         policyDeployFailCount.set(0L);
146         policyDeploySuccessCount.set(0L);
147         policyExecutedCount.set(0L);
148         policyExecutedSuccessCount.set(0L);
149         policyExecutedFailCount.set(0L);
150     }
151
152     public long getPolicyDeployCount() {
153         return policyDeployCount.get();
154     }
155
156     public long getPolicyDeployFailCount() {
157         return policyDeployFailCount.get();
158     }
159
160     public long getPolicyDeploySuccessCount() {
161         return policyDeploySuccessCount.get();
162     }
163
164     public long getPolicyExecutedCount() {
165         return policyExecutedCount.get();
166     }
167
168     public long getPolicyExecutedSuccessCount() {
169         return policyExecutedSuccessCount.get();
170     }
171
172     public long getPolicyExecutedFailCount() {
173         return policyExecutedFailCount.get();
174     }
175 }