blob: 51b5de35120cf760f685a385e8063208fcbdedf4 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
2* Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior
3* University
4*
5* Licensed under the Apache License, Version 2.0 (the "License"); you may
6* not use this file except in compliance with the License. You may obtain
7* a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14* License for the specific language governing permissions and limitations
15* under the License.
16**/
17
18package org.openflow.protocol;
19
20import java.util.LinkedList;
21import java.util.List;
22
23import org.jboss.netty.buffer.ChannelBuffer;
24import org.openflow.protocol.action.OFAction;
25import org.openflow.protocol.factory.OFActionFactory;
26import org.openflow.protocol.factory.OFActionFactoryAware;
27import org.openflow.util.U16;
28
29/**
30 * Represents an ofp_flow_mod message
31 * @author David Erickson (daviderickson@cs.stanford.edu)
32 *
33 */
34public class OFFlowMod extends OFMessage implements OFActionFactoryAware, Cloneable {
35 public static int MINIMUM_LENGTH = 72;
36
37 public static final short OFPFC_ADD = 0; /* New flow. */
38 public static final short OFPFC_MODIFY = 1; /* Modify all matching flows. */
39 public static final short OFPFC_MODIFY_STRICT = 2; /* Modify entry strictly matching wildcards */
40 public static final short OFPFC_DELETE=3; /* Delete all matching flows. */
41 public static final short OFPFC_DELETE_STRICT =4; /* Strictly match wildcards and priority. */
42
43 // Open Flow Flow Mod Flags. Use "or" operation to set multiple flags
44 public static final short OFPFF_SEND_FLOW_REM = 0x1; // 1 << 0
45 public static final short OFPFF_CHECK_OVERLAP = 0x2; // 1 << 1
46 public static final short OFPFF_EMERG = 0x4; // 1 << 2
47
48 protected OFActionFactory actionFactory;
49 protected OFMatch match;
50 protected long cookie;
51 protected short command;
52 protected short idleTimeout;
53 protected short hardTimeout;
54 protected short priority;
55 protected int bufferId;
56 protected short outPort;
57 protected short flags;
58 protected List<OFAction> actions;
59
60 public OFFlowMod() {
61 super();
62 this.type = OFType.FLOW_MOD;
63 this.length = U16.t(MINIMUM_LENGTH);
64 }
65
66 /**
67 * Get buffer_id
68 * @return
69 */
70 public int getBufferId() {
71 return this.bufferId;
72 }
73
74 /**
75 * Set buffer_id
76 * @param bufferId
77 */
78 public OFFlowMod setBufferId(int bufferId) {
79 this.bufferId = bufferId;
80 return this;
81 }
82
83 /**
84 * Get cookie
85 * @return
86 */
87 public long getCookie() {
88 return this.cookie;
89 }
90
91 /**
92 * Set cookie
93 * @param cookie
94 */
95 public OFFlowMod setCookie(long cookie) {
96 this.cookie = cookie;
97 return this;
98 }
99
100 /**
101 * Get command
102 * @return
103 */
104 public short getCommand() {
105 return this.command;
106 }
107
108 /**
109 * Set command
110 * @param command
111 */
112 public OFFlowMod setCommand(short command) {
113 this.command = command;
114 return this;
115 }
116
117 /**
118 * Get flags
119 * @return
120 */
121 public short getFlags() {
122 return this.flags;
123 }
124
125 /**
126 * Set flags
127 * @param flags
128 */
129 public OFFlowMod setFlags(short flags) {
130 this.flags = flags;
131 return this;
132 }
133
134 /**
135 * Get hard_timeout
136 * @return
137 */
138 public short getHardTimeout() {
139 return this.hardTimeout;
140 }
141
142 /**
143 * Set hard_timeout
144 * @param hardTimeout
145 */
146 public OFFlowMod setHardTimeout(short hardTimeout) {
147 this.hardTimeout = hardTimeout;
148 return this;
149 }
150
151 /**
152 * Get idle_timeout
153 * @return
154 */
155 public short getIdleTimeout() {
156 return this.idleTimeout;
157 }
158
159 /**
160 * Set idle_timeout
161 * @param idleTimeout
162 */
163 public OFFlowMod setIdleTimeout(short idleTimeout) {
164 this.idleTimeout = idleTimeout;
165 return this;
166 }
167
168 /**
169 * Gets a copy of the OFMatch object for this FlowMod, changes to this
170 * object do not modify the FlowMod
171 * @return
172 */
173 public OFMatch getMatch() {
174 return this.match;
175 }
176
177 /**
178 * Set match
179 * @param match
180 */
181 public OFFlowMod setMatch(OFMatch match) {
182 this.match = match;
183 return this;
184 }
185
186 /**
187 * Get out_port
188 * @return
189 */
190 public short getOutPort() {
191 return this.outPort;
192 }
193
194 /**
195 * Set out_port
196 * @param outPort
197 */
198 public OFFlowMod setOutPort(short outPort) {
199 this.outPort = outPort;
200 return this;
201 }
202
203 /**
204 * Set out_port
205 * @param port
206 */
207 public OFFlowMod setOutPort(OFPort port) {
208 this.outPort = port.getValue();
209 return this;
210 }
211
212 /**
213 * Get priority
214 * @return
215 */
216 public short getPriority() {
217 return this.priority;
218 }
219
220 /**
221 * Set priority
222 * @param priority
223 */
224 public OFFlowMod setPriority(short priority) {
225 this.priority = priority;
226 return this;
227 }
228
229 /**
230 * Returns read-only copies of the actions contained in this Flow Mod
231 * @return a list of ordered OFAction objects
232 */
233 public List<OFAction> getActions() {
234 return this.actions;
235 }
236
237 /**
238 * Sets the list of actions this Flow Mod contains
239 * @param actions a list of ordered OFAction objects
240 */
241 public OFFlowMod setActions(List<OFAction> actions) {
242 this.actions = actions;
243 return this;
244 }
245
246 @Override
247 public void readFrom(ChannelBuffer data) {
248 super.readFrom(data);
249 if (this.match == null)
250 this.match = new OFMatch();
251 this.match.readFrom(data);
252 this.cookie = data.readLong();
253 this.command = data.readShort();
254 this.idleTimeout = data.readShort();
255 this.hardTimeout = data.readShort();
256 this.priority = data.readShort();
257 this.bufferId = data.readInt();
258 this.outPort = data.readShort();
259 this.flags = data.readShort();
260 if (this.actionFactory == null)
261 throw new RuntimeException("OFActionFactory not set");
262 this.actions = this.actionFactory.parseActions(data, getLengthU() -
263 MINIMUM_LENGTH);
264 }
265
266 @Override
267 public void writeTo(ChannelBuffer data) {
268 super.writeTo(data);
269 this.match.writeTo(data);
270 data.writeLong(cookie);
271 data.writeShort(command);
272 data.writeShort(idleTimeout);
273 data.writeShort(hardTimeout);
274 data.writeShort(priority);
275 data.writeInt(bufferId);
276 data.writeShort(outPort);
277 data.writeShort(flags);
278 if (actions != null) {
279 for (OFAction action : actions) {
280 action.writeTo(data);
281 }
282 }
283 }
284
285 @Override
286 public void setActionFactory(OFActionFactory actionFactory) {
287 this.actionFactory = actionFactory;
288 }
289
290 @Override
291 public int hashCode() {
292 final int prime = 227;
293 int result = super.hashCode();
294 result = prime * result + ((actions == null) ? 0 : actions.hashCode());
295 result = prime * result + bufferId;
296 result = prime * result + command;
297 result = prime * result + (int) (cookie ^ (cookie >>> 32));
298 result = prime * result + flags;
299 result = prime * result + hardTimeout;
300 result = prime * result + idleTimeout;
301 result = prime * result + ((match == null) ? 0 : match.hashCode());
302 result = prime * result + outPort;
303 result = prime * result + priority;
304 return result;
305 }
306
307 @Override
308 public boolean equals(Object obj) {
309 if (this == obj) {
310 return true;
311 }
312 if (!super.equals(obj)) {
313 return false;
314 }
315 if (!(obj instanceof OFFlowMod)) {
316 return false;
317 }
318 OFFlowMod other = (OFFlowMod) obj;
319 if (actions == null) {
320 if (other.actions != null) {
321 return false;
322 }
323 } else if (!actions.equals(other.actions)) {
324 return false;
325 }
326 if (bufferId != other.bufferId) {
327 return false;
328 }
329 if (command != other.command) {
330 return false;
331 }
332 if (cookie != other.cookie) {
333 return false;
334 }
335 if (flags != other.flags) {
336 return false;
337 }
338 if (hardTimeout != other.hardTimeout) {
339 return false;
340 }
341 if (idleTimeout != other.idleTimeout) {
342 return false;
343 }
344 if (match == null) {
345 if (other.match != null) {
346 return false;
347 }
348 } else if (!match.equals(other.match)) {
349 return false;
350 }
351 if (outPort != other.outPort) {
352 return false;
353 }
354 if (priority != other.priority) {
355 return false;
356 }
357 return true;
358 }
359
360 /* (non-Javadoc)
361 * @see java.lang.Object#clone()
362 */
363 @Override
364 public OFFlowMod clone() throws CloneNotSupportedException {
365 OFMatch neoMatch = match.clone();
366 OFFlowMod flowMod= (OFFlowMod) super.clone();
367 flowMod.setMatch(neoMatch);
368 List<OFAction> neoActions = new LinkedList<OFAction>();
369 for(OFAction action: this.actions)
370 neoActions.add((OFAction) action.clone());
371 flowMod.setActions(neoActions);
372 return flowMod;
373 }
374
375 /* (non-Javadoc)
376 * @see java.lang.Object#toString()
377 */
378 @Override
379 public String toString() {
380 return "OFFlowMod [actionFactory=" + actionFactory + ", actions="
381 + actions + ", bufferId=" + bufferId + ", command=" + command
382 + ", cookie=" + cookie + ", flags=" + flags + ", hardTimeout="
383 + hardTimeout + ", idleTimeout=" + idleTimeout + ", match="
384 + match + ", outPort=" + outPort + ", priority=" + priority
385 + ", length=" + length + ", type=" + type + ", version="
386 + version + ", xid=" + xid + "]";
387 }
388}