Default experimenters moved to separate bundle + unit tests
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / QueueGetConfigReplyMessageFactory.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
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistryInjector;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageCodeKey;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.RateQueueProperty;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.RateQueuePropertyBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.QueueId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.QueueProperties;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.get.config.reply.Queues;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.get.config.reply.QueuesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.property.header.QueueProperty;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.property.header.QueuePropertyBuilder;
32
33 /**
34  * Translates QueueGetConfigReply messages
35  * @author timotej.kubas
36  * @author michal.polkorab
37  */
38 public class QueueGetConfigReplyMessageFactory implements OFDeserializer<GetQueueConfigOutput>,
39         DeserializerRegistryInjector {
40
41     private static final byte PADDING_IN_QUEUE_GET_CONFIG_REPLY_HEADER = 4;
42     private static final byte PADDING_IN_PACKET_QUEUE_HEADER = 6;
43     private static final byte PADDING_IN_QUEUE_PROPERTY_HEADER = 4;
44     private static final int PADDING_IN_RATE_QUEUE_PROPERTY = 6;
45     private static final byte PACKET_QUEUE_LENGTH = 16;
46     private static final byte QUEUE_PROP_HEADER_SIZE = 8;
47     private DeserializerRegistry registry;
48
49     @Override
50     public GetQueueConfigOutput deserialize(ByteBuf rawMessage) {
51         GetQueueConfigOutputBuilder builder = new GetQueueConfigOutputBuilder();
52         builder.setVersion((short) EncodeConstants.OF13_VERSION_ID);
53         builder.setXid((rawMessage.readUnsignedInt()));
54         builder.setPort(new PortNumber(rawMessage.readUnsignedInt()));
55         rawMessage.skipBytes(PADDING_IN_QUEUE_GET_CONFIG_REPLY_HEADER);
56         builder.setQueues(createQueuesList(rawMessage));
57         return builder.build();
58     }
59     
60     private List<Queues> createQueuesList(ByteBuf input){
61         List<Queues> queuesList = new ArrayList<>();
62         while (input.readableBytes() > 0) {
63             QueuesBuilder queueBuilder = new QueuesBuilder();
64             queueBuilder.setQueueId(new QueueId(input.readUnsignedInt()));
65             queueBuilder.setPort(new PortNumber(input.readUnsignedInt()));
66             int length = input.readUnsignedShort();
67             input.skipBytes(PADDING_IN_PACKET_QUEUE_HEADER);
68             queueBuilder.setQueueProperty(createPropertiesList(input, length - PACKET_QUEUE_LENGTH));
69             queuesList.add(queueBuilder.build());
70         } 
71         return queuesList;
72     }
73     
74     private List<QueueProperty> createPropertiesList(ByteBuf input, int length){
75         int propertiesLength = length;
76         List<QueueProperty> propertiesList = new ArrayList<>();
77         while (propertiesLength > 0) {
78             QueuePropertyBuilder propertiesBuilder = new QueuePropertyBuilder();
79             QueueProperties property = QueueProperties.forValue(input.readUnsignedShort());
80             propertiesBuilder.setProperty(property);
81             int currentPropertyLength = input.readUnsignedShort();
82             propertiesLength -= currentPropertyLength;
83             input.skipBytes(PADDING_IN_QUEUE_PROPERTY_HEADER);
84             if (property.equals(QueueProperties.OFPQTMINRATE) || property.equals(QueueProperties.OFPQTMAXRATE)) {
85                 RateQueuePropertyBuilder rateBuilder = new RateQueuePropertyBuilder();
86                 rateBuilder.setRate(input.readUnsignedShort());
87                 propertiesBuilder.addAugmentation(RateQueueProperty.class, rateBuilder.build());
88                 input.skipBytes(PADDING_IN_RATE_QUEUE_PROPERTY);
89             } else if (property.equals(QueueProperties.OFPQTEXPERIMENTER)) {
90                 // return index to property start, so that the experimenter properties are deserialized
91                 // correctly - as whole ofp_queue_prop_experimenter property
92                 input.readerIndex(input.readerIndex() - 2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES
93                         - PADDING_IN_QUEUE_PROPERTY_HEADER);
94                 OFDeserializer<QueueProperty> deserializer = registry.getDeserializer(
95                         new MessageCodeKey(EncodeConstants.OF13_VERSION_ID,
96                                 EncodeConstants.EXPERIMENTER_VALUE, QueueProperty.class));
97                 QueueProperty expProp = deserializer.deserialize(input.slice(input.readerIndex(),
98                         currentPropertyLength - QUEUE_PROP_HEADER_SIZE));
99                 propertiesList.add(expProp);
100                 continue;
101             }
102             propertiesList.add(propertiesBuilder.build());
103         }
104         return propertiesList;
105     }
106
107     @Override
108     public void injectDeserializerRegistry(DeserializerRegistry deserializerRegistry) {
109         this.registry = deserializerRegistry;
110     }
111
112 }