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