Bump upstreams for 2022.09 Chlorine
[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.Uint16;
42 import org.opendaylight.yangtools.yang.common.Uint32;
43 import org.opendaylight.yangtools.yang.common.Uint64;
44 import org.opendaylight.yangtools.yang.common.Uint8;
45
46 /**
47  * Unit tests for PacketInMessageFactory.
48  *
49  * @author giuseppex.petralia@intel.com
50  */
51 public class PacketInMessageFactoryTest {
52     private OFSerializer<PacketInMessage> factory;
53     private static final byte MESSAGE_TYPE = 10;
54     private static final byte PADDING = 2;
55
56     @Before
57     public void startUp() {
58         SerializerRegistry registry = new SerializerRegistryImpl();
59         registry.init();
60         factory = registry.getSerializer(new MessageTypeKey<>(EncodeConstants.OF_VERSION_1_3, PacketInMessage.class));
61     }
62
63     @Test
64     public void testSerialize() throws Exception {
65         PacketInMessageBuilder builder = new PacketInMessageBuilder();
66         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
67         builder.setBufferId(Uint32.valueOf(256));
68         builder.setTotalLen(Uint16.TEN);
69         builder.setReason(PacketInReason.forValue(0));
70         builder.setTableId(new TableId(Uint32.ONE));
71         builder.setCookie(Uint64.valueOf("FF01040106000701", 16));
72         MatchBuilder matchBuilder = new MatchBuilder();
73         matchBuilder.setType(OxmMatchType.VALUE);
74         final List<MatchEntry> entries = new ArrayList<>();
75         MatchEntryBuilder entriesBuilder = new MatchEntryBuilder();
76         entriesBuilder.setOxmClass(OpenflowBasicClass.VALUE);
77         entriesBuilder.setOxmMatchField(InPhyPort.VALUE);
78         entriesBuilder.setHasMask(false);
79         InPhyPortCaseBuilder inPhyPortCaseBuilder = new InPhyPortCaseBuilder();
80         InPhyPortBuilder inPhyPortBuilder = new InPhyPortBuilder();
81         inPhyPortBuilder.setPortNumber(new PortNumber(Uint32.valueOf(42)));
82         inPhyPortCaseBuilder.setInPhyPort(inPhyPortBuilder.build());
83         entriesBuilder.setMatchEntryValue(inPhyPortCaseBuilder.build());
84         entries.add(entriesBuilder.build());
85         entriesBuilder.setOxmClass(OpenflowBasicClass.VALUE);
86         entriesBuilder.setOxmMatchField(IpEcn.VALUE);
87         entriesBuilder.setHasMask(false);
88         IpEcnCaseBuilder ipEcnCaseBuilder = new IpEcnCaseBuilder();
89         IpEcnBuilder ipEcnBuilder = new IpEcnBuilder();
90         ipEcnBuilder.setEcn(Uint8.valueOf(4));
91         ipEcnCaseBuilder.setIpEcn(ipEcnBuilder.build());
92         entriesBuilder.setMatchEntryValue(ipEcnCaseBuilder.build());
93         entries.add(entriesBuilder.build());
94         matchBuilder.setMatchEntry(entries);
95         builder.setMatch(matchBuilder.build());
96         byte[] data = ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14");
97         builder.setData(data);
98         PacketInMessage message = builder.build();
99
100         ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
101         factory.serialize(message, serializedBuffer);
102         BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 66);
103         Assert.assertEquals("Wrong BufferId", message.getBufferId().longValue(), serializedBuffer.readUnsignedInt());
104         Assert.assertEquals("Wrong actions length", message.getTotalLen().intValue(),
105                 serializedBuffer.readUnsignedShort());
106         Assert.assertEquals("Wrong reason", message.getReason().getIntValue(), serializedBuffer.readUnsignedByte());
107         Assert.assertEquals("Wrong tableId", message.getTableId().getValue().intValue(),
108                 serializedBuffer.readUnsignedByte());
109         byte[] cookie = new byte[Long.BYTES];
110         serializedBuffer.readBytes(cookie);
111         Assert.assertEquals("Wrong cookie", message.getCookie(), Uint64.valueOf(new BigInteger(1, cookie)));
112         Assert.assertEquals("Wrong match type", 1, serializedBuffer.readUnsignedShort());
113         serializedBuffer.skipBytes(Short.BYTES);
114         Assert.assertEquals("Wrong oxm class", 0x8000, serializedBuffer.readUnsignedShort());
115         short fieldAndMask = serializedBuffer.readUnsignedByte();
116         Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
117         Assert.assertEquals("Wrong oxm field", 1, fieldAndMask >> 1);
118         serializedBuffer.skipBytes(Byte.BYTES);
119         Assert.assertEquals("Wrong oxm value", 42, serializedBuffer.readUnsignedInt());
120         Assert.assertEquals("Wrong oxm class", 0x8000, serializedBuffer.readUnsignedShort());
121         fieldAndMask = serializedBuffer.readUnsignedByte();
122         Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
123         Assert.assertEquals("Wrong oxm field", 9, fieldAndMask >> 1);
124         serializedBuffer.skipBytes(Byte.BYTES);
125         Assert.assertEquals("Wrong oxm value", 4, serializedBuffer.readUnsignedByte());
126         serializedBuffer.skipBytes(7);
127         serializedBuffer.skipBytes(PADDING);
128         byte[] readData = new byte[serializedBuffer.readableBytes()];
129         serializedBuffer.readBytes(readData);
130         Assert.assertArrayEquals("Wrong data", message.getData(), readData);
131     }
132
133 }