2 * Copyright (c) 2013 Pantheon Technologies s.r.o. 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.SetAsyncInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInputBuilder;
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 SetAsyncInputMessageFactory.
38 * @author timotej.kubas
39 * @author michal.polkorab
41 public class SetAsyncInputMessageFactoryTest {
43 private SerializerRegistry registry;
44 private OFSerializer<SetAsyncInput> setAsyncFactory;
47 * Initializes serializer registry and stores correct factory in field.
50 public void startUp() {
51 registry = new SerializerRegistryImpl();
53 setAsyncFactory = registry.getSerializer(
54 new MessageTypeKey<>(EncodeConstants.OF_VERSION_1_3, SetAsyncInput.class));
58 * Testing of {@link SetAsyncInputMessageFactory} for correct translation from POJO.
61 public void testSetAsyncInputMessage() throws Exception {
62 SetAsyncInputBuilder builder = new SetAsyncInputBuilder();
63 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
64 builder.setPacketInMask(createPacketInMask());
65 builder.setPortStatusMask(createPortStatusMask());
66 builder.setFlowRemovedMask(createFlowRemowedMask());
67 SetAsyncInput message = builder.build();
69 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
70 setAsyncFactory.serialize(message, out);
72 BufferHelper.checkHeaderV13(out,(byte) 28, 32);
73 Assert.assertEquals("Wrong packetInMask", 7, out.readUnsignedInt());
74 Assert.assertEquals("Wrong packetInMask", 0, out.readUnsignedInt());
75 Assert.assertEquals("Wrong portStatusMask", 7, out.readUnsignedInt());
76 Assert.assertEquals("Wrong portStatusMask", 0, out.readUnsignedInt());
77 Assert.assertEquals("Wrong flowRemovedMask", 15, out.readUnsignedInt());
78 Assert.assertEquals("Wrong flowRemovedMask", 0, out.readUnsignedInt());
82 private static List<PacketInMask> createPacketInMask() {
84 // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
85 new PacketInMaskBuilder()
86 .setMask(Set.of(PacketInReason.OFPRNOMATCH, PacketInReason.OFPRACTION, PacketInReason.OFPRINVALIDTTL))
89 new PacketInMaskBuilder().setMask(Set.of()).build());
92 private static List<PortStatusMask> createPortStatusMask() {
94 // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
95 new PortStatusMaskBuilder()
96 .setMask(Set.of(PortReason.OFPPRADD, PortReason.OFPPRDELETE, PortReason.OFPPRMODIFY))
99 new PortStatusMaskBuilder().setMask(Set.of()).build());
102 private static List<FlowRemovedMask> createFlowRemowedMask() {
104 // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
105 new FlowRemovedMaskBuilder()
107 FlowRemovedReason.OFPRRIDLETIMEOUT,
108 FlowRemovedReason.OFPRRHARDTIMEOUT,
109 FlowRemovedReason.OFPRRDELETE,
110 FlowRemovedReason.OFPRRGROUPDELETE))
113 new FlowRemovedMaskBuilder().setMask(Set.of()).build());
117 * Testing of {@link SetAsyncInputMessageFactory} for correct translation from POJO.
120 public void testSetAsyncInputWithNullMasks() throws Exception {
121 SetAsyncInputBuilder builder = new SetAsyncInputBuilder();
122 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
123 builder.setPacketInMask(null);
124 builder.setPortStatusMask(null);
125 builder.setFlowRemovedMask(null);
126 SetAsyncInput message = builder.build();
128 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
129 setAsyncFactory.serialize(message, out);
131 BufferHelper.checkHeaderV13(out,(byte) 28, 8);
132 Assert.assertTrue("Unexpected data", out.readableBytes() == 0);