Fix checkstyle warnings in netconf-cli
[controller.git] / third-party / openflowj / src / main / java / org / openflow / protocol / OFPacketOut.java
1 package org.openflow.protocol;
2
3 import java.nio.ByteBuffer;
4 import java.util.Arrays;
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_packet_out message
14  *
15  * @author David Erickson (daviderickson@cs.stanford.edu) - Mar 12, 2010
16  */
17 public class OFPacketOut extends OFMessage implements OFActionFactoryAware {
18     public static int MINIMUM_LENGTH = 16;
19     public static int BUFFER_ID_NONE = 0xffffffff;
20
21     protected OFActionFactory actionFactory;
22     protected int bufferId;
23     protected short inPort;
24     protected short actionsLength;
25     protected List<OFAction> actions;
26     protected byte[] packetData;
27
28     public OFPacketOut() {
29         super();
30         this.type = OFType.PACKET_OUT;
31         this.length = U16.t(MINIMUM_LENGTH);
32     }
33
34     /**
35      * Get buffer_id
36      * @return
37      */
38     public int getBufferId() {
39         return this.bufferId;
40     }
41
42     /**
43      * Set buffer_id
44      * @param bufferId
45      */
46     public OFPacketOut setBufferId(int bufferId) {
47         this.bufferId = bufferId;
48         return this;
49     }
50
51     /**
52      * Returns the packet data
53      * @return
54      */
55     public byte[] getPacketData() {
56         return this.packetData;
57     }
58
59     /**
60      * Sets the packet data
61      * @param packetData
62      */
63     public OFPacketOut setPacketData(byte[] packetData) {
64         this.packetData = packetData;
65         return this;
66     }
67
68     /**
69      * Get in_port
70      * @return
71      */
72     public short getInPort() {
73         return this.inPort;
74     }
75
76     /**
77      * Set in_port
78      * @param inPort
79      */
80     public OFPacketOut setInPort(short inPort) {
81         this.inPort = inPort;
82         return this;
83     }
84
85     /**
86      * Set in_port. Convenience method using OFPort enum.
87      * @param inPort
88      */
89     public OFPacketOut setInPort(OFPort inPort) {
90         this.inPort = inPort.getValue();
91         return this;
92     }
93
94     /**
95      * Get actions_len
96      * @return
97      */
98     public short getActionsLength() {
99         return this.actionsLength;
100     }
101
102     /**
103      * Get actions_len, unsigned
104      * @return
105      */
106     public int getActionsLengthU() {
107         return U16.f(this.actionsLength);
108     }
109
110     /**
111      * Set actions_len
112      * @param actionsLength
113      */
114     public OFPacketOut setActionsLength(short actionsLength) {
115         this.actionsLength = actionsLength;
116         return this;
117     }
118
119     /**
120      * Returns the actions contained in this message
121      * @return a list of ordered OFAction objects
122      */
123     public List<OFAction> getActions() {
124         return this.actions;
125     }
126
127     /**
128      * Sets the list of actions on this message
129      * @param actions a list of ordered OFAction objects
130      */
131     public OFPacketOut setActions(List<OFAction> actions) {
132         this.actions = actions;
133         return this;
134     }
135
136     @Override
137     public void setActionFactory(OFActionFactory actionFactory) {
138         this.actionFactory = actionFactory;
139     }
140
141     @Override
142     public void readFrom(ByteBuffer data) {
143         super.readFrom(data);
144         this.bufferId = data.getInt();
145         this.inPort = data.getShort();
146         this.actionsLength = data.getShort();
147         if ( this.actionFactory == null)
148             throw new RuntimeException("ActionFactory not set");
149         this.actions = this.actionFactory.parseActions(data, getActionsLengthU());
150         this.packetData = new byte[getLengthU() - MINIMUM_LENGTH - getActionsLengthU()];
151         data.get(this.packetData);
152     }
153
154     @Override
155     public void writeTo(ByteBuffer data) {
156         super.writeTo(data);
157         data.putInt(bufferId);
158         data.putShort(inPort);
159         data.putShort(actionsLength);
160         for (OFAction action : actions) {
161             action.writeTo(data);
162         }
163         if (this.packetData != null)
164             data.put(this.packetData);
165     }
166
167     @Override
168     public int hashCode() {
169         final int prime = 293;
170         int result = super.hashCode();
171         result = prime * result + ((actions == null) ? 0 : actions.hashCode());
172         result = prime * result + actionsLength;
173         result = prime * result + bufferId;
174         result = prime * result + inPort;
175         result = prime * result + Arrays.hashCode(packetData);
176         return result;
177     }
178
179     @Override
180     public boolean equals(Object obj) {
181         if (this == obj) {
182             return true;
183         }
184         if (!super.equals(obj)) {
185             return false;
186         }
187         if (!(obj instanceof OFPacketOut)) {
188             return false;
189         }
190         OFPacketOut other = (OFPacketOut) obj;
191         if (actions == null) {
192             if (other.actions != null) {
193                 return false;
194             }
195         } else if (!actions.equals(other.actions)) {
196             return false;
197         }
198         if (actionsLength != other.actionsLength) {
199             return false;
200         }
201         if (bufferId != other.bufferId) {
202             return false;
203         }
204         if (inPort != other.inPort) {
205             return false;
206         }
207         if (!Arrays.equals(packetData, other.packetData)) {
208             return false;
209         }
210         return true;
211     }
212
213     /* (non-Javadoc)
214      * @see java.lang.Object#toString()
215      */
216     @Override
217     public String toString() {
218         return "OFPacketOut [actionFactory=" + actionFactory + ", actions="
219                 + actions + ", actionsLength=" + actionsLength + ", bufferId=0x"
220                 + Integer.toHexString(bufferId) + ", inPort=" + inPort + ", packetData="
221                 + Arrays.toString(packetData) + "]";
222     }
223 }