1924d9f8a32aac3783aef0b3824f4fc8e2b01f77
[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  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.service.engine.main;
23
24 import java.util.concurrent.atomic.AtomicLong;
25 import lombok.NoArgsConstructor;
26 import org.onap.policy.common.utils.services.Registry;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 @NoArgsConstructor
31 public class ApexPolicyStatisticsManager {
32     private static final Logger LOGGER = LoggerFactory.getLogger(ApexPolicyStatisticsManager.class);
33     public static final String REG_APEX_PDP_POLICY_COUNTER = "object:pdp/statistics/policy/counter";
34     private final AtomicLong policyDeployCount = new AtomicLong(0);
35     private final AtomicLong policyDeploySuccessCount = new AtomicLong(0);
36     private final AtomicLong policyDeployFailCount = new AtomicLong(0);
37     private final AtomicLong policyUndeployCount = new AtomicLong(0);
38     private final AtomicLong policyUndeploySuccessCount = new AtomicLong(0);
39     private final AtomicLong policyUndeployFailCount = new AtomicLong(0);
40     private final AtomicLong policyExecutedCount = new AtomicLong(0);
41     private final AtomicLong policyExecutedSuccessCount = new AtomicLong(0);
42     private final AtomicLong policyExecutedFailCount = new AtomicLong(0);
43
44     /**
45      * To get the ApexPolicyStatisticsManager in Registry.
46      *
47      * @return ApexPolicyStatisticsManager The obj in Registry.
48      */
49     public static ApexPolicyStatisticsManager getInstanceFromRegistry() {
50         ApexPolicyStatisticsManager instance = null;
51         try {
52             instance = Registry.get(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER);
53         } catch (IllegalArgumentException e) {
54             LOGGER.debug("ApexPolicyStatisticsManager is not registered yet");
55         }
56         return instance;
57     }
58
59
60     /**
61      * Update the policy deploy count.
62      */
63     public void updatePolicyDeployCounter(final boolean isSuccessful) {
64         this.updatepPolicyDeployCount();
65         if (!isSuccessful) {
66             this.updatePolicyDeployFailCount();
67         } else {
68             this.updatePolicyDeploySuccessCount();
69         }
70     }
71
72     /**
73      * Update the policy executed count.
74      */
75     public void updatePolicyExecutedCounter(final boolean isSuccessful) {
76         this.updatePolicyExecutedCount();
77         if (isSuccessful) {
78             this.updatePolicyExecutedSuccessCount();
79         } else {
80             this.updatePolicyExecutedFailCount();
81         }
82     }
83
84
85     /**
86      * Update the policy undeploy count.
87      */
88     public void updatePolicyUndeployCounter(final boolean isSuccessful) {
89         this.policyUndeployCount.incrementAndGet();
90         if (isSuccessful) {
91             this.policyUndeploySuccessCount.incrementAndGet();
92         } else {
93             this.policyUndeployFailCount.incrementAndGet();
94         }
95     }
96
97     /**
98      * Method to update the total policy deploy count.
99      *
100      * @return the updated value of policyDeployCount
101      */
102     private long updatepPolicyDeployCount() {
103         return policyDeployCount.incrementAndGet();
104     }
105
106     /**
107      * Method to update the total policy deploy failed count.
108      *
109      * @return the updated value of totalPolicyDeployCount
110      */
111     private long updatePolicyDeployFailCount() {
112         return policyDeployFailCount.incrementAndGet();
113     }
114
115     /**
116      * Method to update the policy deploy success count.
117      *
118      * @return the updated value of policyDeploySuccessCount
119      */
120     private long updatePolicyDeploySuccessCount() {
121         return policyDeploySuccessCount.incrementAndGet();
122     }
123
124
125     /**
126      * Method to update the total policy executed count.
127      *
128      * @return the updated value of policyExecutedCount
129      */
130     private long updatePolicyExecutedCount() {
131         return policyExecutedCount.incrementAndGet();
132     }
133
134     /**
135      * Method to update the policy executed success count.
136      *
137      * @return the updated value of policyExecutedSuccessCount
138      */
139     private long updatePolicyExecutedSuccessCount() {
140         return policyExecutedSuccessCount.incrementAndGet();
141     }
142
143     /**
144      * Method to update the policy executed failure count.
145      *
146      * @return the updated value of policyExecutedFailCount
147      */
148     private long updatePolicyExecutedFailCount() {
149         return policyExecutedFailCount.incrementAndGet();
150     }
151
152     /**
153      * Reset all the statistics counts to 0.
154      */
155     public void resetAllStatistics() {
156         policyDeployCount.set(0L);
157         policyDeployFailCount.set(0L);
158         policyDeploySuccessCount.set(0L);
159         policyUndeployCount.set(0L);
160         policyUndeployFailCount.set(0L);
161         policyUndeploySuccessCount.set(0L);
162         policyExecutedCount.set(0L);
163         policyExecutedSuccessCount.set(0L);
164         policyExecutedFailCount.set(0L);
165     }
166
167     public long getPolicyDeployCount() {
168         return policyDeployCount.get();
169     }
170
171     public long getPolicyDeployFailCount() {
172         return policyDeployFailCount.get();
173     }
174
175     public long getPolicyDeploySuccessCount() {
176         return policyDeploySuccessCount.get();
177     }
178
179     public long getPolicyExecutedCount() {
180         return policyExecutedCount.get();
181     }
182
183     public long getPolicyExecutedSuccessCount() {
184         return policyExecutedSuccessCount.get();
185     }
186
187     public long getPolicyExecutedFailCount() {
188         return policyExecutedFailCount.get();
189     }
190
191     public long getPolicyUndeployCount() {
192         return policyUndeployCount.get();
193     }
194
195     public long getPolicyUndeploySuccessCount() {
196         return policyUndeploySuccessCount.get();
197     }
198
199     public long getPolicyUndeployFailCount() {
200         return policyUndeployFailCount.get();
201     }
202 }