JConsole support for statistics collection
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / OFEncoderStatisticsTest.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.protocol.impl.core;
9
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Matchers.anyShort;
12 import static org.mockito.Mockito.doThrow;
13 import static org.mockito.Mockito.when;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.channel.ChannelHandlerContext;
16 import io.netty.util.concurrent.Future;
17 import io.netty.util.concurrent.GenericFutureListener;
18
19 import org.junit.After;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
26 import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageListenerWrapper;
27 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
28 import org.opendaylight.openflowjava.statistics.CounterEventTypes;
29 import org.opendaylight.openflowjava.statistics.StatisticsCounters;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33
34 /**
35  * Test counters for encoding (at least DS_ENCODE_SUCCESS, DS_ENCODE_FAIL and DS_FLOW_MODS_SENT counters have to be enabled)
36  * @author madamjak
37  *
38  */
39 public class OFEncoderStatisticsTest {
40
41     @Mock ChannelHandlerContext mockChHndlrCtx ;
42     @Mock SerializationFactory mockSerializationFactory ;
43     @Mock MessageListenerWrapper wrapper;
44     @Mock OfHeader mockMsg ;
45     @Mock ByteBuf mockOut ;
46     @Mock Future<Void> future;
47     @Mock GenericFutureListener<Future<Void>> listener;
48     @Mock FlowModInput mockFlowModInput;
49
50     private StatisticsCounters statCounters;
51     private OFEncoder ofEncoder;
52
53     /**
54      * Initialize tests, start and reset counters before each test
55      */
56     @Before
57     public void initTlest(){
58         MockitoAnnotations.initMocks(this);
59         ofEncoder = new OFEncoder() ;
60         ofEncoder.setSerializationFactory(mockSerializationFactory) ;
61         statCounters = StatisticsCounters.getInstance();
62         statCounters.startCounting(false, 0);
63     }
64
65     /**
66      * Stop counting after each test
67      */
68     @After
69     public void tierDown(){
70         statCounters.stopCounting();
71     }
72
73     /**
74      * Test counting of success encode (counter DS_ENCODE_SUCCESS has to be enabled)
75      */
76     @Test
77     public void testEncodeSuccessCounter() {
78         CounterEventTypes cet = CounterEventTypes.DS_ENCODE_SUCCESS;
79         if(! statCounters.isCounterEnabled(cet)){
80             Assert.fail("Counter " + cet + " is not enabled.");
81         }
82         int count = 4;
83         when(mockOut.readableBytes()).thenReturn(1);
84         when(wrapper.getMsg()).thenReturn(mockMsg);
85         when(wrapper.getMsg().getVersion()).thenReturn((short) EncodeConstants.OF13_VERSION_ID);
86         try {
87             for(int i = 0; i< count; i++){
88                 ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
89             }
90         } catch (Exception e) {
91             Assert.fail();
92         }
93         Assert.assertEquals("Wrong - bad counter value for OFEncoder encode succesfully ", count, statCounters.getCounter(cet).getCounterValue());
94     }
95
96     /**
97      * Test counting of flow-mod sent (counter DS_FLOW_MODS_SENT has to be enabled)
98      */
99     @Test
100     public void testFlowModSentCounter() {
101         CounterEventTypes cet = CounterEventTypes.DS_FLOW_MODS_SENT;
102         if(! statCounters.isCounterEnabled(cet)){
103             Assert.fail("Counter " + cet + " is not enabled.");
104         }
105         int count = 4;
106         when(mockOut.readableBytes()).thenReturn(1);
107         when(wrapper.getMsg()).thenReturn(mockFlowModInput);
108         when(wrapper.getMsg().getVersion()).thenReturn((short) EncodeConstants.OF13_VERSION_ID);
109         try {
110             for(int i = 0; i< count; i++){
111                 ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
112             }
113         } catch (Exception e) {
114             Assert.fail();
115         }
116         Assert.assertEquals("Wrong - bad counter value for OFEncoder flow-mod sent", count, statCounters.getCounter(cet).getCounterValue());
117     }
118     /**
119      * Test counting of encode fail (counter DS_ENCODE_FAIL has to be enabled)
120      */
121
122     @Test
123     public void testEncodeEncodeFailCounter() {
124         CounterEventTypes cet = CounterEventTypes.DS_ENCODE_FAIL;
125         if(! statCounters.isCounterEnabled(cet)){
126             Assert.fail("Counter " + cet + " is not enabled.");
127         }
128         int count = 2;
129         when(wrapper.getMsg()).thenReturn(mockMsg);
130         when(wrapper.getListener()).thenReturn(listener);
131         when(wrapper.getMsg().getVersion()).thenReturn((short) EncodeConstants.OF13_VERSION_ID);
132         doThrow(new IllegalArgumentException()).when(mockSerializationFactory).messageToBuffer(anyShort(),any(ByteBuf.class), any(DataObject.class));
133         try {
134             for(int i = 0; i< count; i++){
135                 ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
136             }
137         } catch (Exception e) {
138             Assert.fail();
139         }
140         Assert.assertEquals("Wrong - bad counter value for OFEncoder fail encode", count, statCounters.getCounter(cet).getCounterValue());
141     }
142 }