Merge "Bump odlparent/yangtools/mdsal/controller"
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / OF10QueueGetConfigReplyMessageFactory.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowjava.protocol.impl.deserialization.factories;
10
11 import io.netty.buffer.ByteBuf;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
15 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.RateQueuePropertyBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.QueueId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.QueueProperties;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.get.config.reply.Queues;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.get.config.reply.QueuesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.property.header.QueueProperty;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.property.header.QueuePropertyBuilder;
26
27 /**
28  * Translates QueueGetConfigReply messages (OpenFlow v1.0).
29  *
30  * @author michal.polkorab
31  */
32 public class OF10QueueGetConfigReplyMessageFactory implements OFDeserializer<GetQueueConfigOutput> {
33
34     private static final byte PADDING_IN_QUEUE_GET_CONFIG_REPLY_HEADER = 6;
35     private static final byte PADDING_IN_PACKET_QUEUE_HEADER = 2;
36     private static final byte PADDING_IN_QUEUE_PROPERTY_HEADER = 4;
37     private static final byte PADDING_IN_RATE_QUEUE_PROPERTY = 6;
38     private static final byte PACKET_QUEUE_HEADER_LENGTH = 8;
39
40     @Override
41     public GetQueueConfigOutput deserialize(ByteBuf rawMessage) {
42         GetQueueConfigOutputBuilder builder = new GetQueueConfigOutputBuilder();
43         builder.setVersion((short) EncodeConstants.OF10_VERSION_ID);
44         builder.setXid(rawMessage.readUnsignedInt());
45         builder.setPort(new PortNumber((long) rawMessage.readUnsignedShort()));
46         rawMessage.skipBytes(PADDING_IN_QUEUE_GET_CONFIG_REPLY_HEADER);
47         builder.setQueues(createQueuesList(rawMessage));
48         return builder.build();
49     }
50
51     private static List<Queues> createQueuesList(ByteBuf input) {
52         List<Queues> queuesList = new ArrayList<>();
53         while (input.readableBytes() > 0) {
54             QueuesBuilder queueBuilder = new QueuesBuilder();
55             queueBuilder.setQueueId(new QueueId(input.readUnsignedInt()));
56             int length = input.readUnsignedShort();
57             input.skipBytes(PADDING_IN_PACKET_QUEUE_HEADER);
58             queueBuilder.setQueueProperty(createPropertiesList(input, length - PACKET_QUEUE_HEADER_LENGTH));
59             queuesList.add(queueBuilder.build());
60         }
61         return queuesList;
62     }
63
64     private static List<QueueProperty> createPropertiesList(ByteBuf input, int length) {
65         int propertiesLength = length;
66         List<QueueProperty> propertiesList = new ArrayList<>();
67         while (propertiesLength > 0) {
68             QueuePropertyBuilder propertiesBuilder = new QueuePropertyBuilder();
69             QueueProperties property = QueueProperties.forValue(input.readUnsignedShort());
70             propertiesBuilder.setProperty(property);
71             propertiesLength -= input.readUnsignedShort();
72             input.skipBytes(PADDING_IN_QUEUE_PROPERTY_HEADER);
73             if (property.equals(QueueProperties.OFPQTMINRATE)) {
74                 propertiesBuilder.addAugmentation(new RateQueuePropertyBuilder()
75                     .setRate(input.readUnsignedShort())
76                     .build());
77                 input.skipBytes(PADDING_IN_RATE_QUEUE_PROPERTY);
78             }
79             propertiesList.add(propertiesBuilder.build());
80         }
81         return propertiesList;
82     }
83 }