Upgrade ietf-{inet,yang}-types to 2013-07-15
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / match / IpSrcCodecTest.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
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.NxmOfIpSrc;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.of.match.ip.src.grouping.IpSrcValuesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.IpSrcCaseValue;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.IpSrcCaseValueBuilder;
25
26 public class IpSrcCodecTest {
27
28     IpSrcCodec ipSrcCodec;
29     ByteBuf buffer;
30     MatchEntry input;
31
32     private static final int VALUE_LENGTH = 4;
33     private static final int NXM_FIELD_CODE = 7;
34
35     @Before
36     public void setUp() {
37         ipSrcCodec = new IpSrcCodec();
38         buffer = ByteBufAllocator.DEFAULT.buffer();
39     }
40
41     @Test
42     public void serializeTest() {
43         input = createMatchEntry();
44         ipSrcCodec.serialize(input, buffer);
45
46         assertEquals(OxmMatchConstants.NXM_0_CLASS, buffer.readUnsignedShort());
47         short fieldMask = buffer.readUnsignedByte();
48         assertEquals(NXM_FIELD_CODE, fieldMask >> 1);
49         assertEquals(0, fieldMask & 1);
50         assertEquals(VALUE_LENGTH, buffer.readUnsignedByte());
51         assertEquals(1, buffer.readUnsignedInt());
52     }
53
54     @Test
55     public void deserializeTest() {
56         createBuffer(buffer);
57
58         input = ipSrcCodec.deserialize(buffer);
59
60         IpSrcCaseValue result = ((IpSrcCaseValue) input.getMatchEntryValue());
61
62         assertEquals(Nxm0Class.class, input.getOxmClass());
63         assertEquals(NxmOfIpSrc.class, input.getOxmMatchField());
64         assertEquals(false, input.isHasMask());
65         assertEquals(2, result.getIpSrcValues().getValue().intValue());
66     }
67
68     private MatchEntry createMatchEntry() {
69         MatchEntryBuilder matchEntryBuilder = new MatchEntryBuilder();
70         IpSrcCaseValueBuilder caseBuilder = new IpSrcCaseValueBuilder();
71         IpSrcValuesBuilder valuesBuilder = new IpSrcValuesBuilder();
72
73         matchEntryBuilder.setOxmClass(Nxm0Class.class);
74         matchEntryBuilder.setOxmMatchField(NxmOfIpSrc.class);
75         matchEntryBuilder.setHasMask(false);
76
77         valuesBuilder.setValue((long)1);
78
79         caseBuilder.setIpSrcValues(valuesBuilder.build());
80         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
81         return matchEntryBuilder.build();
82     }
83
84     private void createBuffer(ByteBuf message) {
85         message.writeShort(OxmMatchConstants.NXM_0_CLASS);
86
87         int fieldMask = (NXM_FIELD_CODE << 1);
88         message.writeByte(fieldMask);
89         message.writeByte(VALUE_LENGTH);
90         message.writeInt(2);
91     }
92
93 }