[POLICY-122] Policy GUI Fixes
[policy/engine.git] / ECOMP-PDP-REST / src / main / java / org / openecomp / policy / pdp / rest / jmx / PdpRestMonitor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PDP-REST
4  * ================================================================================
5  * Copyright (C) 2017 AT&T 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  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.policy.pdp.rest.jmx;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.concurrent.atomic.AtomicLong;
26
27 import org.openecomp.policy.xacml.util.MetricsUtil.AvgLatency;
28 import org.openecomp.policy.xacml.util.MetricsUtil.MaxLatency;
29 import org.openecomp.policy.xacml.util.MetricsUtil.MinLatency;
30
31 public class PdpRestMonitor implements PdpRestMonitorMBean {    
32         private static PdpRestMonitor singleton = new PdpRestMonitor();
33         
34         private final AtomicLong pdpEvaluationAttempts = new AtomicLong();
35         private final AtomicLong pdpEvaluationSuccesses = new AtomicLong();
36         private final AtomicLong pdpEvaluationNA = new AtomicLong();
37         private final AtomicLong pdpEvaluationPermit = new AtomicLong();
38         private final AtomicLong pdpEvaluationDeny = new AtomicLong();
39         private final Map<String, Integer> policyCount = new HashMap<>();
40         
41         private final MinLatency pdpEngineDecisionMinLatency = new MinLatency();
42         private final MaxLatency pdpEngineDecisionMaxLatency = new MaxLatency();
43         private final AvgLatency pdpEngineDecisionAvgLatency =  new AvgLatency();
44         
45         private volatile long lastDecisionLatency = 0;
46         
47         @Override
48         public long getPdpEvaluationAttempts() {
49                 return pdpEvaluationAttempts.longValue();
50         }
51         @Override
52         public long getPdpEvaluationPermit() {
53                 return pdpEvaluationPermit.longValue();
54         }
55         @Override
56         public long getPdpEvaluationDeny() {
57                 return pdpEvaluationDeny.longValue();
58         }
59         @Override
60         public long getPdpEvaluationSuccesses() {
61                 return pdpEvaluationSuccesses.longValue();
62         }
63
64         @Override
65         public long getpdpEvaluationNA() {
66                 return pdpEvaluationNA.longValue();
67         }
68         @Override
69         public long getLastDecisionLatency() {
70                 return lastDecisionLatency;
71         }
72         
73         /**
74          * @return the pdpEngineDecisionMinLatency
75          */
76         @Override
77         public long getPdpEngineDecisionMinLatency() {
78                 return pdpEngineDecisionMinLatency.min();
79         }
80
81         /**
82          * @return the pdpEngineDecisionMaxLatency
83          */
84         @Override
85         public long getPdpEngineDecisionMaxLatency() {
86                 return pdpEngineDecisionMaxLatency.max();
87         }
88
89         /**
90          * @return the pdpEngineDecisionAvgLatency
91          */
92         @Override
93         public long getPdpEngineDecisionAvgLatency() {
94                 return pdpEngineDecisionAvgLatency.avg();
95         }
96
97         @Override
98         public synchronized void resetLatency() {
99                 this.lastDecisionLatency = 0;
100                 this.pdpEngineDecisionMinLatency.reset();
101                 this.pdpEngineDecisionMaxLatency.reset();
102                 this.pdpEngineDecisionAvgLatency.reset();
103         }
104         
105         @Override
106         public synchronized void resetCounters() {
107                 this.pdpEvaluationAttempts.set(0);
108                 this.pdpEvaluationSuccesses.set(0);
109                 this.pdpEvaluationNA.set(0);
110                 this.policyCount.clear();
111         }
112         
113         public void pdpEvaluationAttempts() {
114                 pdpEvaluationAttempts.incrementAndGet();
115         }
116         
117         public void pdpEvaluationSuccess() {
118                 pdpEvaluationSuccesses.incrementAndGet();               
119         }
120         
121         public void pdpEvaluationNA(){
122                 pdpEvaluationNA.incrementAndGet();
123         }
124         public void pdpEvaluationPermit(){
125                 pdpEvaluationPermit.incrementAndGet();
126         }
127         public void pdpEvaluationDeny(){
128                 pdpEvaluationDeny.incrementAndGet();
129         }
130         
131         public synchronized void computeLatency(long latency) {
132                 this.lastDecisionLatency = latency;
133                 this.pdpEngineDecisionMinLatency.compute(latency);
134                 this.pdpEngineDecisionMaxLatency.compute(latency);
135                 this.pdpEngineDecisionAvgLatency.compute(latency);
136         }
137
138         public void policyCountAdd(String policyID, Integer count){
139                 int countValue = count;
140                 if (policyCount.containsKey(policyID)){
141                         countValue = countValue + policyCount.get(policyID);            
142                 }
143                 policyCount.put(policyID, countValue);
144         }
145         public Map<String, Integer> getpolicyMap() {
146                 return policyCount;
147         }
148         public Integer getpolicyCount(String policyID) {
149                 if (policyCount.containsKey(policyID)){
150                         return policyCount.get(policyID);
151                 }
152                 return null;
153         }
154         
155         public static PdpRestMonitor getSingleton() {
156                 return singleton;
157         }
158         public static void setSingleton(PdpRestMonitor singleton) {
159                 PdpRestMonitor.singleton = singleton;
160         }
161
162 }