Use ByteBuf.readRetainedSlice()
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / IcmpTypeCodecTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.nx.codec.match;
9
10 import static org.junit.Assert.assertEquals;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.ByteBufAllocator;
14 import org.junit.Test;
15 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm0Class;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmOfIcmpType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.IcmpTypeCaseValue;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.IcmpTypeCaseValueBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.of.match.icmp.type.grouping.IcmpTypeValuesBuilder;
23 import org.opendaylight.yangtools.yang.common.Uint8;
24
25 public class IcmpTypeCodecTest {
26     private final ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
27     private final IcmpTypeCodec icmpTypeCodec = new IcmpTypeCodec();
28
29     private MatchEntry input;
30
31     private static final int VALUE_LENGTH = 1;
32     private static final int NXM_FIELD_CODE = 13;
33
34     @Test
35     public void serializeTest() {
36         input = createMatchEntry();
37         icmpTypeCodec.serialize(input, buffer);
38
39         assertEquals(OxmMatchConstants.NXM_0_CLASS, buffer.readUnsignedShort());
40         short fieldMask = buffer.readUnsignedByte();
41         assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
42         assertEquals(0, fieldMask & 1);
43         assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
44         assertEquals(1, buffer.readUnsignedByte());
45     }
46
47     @Test
48     public void deserializeTest() {
49         createBuffer(buffer);
50
51         input = icmpTypeCodec.deserialize(buffer);
52
53         final IcmpTypeCaseValue result = (IcmpTypeCaseValue) input.getMatchEntryValue();
54
55         assertEquals(Nxm0Class.VALUE, input.getOxmClass());
56         assertEquals(NxmOfIcmpType.VALUE, input.getOxmMatchField());
57         assertEquals(false, input.getHasMask());
58         assertEquals(2, result.getIcmpTypeValues().getValue().shortValue());
59     }
60
61     private static MatchEntry createMatchEntry() {
62         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
63         final IcmpTypeCaseValueBuilder caseBuilder = new IcmpTypeCaseValueBuilder();
64         final IcmpTypeValuesBuilder valuesBuilder = new IcmpTypeValuesBuilder();
65
66         matchEntryBuilder.setOxmClass(Nxm0Class.VALUE);
67         matchEntryBuilder.setOxmMatchField(NxmOfIcmpType.VALUE);
68         matchEntryBuilder.setHasMask(false);
69
70         valuesBuilder.setValue(Uint8.ONE);
71
72         caseBuilder.setIcmpTypeValues(valuesBuilder.build());
73         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
74         return matchEntryBuilder.build();
75     }
76
77     private static void createBuffer(final ByteBuf message) {
78         message.writeShort(OxmMatchConstants.NXM_0_CLASS);
79
80         int fieldMask = NXM_FIELD_CODE << 1;
81         message.writeByte(fieldMask);
82         message.writeByte(VALUE_LENGTH);
83         message.writeByte(2);
84     }
85 }