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.Matchers.any;
11 import static org.mockito.Matchers.anyShort;
12 import static org.mockito.Mockito.when;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.channel.ChannelHandlerContext;
16 import java.util.ArrayList;
17 import java.util.List;
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.impl.deserialization.DeserializationFactory;
26 import org.opendaylight.openflowjava.statistics.CounterEventTypes;
27 import org.opendaylight.openflowjava.statistics.StatisticsCounters;
28 import org.opendaylight.openflowjava.util.ByteBufUtils;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
32 * Test to count decoder events (counters US_DECODE_SUCCESS, US_DECODE_FAIL and
33 * US_RECEIVED_IN_OFJAVA have to be enabled)
38 public class OFDecoderStatisticsTest {
40 @Mock ChannelHandlerContext mockChHndlrCtx;
41 @Mock DeserializationFactory mockDeserializationFactory;
42 @Mock DataObject mockDataObject;
44 private OFDecoder ofDecoder;
45 private ByteBuf writeObj;
46 private VersionMessageWrapper inMsg;
47 private List<Object> outList;
48 private StatisticsCounters statCounters;
51 * Sets up test environment Start counting and reset counters before each
56 MockitoAnnotations.initMocks(this);
57 ofDecoder = new OFDecoder();
58 ofDecoder.setDeserializationFactory(mockDeserializationFactory);
59 outList = new ArrayList<>();
60 statCounters = StatisticsCounters.getInstance();
61 statCounters.startCounting(false, 0);
65 * Stop counting after each test
68 public void tierDown() {
69 statCounters.stopCounting();
73 * Test decode success counter
76 public void testDecodeSuccesfullCounter() {
77 if (!statCounters.isCounterEnabled(CounterEventTypes.US_DECODE_SUCCESS)) {
78 Assert.fail("Counter " + CounterEventTypes.US_DECODE_SUCCESS + " is not enable");
80 if (!statCounters.isCounterEnabled(CounterEventTypes.US_DECODE_FAIL)) {
81 Assert.fail("Counter " + CounterEventTypes.US_DECODE_FAIL + " is not enable");
84 .isCounterEnabled(CounterEventTypes.US_RECEIVED_IN_OFJAVA)) {
85 Assert.fail("Counter " + CounterEventTypes.US_RECEIVED_IN_OFJAVA + " is not enable");
88 when(mockDeserializationFactory.deserialize(any(ByteBuf.class),anyShort())).thenReturn(mockDataObject);
90 for (int i = 0; i < count; i++) {
91 writeObj = ByteBufUtils.hexStringToByteBuf("16 03 01 00");
92 inMsg = new VersionMessageWrapper((short) 8, writeObj);
93 ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
95 } catch (Exception e) {
98 Assert.assertEquals("Wrong - bad counter value for OFEncoder encode succesfully ",
99 count,statCounters.getCounter(CounterEventTypes.US_DECODE_SUCCESS).getCounterValue());
101 "Wrong - different between RECEIVED_IN_OFJAVA and (US_DECODE_SUCCESS + US_DECODE_FAIL)",
102 statCounters.getCounter(CounterEventTypes.US_RECEIVED_IN_OFJAVA).getCounterValue(),
103 statCounters.getCounter(CounterEventTypes.US_DECODE_SUCCESS).getCounterValue()
104 + statCounters.getCounter(CounterEventTypes.US_DECODE_FAIL).getCounterValue());
108 * Test fail decode counter
111 public void testDecodeFailCounter() {
112 if (!statCounters.isCounterEnabled(CounterEventTypes.US_DECODE_SUCCESS)) {
113 Assert.fail("Counter " + CounterEventTypes.US_DECODE_SUCCESS + " is not enable");
115 if (!statCounters.isCounterEnabled(CounterEventTypes.US_DECODE_FAIL)) {
116 Assert.fail("Counter " + CounterEventTypes.US_DECODE_FAIL + " is not enable");
118 if (!statCounters.isCounterEnabled(CounterEventTypes.US_RECEIVED_IN_OFJAVA)) {
119 Assert.fail("Counter " + CounterEventTypes.US_RECEIVED_IN_OFJAVA + " is not enable");
122 when( mockDeserializationFactory.deserialize(any(ByteBuf.class),anyShort())).thenThrow(new IllegalArgumentException());
124 for (int i = 0; i < count; i++) {
125 writeObj = ByteBufUtils.hexStringToByteBuf("16 03 01 00");
126 inMsg = new VersionMessageWrapper((short) 8, writeObj);
127 ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
129 } catch (Exception e) {
133 "Wrong - bad counter value for OFEncoder encode succesfully ",
134 count, statCounters.getCounter(CounterEventTypes.US_DECODE_FAIL).getCounterValue());
136 "Wrong - different between RECEIVED_IN_OFJAVA and (US_DECODE_SUCCESS + US_DECODE_FAIL)",
137 statCounters.getCounter(CounterEventTypes.US_RECEIVED_IN_OFJAVA).getCounterValue(),
138 statCounters.getCounter(CounterEventTypes.US_DECODE_SUCCESS).getCounterValue()
139 + statCounters.getCounter(CounterEventTypes.US_DECODE_FAIL).getCounterValue());