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