2 * Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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
18 package org.openflow.protocol;
20 import java.text.DateFormat;
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23 import java.util.concurrent.ConcurrentHashMap;
26 import org.jboss.netty.buffer.ChannelBuffer;
27 import org.openflow.util.HexString;
28 import org.openflow.util.U16;
29 import org.openflow.util.U32;
30 import org.openflow.util.U8;
33 * The base class for all OpenFlow protocol messages. This class contains the
34 * equivalent of the ofp_header which is present in all OpenFlow messages.
36 * @author David Erickson (daviderickson@cs.stanford.edu) - Feb 3, 2010
37 * @author Rob Sherwood (rob.sherwood@stanford.edu) - Feb 3, 2010
39 public class OFMessage {
40 public static final int MAXIMUM_LENGTH = (1 << 16) - 1;
41 public static byte OFP_VERSION = 0x01;
42 public static int MINIMUM_LENGTH = 8;
44 protected byte version;
45 protected OFType type;
46 protected short length;
49 private ConcurrentHashMap<String, Object> storage;
53 this.version = OFP_VERSION;
56 protected synchronized ConcurrentHashMap<String, Object> getMessageStore() {
57 if (storage == null) {
58 storage = new ConcurrentHashMap<String, Object>();;
64 * Get the length of this message
68 public short getLength() {
73 * Get the length of this message, unsigned
77 public int getLengthU() {
82 * Set the length of this message
86 public OFMessage setLength(short length) {
92 * Set the length of this message, unsigned
96 public OFMessage setLengthU(int length) {
97 this.length = U16.t(length);
102 * Get the type of this message
106 public OFType getType() {
111 * Set the type of this message
115 public void setType(OFType type) {
120 * Get the OpenFlow version of this message
124 public byte getVersion() {
129 * Set the OpenFlow version of this message
133 public void setVersion(byte version) {
134 this.version = version;
138 * Get the transaction id of this message
142 public int getXid() {
147 * Set the transaction id of this message
151 public void setXid(int xid) {
156 * Read this message off the wire from the specified ByteBuffer
159 public void readFrom(ChannelBuffer data) {
160 this.version = data.readByte();
161 this.type = OFType.valueOf(data.readByte());
162 this.length = data.readShort();
163 this.xid = data.readInt();
167 * Write this message's binary format to the specified ByteBuffer
170 public void writeTo(ChannelBuffer data) {
171 data.writeByte(version);
172 data.writeByte(type.getTypeValue());
173 data.writeShort(length);
178 * Returns a summary of the message
179 * @return "ofmsg=v=$version;t=$type:l=$len:xid=$xid"
182 public String toString() {
184 ":v=" + U8.f(this.getVersion()) +
185 ";t=" + this.getType() +
186 ";l=" + this.getLengthU() +
187 ";x=" + U32.f(this.getXid());
191 public int hashCode() {
192 final int prime = 97;
194 result = prime * result + length;
195 result = prime * result + ((type == null) ? 0 : type.hashCode());
196 result = prime * result + version;
197 result = prime * result + xid;
202 public boolean equals(Object obj) {
209 if (!(obj instanceof OFMessage)) {
212 OFMessage other = (OFMessage) obj;
213 if (length != other.length) {
217 if (other.type != null) {
220 } else if (!type.equals(other.type)) {
223 if (version != other.version) {
226 if (xid != other.xid) {
233 public static String getDataAsString(IOFSwitch sw, OFMessage msg) {
237 StringBuffer sb = new StringBuffer("");
239 DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
240 Date date = new Date();
242 sb.append(dateFormat.format(date));
245 switch (msg.getType()) {
247 OFPacketIn pktIn = (OFPacketIn) msg;
248 sb.append("packet_in [ ");
249 sb.append(sw.getStringId());
250 sb.append(" -> Controller");
253 sb.append("\ntotal length: ");
254 sb.append(pktIn.getTotalLength());
255 sb.append("\nin_port: ");
256 sb.append(pktIn.getInPort());
257 sb.append("\ndata_length: ");
258 sb.append(pktIn.getTotalLength() - OFPacketIn.MINIMUM_LENGTH);
259 sb.append("\nbuffer: ");
260 sb.append(pktIn.getBufferId());
265 OFPacketOut pktOut = (OFPacketOut) msg;
266 sb.append("packet_out [ ");
267 sb.append("Controller -> ");
268 sb.append(HexString.toHexString(sw.getId()));
271 sb.append("\nin_port: ");
272 sb.append(pktOut.getInPort());
273 sb.append("\nactions_len: ");
274 sb.append(pktOut.getActionsLength());
275 if (pktOut.getActions() != null) {
276 sb.append("\nactions: ");
277 sb.append(pktOut.getActions().toString());
282 OFFlowMod fm = (OFFlowMod) msg;
283 sb.append("flow_mod [ ");
284 sb.append("Controller -> ");
285 sb.append(HexString.toHexString(sw.getId()));
289 sb.append("\nADD: cookie: ");
290 sb.append(fm.getCookie());
291 sb.append(" idle: ");
292 sb.append(fm.getIdleTimeout());
293 sb.append(" hard: ");
294 sb.append(fm.getHardTimeout());
296 sb.append(fm.getPriority());
298 sb.append(fm.getBufferId());
300 sb.append(fm.getFlags());
301 if (fm.getActions() != null) {
302 sb.append("\nactions: ");
303 sb.append(fm.getActions().toString());
308 sb.append("[Unknown Packet]");
312 return sb.toString();
318 // Check if this is really required
320 public static byte[] getData(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
321 return OFMessage.getDataAsString(sw, msg, cntx).getBytes();