blob: 0dde61caf4bc4f100a3793989081ca879be92867 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 * Copyright 2011, Big Switch Networks, Inc.
3 * Originally created by David Erickson, Stanford 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 **/
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080017
18package net.floodlightcontroller.core.internal;
19
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080020import org.jboss.netty.buffer.ChannelBuffer;
21import org.jboss.netty.channel.Channel;
22import org.jboss.netty.channel.ChannelHandlerContext;
23import org.jboss.netty.handler.codec.frame.FrameDecoder;
Saurav Dascc3e35f2014-10-10 15:33:32 -070024import org.projectfloodlight.openflow.exceptions.OFParseError;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070025import org.projectfloodlight.openflow.protocol.OFFactories;
26import org.projectfloodlight.openflow.protocol.OFMessage;
27import org.projectfloodlight.openflow.protocol.OFMessageReader;
Saurav Dascc3e35f2014-10-10 15:33:32 -070028import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080030
31/**
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070032 * Decode an openflow message from a Channel, for use in a netty pipeline
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080033 */
34public class OFMessageDecoder extends FrameDecoder {
Saurav Dascc3e35f2014-10-10 15:33:32 -070035 private static final Logger log = LoggerFactory.getLogger(OFMessageDecoder.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080036 @Override
37 protected Object decode(ChannelHandlerContext ctx, Channel channel,
38 ChannelBuffer buffer) throws Exception {
39 if (!channel.isConnected()) {
40 // In testing, I see decode being called AFTER decode last.
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070041 // This check avoids that from reading corrupted frames
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080042 return null;
43 }
44
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070045 // Note that a single call to decode results in reading a single
46 // OFMessage from the channel buffer, which is passed on to, and processed
47 // by, the controller (in OFChannelHandler).
48 // This is different from earlier behavior (with the original openflowj),
49 // where we parsed all the messages in the buffer, before passing on
50 // a list of the parsed messages to the controller.
51 // The performance *may or may not* not be as good as before.
52 OFMessageReader<OFMessage> reader = OFFactories.getGenericReader();
Saurav Dascc3e35f2014-10-10 15:33:32 -070053 OFMessage message = null;
54 try {
55 message = reader.readFrom(buffer);
56 } catch (OFParseError e) {
57 OFChannelHandler ofch = (OFChannelHandler) ctx.getPipeline().getLast();
58 log.error("Parse failure of incoming message from switch "
59 + ofch.getChannelSwitchInfo() + " Index:Byte ==> {}:{} {}",
60 buffer.readerIndex(),
61 buffer.getByte(buffer.readerIndex()),
62 buffer.array());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080063
Saurav Dascc3e35f2014-10-10 15:33:32 -070064 buffer.clear(); // MUST CLEAR BUFFER or next message will be read
65 // incorrectly
66 return null;
67 }
68
69 return message;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080070 }
71
72}