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
9 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.UnpooledByteBufAllocator;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
20 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
21 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
22 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
23 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowRemovedReason;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.FlowRemovedMask;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.FlowRemovedMaskBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PacketInMask;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PacketInMaskBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PortStatusMask;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PortStatusMaskBuilder;
37 * Unit tests for SetAsyncInputMessageFactory.
39 * @author timotej.kubas
40 * @author michal.polkorab
42 public class SetAsyncInputMessageFactoryTest {
44 private SerializerRegistry registry;
45 private OFSerializer<SetAsyncInput> setAsyncFactory;
48 * Initializes serializer registry and stores correct factory in field.
51 public void startUp() {
52 registry = new SerializerRegistryImpl();
54 setAsyncFactory = registry.getSerializer(
55 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, SetAsyncInput.class));
59 * Testing of {@link SetAsyncInputMessageFactory} for correct translation from POJO.
62 public void testSetAsyncInputMessage() throws Exception {
63 SetAsyncInputBuilder builder = new SetAsyncInputBuilder();
64 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
65 builder.setPacketInMask(createPacketInMask());
66 builder.setPortStatusMask(createPortStatusMask());
67 builder.setFlowRemovedMask(createFlowRemowedMask());
68 SetAsyncInput message = builder.build();
70 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
71 setAsyncFactory.serialize(message, out);
73 BufferHelper.checkHeaderV13(out,(byte) 28, 32);
74 Assert.assertEquals("Wrong packetInMask", 7, out.readUnsignedInt());
75 Assert.assertEquals("Wrong packetInMask", 0, out.readUnsignedInt());
76 Assert.assertEquals("Wrong portStatusMask", 7, out.readUnsignedInt());
77 Assert.assertEquals("Wrong portStatusMask", 0, out.readUnsignedInt());
78 Assert.assertEquals("Wrong flowRemovedMask", 15, out.readUnsignedInt());
79 Assert.assertEquals("Wrong flowRemovedMask", 0, out.readUnsignedInt());
83 private static List<PacketInMask> createPacketInMask() {
84 final List<PacketInMask> masks = new ArrayList<>();
85 PacketInMaskBuilder builder;
86 // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
87 builder = new PacketInMaskBuilder();
88 List<PacketInReason> packetInReasonList = new ArrayList<>();
89 packetInReasonList.add(PacketInReason.OFPRNOMATCH);
90 packetInReasonList.add(PacketInReason.OFPRACTION);
91 packetInReasonList.add(PacketInReason.OFPRINVALIDTTL);
92 builder.setMask(packetInReasonList);
93 masks.add(builder.build());
95 builder = new PacketInMaskBuilder();
96 packetInReasonList = new ArrayList<>();
97 builder.setMask(packetInReasonList);
98 masks.add(builder.build());
102 private static List<PortStatusMask> createPortStatusMask() {
103 final List<PortStatusMask> masks = new ArrayList<>();
104 PortStatusMaskBuilder builder;
105 builder = new PortStatusMaskBuilder();
106 // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
107 List<PortReason> portReasonList = new ArrayList<>();
108 portReasonList.add(PortReason.OFPPRADD);
109 portReasonList.add(PortReason.OFPPRDELETE);
110 portReasonList.add(PortReason.OFPPRMODIFY);
111 builder.setMask(portReasonList);
112 masks.add(builder.build());
114 builder = new PortStatusMaskBuilder();
115 portReasonList = new ArrayList<>();
116 builder.setMask(portReasonList);
117 masks.add(builder.build());
121 private static List<FlowRemovedMask> createFlowRemowedMask() {
122 final List<FlowRemovedMask> masks = new ArrayList<>();
123 FlowRemovedMaskBuilder builder;
124 // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
125 builder = new FlowRemovedMaskBuilder();
126 List<FlowRemovedReason> flowRemovedReasonList = new ArrayList<>();
127 flowRemovedReasonList.add(FlowRemovedReason.OFPRRIDLETIMEOUT);
128 flowRemovedReasonList.add(FlowRemovedReason.OFPRRHARDTIMEOUT);
129 flowRemovedReasonList.add(FlowRemovedReason.OFPRRDELETE);
130 flowRemovedReasonList.add(FlowRemovedReason.OFPRRGROUPDELETE);
131 builder.setMask(flowRemovedReasonList);
132 masks.add(builder.build());
134 builder = new FlowRemovedMaskBuilder();
135 flowRemovedReasonList = new ArrayList<>();
136 builder.setMask(flowRemovedReasonList);
137 masks.add(builder.build());
142 * Testing of {@link SetAsyncInputMessageFactory} for correct translation from POJO.
145 public void testSetAsyncInputWithNullMasks() throws Exception {
146 SetAsyncInputBuilder builder = new SetAsyncInputBuilder();
147 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
148 builder.setPacketInMask(null);
149 builder.setPortStatusMask(null);
150 builder.setFlowRemovedMask(null);
151 SetAsyncInput message = builder.build();
153 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
154 setAsyncFactory.serialize(message, out);
156 BufferHelper.checkHeaderV13(out,(byte) 28, 8);
157 Assert.assertTrue("Unexpected data", out.readableBytes() == 0);