Bump upstreams for 2022.09 Chlorine
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / Ipv6DstCodecTest.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.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.NxmOfIpDst;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.IpDstCaseValue;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.aug.nx.match.IpDstCaseValueBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.of.match.ip.dst.grouping.IpDstValuesBuilder;
23 import org.opendaylight.yangtools.yang.common.Uint32;
24
25 public class Ipv6DstCodecTest {
26     private final ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
27     private final Ipv6DstCodec ipv6DstCodec = new Ipv6DstCodec();
28
29     private MatchEntry input;
30
31     private static final int VALUE_LENGTH = 16;
32     private static final int NXM_FIELD_CODE = 20;
33
34     @Test
35     public void serializeTest() {
36         input = createMatchEntry();
37         ipv6DstCodec.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(0, fieldMask & 1);
43         assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
44         assertEquals(1, buffer.readUnsignedInt());
45     }
46
47     @Test
48     public void deserializeTest() {
49         createBuffer(buffer);
50
51         input = ipv6DstCodec.deserialize(buffer);
52
53         final IpDstCaseValue result = (IpDstCaseValue) input.getMatchEntryValue();
54
55         assertEquals(Nxm1Class.VALUE, input.getOxmClass());
56         assertEquals(NxmOfIpDst.VALUE, input.getOxmMatchField());
57         assertEquals(false, input.getHasMask());
58         assertEquals(2, result.getIpDstValues().getValue().intValue());
59     }
60
61     private static MatchEntry createMatchEntry() {
62         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
63         final IpDstCaseValueBuilder caseBuilder = new IpDstCaseValueBuilder();
64         final IpDstValuesBuilder valuesBuilder = new IpDstValuesBuilder();
65
66         matchEntryBuilder.setOxmClass(Nxm1Class.VALUE);
67         matchEntryBuilder.setOxmMatchField(NxmOfIpDst.VALUE);
68         matchEntryBuilder.setHasMask(false);
69
70         valuesBuilder.setValue(Uint32.ONE);
71
72         caseBuilder.setIpDstValues(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_1_CLASS);
79
80         int fieldMask = NXM_FIELD_CODE << 1;
81         message.writeByte(fieldMask);
82         message.writeByte(VALUE_LENGTH);
83         message.writeInt(2);
84     }
85 }