b442a49a6c4cd27b0ef9f1d848d6098c01d3490d
[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.impl.deserialization.OFDeserializer;
17 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterQueuePropertyBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.RateQueueProperty;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.RateQueuePropertyBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.QueueId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.QueueProperties;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.get.config.reply.Queues;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.get.config.reply.QueuesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.property.header.QueueProperty;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.property.header.QueuePropertyBuilder;
30
31 /**
32  * Translates QueueGetConfigReply messages
33  * @author timotej.kubas
34  * @author michal.polkorab
35  */
36 public class QueueGetConfigReplyMessageFactory implements OFDeserializer<GetQueueConfigOutput> {
37
38     private static final byte PADDING_IN_QUEUE_GET_CONFIG_REPLY_HEADER = 4;
39     private static final byte PADDING_IN_PACKET_QUEUE_HEADER = 6;
40     private static final byte PADDING_IN_QUEUE_PROPERTY_HEADER = 4;
41     private static final int PADDING_IN_RATE_QUEUE_PROPERTY = 6;
42     private static final int PADDING_IN_EXPERIMENTER_QUEUE_PROPERTY = 4;
43     private static final byte PACKET_QUEUE_LENGTH = 16;
44
45     private static QueueGetConfigReplyMessageFactory instance;
46     
47     private QueueGetConfigReplyMessageFactory() {
48         // singleton
49     }
50     
51     /**
52      * 
53      * @return singleton factory
54      */
55     public static synchronized QueueGetConfigReplyMessageFactory getInstance(){
56         
57         if(instance == null){
58             instance = new QueueGetConfigReplyMessageFactory();
59         }
60         return instance;
61     }
62     
63     @Override
64     public GetQueueConfigOutput bufferToMessage(ByteBuf rawMessage, short version) {
65         GetQueueConfigOutputBuilder builder = new GetQueueConfigOutputBuilder();
66         builder.setVersion(version);
67         builder.setXid((rawMessage.readUnsignedInt()));
68         builder.setPort(new PortNumber(rawMessage.readUnsignedInt()));
69         rawMessage.skipBytes(PADDING_IN_QUEUE_GET_CONFIG_REPLY_HEADER);
70         builder.setQueues(createQueuesList(rawMessage));
71         return builder.build();
72     }
73     
74     private static List<Queues> createQueuesList(ByteBuf input){
75         List<Queues> queuesList = new ArrayList<>();
76         while (input.readableBytes() > 0) {
77             QueuesBuilder queueBuilder = new QueuesBuilder();
78             queueBuilder.setQueueId(new QueueId(input.readUnsignedInt()));
79             queueBuilder.setPort(new PortNumber(input.readUnsignedInt()));
80             int length = input.readUnsignedShort();
81             input.skipBytes(PADDING_IN_PACKET_QUEUE_HEADER);
82             queueBuilder.setQueueProperty(createPropertiesList(input, length - PACKET_QUEUE_LENGTH));
83             queuesList.add(queueBuilder.build());
84         } 
85         return queuesList;
86     }
87     
88     private static List<QueueProperty> createPropertiesList(ByteBuf input, int length){
89         int propertiesLength = length;
90         List<QueueProperty> propertiesList = new ArrayList<>();
91         while (propertiesLength > 0) {
92             QueuePropertyBuilder propertiesBuilder = new QueuePropertyBuilder();
93             QueueProperties property = QueueProperties.forValue(input.readUnsignedShort());
94             propertiesBuilder.setProperty(property);
95             int currentPropertyLength = input.readUnsignedShort();
96             propertiesLength -= currentPropertyLength;
97             input.skipBytes(PADDING_IN_QUEUE_PROPERTY_HEADER);
98             if (property.equals(QueueProperties.OFPQTMINRATE) || property.equals(QueueProperties.OFPQTMAXRATE)) {
99                 RateQueuePropertyBuilder rateBuilder = new RateQueuePropertyBuilder();
100                 rateBuilder.setRate(input.readUnsignedShort());
101                 propertiesBuilder.addAugmentation(RateQueueProperty.class, rateBuilder.build());
102                 input.skipBytes(PADDING_IN_RATE_QUEUE_PROPERTY);
103             } else if (property.equals(QueueProperties.OFPQTEXPERIMENTER)) {
104                 ExperimenterQueuePropertyBuilder expBuilder = new ExperimenterQueuePropertyBuilder();
105                 expBuilder.setExperimenter(input.readUnsignedInt());
106                 input.skipBytes(PADDING_IN_EXPERIMENTER_QUEUE_PROPERTY);
107                 expBuilder.setData(input.readBytes(currentPropertyLength
108                         - EncodeConstants.SIZE_OF_INT_IN_BYTES - PADDING_IN_EXPERIMENTER_QUEUE_PROPERTY).array());
109                 propertiesBuilder.addAugmentation(RateQueueProperty.class, expBuilder.build());
110             }
111             propertiesList.add(propertiesBuilder.build());
112         }
113         return propertiesList;
114     }
115
116 }