1 package org.openflow.codec.protocol.instruction;
3 import java.util.ArrayList;
6 import org.openflow.codec.io.IDataBuffer;
7 import org.openflow.codec.protocol.action.OFPAction;
8 import org.openflow.codec.protocol.factory.OFPActionFactory;
9 import org.openflow.codec.protocol.factory.OFPActionFactoryAware;
10 import org.openflow.codec.util.U16;
13 * base class for instruction structure WRITE/APPLY/CLEAR_ACTIONS correspond to
14 * struct ofp_instruction_actions
19 public abstract class OFPInstructionActions extends OFPInstruction implements OFPActionFactoryAware {
21 private final static short MINIMUM_LENGTH = 8;
22 private List<OFPAction> actions;
23 private OFPActionFactory actionFactory;
28 public OFPInstructionActions() {
29 actions = new ArrayList<OFPAction>();
30 this.setLength(MINIMUM_LENGTH);
35 * read OFPInstruction from buffer
39 public void readFrom(IDataBuffer data) {
42 if (null == actionFactory) {
43 throw new RuntimeException("OFPActionFactory is not set.");
45 int actionDataLength = U16.f(this.getLength()) - MINIMUM_LENGTH;
47 actions = actionFactory.parseActions(data, actionDataLength);
52 * write OFPInstruction to buffer
56 public void writeTo(IDataBuffer data) {
58 data.putInt(0); // pad
60 for (OFPAction ofAction : actions) {
61 ofAction.writeTo(data);
66 * get actions in this instruction
70 public List<OFPAction> getActions() {
75 * set actions in this instructions.
80 public void setActions(List<OFPAction> actions) {
81 this.actions = actions;
86 * get the length of instruction actions
90 private void updateLength() {
91 length = MINIMUM_LENGTH;
92 for (OFPAction ofAction : actions) {
93 length += ofAction.getLengthU();
98 public void setActionFactory(OFPActionFactory actionFactory) {
99 this.actionFactory = actionFactory;
104 public int hashCode() {
105 final int prime = 742;
106 int result = super.hashCode();
107 result = prime * result + actions.hashCode();
112 public boolean equals(Object obj) {
116 if (!super.equals(obj)) {
119 if (!(obj instanceof OFPInstructionActions)) {
122 OFPInstructionActions other = (OFPInstructionActions) obj;
123 if (!this.actions.equals(other.actions)) {
132 * @see java.lang.Object#clone()
135 public OFPInstructionActions clone() throws CloneNotSupportedException {
136 OFPInstructionActions clone = (OFPInstructionActions) super.clone();
137 clone.actions = new ArrayList<OFPAction>();
138 for (OFPAction action : this.actions) {
139 clone.actions.add(action.clone());
146 * Returns a string representation of the instruction
148 public String toString() {
149 return "OFPInstruction[" + "type=" + this.getOFInstructionType() + ", length=" + this.getLength()
150 + ", actions=" + actions.toString() + "]";