Merge "Bug 8882 - With conntrack SNAT communication with PNF fails "
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / MetadataCodecTest.java
1 /*
2  * Copyright (c) 2017 Red Hat, 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
9 package org.opendaylight.openflowjava.nx.codec.match;
10
11 import static org.junit.Assert.assertEquals;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufAllocator;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm0Class;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmOfMetadata;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.oxm.of.metadata.grouping.MetadataValuesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.OfMetadataCaseValueBuilder;
24
25 public class MetadataCodecTest {
26
27     MetadataCodec metadataCodec;
28     ByteBuf buffer;
29     MatchEntry input;
30
31     private static final int NXM_FIELD_CODE = 2;
32     private static final int VALUE_LENGTH = 8;
33
34     @Before
35     public void setUp() {
36         metadataCodec = new MetadataCodec();
37         buffer = ByteBufAllocator.DEFAULT.buffer();
38     }
39
40     @Test
41     public void serializeTest() {
42         input = createMatchEntry();
43         metadataCodec.serialize(input, buffer);
44
45         assertEquals(OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
46         short fieldMask = buffer.readUnsignedByte();
47         assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
48         assertEquals(0, fieldMask & 1);
49         assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
50     }
51
52     @Test
53     public void deserializeTest() {
54         createBuffer(buffer);
55         input = metadataCodec.deserialize(buffer);
56         assertEquals(Nxm0Class.class, input.getOxmClass());
57         assertEquals(NxmOfMetadata.class, input.getOxmMatchField());
58         assertEquals(false, input.isHasMask());
59     }
60
61     private MatchEntry createMatchEntry() {
62         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
63         matchEntryBuilder.setOxmClass(Nxm0Class.class);
64         matchEntryBuilder.setOxmMatchField(NxmOfMetadata.class);
65         matchEntryBuilder.setHasMask(false);
66
67         OfMetadataCaseValueBuilder caseBuilder = new OfMetadataCaseValueBuilder();
68         MetadataValuesBuilder valuesBuilder = new MetadataValuesBuilder();
69
70         caseBuilder.setMetadataValues(valuesBuilder.build());
71         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
72         return matchEntryBuilder.build();
73     }
74
75     private void createBuffer(ByteBuf message) {
76         message.writeShort(OxmMatchConstants.NXM_1_CLASS);
77         int fieldMask = (NXM_FIELD_CODE << 1);
78         message.writeByte(fieldMask);
79         message.writeByte(VALUE_LENGTH);
80     }
81
82 }