Improve ConcurrentLispSouthboundStats
[lispflowmapping.git] / mappingservice / southbound / src / main / java / org / opendaylight / lispflowmapping / southbound / ConcurrentLispSouthboundStats.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.lispflowmapping.southbound;
9
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
11
12 /**
13  * Object to hold statistics about LISP southbound events.
14  *
15  * @author Lorand Jakab
16  *
17  */
18 public class ConcurrentLispSouthboundStats {
19     public static final int MAX_LISP_TYPES = getMaxMessageTypeValue();
20
21     private long[] rx = new long[MAX_LISP_TYPES + 1];
22     private long[] tx = new long[MAX_LISP_TYPES + 1];
23     private long rxUnknown = 0;
24     private long txErrors = 0;
25     private long cacheHits = 0;
26     private long cacheMisses = 0;
27
28     public ConcurrentLispSouthboundStats() {
29         resetStats();
30     }
31
32     public synchronized void resetStats() {
33         for (int i = 0; i <= MAX_LISP_TYPES; i++) {
34             rx[i] = 0;
35             tx[i] = 0;
36         }
37     }
38
39     public synchronized long[] getRx() {
40         return rx;
41     }
42
43     public synchronized void incrementRx(int type) {
44         this.rx[type] = incrementWithWrap(rx[type]);
45     }
46
47     public synchronized long[] getTx() {
48         return tx;
49     }
50
51     public synchronized void incrementTx(int type) {
52         this.tx[type] = incrementWithWrap(tx[type]);
53     }
54
55     public synchronized long getRxUnknown() {
56         return rxUnknown;
57     }
58
59     public synchronized void incrementRxUnknown() {
60         this.rxUnknown = incrementWithWrap(rxUnknown);
61     }
62
63     public synchronized long getTxErrors() {
64         return txErrors;
65     }
66
67     public synchronized void incrementTxErrors() {
68         this.txErrors = incrementWithWrap(txErrors);
69     }
70
71     public synchronized long getCacheHits() {
72         return cacheHits;
73     }
74
75     public synchronized void incrementCacheHits() {
76         this.cacheHits = incrementWithWrap(cacheHits);
77     }
78
79     public synchronized long getCacheMisses() {
80         return cacheMisses;
81     }
82
83     public synchronized void incrementCacheMisses() {
84         this.cacheMisses = incrementWithWrap(cacheMisses);
85     }
86
87     private static long incrementWithWrap(long value) {
88         return value == Long.MAX_VALUE ? 0 : value + 1;
89     }
90
91     // TODO move this method to the appropriate helper class if we start using MessageType in other places
92     public static synchronized int getMaxMessageTypeValue() {
93         int max = 0;
94         for (MessageType mt : MessageType.values()) {
95             if (mt.getIntValue() > max) {
96                 max = mt.getIntValue();
97             }
98         }
99         return max;
100     }
101 }