blob: fce2804cd64860ee2fb82a95ee850630bf8a5bea [file] [log] [blame]
tom7ef8ff92014-09-17 13:08:06 -07001/**
2 * 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 **/
17
tom9c94c5b2014-09-17 13:14:42 -070018package org.onlab.onos.openflow.controller.impl;
tom7ef8ff92014-09-17 13:08:06 -070019
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.jboss.netty.channel.Channel;
23import org.jboss.netty.channel.ChannelHandlerContext;
24import org.jboss.netty.handler.codec.frame.FrameDecoder;
25import org.projectfloodlight.openflow.protocol.OFFactories;
26import org.projectfloodlight.openflow.protocol.OFMessage;
27import org.projectfloodlight.openflow.protocol.OFMessageReader;
28
29/**
30 * Decode an openflow message from a Channel, for use in a netty pipeline.
31 */
32public class OFMessageDecoder extends FrameDecoder {
33
34 @Override
35 protected Object decode(ChannelHandlerContext ctx, Channel channel,
36 ChannelBuffer buffer) throws Exception {
37 if (!channel.isConnected()) {
38 // In testing, I see decode being called AFTER decode last.
39 // This check avoids that from reading corrupted frames
40 return null;
41 }
42
43 // Note that a single call to decode results in reading a single
44 // OFMessage from the channel buffer, which is passed on to, and processed
45 // by, the controller (in OFChannelHandler).
46 // This is different from earlier behavior (with the original openflowj),
47 // where we parsed all the messages in the buffer, before passing on
48 // a list of the parsed messages to the controller.
49 // The performance *may or may not* not be as good as before.
50 OFMessageReader<OFMessage> reader = OFFactories.getGenericReader();
51 OFMessage message = reader.readFrom(buffer);
52
53 return message;
54 }
55
56}