Final round of isFoo() migration
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / CtZoneCodecTest.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.NxmNxCtZone;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.ct.zone.grouping.CtZoneValuesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.CtZoneCaseValue;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.CtZoneCaseValueBuilder;
23 import org.opendaylight.yangtools.yang.common.Uint16;
24
25 public class CtZoneCodecTest {
26     private final ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer();
27     private final CtZoneCodec ctZoneCodec = new CtZoneCodec();
28
29     private MatchEntry input;
30
31     private static final int VALUE_LENGTH = 2;
32     private static final int NXM_FIELD_CODE = 106;
33
34     @Test
35     public void serializeTest() {
36         input = createMatchEntry();
37         ctZoneCodec.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.readUnsignedShort());
45     }
46
47     @Test
48     public void deserializeTest() {
49         createBuffer(buffer);
50         input = ctZoneCodec.deserialize(buffer);
51
52         final CtZoneCaseValue result = (CtZoneCaseValue) input.getMatchEntryValue();
53
54         assertEquals(Nxm1Class.class, input.getOxmClass());
55         assertEquals(NxmNxCtZone.class, input.getOxmMatchField());
56         assertEquals(false, input.getHasMask());
57         assertEquals(2, result.getCtZoneValues().getCtZone().shortValue());
58     }
59
60     private static MatchEntry createMatchEntry() {
61         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
62         final CtZoneCaseValueBuilder caseBuilder = new CtZoneCaseValueBuilder();
63         final CtZoneValuesBuilder valuesBuilder = new CtZoneValuesBuilder();
64
65         matchEntryBuilder.setOxmClass(Nxm1Class.class);
66         matchEntryBuilder.setOxmMatchField(NxmNxCtZone.class);
67         matchEntryBuilder.setHasMask(false);
68
69         valuesBuilder.setCtZone(Uint16.ONE);
70
71         caseBuilder.setCtZoneValues(valuesBuilder.build());
72         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
73         return matchEntryBuilder.build();
74     }
75
76     private static void createBuffer(final ByteBuf message) {
77         message.writeShort(OxmMatchConstants.NXM_1_CLASS);
78
79         int fieldMask = NXM_FIELD_CODE << 1;
80         message.writeByte(fieldMask);
81         message.writeByte(VALUE_LENGTH);
82         message.writeShort(2);
83     }
84 }