d6090a3254b0fede2a591eb79f8775be70409d42
[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.OFDeserializer;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20 import org.opendaylight.openflowjava.util.ExperimenterDeserializerKeyFactory;
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 DeserializerRegistry registry;
47
48     @Override
49     public GetQueueConfigOutput deserialize(ByteBuf rawMessage) {
50         GetQueueConfigOutputBuilder builder = new GetQueueConfigOutputBuilder();
51         builder.setVersion((short) EncodeConstants.OF13_VERSION_ID);
52         builder.setXid((rawMessage.readUnsignedInt()));
53         builder.setPort(new PortNumber(rawMessage.readUnsignedInt()));
54         rawMessage.skipBytes(PADDING_IN_QUEUE_GET_CONFIG_REPLY_HEADER);
55         builder.setQueues(createQueuesList(rawMessage));
56         return builder.build();
57     }
58
59     private List<Queues> createQueuesList(ByteBuf input){
60         List<Queues> queuesList = new ArrayList<>();
61         while (input.readableBytes() > 0) {
62             QueuesBuilder queueBuilder = new QueuesBuilder();
63             queueBuilder.setQueueId(new QueueId(input.readUnsignedInt()));
64             queueBuilder.setPort(new PortNumber(input.readUnsignedInt()));
65             int length = input.readUnsignedShort();
66             input.skipBytes(PADDING_IN_PACKET_QUEUE_HEADER);
67             queueBuilder.setQueueProperty(createPropertiesList(input, length - PACKET_QUEUE_LENGTH));
68             queuesList.add(queueBuilder.build());
69         }
70         return queuesList;
71     }
72
73     private List<QueueProperty> createPropertiesList(ByteBuf input, int length){
74         int propertiesLength = length;
75         List<QueueProperty> propertiesList = new ArrayList<>();
76         while (propertiesLength > 0) {
77             int propertyStartIndex = input.readerIndex();
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                 long expId = input.readUnsignedInt();
91                 input.readerIndex(propertyStartIndex);
92                 OFDeserializer<QueueProperty> deserializer = registry.getDeserializer(
93                         ExperimenterDeserializerKeyFactory.createQueuePropertyDeserializerKey(
94                                 EncodeConstants.OF13_VERSION_ID, expId));
95                 propertiesList.add(deserializer.deserialize(input));
96                 continue;
97             }
98             propertiesList.add(propertiesBuilder.build());
99         }
100         return propertiesList;
101     }
102
103     @Override
104     public void injectDeserializerRegistry(DeserializerRegistry deserializerRegistry) {
105         this.registry = deserializerRegistry;
106     }
107 }