Bump MRI upstreams
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / GetAsyncReplyMessageFactoryTest.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.util.List;
13 import java.util.Set;
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;
34
35 /**
36  * Unit tests for GetAsyncReplyMessageFactory.
37  *
38  * @author giuseppex.petralia@intel.com
39  */
40 public class GetAsyncReplyMessageFactoryTest {
41     private OFSerializer<GetAsyncOutput> factory;
42     private static final byte MESSAGE_TYPE = 27;
43
44     @Before
45     public void startUp() {
46         SerializerRegistry registry = new SerializerRegistryImpl();
47         registry.init();
48         factory = registry.getSerializer(new MessageTypeKey<>(EncodeConstants.OF_VERSION_1_3, GetAsyncOutput.class));
49     }
50
51     @Test
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();
59
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());
69     }
70
71     private static List<PacketInMask> createPacketInMask() {
72         return List.of(
73             // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
74             new PacketInMaskBuilder()
75                 .setMask(Set.of(PacketInReason.OFPRNOMATCH, PacketInReason.OFPRACTION, PacketInReason.OFPRINVALIDTTL))
76                 .build(),
77             // OFPCR_ROLE_SLAVE
78             new PacketInMaskBuilder().setMask(Set.of()).build());
79     }
80
81     private static List<PortStatusMask> createPortStatusMask() {
82         return List.of(
83             // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
84             new PortStatusMaskBuilder()
85                 .setMask(Set.of(PortReason.OFPPRADD, PortReason.OFPPRDELETE, PortReason.OFPPRMODIFY))
86                 .build(),
87             // OFPCR_ROLE_SLAVE
88             new PortStatusMaskBuilder().setMask(Set.of()).build());
89     }
90
91     private static List<FlowRemovedMask> createFlowRemowedMask() {
92         return List.of(
93             // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
94             new FlowRemovedMaskBuilder()
95                 .setMask(Set.of(
96                     FlowRemovedReason.OFPRRIDLETIMEOUT,
97                     FlowRemovedReason.OFPRRHARDTIMEOUT,
98                     FlowRemovedReason.OFPRRDELETE,
99                     FlowRemovedReason.OFPRRGROUPDELETE))
100                 .build(),
101             // OFPCR_ROLE_SLAVE
102             new FlowRemovedMaskBuilder().setMask(Set.of()).build());
103     }
104
105     @Test
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();
115         registry.init();
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);
120     }
121 }