Final round of isFoo() migration
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / CtStateCodecTest.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.Nxm1Class;
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.NxmNxCtState;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.ct.state.grouping.CtStateValuesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.CtStateCaseValue;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.CtStateCaseValueBuilder;
23 import org.opendaylight.yangtools.yang.common.Uint32;
24
25 public class CtStateCodecTest {
26     private final ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
27     private final CtStateCodec ctStateCodec = new CtStateCodec();
28
29     private MatchEntry input;
30
31     private static final int VALUE_LENGTH = 8;
32     private static final int NXM_FIELD_CODE = 105;
33
34     @Test
35     public void serializeTest() {
36         input = createMatchEntry();
37         ctStateCodec.serialize(input, buffer);
38
39         assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort());
40         short fieldMask = buffer.readUnsignedByte();
41         assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
42         assertEquals(1, fieldMask & 1);
43         assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
44         assertEquals(1, buffer.readUnsignedInt());
45         assertEquals(2, buffer.readUnsignedInt());
46     }
47
48     @Test
49     public void deserializeTest() {
50         createBuffer(buffer);
51         input = ctStateCodec.deserialize(buffer);
52
53         final CtStateCaseValue result = (CtStateCaseValue) input.getMatchEntryValue();
54
55         assertEquals(Nxm1Class.class, input.getOxmClass());
56         assertEquals(NxmNxCtState.class, input.getOxmMatchField());
57         assertEquals(true, input.getHasMask());
58         assertEquals(1, result.getCtStateValues().getCtState().intValue());
59         assertEquals(2, result.getCtStateValues().getMask().intValue());
60     }
61
62     private static MatchEntry createMatchEntry() {
63         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
64         final CtStateCaseValueBuilder caseBuilder = new CtStateCaseValueBuilder();
65         final CtStateValuesBuilder valuesBuilder = new CtStateValuesBuilder();
66
67         matchEntryBuilder.setOxmClass(Nxm1Class.class);
68         matchEntryBuilder.setOxmMatchField(NxmNxCtState.class);
69         matchEntryBuilder.setHasMask(true);
70
71         valuesBuilder.setCtState(Uint32.ONE);
72         valuesBuilder.setMask(Uint32.TWO);
73
74         caseBuilder.setCtStateValues(valuesBuilder.build());
75         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
76         return matchEntryBuilder.build();
77     }
78
79     private static void createBuffer(final ByteBuf message) {
80         message.writeShort(OxmMatchConstants.NXM_1_CLASS);
81
82         int fieldMask = NXM_FIELD_CODE << 1;
83         message.writeByte(fieldMask);
84         message.writeByte(VALUE_LENGTH);
85         //CtState = 1
86         message.writeInt(1);
87         //Mask = 2
88         message.writeInt(2);
89     }
90 }