Bump mdsal to 5.0.2
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / PacketInMessageFactoryTest.java
1 /*
2  * Copyright (c) 2015 NetIDE Consortium 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.protocol.impl.serialization.factories;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.UnpooledByteBufAllocator;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
20 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
21 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
22 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
23 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
24 import org.opendaylight.openflowjava.util.ByteBufUtils;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.InPhyPort;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.IpEcn;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmMatchType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPhyPortCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.IpEcnCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.phy.port._case.InPhyPortBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ip.ecn._case.IpEcnBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessageBuilder;
41 import org.opendaylight.yangtools.yang.common.Uint64;
42
43 /**
44  * Unit tests for PacketInMessageFactory.
45  *
46  * @author giuseppex.petralia@intel.com
47  */
48 public class PacketInMessageFactoryTest {
49     private OFSerializer<PacketInMessage> factory;
50     private static final byte MESSAGE_TYPE = 10;
51     private static final byte PADDING = 2;
52
53     @Before
54     public void startUp() {
55         SerializerRegistry registry = new SerializerRegistryImpl();
56         registry.init();
57         factory = registry.getSerializer(new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, PacketInMessage.class));
58     }
59
60     @Test
61     public void testSerialize() throws Exception {
62         PacketInMessageBuilder builder = new PacketInMessageBuilder();
63         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
64         builder.setBufferId(256L);
65         builder.setTotalLen(10);
66         builder.setReason(PacketInReason.forValue(0));
67         builder.setTableId(new TableId(1L));
68         byte[] cookie = new byte[] { (byte) 0xFF, 0x01, 0x04, 0x01, 0x06, 0x00, 0x07, 0x01 };
69         builder.setCookie(new BigInteger(1, cookie));
70         MatchBuilder matchBuilder = new MatchBuilder();
71         matchBuilder.setType(OxmMatchType.class);
72         final List<MatchEntry> entries = new ArrayList<>();
73         MatchEntryBuilder entriesBuilder = new MatchEntryBuilder();
74         entriesBuilder.setOxmClass(OpenflowBasicClass.class);
75         entriesBuilder.setOxmMatchField(InPhyPort.class);
76         entriesBuilder.setHasMask(false);
77         InPhyPortCaseBuilder inPhyPortCaseBuilder = new InPhyPortCaseBuilder();
78         InPhyPortBuilder inPhyPortBuilder = new InPhyPortBuilder();
79         inPhyPortBuilder.setPortNumber(new PortNumber(42L));
80         inPhyPortCaseBuilder.setInPhyPort(inPhyPortBuilder.build());
81         entriesBuilder.setMatchEntryValue(inPhyPortCaseBuilder.build());
82         entries.add(entriesBuilder.build());
83         entriesBuilder.setOxmClass(OpenflowBasicClass.class);
84         entriesBuilder.setOxmMatchField(IpEcn.class);
85         entriesBuilder.setHasMask(false);
86         IpEcnCaseBuilder ipEcnCaseBuilder = new IpEcnCaseBuilder();
87         IpEcnBuilder ipEcnBuilder = new IpEcnBuilder();
88         ipEcnBuilder.setEcn((short) 4);
89         ipEcnCaseBuilder.setIpEcn(ipEcnBuilder.build());
90         entriesBuilder.setMatchEntryValue(ipEcnCaseBuilder.build());
91         entries.add(entriesBuilder.build());
92         matchBuilder.setMatchEntry(entries);
93         builder.setMatch(matchBuilder.build());
94         byte[] data = ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14");
95         builder.setData(data);
96         PacketInMessage message = builder.build();
97
98         ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
99         factory.serialize(message, serializedBuffer);
100         BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 66);
101         Assert.assertEquals("Wrong BufferId", message.getBufferId().longValue(), serializedBuffer.readUnsignedInt());
102         Assert.assertEquals("Wrong actions length", message.getTotalLen().intValue(),
103                 serializedBuffer.readUnsignedShort());
104         Assert.assertEquals("Wrong reason", message.getReason().getIntValue(), serializedBuffer.readUnsignedByte());
105         Assert.assertEquals("Wrong tableId", message.getTableId().getValue().intValue(),
106                 serializedBuffer.readUnsignedByte());
107         cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
108         serializedBuffer.readBytes(cookie);
109         Assert.assertEquals("Wrong cookie", message.getCookie(), Uint64.valueOf(new BigInteger(1, cookie)));
110         Assert.assertEquals("Wrong match type", 1, serializedBuffer.readUnsignedShort());
111         serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
112         Assert.assertEquals("Wrong oxm class", 0x8000, serializedBuffer.readUnsignedShort());
113         short fieldAndMask = serializedBuffer.readUnsignedByte();
114         Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
115         Assert.assertEquals("Wrong oxm field", 1, fieldAndMask >> 1);
116         serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
117         Assert.assertEquals("Wrong oxm value", 42, serializedBuffer.readUnsignedInt());
118         Assert.assertEquals("Wrong oxm class", 0x8000, serializedBuffer.readUnsignedShort());
119         fieldAndMask = serializedBuffer.readUnsignedByte();
120         Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
121         Assert.assertEquals("Wrong oxm field", 9, fieldAndMask >> 1);
122         serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
123         Assert.assertEquals("Wrong oxm value", 4, serializedBuffer.readUnsignedByte());
124         serializedBuffer.skipBytes(7);
125         serializedBuffer.skipBytes(PADDING);
126         byte[] readData = new byte[serializedBuffer.readableBytes()];
127         serializedBuffer.readBytes(readData);
128         Assert.assertArrayEquals("Wrong data", message.getData(), readData);
129     }
130
131 }