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;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;
21 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
22 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
23 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
24 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
25 import org.opendaylight.openflowjava.util.ByteBufUtils;
26 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowRemovedReason;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.FlowRemovedMask;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.FlowRemovedMaskBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PacketInMask;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PacketInMaskBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PortStatusMask;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PortStatusMaskBuilder;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * @author timotej.kubas
43 * @author michal.polkorab
45 public class SetAsyncInputMessageFactoryTest {
46 private static final Logger LOGGER = LoggerFactory
47 .getLogger(SetAsyncInputMessageFactoryTest.class);
48 private SerializerRegistry registry;
49 private OFSerializer<SetAsyncInput> setAsyncFactory;
52 * Initializes serializer registry and stores correct factory in field
55 public void startUp() {
56 registry = new SerializerRegistryImpl();
58 setAsyncFactory = registry.getSerializer(
59 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, SetAsyncInput.class));
64 * Testing of {@link SetAsyncInputMessageFactory} for correct translation from POJO
67 public void testSetAsyncInputMessage() throws Exception {
68 SetAsyncInputBuilder builder = new SetAsyncInputBuilder();
69 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
70 builder.setPacketInMask(createPacketInMask());
71 builder.setPortStatusMask(createPortStatusMask());
72 builder.setFlowRemovedMask(createFlowRemowedMask());
73 SetAsyncInput message = builder.build();
75 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
76 setAsyncFactory.serialize(message, out);
77 LOGGER.debug("<< " + ByteBufUtils.byteBufToHexString(out));
78 BufferHelper.checkHeaderV13(out,(byte) 28, 32);
79 Assert.assertEquals("Wrong packetInMask", 5, out.readUnsignedInt());
80 Assert.assertEquals("Wrong packetInMask", 7, out.readUnsignedInt());
81 Assert.assertEquals("Wrong portStatusMask", 6, out.readUnsignedInt());
82 Assert.assertEquals("Wrong portStatusMask", 0, out.readUnsignedInt());
83 Assert.assertEquals("Wrong flowRemovedMask", 10, out.readUnsignedInt());
84 Assert.assertEquals("Wrong flowRemovedMask", 5, out.readUnsignedInt());
88 private static List<PacketInMask> createPacketInMask() {
89 List<PacketInMask> masks = new ArrayList<>();
90 PacketInMaskBuilder builder;
91 // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
92 builder = new PacketInMaskBuilder();
93 List<PacketInReason> packetInReasonList = new ArrayList<>();
94 packetInReasonList.add(PacketInReason.OFPRNOMATCH);
95 packetInReasonList.add(PacketInReason.OFPRINVALIDTTL);
96 builder.setMask(packetInReasonList);
97 masks.add(builder.build());
99 builder = new PacketInMaskBuilder();
100 packetInReasonList = new ArrayList<>();
101 packetInReasonList.add(PacketInReason.OFPRNOMATCH);
102 packetInReasonList.add(PacketInReason.OFPRACTION);
103 packetInReasonList.add(PacketInReason.OFPRINVALIDTTL);
104 builder.setMask(packetInReasonList);
105 masks.add(builder.build());
109 private static List<PortStatusMask> createPortStatusMask() {
110 List<PortStatusMask> masks = new ArrayList<>();
111 PortStatusMaskBuilder builder;
112 builder = new PortStatusMaskBuilder();
113 // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
114 List<PortReason> portReasonList = new ArrayList<>();
115 portReasonList.add(PortReason.OFPPRDELETE);
116 portReasonList.add(PortReason.OFPPRMODIFY);
117 builder.setMask(portReasonList);
118 masks.add(builder.build());
120 builder = new PortStatusMaskBuilder();
121 portReasonList = new ArrayList<>();
122 builder.setMask(portReasonList);
123 masks.add(builder.build());
127 private static List<FlowRemovedMask> createFlowRemowedMask() {
128 List<FlowRemovedMask> masks = new ArrayList<>();
129 FlowRemovedMaskBuilder builder;
130 // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER
131 builder = new FlowRemovedMaskBuilder();
132 List<FlowRemovedReason> flowRemovedReasonList = new ArrayList<>();
133 flowRemovedReasonList.add(FlowRemovedReason.OFPRRHARDTIMEOUT);
134 flowRemovedReasonList.add(FlowRemovedReason.OFPRRGROUPDELETE);
135 builder.setMask(flowRemovedReasonList);
136 masks.add(builder.build());
138 builder = new FlowRemovedMaskBuilder();
139 flowRemovedReasonList = new ArrayList<>();
140 flowRemovedReasonList.add(FlowRemovedReason.OFPRRIDLETIMEOUT);
141 flowRemovedReasonList.add(FlowRemovedReason.OFPRRDELETE);
142 builder.setMask(flowRemovedReasonList);
143 masks.add(builder.build());