Merge "Add SwitchCertificate attributes in TLS failure notification"
[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 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.Nxm0Class;
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.NxmOfMetadata;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.oxm.of.metadata.grouping.MetadataValuesBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.OfMetadataCaseValueBuilder;
23
24 public class MetadataCodecTest {
25
26     MetadataCodec metadataCodec;
27     ByteBuf buffer;
28     MatchEntry input;
29
30     private static final int NXM_FIELD_CODE = 2;
31     private static final int VALUE_LENGTH = 8;
32
33     @Before
34     public void setUp() {
35         metadataCodec = new MetadataCodec();
36         buffer = ByteBufAllocator.DEFAULT.buffer();
37     }
38
39     @Test
40     public void serializeTest() {
41         input = createMatchEntry();
42         metadataCodec.serialize(input, buffer);
43
44         assertEquals(OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
45         short fieldMask = buffer.readUnsignedByte();
46         assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
47         assertEquals(0, fieldMask & 1);
48         assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
49     }
50
51     @Test
52     public void deserializeTest() {
53         createBuffer(buffer);
54         input = metadataCodec.deserialize(buffer);
55         assertEquals(Nxm0Class.class, input.getOxmClass());
56         assertEquals(NxmOfMetadata.class, input.getOxmMatchField());
57         assertEquals(false, input.isHasMask());
58     }
59
60     private static MatchEntry createMatchEntry() {
61         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
62         matchEntryBuilder.setOxmClass(Nxm0Class.class);
63         matchEntryBuilder.setOxmMatchField(NxmOfMetadata.class);
64         matchEntryBuilder.setHasMask(false);
65
66         OfMetadataCaseValueBuilder caseBuilder = new OfMetadataCaseValueBuilder();
67         MetadataValuesBuilder valuesBuilder = new MetadataValuesBuilder();
68
69         caseBuilder.setMetadataValues(valuesBuilder.build());
70         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
71         return matchEntryBuilder.build();
72     }
73
74     private static void createBuffer(ByteBuf message) {
75         message.writeShort(OxmMatchConstants.NXM_1_CLASS);
76         int fieldMask = NXM_FIELD_CODE << 1;
77         message.writeByte(fieldMask);
78         message.writeByte(VALUE_LENGTH);
79     }
80 }