eeae9aace6436f42de2c26151aba1f125e26d83b
[openflowjava.git] / third-party / openflowj_netty / src / main / java / org / openflow / vendor / openflow / OFQueueVendorData.java
1 /**
2 *    Copyright 2012, Andrew Ferguson, Brown University
3 *
4 *    Licensed under the Apache License, Version 2.0 (the "License"); you may
5 *    not use this file except in compliance with the License. You may obtain
6 *    a copy of the License at
7 *
8 *         http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *    Unless required by applicable law or agreed to in writing, software
11 *    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 *    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 *    License for the specific language governing permissions and limitations
14 *    under the License.
15 **/
16
17 package org.openflow.vendor.openflow;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.jboss.netty.buffer.ChannelBuffer;
23 import org.openflow.protocol.OFPacketQueue;
24
25 /**
26  * Class that represents the vendor data in a queue modify or delete request
27  *
28  * @author Andrew Ferguson (adf@cs.brown.edu)
29  */
30 public class OFQueueVendorData extends OFOpenFlowVendorData {
31     public static int MINIMUM_LENGTH = 8;
32
33     protected short portNumber;
34     protected List<OFPacketQueue> queues = new ArrayList<OFPacketQueue>();
35
36     public OFQueueVendorData(int dataType) {
37         super(dataType);
38     }
39
40     /**
41      * @return the portNumber
42      */
43     public short getPortNumber() {
44         return portNumber;
45     }
46
47     /**
48      * @param port the port on which the queue is
49      */
50     public void setPortNumber(short portNumber) {
51         this.portNumber = portNumber;
52     }
53
54
55     /**
56      * @return the queues
57      */
58     public List<OFPacketQueue> getQueues() {
59         return queues;
60     }
61
62     /**
63      * @param queues the queues to modify or delete
64      */
65     public void setQueues(List<OFPacketQueue> queues) {
66         this.queues = queues;
67     }
68
69     /**
70      * @return the total length of the queue modify or delete msg
71      */
72     @Override
73     public int getLength() {
74         int queuesLength = 0;
75
76         for (OFPacketQueue queue : queues) {
77             queuesLength += queue.getLength();
78         }
79
80         return super.getLength() + MINIMUM_LENGTH + queuesLength;
81     }
82
83     /**
84      * Read the queue message data from the ChannelBuffer
85      * @param data the channel buffer from which we're deserializing
86      * @param length the length to the end of the enclosing message
87      */
88     public void readFrom(ChannelBuffer data, int length) {
89         super.readFrom(data, length);
90         portNumber = data.readShort();
91         data.readInt();   // pad
92         data.readShort(); // pad
93
94         int availLength = (length - MINIMUM_LENGTH);
95         this.queues.clear();
96
97         while (availLength > 0) {
98             OFPacketQueue queue = new OFPacketQueue();
99             queue.readFrom(data);
100             queues.add(queue);
101             availLength -= queue.getLength();
102         }
103     }
104
105     /**
106      * Write the queue message data to the ChannelBuffer
107      * @param data the channel buffer to which we're serializing
108      */
109     public void writeTo(ChannelBuffer data) {
110         super.writeTo(data);
111         data.writeShort(this.portNumber);
112         data.writeInt(0);   // pad
113         data.writeShort(0); // pad
114
115         for (OFPacketQueue queue : queues) {
116             queue.writeTo(data);
117         }
118     }
119 }