2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.openflowjava.protocol.impl.core;
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.anyShort;
12 import static org.mockito.Mockito.when;
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;
31 * Test to count decoder events (counters US_DECODE_SUCCESS, US_DECODE_FAIL and
32 * US_RECEIVED_IN_OFJAVA have to be enabled).
36 public class OFDecoderStatisticsTest {
38 @Mock ChannelHandlerContext mockChHndlrCtx;
39 @Mock DeserializationFactory mockDeserializationFactory;
40 @Mock DataObject mockDataObject;
42 private OFDecoder ofDecoder;
43 private ByteBuf writeObj;
44 private VersionMessageWrapper inMsg;
45 private List<Object> outList;
46 private StatisticsCounters statCounters;
49 * Sets up test environment Start counting and reset counters before each test.
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);
62 * Stop counting after each test.
65 public void tierDown() {
66 statCounters.stopCounting();
70 * Test decode success counter.
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");
77 if (!statCounters.isCounterEnabled(CounterEventTypes.US_DECODE_FAIL)) {
78 Assert.fail("Counter " + CounterEventTypes.US_DECODE_FAIL + " is not enable");
81 .isCounterEnabled(CounterEventTypes.US_RECEIVED_IN_OFJAVA)) {
82 Assert.fail("Counter " + CounterEventTypes.US_RECEIVED_IN_OFJAVA + " is not enable");
85 when(mockDeserializationFactory.deserialize(any(ByteBuf.class),anyShort())).thenReturn(mockDataObject);
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);
93 Assert.assertEquals("Wrong - bad counter value for OFEncoder encode succesfully ",
94 count,statCounters.getCounter(CounterEventTypes.US_DECODE_SUCCESS).getCounterValue());
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());
103 * Test fail decode counter.
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");
110 if (!statCounters.isCounterEnabled(CounterEventTypes.US_DECODE_FAIL)) {
111 Assert.fail("Counter " + CounterEventTypes.US_DECODE_FAIL + " is not enable");
113 if (!statCounters.isCounterEnabled(CounterEventTypes.US_RECEIVED_IN_OFJAVA)) {
114 Assert.fail("Counter " + CounterEventTypes.US_RECEIVED_IN_OFJAVA + " is not enable");
117 when(mockDeserializationFactory.deserialize(any(ByteBuf.class),anyShort()))
118 .thenThrow(new IllegalArgumentException());
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);
127 "Wrong - bad counter value for OFEncoder encode succesfully ",
128 count, statCounters.getCounter(CounterEventTypes.US_DECODE_FAIL).getCounterValue());
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());