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