2 * Copyright (c) 2015 NetIDE Consortium and others. All rights reserved.
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
8 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
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;
43 * Unit tests for PacketInMessageFactory.
45 * @author giuseppex.petralia@intel.com
47 public class PacketInMessageFactoryTest {
48 private OFSerializer<PacketInMessage> factory;
49 private static final byte MESSAGE_TYPE = 10;
50 private static final byte PADDING = 2;
53 public void startUp() {
54 SerializerRegistry registry = new SerializerRegistryImpl();
56 factory = registry.getSerializer(new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, PacketInMessage.class));
60 public void testSerialize() throws Exception {
61 PacketInMessageBuilder builder = new PacketInMessageBuilder();
62 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
63 builder.setBufferId(256L);
64 builder.setTotalLen(10);
65 builder.setReason(PacketInReason.forValue(0));
66 builder.setTableId(new TableId(1L));
67 byte[] cookie = new byte[] { (byte) 0xFF, 0x01, 0x04, 0x01, 0x06, 0x00, 0x07, 0x01 };
68 builder.setCookie(new BigInteger(1, cookie));
69 MatchBuilder matchBuilder = new MatchBuilder();
70 matchBuilder.setType(OxmMatchType.class);
71 final List<MatchEntry> entries = new ArrayList<>();
72 MatchEntryBuilder entriesBuilder = new MatchEntryBuilder();
73 entriesBuilder.setOxmClass(OpenflowBasicClass.class);
74 entriesBuilder.setOxmMatchField(InPhyPort.class);
75 entriesBuilder.setHasMask(false);
76 InPhyPortCaseBuilder inPhyPortCaseBuilder = new InPhyPortCaseBuilder();
77 InPhyPortBuilder inPhyPortBuilder = new InPhyPortBuilder();
78 inPhyPortBuilder.setPortNumber(new PortNumber(42L));
79 inPhyPortCaseBuilder.setInPhyPort(inPhyPortBuilder.build());
80 entriesBuilder.setMatchEntryValue(inPhyPortCaseBuilder.build());
81 entries.add(entriesBuilder.build());
82 entriesBuilder.setOxmClass(OpenflowBasicClass.class);
83 entriesBuilder.setOxmMatchField(IpEcn.class);
84 entriesBuilder.setHasMask(false);
85 IpEcnCaseBuilder ipEcnCaseBuilder = new IpEcnCaseBuilder();
86 IpEcnBuilder ipEcnBuilder = new IpEcnBuilder();
87 ipEcnBuilder.setEcn((short) 4);
88 ipEcnCaseBuilder.setIpEcn(ipEcnBuilder.build());
89 entriesBuilder.setMatchEntryValue(ipEcnCaseBuilder.build());
90 entries.add(entriesBuilder.build());
91 matchBuilder.setMatchEntry(entries);
92 builder.setMatch(matchBuilder.build());
93 byte[] data = ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14");
94 builder.setData(data);
95 PacketInMessage message = builder.build();
97 ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
98 factory.serialize(message, serializedBuffer);
99 BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 66);
100 Assert.assertEquals("Wrong BufferId", message.getBufferId().longValue(), serializedBuffer.readUnsignedInt());
101 Assert.assertEquals("Wrong actions length", message.getTotalLen().intValue(),
102 serializedBuffer.readUnsignedShort());
103 Assert.assertEquals("Wrong reason", message.getReason().getIntValue(), serializedBuffer.readUnsignedByte());
104 Assert.assertEquals("Wrong tableId", message.getTableId().getValue().intValue(),
105 serializedBuffer.readUnsignedByte());
106 cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
107 serializedBuffer.readBytes(cookie);
108 Assert.assertEquals("Wrong cookie", message.getCookie(), new BigInteger(1, cookie));
109 Assert.assertEquals("Wrong match type", 1, serializedBuffer.readUnsignedShort());
110 serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
111 Assert.assertEquals("Wrong oxm class", 0x8000, serializedBuffer.readUnsignedShort());
112 short fieldAndMask = serializedBuffer.readUnsignedByte();
113 Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
114 Assert.assertEquals("Wrong oxm field", 1, fieldAndMask >> 1);
115 serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
116 Assert.assertEquals("Wrong oxm value", 42, serializedBuffer.readUnsignedInt());
117 Assert.assertEquals("Wrong oxm class", 0x8000, serializedBuffer.readUnsignedShort());
118 fieldAndMask = serializedBuffer.readUnsignedByte();
119 Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
120 Assert.assertEquals("Wrong oxm field", 9, fieldAndMask >> 1);
121 serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
122 Assert.assertEquals("Wrong oxm value", 4, serializedBuffer.readUnsignedByte());
123 serializedBuffer.skipBytes(7);
124 serializedBuffer.skipBytes(PADDING);
125 byte[] readData = new byte[serializedBuffer.readableBytes()];
126 serializedBuffer.readBytes(readData);
127 Assert.assertArrayEquals("Wrong data", message.getData(), readData);