Added JSON and XML payloads tabs with RFC 8040 URL
[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.Before;
15 import org.junit.Test;
16 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm1Class;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxCtState;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.ct.state.grouping.CtStateValuesBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.CtStateCaseValue;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.CtStateCaseValueBuilder;
24
25 public class CtStateCodecTest {
26
27     CtStateCodec ctStateCodec;
28     ByteBuf buffer;
29     MatchEntry input;
30
31     private static final int VALUE_LENGTH = 8;
32     private static final int NXM_FIELD_CODE = 105;
33
34     @Before
35     public void setUp() {
36         ctStateCodec = new CtStateCodec();
37         buffer = ByteBufAllocator.DEFAULT.buffer();
38     }
39
40     @Test
41     public void serializeTest() {
42         input = createMatchEntry();
43         ctStateCodec.serialize(input, buffer);
44
45         assertEquals(OxmMatchConstants.NXM_1_CLASS, buffer.readUnsignedShort());
46         short fieldMask = buffer.readUnsignedByte();
47         assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
48         assertEquals(1, fieldMask & 1);
49         assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
50         assertEquals(1, buffer.readUnsignedInt());
51         assertEquals(2, buffer.readUnsignedInt());
52     }
53
54     @Test
55     public void deserializeTest() {
56         createBuffer(buffer);
57         input = ctStateCodec.deserialize(buffer);
58
59         final CtStateCaseValue result = (CtStateCaseValue) input.getMatchEntryValue();
60
61         assertEquals(Nxm1Class.class, input.getOxmClass());
62         assertEquals(NxmNxCtState.class, input.getOxmMatchField());
63         assertEquals(true, input.isHasMask());
64         assertEquals(1, result.getCtStateValues().getCtState().intValue());
65         assertEquals(2, result.getCtStateValues().getMask().intValue());
66     }
67
68     private MatchEntry createMatchEntry() {
69         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
70         final CtStateCaseValueBuilder caseBuilder = new CtStateCaseValueBuilder();
71         final CtStateValuesBuilder valuesBuilder = new CtStateValuesBuilder();
72
73         matchEntryBuilder.setOxmClass(Nxm1Class.class);
74         matchEntryBuilder.setOxmMatchField(NxmNxCtState.class);
75         matchEntryBuilder.setHasMask(true);
76
77         valuesBuilder.setCtState((long)1);
78         valuesBuilder.setMask((long)2);
79
80         caseBuilder.setCtStateValues(valuesBuilder.build());
81         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
82         return matchEntryBuilder.build();
83     }
84
85     private void createBuffer(ByteBuf message) {
86         message.writeShort(OxmMatchConstants.NXM_1_CLASS);
87
88         int fieldMask = NXM_FIELD_CODE << 1;
89         message.writeByte(fieldMask);
90         message.writeByte(VALUE_LENGTH);
91         //CtState = 1
92         message.writeInt(1);
93         //Mask = 2
94         message.writeInt(2);
95     }
96 }