Merge "OPNFLWPLUG-972: Point to openflowplugin liblldp"
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / QueueGetConfigReplyMessageFactory.java
1 /*
2  * Copyright (c) 2015 NetIDE Consortium 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 org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
13 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
14 import org.opendaylight.openflowjava.util.ByteBufUtils;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.ExperimenterIdQueueProperty;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.RateQueueProperty;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.get.config.reply.Queues;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.property.header.QueueProperty;
20
21 /**
22  * Translates GetQueueConfig messages.
23  *
24  * @author giuseppex.petralia@intel.com
25  */
26 public class QueueGetConfigReplyMessageFactory implements OFSerializer<GetQueueConfigOutput> {
27
28     private static final byte MESSAGE_TYPE = 23;
29     private static final byte PADDING = 4;
30     public static final int QUEUE_LENGTH_INDEX = 8;
31     public static final int PROPERTY_LENGTH_INDEX = 2;
32     private static final byte QUEUE_PADDING = 6;
33     private static final byte PROPERTY_HEADER_PADDING = 4;
34     private static final byte PROPERTY_RATE_PADDING = 6;
35     private static final byte PROPERTY_EXPERIMENTER_PADDING = 4;
36
37     @Override
38     public void serialize(GetQueueConfigOutput message, ByteBuf outBuffer) {
39         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
40         outBuffer.writeInt(message.getPort().getValue().intValue());
41         outBuffer.writeZero(PADDING);
42         for (Queues queue : message.getQueues()) {
43             ByteBuf queueBuff = UnpooledByteBufAllocator.DEFAULT.buffer();
44             queueBuff.writeInt(queue.getQueueId().getValue().intValue());
45             queueBuff.writeInt(queue.getPort().getValue().intValue());
46             queueBuff.writeShort(EncodeConstants.EMPTY_LENGTH);
47             queueBuff.writeZero(QUEUE_PADDING);
48
49             for (QueueProperty property : queue.getQueueProperty()) {
50                 ByteBuf propertyBuff = UnpooledByteBufAllocator.DEFAULT.buffer();
51                 propertyBuff.writeShort(property.getProperty().getIntValue());
52                 propertyBuff.writeShort(EncodeConstants.EMPTY_LENGTH);
53                 propertyBuff.writeZero(PROPERTY_HEADER_PADDING);
54                 switch (property.getProperty()) {
55                     case OFPQTMINRATE:
56                         serializeRateBody(property.getAugmentation(RateQueueProperty.class), propertyBuff);
57                         break;
58                     case OFPQTMAXRATE:
59                         serializeRateBody(property.getAugmentation(RateQueueProperty.class), propertyBuff);
60                         break;
61                     case OFPQTEXPERIMENTER:
62                         serializeExperimenterBody(property.getAugmentation(ExperimenterIdQueueProperty.class),
63                                 propertyBuff);
64                         break;
65                     default:
66                         break;
67                 }
68                 propertyBuff.setShort(PROPERTY_LENGTH_INDEX, propertyBuff.readableBytes());
69                 queueBuff.writeBytes(propertyBuff);
70             }
71
72             queueBuff.setShort(QUEUE_LENGTH_INDEX, queueBuff.readableBytes());
73             outBuffer.writeBytes(queueBuff);
74         }
75         ByteBufUtils.updateOFHeaderLength(outBuffer);
76     }
77
78     private void serializeRateBody(RateQueueProperty body, ByteBuf outBuffer) {
79         outBuffer.writeShort(body.getRate());
80         outBuffer.writeZero(PROPERTY_RATE_PADDING);
81     }
82
83     private void serializeExperimenterBody(ExperimenterIdQueueProperty body, ByteBuf outBuffer) {
84         // TODO: Experimenter Data is vendor specific that should implement its
85         // own serializer
86         outBuffer.writeInt(body.getExperimenter().getValue().intValue());
87         outBuffer.writeZero(PROPERTY_EXPERIMENTER_PADDING);
88     }
89 }