7e070d700242961027663a8bbff42a79ed574de2
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 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.clamp.controlloop.runtime.supervision;
22
23 import java.time.Instant;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Set;
28 import lombok.Getter;
29 import lombok.Setter;
30
31 public class HandleCounter<K> {
32     @Getter
33     @Setter
34     private long maxWaitMs;
35
36     @Getter
37     @Setter
38     private int maxRetryCount;
39
40     private Map<K, Integer> mapCounter = new HashMap<>();
41     private Set<K> mapFault = new HashSet<>();
42     private Map<K, Long> mapTimer = new HashMap<>();
43
44     public long getDuration(K id) {
45         mapTimer.putIfAbsent(id, getEpochMilli());
46         return getEpochMilli() - mapTimer.get(id);
47     }
48
49     /**
50      * Reset timer and clear counter and fault by id.
51      *
52      * @param id the id
53      */
54     public void clear(K id) {
55         mapFault.remove(id);
56         mapCounter.put(id, 0);
57         mapTimer.put(id, getEpochMilli());
58     }
59
60     /**
61      * Remove counter, timer and fault by id.
62      *
63      * @param id the id
64      */
65     public void remove(K id) {
66         mapFault.remove(id);
67         mapCounter.remove(id);
68         mapTimer.remove(id);
69     }
70
71     public void setFault(K id) {
72         mapCounter.put(id, 0);
73         mapFault.add(id);
74     }
75
76     /**
77      * Increment RetryCount by id e return true if minor or equal of maxRetryCount.
78      *
79      * @param id the identifier
80      * @return false if count is major of maxRetryCount
81      */
82     public boolean count(K id) {
83         int counter = mapCounter.getOrDefault(id, 0) + 1;
84         if (counter <= maxRetryCount) {
85             mapCounter.put(id, counter);
86             return true;
87         }
88         return false;
89     }
90
91     public boolean isFault(K id) {
92         return mapFault.contains(id);
93     }
94
95     public int getCounter(K id) {
96         return mapCounter.getOrDefault(id, 0);
97     }
98
99     protected long getEpochMilli() {
100         return Instant.now().toEpochMilli();
101     }
102
103     public Set<K> keySet() {
104         return mapCounter.keySet();
105     }
106 }