blob: c5cb9f7de48af228d90679675e8bb777bc71ba47 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
tom7ef8ff92014-09-17 13:08:06 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
tom7ef8ff92014-09-17 13:08:06 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
tom7ef8ff92014-09-17 13:08:06 -070016
Brian O'Connorabafb502014-12-02 22:26:20 -080017package org.onosproject.openflow.controller.impl;
tom7ef8ff92014-09-17 13:08:06 -070018
19
Jimmy Jine9b7a022016-08-12 16:56:48 -070020import io.netty.buffer.ByteBuf;
21import io.netty.buffer.Unpooled;
tom7ef8ff92014-09-17 13:08:06 -070022import org.jboss.netty.buffer.ChannelBuffer;
23import org.jboss.netty.channel.Channel;
24import org.jboss.netty.channel.ChannelHandlerContext;
25import org.jboss.netty.handler.codec.frame.FrameDecoder;
26import org.projectfloodlight.openflow.protocol.OFFactories;
27import org.projectfloodlight.openflow.protocol.OFMessage;
28import org.projectfloodlight.openflow.protocol.OFMessageReader;
29
30/**
31 * Decode an openflow message from a Channel, for use in a netty pipeline.
32 */
33public class OFMessageDecoder extends FrameDecoder {
34
35 @Override
36 protected Object decode(ChannelHandlerContext ctx, Channel channel,
37 ChannelBuffer buffer) throws Exception {
38 if (!channel.isConnected()) {
39 // In testing, I see decode being called AFTER decode last.
40 // This check avoids that from reading corrupted frames
41 return null;
42 }
43
44 // Note that a single call to decode results in reading a single
45 // OFMessage from the channel buffer, which is passed on to, and processed
46 // by, the controller (in OFChannelHandler).
47 // This is different from earlier behavior (with the original openflowj),
48 // where we parsed all the messages in the buffer, before passing on
49 // a list of the parsed messages to the controller.
50 // The performance *may or may not* not be as good as before.
51 OFMessageReader<OFMessage> reader = OFFactories.getGenericReader();
Jimmy Jine9b7a022016-08-12 16:56:48 -070052
53 //toByteBuffer is optimized to avoid copying.
54 ByteBuf byteBuf = Unpooled.wrappedBuffer(buffer.toByteBuffer());
55 OFMessage message = reader.readFrom(byteBuf);
56
57 if (message != null) {
58 //set buffer's read index, as it has not been changed.
59 buffer.readerIndex(buffer.readerIndex() + byteBuf.readerIndex());
60 }
tom7ef8ff92014-09-17 13:08:06 -070061
62 return message;
63 }
64
65}