OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / OFDecoderStatisticsTest.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.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.anyShort;
12 import static org.mockito.Mockito.when;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.channel.ChannelHandlerContext;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.junit.After;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializationFactory;
25 import org.opendaylight.openflowjava.statistics.CounterEventTypes;
26 import org.opendaylight.openflowjava.statistics.StatisticsCounters;
27 import org.opendaylight.openflowjava.util.ByteBufUtils;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29
30 /**
31  * Test to count decoder events (counters US_DECODE_SUCCESS, US_DECODE_FAIL and
32  * US_RECEIVED_IN_OFJAVA have to be enabled).
33  *
34  * @author madamjak
35  */
36 public class OFDecoderStatisticsTest {
37
38     @Mock ChannelHandlerContext mockChHndlrCtx;
39     @Mock DeserializationFactory mockDeserializationFactory;
40     @Mock DataObject mockDataObject;
41
42     private OFDecoder ofDecoder;
43     private ByteBuf writeObj;
44     private VersionMessageWrapper inMsg;
45     private List<Object> outList;
46     private StatisticsCounters statCounters;
47
48     /**
49      * Sets up test environment Start counting and reset counters before each test.
50      */
51     @Before
52     public void setUp() {
53         MockitoAnnotations.initMocks(this);
54         ofDecoder = new OFDecoder();
55         ofDecoder.setDeserializationFactory(mockDeserializationFactory);
56         outList = new ArrayList<>();
57         statCounters = StatisticsCounters.getInstance();
58         statCounters.startCounting(false, 0);
59     }
60
61     /**
62      * Stop counting after each test.
63      */
64     @After
65     public void tierDown() {
66         statCounters.stopCounting();
67     }
68
69     /**
70      * Test decode success counter.
71      */
72     @Test
73     public void testDecodeSuccesfullCounter() throws Exception {
74         if (!statCounters.isCounterEnabled(CounterEventTypes.US_DECODE_SUCCESS)) {
75             Assert.fail("Counter " + CounterEventTypes.US_DECODE_SUCCESS + " is not enable");
76         }
77         if (!statCounters.isCounterEnabled(CounterEventTypes.US_DECODE_FAIL)) {
78             Assert.fail("Counter " + CounterEventTypes.US_DECODE_FAIL + " is not enable");
79         }
80         if (!statCounters
81                 .isCounterEnabled(CounterEventTypes.US_RECEIVED_IN_OFJAVA)) {
82             Assert.fail("Counter " + CounterEventTypes.US_RECEIVED_IN_OFJAVA + " is not enable");
83         }
84         int count = 4;
85         when(mockDeserializationFactory.deserialize(any(ByteBuf.class),anyShort())).thenReturn(mockDataObject);
86
87         for (int i = 0; i < count; i++) {
88             writeObj = ByteBufUtils.hexStringToByteBuf("16 03 01 00");
89             inMsg = new VersionMessageWrapper((short) 8, writeObj);
90             ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
91         }
92
93         Assert.assertEquals("Wrong - bad counter value for OFEncoder encode succesfully ",
94                 count,statCounters.getCounter(CounterEventTypes.US_DECODE_SUCCESS).getCounterValue());
95         Assert.assertEquals(
96                 "Wrong - different between RECEIVED_IN_OFJAVA and (US_DECODE_SUCCESS + US_DECODE_FAIL)",
97                 statCounters.getCounter(CounterEventTypes.US_RECEIVED_IN_OFJAVA).getCounterValue(),
98                 statCounters.getCounter(CounterEventTypes.US_DECODE_SUCCESS).getCounterValue()
99                 + statCounters.getCounter(CounterEventTypes.US_DECODE_FAIL).getCounterValue());
100     }
101
102     /**
103      * Test fail decode counter.
104      */
105     @Test
106     public void testDecodeFailCounter() throws Exception {
107         if (!statCounters.isCounterEnabled(CounterEventTypes.US_DECODE_SUCCESS)) {
108             Assert.fail("Counter " + CounterEventTypes.US_DECODE_SUCCESS + " is not enable");
109         }
110         if (!statCounters.isCounterEnabled(CounterEventTypes.US_DECODE_FAIL)) {
111             Assert.fail("Counter " + CounterEventTypes.US_DECODE_FAIL + " is not enable");
112         }
113         if (!statCounters.isCounterEnabled(CounterEventTypes.US_RECEIVED_IN_OFJAVA)) {
114             Assert.fail("Counter " + CounterEventTypes.US_RECEIVED_IN_OFJAVA + " is not enable");
115         }
116         int count = 2;
117         when(mockDeserializationFactory.deserialize(any(ByteBuf.class),anyShort()))
118             .thenThrow(new IllegalArgumentException());
119
120         for (int i = 0; i < count; i++) {
121             writeObj = ByteBufUtils.hexStringToByteBuf("16 03 01 00");
122             inMsg = new VersionMessageWrapper((short) 8, writeObj);
123             ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
124         }
125
126         Assert.assertEquals(
127                 "Wrong - bad counter value for OFEncoder encode succesfully ",
128                 count, statCounters.getCounter(CounterEventTypes.US_DECODE_FAIL).getCounterValue());
129         Assert.assertEquals(
130                 "Wrong - different between RECEIVED_IN_OFJAVA and (US_DECODE_SUCCESS + US_DECODE_FAIL)",
131                 statCounters.getCounter(CounterEventTypes.US_RECEIVED_IN_OFJAVA).getCounterValue(),
132                 statCounters.getCounter(CounterEventTypes.US_DECODE_SUCCESS).getCounterValue()
133                 + statCounters.getCounter(CounterEventTypes.US_DECODE_FAIL).getCounterValue());
134     }
135 }