6c2700a24063d9cc4b1752adf3fb3eca4bc40a09
[openflowjava.git] / third-party / openflow-codec / src / main / java / org / openflow / codec / protocol / instruction / OFPInstructionActions.java
1 package org.openflow.codec.protocol.instruction;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
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;
11
12 /**
13  * base class for instruction structure WRITE/APPLY/CLEAR_ACTIONS correspond to
14  * struct ofp_instruction_actions
15  *
16  * @author AnilGujele
17  *
18  */
19 public abstract class OFPInstructionActions extends OFPInstruction implements OFPActionFactoryAware {
20
21     private final static short MINIMUM_LENGTH = 8;
22     private List<OFPAction> actions;
23     private OFPActionFactory actionFactory;
24
25     /**
26      * constructor
27      */
28     public OFPInstructionActions() {
29         actions = new ArrayList<OFPAction>();
30         this.setLength(MINIMUM_LENGTH);
31
32     }
33
34     /**
35      * read OFPInstruction from buffer
36      *
37      * @param data
38      */
39     public void readFrom(IDataBuffer data) {
40         super.readFrom(data);
41         data.getInt(); // pad
42         if (null == actionFactory) {
43             throw new RuntimeException("OFPActionFactory is not set.");
44         }
45         int actionDataLength = U16.f(this.getLength()) - MINIMUM_LENGTH;
46         // read actions
47         actions = actionFactory.parseActions(data, actionDataLength);
48
49     }
50
51     /**
52      * write OFPInstruction to buffer
53      *
54      * @param data
55      */
56     public void writeTo(IDataBuffer data) {
57         super.writeTo(data);
58         data.putInt(0); // pad
59         // write action
60         for (OFPAction ofAction : actions) {
61             ofAction.writeTo(data);
62         }
63     }
64
65     /**
66      * get actions in this instruction
67      *
68      * @return
69      */
70     public List<OFPAction> getActions() {
71         return actions;
72     }
73
74     /**
75      * set actions in this instructions.
76      *
77      * @param actions
78      */
79
80     public void setActions(List<OFPAction> actions) {
81         this.actions = actions;
82         updateLength();
83     }
84
85     /**
86      * get the length of instruction actions
87      *
88      * @return
89      */
90     private void updateLength() {
91         length = MINIMUM_LENGTH;
92         for (OFPAction ofAction : actions) {
93             length += ofAction.getLengthU();
94         }
95     }
96
97     @Override
98     public void setActionFactory(OFPActionFactory actionFactory) {
99         this.actionFactory = actionFactory;
100
101     }
102
103     @Override
104     public int hashCode() {
105         final int prime = 742;
106         int result = super.hashCode();
107         result = prime * result + actions.hashCode();
108         return result;
109     }
110
111     @Override
112     public boolean equals(Object obj) {
113         if (this == obj) {
114             return true;
115         }
116         if (!super.equals(obj)) {
117             return false;
118         }
119         if (!(obj instanceof OFPInstructionActions)) {
120             return false;
121         }
122         OFPInstructionActions other = (OFPInstructionActions) obj;
123         if (!this.actions.equals(other.actions)) {
124             return false;
125         }
126         return true;
127     }
128
129     /*
130      * (non-Javadoc)
131      *
132      * @see java.lang.Object#clone()
133      */
134     @Override
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());
140         }
141
142         return clone;
143     }
144
145     /**
146      * Returns a string representation of the instruction
147      */
148     public String toString() {
149         return "OFPInstruction[" + "type=" + this.getOFInstructionType() + ", length=" + this.getLength()
150                 + ", actions=" + actions.toString() + "]";
151     }
152
153 }