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