Performacne improvements via adding a netty-based openflowj and openflow plugin;...
[controller.git] / third-party / openflowj_netty / src / main / java / org / openflow / protocol / OFQueueProp.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 org.jboss.netty.buffer.ChannelBuffer;
20 import org.openflow.util.U16;
21
22 public class OFQueueProp {
23     private int NONE_MINIMUM_LENGTH = 8;
24     private int RATE_MINIMUM_LENGTH = 16;
25
26     public enum OFQueuePropType {
27         OFPQT_NONE       (0),
28         OFPQT_MIN_RATE   (1),
29         OFPQT_MAX_RATE   (2);
30
31         protected int value;
32
33         private OFQueuePropType(int value) {
34             this.value = value;
35         }
36
37         /**
38          * @return the value
39          */
40         public int getValue() {
41             return value;
42         }
43
44         public static OFQueuePropType fromShort(short x) {
45             switch (x) {
46                 case 0:
47                     return OFPQT_NONE;
48                 case 1:
49                     return OFPQT_MIN_RATE;
50                 case 2:
51                     return OFPQT_MAX_RATE;
52             }
53             return null;
54         }
55     }
56
57     protected OFQueuePropType type;
58     protected short length;
59     protected short rate = -1; // not valid if type == OFPQT_NONE
60
61     public OFQueueProp() {
62         this.type = OFQueuePropType.OFPQT_NONE;
63         this.length = U16.t(NONE_MINIMUM_LENGTH);
64     }
65
66     /**
67      * @return the type
68      */
69     public OFQueuePropType getType() {
70         return type;
71     }
72
73     /**
74      * @param type the type to set
75      */
76     public void setType(OFQueuePropType type) {
77         this.type = type;
78
79         switch (type) {
80             case OFPQT_NONE:
81                 this.length = U16.t(NONE_MINIMUM_LENGTH);
82                 break;
83             case OFPQT_MIN_RATE:
84                 this.length = U16.t(RATE_MINIMUM_LENGTH);
85                 break;
86             case OFPQT_MAX_RATE:
87                 this.length = U16.t(RATE_MINIMUM_LENGTH);
88                 break;
89         }
90     }
91
92     /**
93      * @return the rate
94      */
95     public short getRate() {
96         return rate;
97     }
98
99     /**
100      * @param rate the rate to set
101      */
102     public void setRate(short rate) {
103         this.rate = rate;
104     }
105
106     /**
107      * @return the length
108      */
109     public short getLength() {
110         return length;
111     }
112
113     public void readFrom(ChannelBuffer data) {
114         this.type = OFQueuePropType.fromShort(data.readShort());
115         this.length = data.readShort();
116         data.readInt(); // pad
117
118         if (this.type == OFQueuePropType.OFPQT_MIN_RATE ||
119             this.type == OFQueuePropType.OFPQT_MAX_RATE) {
120             assert(this.length == RATE_MINIMUM_LENGTH);
121
122             this.rate = data.readShort();
123             data.readInt(); // pad
124             data.readShort(); // pad
125         } else {
126             assert(this.length == NONE_MINIMUM_LENGTH);
127         }
128     }
129
130     public void writeTo(ChannelBuffer data) {
131         data.writeShort(this.type.getValue());
132         data.writeShort(this.length);
133         data.writeInt(0); // pad
134
135         if (this.type == OFQueuePropType.OFPQT_MIN_RATE ||
136             this.type == OFQueuePropType.OFPQT_MAX_RATE) {
137             data.writeShort(this.rate);
138             data.writeInt(0); // pad
139             data.writeShort(0); // pad
140         }
141     }
142
143     @Override
144     public int hashCode() {
145         final int prime = 353;
146         int result = super.hashCode();
147         result = prime * result + type.getValue();
148         result = prime * result + rate;
149         return result;
150     }
151
152     @Override
153     public boolean equals(Object obj) {
154         if (this == obj) {
155             return true;
156         }
157         if (!super.equals(obj)) {
158             return false;
159         }
160         if (!(obj instanceof OFQueueProp)) {
161             return false;
162         }
163         OFQueueProp other = (OFQueueProp) obj;
164         if (type != other.type) {
165             return false;
166         }
167         if (type == OFQueuePropType.OFPQT_MIN_RATE ||
168             type == OFQueuePropType.OFPQT_MAX_RATE) {
169             if (rate != other.rate) {
170                 return false;
171             }
172         }
173         return true;
174     }
175 }