Performacne improvements via adding a netty-based openflowj and openflow plugin;...
[controller.git] / third-party / openflowj_netty / src / main / java / org / openflow / protocol / OFPacketQueue.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.protocol;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.jboss.netty.buffer.ChannelBuffer;
23 import org.openflow.util.U16;
24
25 /**
26  * Represents ofp_packet_queue
27  * @author Andrew Ferguson (adf@cs.brown.edu)
28  */
29 public class OFPacketQueue {
30     public static int MINIMUM_LENGTH = 8;
31
32     protected int queueId;
33     protected short length;
34     protected List<OFQueueProp> properties = new ArrayList<OFQueueProp>();
35
36     public OFPacketQueue() {
37         this.queueId = -1;
38         this.length = U16.t(MINIMUM_LENGTH);
39     }
40
41     public OFPacketQueue(int queueId) {
42         this.queueId = queueId;
43         this.length = U16.t(MINIMUM_LENGTH);
44     }
45
46     /**
47      * @return the queueId
48      */
49     public long getQueueId() {
50         return queueId;
51     }
52
53     /**
54      * @param queueId the queueId to set
55      */
56     public void setQueueId(int queueId) {
57         this.queueId = queueId;
58     }
59
60     /**
61      * @return the queue's properties
62      */
63     public List<OFQueueProp> getProperties() {
64         return properties;
65     }
66
67     /**
68      * @param properties the properties to set
69      */
70     public void setProperties(List<OFQueueProp> properties) {
71         this.properties = properties;
72
73         this.length = U16.t(MINIMUM_LENGTH);
74         for (OFQueueProp prop : properties) {
75             this.length += prop.getLength();
76         }
77     }
78
79     /**
80      * @return the length
81      */
82     public short getLength() {
83         return length;
84     }
85
86     public void readFrom(ChannelBuffer data) {
87         this.queueId = data.readInt();
88         this.length = data.readShort();
89         data.readShort(); // pad
90
91         int availLength = (this.length - MINIMUM_LENGTH);
92         this.properties.clear();
93
94         while (availLength > 0) {
95             OFQueueProp prop = new OFQueueProp();
96             prop.readFrom(data);
97             properties.add(prop);
98             availLength -= prop.getLength();
99         }
100     }
101
102     public void writeTo(ChannelBuffer data) {
103         data.writeInt(queueId);
104         data.writeShort(length);
105         data.writeShort(0); // pad
106
107         for (OFQueueProp prop : properties) {
108             prop.writeTo(data);
109         }
110     }
111
112     @Override
113     public int hashCode() {
114         final int prime = 359;
115         int result = super.hashCode();
116         result = prime * result + queueId;
117         result = prime * result + length;
118         result = prime * result + properties.hashCode();
119         return result;
120     }
121
122     @Override
123     public boolean equals(Object obj) {
124         if (this == obj) {
125             return true;
126         }
127         if (!super.equals(obj)) {
128             return false;
129         }
130         if (!(obj instanceof OFPacketQueue)) {
131             return false;
132         }
133         OFPacketQueue other = (OFPacketQueue) obj;
134         if (queueId != other.queueId) {
135             return false;
136         }
137         if (! properties.equals(other.properties)) {
138             return false;
139         }
140         return true;
141     }
142 }