Initial opendaylight infrastructure commit!!
[controller.git] / third-party / openflowj / src / main / java / org / openflow / protocol / OFFeaturesReply.java
1 package org.openflow.protocol;
2
3 import java.io.Serializable;
4 import java.nio.ByteBuffer;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import org.openflow.util.U16;
9
10
11 /**
12  * Represents a features reply message
13  * @author David Erickson (daviderickson@cs.stanford.edu)
14  *
15  */
16 public class OFFeaturesReply extends OFMessage implements Serializable {
17     public static int MINIMUM_LENGTH = 32;
18
19     /**
20      * Corresponds to bits on the capabilities field
21      */
22     public enum OFCapabilities {
23         OFPC_FLOW_STATS     (1 << 0),
24         OFPC_TABLE_STATS    (1 << 1),
25         OFPC_PORT_STATS     (1 << 2),
26         OFPC_STP            (1 << 3),
27         OFPC_RESERVED       (1 << 4),
28         OFPC_IP_REASM       (1 << 5),
29         OFPC_QUEUE_STATS    (1 << 6),
30         OFPC_ARP_MATCH_IP   (1 << 7);
31
32         protected int value;
33
34         private OFCapabilities(int value) {
35             this.value = value;
36         }
37
38         /**
39          * @return the value
40          */
41         public int getValue() {
42             return value;
43         }
44     }
45
46     protected long datapathId;
47     protected int buffers;
48     protected byte tables;
49     protected int capabilities;
50     protected int actions;
51     protected List<OFPhysicalPort> ports;
52
53     public OFFeaturesReply() {
54         super();
55         this.type = OFType.FEATURES_REPLY;
56         this.length = U16.t(MINIMUM_LENGTH);
57     }
58
59     /**
60      * @return the datapathId
61      */
62     public long getDatapathId() {
63         return datapathId;
64     }
65
66     /**
67      * @param datapathId the datapathId to set
68      */
69     public void setDatapathId(long datapathId) {
70         this.datapathId = datapathId;
71     }
72
73     /**
74      * @return the buffers
75      */
76     public int getBuffers() {
77         return buffers;
78     }
79
80     /**
81      * @param buffers the buffers to set
82      */
83     public void setBuffers(int buffers) {
84         this.buffers = buffers;
85     }
86
87     /**
88      * @return the tables
89      */
90     public byte getTables() {
91         return tables;
92     }
93
94     /**
95      * @param tables the tables to set
96      */
97     public void setTables(byte tables) {
98         this.tables = tables;
99     }
100
101     /**
102      * @return the capabilities
103      */
104     public int getCapabilities() {
105         return capabilities;
106     }
107
108     /**
109      * @param capabilities the capabilities to set
110      */
111     public void setCapabilities(int capabilities) {
112         this.capabilities = capabilities;
113     }
114
115     /**
116      * @return the actions
117      */
118     public int getActions() {
119         return actions;
120     }
121
122     /**
123      * @param actions the actions to set
124      */
125     public void setActions(int actions) {
126         this.actions = actions;
127     }
128
129     /**
130      * @return the ports
131      */
132     public List<OFPhysicalPort> getPorts() {
133         return ports;
134     }
135
136     /**
137      * @param ports the ports to set
138      */
139     public void setPorts(List<OFPhysicalPort> ports) {
140         this.ports = ports;
141         if (ports == null) {
142             this.setLengthU(MINIMUM_LENGTH);
143         } else {
144             this.setLengthU(MINIMUM_LENGTH + ports.size()
145                     * OFPhysicalPort.MINIMUM_LENGTH);
146         }
147     }
148
149     @Override
150     public void readFrom(ByteBuffer data) {
151         super.readFrom(data);
152         this.datapathId = data.getLong();
153         this.buffers = data.getInt();
154         this.tables = data.get();
155         data.position(data.position() + 3); // pad
156         this.capabilities = data.getInt();
157         this.actions = data.getInt();
158         if (this.ports == null) {
159             this.ports = new ArrayList<OFPhysicalPort>();
160         } else {
161             this.ports.clear();
162         }
163         int portCount = (super.getLengthU() - 32)
164                 / OFPhysicalPort.MINIMUM_LENGTH;
165         OFPhysicalPort port;
166         for (int i = 0; i < portCount; ++i) {
167             port = new OFPhysicalPort();
168             port.readFrom(data);
169             this.ports.add(port);
170         }
171     }
172
173     @Override
174     public void writeTo(ByteBuffer data) {
175         super.writeTo(data);
176         data.putLong(this.datapathId);
177         data.putInt(this.buffers);
178         data.put(this.tables);
179         data.putShort((short) 0); // pad
180         data.put((byte) 0); // pad
181         data.putInt(this.capabilities);
182         data.putInt(this.actions);
183         if (this.ports != null)
184             for (OFPhysicalPort port : this.ports) {
185                 port.writeTo(data);
186             }
187     }
188
189     @Override
190     public int hashCode() {
191         final int prime = 139;
192         int result = super.hashCode();
193         result = prime * result + actions;
194         result = prime * result + buffers;
195         result = prime * result + capabilities;
196         result = prime * result + (int) (datapathId ^ (datapathId >>> 32));
197         result = prime * result + ((ports == null) ? 0 : ports.hashCode());
198         result = prime * result + tables;
199         return result;
200     }
201
202     @Override
203     public boolean equals(Object obj) {
204         if (this == obj) {
205             return true;
206         }
207         if (!super.equals(obj)) {
208             return false;
209         }
210         if (!(obj instanceof OFFeaturesReply)) {
211             return false;
212         }
213         OFFeaturesReply other = (OFFeaturesReply) obj;
214         if (actions != other.actions) {
215             return false;
216         }
217         if (buffers != other.buffers) {
218             return false;
219         }
220         if (capabilities != other.capabilities) {
221             return false;
222         }
223         if (datapathId != other.datapathId) {
224             return false;
225         }
226         if (ports == null) {
227             if (other.ports != null) {
228                 return false;
229             }
230         } else if (!ports.equals(other.ports)) {
231             return false;
232         }
233         if (tables != other.tables) {
234             return false;
235         }
236         return true;
237     }
238 }