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.util.List;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
19 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
21 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
22 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowRemovedReason;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.FlowRemovedMask;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.FlowRemovedMaskBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PacketInMask;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PacketInMaskBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PortStatusMask;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PortStatusMaskBuilder;
36 * Unit tests for GetAsyncReplyMessageFactory.
38 * @author giuseppex.petralia@intel.com
40 public class GetAsyncReplyMessageFactoryTest {
41 private OFSerializer<GetAsyncOutput> factory;
42 private static final byte MESSAGE_TYPE = 27;
45 public void startUp() {
46 SerializerRegistry registry = new SerializerRegistryImpl();
48 factory = registry.getSerializer(new MessageTypeKey<>(EncodeConstants.OF_VERSION_1_3, GetAsyncOutput.class));
52 public void testSerialize() throws Exception {
53 GetAsyncOutputBuilder builder = new GetAsyncOutputBuilder();
54 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
55 builder.setPacketInMask(createPacketInMask());
56 builder.setPortStatusMask(createPortStatusMask());
57 builder.setFlowRemovedMask(createFlowRemowedMask());
58 GetAsyncOutput message = builder.build();
60 ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
61 factory.serialize(message, serializedBuffer);
62 BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 32);
63 Assert.assertEquals("Wrong packetInMask", 7, serializedBuffer.readUnsignedInt());
64 Assert.assertEquals("Wrong packetInMask", 0, serializedBuffer.readUnsignedInt());
65 Assert.assertEquals("Wrong portStatusMask", 7, serializedBuffer.readUnsignedInt());
66 Assert.assertEquals("Wrong portStatusMask", 0, serializedBuffer.readUnsignedInt());
67 Assert.assertEquals("Wrong flowRemovedMask", 15, serializedBuffer.readUnsignedInt());
68 Assert.assertEquals("Wrong flowRemovedMask", 0, serializedBuffer.readUnsignedInt());
71 private static List<PacketInMask> createPacketInMask() {
73 // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
74 new PacketInMaskBuilder()
75 .setMask(Set.of(PacketInReason.OFPRNOMATCH, PacketInReason.OFPRACTION, PacketInReason.OFPRINVALIDTTL))
78 new PacketInMaskBuilder().setMask(Set.of()).build());
81 private static List<PortStatusMask> createPortStatusMask() {
83 // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
84 new PortStatusMaskBuilder()
85 .setMask(Set.of(PortReason.OFPPRADD, PortReason.OFPPRDELETE, PortReason.OFPPRMODIFY))
88 new PortStatusMaskBuilder().setMask(Set.of()).build());
91 private static List<FlowRemovedMask> createFlowRemowedMask() {
93 // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
94 new FlowRemovedMaskBuilder()
96 FlowRemovedReason.OFPRRIDLETIMEOUT,
97 FlowRemovedReason.OFPRRHARDTIMEOUT,
98 FlowRemovedReason.OFPRRDELETE,
99 FlowRemovedReason.OFPRRGROUPDELETE))
102 new FlowRemovedMaskBuilder().setMask(Set.of()).build());
106 public void testSetAsyncInputWithNullMasks() throws Exception {
107 GetAsyncOutputBuilder builder = new GetAsyncOutputBuilder();
108 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
109 builder.setPacketInMask(null);
110 builder.setPortStatusMask(null);
111 builder.setFlowRemovedMask(null);
112 GetAsyncOutput message = builder.build();
113 GetAsyncReplyMessageFactory serializer = new GetAsyncReplyMessageFactory();
114 SerializerRegistry registry = new SerializerRegistryImpl();
116 ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
117 serializer.serialize(message, serializedBuffer);
118 BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 8);
119 Assert.assertTrue("Unexpected data", serializedBuffer.readableBytes() == 0);