2 * Copyright (c) 2015 NetIDE Consortium 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
8 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
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.augments.rev150225.RateQueueProperty;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.RateQueuePropertyBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.QueueId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.QueueProperties;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.get.config.reply.Queues;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.get.config.reply.QueuesBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.property.header.QueueProperty;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.property.header.QueuePropertyBuilder;
34 import org.opendaylight.yangtools.yang.common.Uint16;
35 import org.opendaylight.yangtools.yang.common.Uint32;
38 * Unit tests for QueueGetConfigReplyMessageFactory.
40 * @author giuseppex.petralia@intel.com
42 public class QueueGetConfigReplyMessageFactoryTest {
43 private OFSerializer<GetQueueConfigOutput> factory;
44 private static final byte MESSAGE_TYPE = 23;
45 private static final byte PADDING = 4;
46 private static final byte QUEUE_PADDING = 6;
47 private static final byte PROPERTY_HEADER_PADDING = 4;
48 private static final byte PROPERTY_RATE_PADDING = 6;
51 public void startUp() {
52 SerializerRegistry registry = new SerializerRegistryImpl();
55 .getSerializer(new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, GetQueueConfigOutput.class));
59 public void testSerialize() throws Exception {
60 GetQueueConfigOutputBuilder builder = new GetQueueConfigOutputBuilder();
61 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
62 builder.setPort(new PortNumber(Uint32.valueOf(0x00010203)));
63 builder.setQueues(createQueuesList());
64 GetQueueConfigOutput message = builder.build();
66 ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
67 factory.serialize(message, serializedBuffer);
68 BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 80);
69 Assert.assertEquals("Wrong port", message.getPort().getValue().longValue(), serializedBuffer.readInt());
70 serializedBuffer.skipBytes(PADDING);
72 Assert.assertEquals("Wrong queue Id", message.getQueues().get(0).getQueueId().getValue().longValue(),
73 serializedBuffer.readInt());
74 Assert.assertEquals("Wrong port", message.getQueues().get(0).getPort().getValue().longValue(),
75 serializedBuffer.readInt());
76 Assert.assertEquals("Wrong length", 32, serializedBuffer.readShort());
77 serializedBuffer.skipBytes(QUEUE_PADDING);
78 List<QueueProperty> properties = message.getQueues().get(0).getQueueProperty();
79 Assert.assertEquals("Wrong property", properties.get(0).getProperty().getIntValue(),
80 serializedBuffer.readShort());
81 Assert.assertEquals("Wrong property length", 16, serializedBuffer.readShort());
82 serializedBuffer.skipBytes(PROPERTY_HEADER_PADDING);
83 RateQueueProperty rateQueueProperty = properties.get(0).augmentation(RateQueueProperty.class);
84 Assert.assertEquals("Wrong rate", rateQueueProperty.getRate().intValue(), serializedBuffer.readShort());
85 serializedBuffer.skipBytes(PROPERTY_RATE_PADDING);
87 Assert.assertEquals("Wrong queue Id", message.getQueues().get(1).getQueueId().getValue().longValue(),
88 serializedBuffer.readInt());
89 Assert.assertEquals("Wrong queue Id", message.getQueues().get(1).getPort().getValue().longValue(),
90 serializedBuffer.readInt());
91 Assert.assertEquals("Wrong length", 32, serializedBuffer.readShort());
92 serializedBuffer.skipBytes(QUEUE_PADDING);
93 List<QueueProperty> propertiesTwo = message.getQueues().get(1).getQueueProperty();
94 Assert.assertEquals("Wrong property", propertiesTwo.get(0).getProperty().getIntValue(),
95 serializedBuffer.readShort());
96 Assert.assertEquals("Wrong property length", 16, serializedBuffer.readShort());
97 serializedBuffer.skipBytes(PROPERTY_HEADER_PADDING);
98 RateQueueProperty rateQueuePropertyTwo = propertiesTwo.get(0).augmentation(RateQueueProperty.class);
99 Assert.assertEquals("Wrong rate", rateQueuePropertyTwo.getRate().intValue(), serializedBuffer.readShort());
100 serializedBuffer.skipBytes(PROPERTY_RATE_PADDING);
104 private static List<Queues> createQueuesList() {
105 List<Queues> queuesList = new ArrayList<>();
106 for (int i = 1; i < 3; i++) {
107 queuesList.add(new QueuesBuilder()
108 .setQueueId(new QueueId(Uint32.valueOf(i)))
109 .setPort(new PortNumber(Uint32.valueOf(i)))
110 .setQueueProperty(createPropertiesList())
116 private static List<QueueProperty> createPropertiesList() {
117 final List<QueueProperty> propertiesList = new ArrayList<>();
118 propertiesList.add(new QueuePropertyBuilder()
119 .setProperty(QueueProperties.forValue(2))
120 .addAugmentation(new RateQueuePropertyBuilder().setRate(Uint16.valueOf(5)).build())
122 return propertiesList;