604646058516cc26a3fdaa11438bd3d9e8fcb057
[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.ArrayList;
13 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;
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         final List<PacketInMask> masks = new ArrayList<>();
84         PacketInMaskBuilder builder;
85         // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
86         builder = new PacketInMaskBuilder();
87         List<PacketInReason> packetInReasonList = new ArrayList<>();
88         packetInReasonList.add(PacketInReason.OFPRNOMATCH);
89         packetInReasonList.add(PacketInReason.OFPRACTION);
90         packetInReasonList.add(PacketInReason.OFPRINVALIDTTL);
91         builder.setMask(packetInReasonList);
92         masks.add(builder.build());
93         // OFPCR_ROLE_SLAVE
94         builder = new PacketInMaskBuilder();
95         packetInReasonList = new ArrayList<>();
96         builder.setMask(packetInReasonList);
97         masks.add(builder.build());
98         return masks;
99     }
100
101     private static List<PortStatusMask> createPortStatusMask() {
102         final List<PortStatusMask> masks = new ArrayList<>();
103         PortStatusMaskBuilder builder;
104         builder = new PortStatusMaskBuilder();
105         // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
106         List<PortReason> portReasonList = new ArrayList<>();
107         portReasonList.add(PortReason.OFPPRADD);
108         portReasonList.add(PortReason.OFPPRDELETE);
109         portReasonList.add(PortReason.OFPPRMODIFY);
110         builder.setMask(portReasonList);
111         masks.add(builder.build());
112         // OFPCR_ROLE_SLAVE
113         builder = new PortStatusMaskBuilder();
114         portReasonList = new ArrayList<>();
115         builder.setMask(portReasonList);
116         masks.add(builder.build());
117         return masks;
118     }
119
120     private static List<FlowRemovedMask> createFlowRemowedMask() {
121         final List<FlowRemovedMask> masks = new ArrayList<>();
122         FlowRemovedMaskBuilder builder;
123         // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
124         builder = new FlowRemovedMaskBuilder();
125         List<FlowRemovedReason> flowRemovedReasonList = new ArrayList<>();
126         flowRemovedReasonList.add(FlowRemovedReason.OFPRRIDLETIMEOUT);
127         flowRemovedReasonList.add(FlowRemovedReason.OFPRRHARDTIMEOUT);
128         flowRemovedReasonList.add(FlowRemovedReason.OFPRRDELETE);
129         flowRemovedReasonList.add(FlowRemovedReason.OFPRRGROUPDELETE);
130         builder.setMask(flowRemovedReasonList);
131         masks.add(builder.build());
132         // OFPCR_ROLE_SLAVE
133         builder = new FlowRemovedMaskBuilder();
134         flowRemovedReasonList = new ArrayList<>();
135         builder.setMask(flowRemovedReasonList);
136         masks.add(builder.build());
137         return masks;
138     }
139
140     /**
141      * Testing of {@link SetAsyncInputMessageFactory} for correct translation from POJO.
142      */
143     @Test
144     public void testSetAsyncInputWithNullMasks() throws Exception {
145         SetAsyncInputBuilder builder = new SetAsyncInputBuilder();
146         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
147         builder.setPacketInMask(null);
148         builder.setPortStatusMask(null);
149         builder.setFlowRemovedMask(null);
150         SetAsyncInput message = builder.build();
151
152         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
153         setAsyncFactory.serialize(message, out);
154
155         BufferHelper.checkHeaderV13(out,(byte) 28, 8);
156         Assert.assertTrue("Unexpected data", out.readableBytes() == 0);
157     }
158 }