Statistics collection - initial change
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / statistics / StatisticsCountersTest.java
1 /*
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. 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.openflowjava.statistics;
9
10 import org.junit.After;
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  * @author madamjak
19  *
20  */
21 public class StatisticsCountersTest {
22
23     private static final Logger LOGGER = LoggerFactory.getLogger(StatisticsCounters.class);
24     private StatisticsCounters statCounters;
25
26     @Before
27     public void initTest(){
28         statCounters = StatisticsCounters.getInstance();
29         statCounters.resetCounters();
30     }
31
32     @After
33     public void tierDown(){
34         statCounters.resetCounters();
35     }
36
37     @Test
38     public void testCounterAll() throws InterruptedException{
39         int testCount = 4;
40         incrementCounter(CounterEventTypes.DS_ENTERED_OFJAVA,testCount);
41         incrementCounter(CounterEventTypes.DS_ENCODE_SUCCESS,testCount);
42         incrementCounter(CounterEventTypes.DS_ENCODE_FAIL,testCount);
43         incrementCounter(CounterEventTypes.US_DECODE_FAIL,testCount);
44         incrementCounter(CounterEventTypes.US_DECODE_SUCCESS,testCount);
45         incrementCounter(CounterEventTypes.US_MESSAGE_PASS,testCount);
46         incrementCounter(CounterEventTypes.US_RECEIVED_IN_OFJAVA,testCount);
47         LOGGER.debug("Waiting to process event queue");
48         Assert.assertEquals("Wrong - bad counter value " + CounterEventTypes.DS_ENTERED_OFJAVA, testCount, statCounters.getCounter(CounterEventTypes.DS_ENTERED_OFJAVA).getCounterValue());
49         Assert.assertEquals("Wrong - bad counter value " + CounterEventTypes.DS_ENCODE_SUCCESS, testCount, statCounters.getCounter(CounterEventTypes.DS_ENCODE_SUCCESS).getCounterValue());
50         Assert.assertEquals("Wrong - bad counter value " + CounterEventTypes.DS_ENCODE_FAIL, testCount, statCounters.getCounter(CounterEventTypes.DS_ENCODE_FAIL).getCounterValue());
51         Assert.assertEquals("Wrong - bad counter value " + CounterEventTypes.US_DECODE_FAIL, testCount, statCounters.getCounter(CounterEventTypes.US_DECODE_FAIL).getCounterValue());
52         Assert.assertEquals("Wrong - bad counter value " + CounterEventTypes.US_DECODE_SUCCESS, testCount, statCounters.getCounter(CounterEventTypes.US_DECODE_SUCCESS).getCounterValue());
53         Assert.assertEquals("Wrong - bad counter value " + CounterEventTypes.US_MESSAGE_PASS, testCount, statCounters.getCounter(CounterEventTypes.US_MESSAGE_PASS).getCounterValue());
54         Assert.assertEquals("Wrong - bad counter value " + CounterEventTypes.US_RECEIVED_IN_OFJAVA, testCount, statCounters.getCounter(CounterEventTypes.US_RECEIVED_IN_OFJAVA).getCounterValue());
55         statCounters.resetCounters();
56         Assert.assertEquals("Wrong - bad counter value after reset " + CounterEventTypes.DS_ENTERED_OFJAVA, 0, statCounters.getCounter(CounterEventTypes.DS_ENTERED_OFJAVA).getCounterValue());
57         Assert.assertEquals("Wrong - bad counter value after reset " + CounterEventTypes.DS_ENCODE_SUCCESS, 0, statCounters.getCounter(CounterEventTypes.DS_ENCODE_SUCCESS).getCounterValue());
58         Assert.assertEquals("Wrong - bad counter value after reset " + CounterEventTypes.DS_ENCODE_FAIL, 0, statCounters.getCounter(CounterEventTypes.DS_ENCODE_FAIL).getCounterValue());
59         Assert.assertEquals("Wrong - bad counter value after reset " + CounterEventTypes.US_DECODE_FAIL, 0, statCounters.getCounter(CounterEventTypes.US_DECODE_FAIL).getCounterValue());
60         Assert.assertEquals("Wrong - bad counter value after reset " + CounterEventTypes.US_DECODE_SUCCESS, 0, statCounters.getCounter(CounterEventTypes.US_DECODE_SUCCESS).getCounterValue());
61         Assert.assertEquals("Wrong - bad counter value after reset " + CounterEventTypes.US_MESSAGE_PASS, 0, statCounters.getCounter(CounterEventTypes.US_MESSAGE_PASS).getCounterValue());
62         Assert.assertEquals("Wrong - bad counter value after reset " + CounterEventTypes.US_RECEIVED_IN_OFJAVA, 0, statCounters.getCounter(CounterEventTypes.US_RECEIVED_IN_OFJAVA).getCounterValue());
63     }
64
65     @Test
66     public void testCounterLastRead() throws InterruptedException{
67         int testCount = 4;
68         incrementCounter(CounterEventTypes.DS_ENTERED_OFJAVA,testCount);
69         LOGGER.debug("Waiting to process event queue");
70         Assert.assertEquals("Wrong - bad last read value.", 0,statCounters.getCounter(CounterEventTypes.DS_ENTERED_OFJAVA).getCounterLastReadValue());
71         Assert.assertEquals("Wrong - bad value", 4,statCounters.getCounter(CounterEventTypes.DS_ENTERED_OFJAVA).getCounterValue(false));
72         Assert.assertEquals("Wrong - bad last read value.", 0,statCounters.getCounter(CounterEventTypes.DS_ENTERED_OFJAVA).getCounterLastReadValue());
73         Assert.assertEquals("Wrong - bad last read value.", 4,statCounters.getCounter(CounterEventTypes.DS_ENTERED_OFJAVA).getCounterValue());
74         Assert.assertEquals("Wrong - bad last read value.", 4,statCounters.getCounter(CounterEventTypes.DS_ENTERED_OFJAVA).getCounterLastReadValue());
75         incrementCounter(CounterEventTypes.DS_ENTERED_OFJAVA,testCount);
76         LOGGER.debug("Waiting to process event queue");
77         Assert.assertEquals("Wrong - bad last read value.", 4,statCounters.getCounter(CounterEventTypes.DS_ENTERED_OFJAVA).getCounterLastReadValue());
78         Assert.assertEquals("Wrong - bad last read value.", 8,statCounters.getCounter(CounterEventTypes.DS_ENTERED_OFJAVA).getCounterValue());
79     }
80
81     private void incrementCounter(CounterEventTypes cet, int count){
82         for(int i = 0; i< count; i++){
83             statCounters.incrementCounter(cet);
84         }
85     }
86 }