2 * Copyright 2014 Open Networking Laboratory
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onosproject.openflow.controller.impl;
20 import org.jboss.netty.buffer.ChannelBuffer;
21 import org.jboss.netty.channel.Channel;
22 import org.jboss.netty.channel.ChannelHandlerContext;
23 import org.jboss.netty.handler.codec.frame.FrameDecoder;
24 import org.projectfloodlight.openflow.protocol.OFFactories;
25 import org.projectfloodlight.openflow.protocol.OFMessage;
26 import org.projectfloodlight.openflow.protocol.OFMessageReader;
29 * Decode an openflow message from a Channel, for use in a netty pipeline.
31 public class OFMessageDecoder extends FrameDecoder {
34 protected Object decode(ChannelHandlerContext ctx, Channel channel,
35 ChannelBuffer buffer) throws Exception {
36 if (!channel.isConnected()) {
37 // In testing, I see decode being called AFTER decode last.
38 // This check avoids that from reading corrupted frames
42 // Note that a single call to decode results in reading a single
43 // OFMessage from the channel buffer, which is passed on to, and processed
44 // by, the controller (in OFChannelHandler).
45 // This is different from earlier behavior (with the original openflowj),
46 // where we parsed all the messages in the buffer, before passing on
47 // a list of the parsed messages to the controller.
48 // The performance *may or may not* not be as good as before.
49 OFMessageReader<OFMessage> reader = OFFactories.getGenericReader();
50 OFMessage message = reader.readFrom(buffer);