Bump MRI upstreams
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / SetAsyncInputMessageFactoryTest.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. 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.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;
34
35 /**
36  * Unit tests for SetAsyncInputMessageFactory.
37  *
38  * @author timotej.kubas
39  * @author michal.polkorab
40  */
41 public class SetAsyncInputMessageFactoryTest {
42
43     private SerializerRegistry registry;
44     private OFSerializer<SetAsyncInput> setAsyncFactory;
45
46     /**
47      * Initializes serializer registry and stores correct factory in field.
48      */
49     @Before
50     public void startUp() {
51         registry = new SerializerRegistryImpl();
52         registry.init();
53         setAsyncFactory = registry.getSerializer(
54                 new MessageTypeKey<>(EncodeConstants.OF_VERSION_1_3, SetAsyncInput.class));
55     }
56
57     /**
58      * Testing of {@link SetAsyncInputMessageFactory} for correct translation from POJO.
59      */
60     @Test
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();
68
69         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
70         setAsyncFactory.serialize(message, out);
71
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());
79
80     }
81
82     private static List<PacketInMask> createPacketInMask() {
83         return List.of(
84             // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
85             new PacketInMaskBuilder()
86                 .setMask(Set.of(PacketInReason.OFPRNOMATCH, PacketInReason.OFPRACTION, PacketInReason.OFPRINVALIDTTL))
87                 .build(),
88             // OFPCR_ROLE_SLAVE
89             new PacketInMaskBuilder().setMask(Set.of()).build());
90     }
91
92     private static List<PortStatusMask> createPortStatusMask() {
93         return List.of(
94             // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
95             new PortStatusMaskBuilder()
96                 .setMask(Set.of(PortReason.OFPPRADD, PortReason.OFPPRDELETE, PortReason.OFPPRMODIFY))
97                 .build(),
98             // OFPCR_ROLE_SLAVE
99             new PortStatusMaskBuilder().setMask(Set.of()).build());
100     }
101
102     private static List<FlowRemovedMask> createFlowRemowedMask() {
103         return List.of(
104             // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
105             new FlowRemovedMaskBuilder()
106                 .setMask(Set.of(
107                     FlowRemovedReason.OFPRRIDLETIMEOUT,
108                     FlowRemovedReason.OFPRRHARDTIMEOUT,
109                     FlowRemovedReason.OFPRRDELETE,
110                     FlowRemovedReason.OFPRRGROUPDELETE))
111                 .build(),
112             // OFPCR_ROLE_SLAVE
113             new FlowRemovedMaskBuilder().setMask(Set.of()).build());
114     }
115
116     /**
117      * Testing of {@link SetAsyncInputMessageFactory} for correct translation from POJO.
118      */
119     @Test
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();
127
128         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
129         setAsyncFactory.serialize(message, out);
130
131         BufferHelper.checkHeaderV13(out,(byte) 28, 8);
132         Assert.assertTrue("Unexpected data", out.readableBytes() == 0);
133     }
134 }