Update OF header lenght
[openflowjava.git] / 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  * @author giuseppex.petralia@intel.com
23  *
24  */
25 public class QueueGetConfigReplyMessageFactory implements OFSerializer<GetQueueConfigOutput> {
26
27     private static final byte MESSAGE_TYPE = 23;
28     private static final byte PADDING = 4;
29     public static final int QUEUE_LENGTH_INDEX = 8;
30     public static final int PROPERTY_LENGTH_INDEX = 2;
31     private static final byte QUEUE_PADDING = 6;
32     private static final byte PROPERTY_HEADER_PADDING = 4;
33     private static final byte PROPERTY_RATE_PADDING = 6;
34     private static final byte PROPERTY_EXPERIMENTER_PADDING = 4;
35
36     @Override
37     public void serialize(GetQueueConfigOutput message, ByteBuf outBuffer) {
38         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
39         outBuffer.writeInt(message.getPort().getValue().intValue());
40         outBuffer.writeZero(PADDING);
41         for (Queues queue : message.getQueues()) {
42             ByteBuf queueBuff = UnpooledByteBufAllocator.DEFAULT.buffer();
43             queueBuff.writeInt(queue.getQueueId().getValue().intValue());
44             queueBuff.writeInt(queue.getPort().getValue().intValue());
45             queueBuff.writeShort(EncodeConstants.EMPTY_LENGTH);
46             queueBuff.writeZero(QUEUE_PADDING);
47
48             for (QueueProperty property : queue.getQueueProperty()) {
49                 ByteBuf propertyBuff = UnpooledByteBufAllocator.DEFAULT.buffer();
50                 propertyBuff.writeShort(property.getProperty().getIntValue());
51                 propertyBuff.writeShort(EncodeConstants.EMPTY_LENGTH);
52                 propertyBuff.writeZero(PROPERTY_HEADER_PADDING);
53                 switch (property.getProperty()) {
54                 case OFPQTMINRATE:
55                     serializeRateBody(property.getAugmentation(RateQueueProperty.class), propertyBuff);
56                     break;
57                 case OFPQTMAXRATE:
58                     serializeRateBody(property.getAugmentation(RateQueueProperty.class), propertyBuff);
59                     break;
60                 case OFPQTEXPERIMENTER:
61                     serializeExperimenterBody(property.getAugmentation(ExperimenterIdQueueProperty.class),
62                             propertyBuff);
63                     break;
64                 default:
65                     break;
66                 }
67                 propertyBuff.setShort(PROPERTY_LENGTH_INDEX, propertyBuff.readableBytes());
68                 queueBuff.writeBytes(propertyBuff);
69             }
70
71             queueBuff.setShort(QUEUE_LENGTH_INDEX, queueBuff.readableBytes());
72             outBuffer.writeBytes(queueBuff);
73         }
74         ByteBufUtils.updateOFHeaderLength(outBuffer);
75     }
76
77     private void serializeRateBody(RateQueueProperty body, ByteBuf outBuffer) {
78         outBuffer.writeShort(body.getRate());
79         outBuffer.writeZero(PROPERTY_RATE_PADDING);
80     }
81
82     private void serializeExperimenterBody(ExperimenterIdQueueProperty body, ByteBuf outBuffer) {
83         // TODO: Experimenter Data is vendor specific that should implement its
84         // own serializer
85         outBuffer.writeInt(body.getExperimenter().getValue().intValue());
86         outBuffer.writeZero(PROPERTY_EXPERIMENTER_PADDING);
87     }
88 }