Merge "Rework bit-copying functions and re-enable tests"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / messages / PacketInMessageDeserializerTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.openflowplugin.impl.protocol.deserialization.messages;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.UnpooledByteBufAllocator;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
19 import org.opendaylight.openflowplugin.impl.protocol.deserialization.AbstractDeserializerTest;
20 import org.opendaylight.openflowplugin.impl.util.PacketInUtil;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketInMessage;
22
23 public class PacketInMessageDeserializerTest extends AbstractDeserializerTest {
24     private static final byte PADDING_IN_PACKET_IN_HEADER = 2;
25     private static final short REASON = 0; // no match
26     private static final short TABLE_ID = 13;
27     private static final long FLOW_COOKIE = 12;
28     private static final int XID = 42;
29     private static final int TYPE = 10;
30     private static final int OXM_MATCH_TYPE_CODE = 1;
31     private static final int MPLS_LABEL = 135;
32     private static final byte[] PAYLOAD = new byte[] { 1, 2, 3, 4, 5 };
33
34     private ByteBuf buffer;
35
36     @Override
37     protected void init() {
38         buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
39     }
40
41     @Test
42     public void deserialize() throws Exception {
43         buffer.writeByte(TYPE);
44         buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
45         buffer.writeInt(XID);
46         buffer.writeInt(EncodeConstants.EMPTY_VALUE); // Buffer id - irrelevant
47         buffer.writeShort(EncodeConstants.EMPTY_LENGTH); // Total len - irrelevant
48         buffer.writeByte(REASON);
49         buffer.writeByte(TABLE_ID);
50         buffer.writeLong(FLOW_COOKIE);
51
52         // Match header
53         int matchStartIndex = buffer.writerIndex();
54         buffer.writeShort(OXM_MATCH_TYPE_CODE);
55         int matchLengthIndex = buffer.writerIndex();
56         buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
57
58         // MplsLabel match
59         buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
60         buffer.writeByte(OxmMatchConstants.MPLS_LABEL << 1);
61         buffer.writeByte(EncodeConstants.SIZE_OF_INT_IN_BYTES);
62         buffer.writeInt(MPLS_LABEL);
63
64         // Match footer
65         int matchLength = buffer.writerIndex() - matchStartIndex;
66         buffer.setShort(matchLengthIndex, matchLength);
67         int paddingRemainder = matchLength % EncodeConstants.PADDING;
68         if (paddingRemainder != 0) {
69             buffer.writeZero(EncodeConstants.PADDING - paddingRemainder);
70         }
71
72         buffer.writeZero(PADDING_IN_PACKET_IN_HEADER);
73         buffer.writeBytes(PAYLOAD);
74
75         final PacketInMessage message =
76                 (PacketInMessage) getFactory().deserialize(buffer, EncodeConstants.OF13_VERSION_ID);
77
78         assertEquals(XID, message.getXid().intValue());
79         assertEquals(PacketInUtil.getMdSalPacketInReason(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common
80                 .types.rev130731.PacketInReason
81                 .forValue(REASON)), message.getPacketInReason());
82         assertEquals(TABLE_ID, message.getTableId().getValue().shortValue());
83         assertEquals(FLOW_COOKIE, message.getFlowCookie().getValue().longValue());
84         assertEquals(MPLS_LABEL, message.getMatch().getProtocolMatchFields().getMplsLabel().intValue());
85         assertArrayEquals(PAYLOAD, message.getPayload());
86         assertEquals(buffer.readableBytes(), 0);
87     }
88
89 }