Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 16 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 17 | package org.onosproject.openflow.controller.impl; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 18 | |
| 19 | |
| 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; |
| 27 | |
| 28 | /** |
| 29 | * Decode an openflow message from a Channel, for use in a netty pipeline. |
| 30 | */ |
| 31 | public class OFMessageDecoder extends FrameDecoder { |
| 32 | |
| 33 | @Override |
| 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 |
| 39 | return null; |
| 40 | } |
| 41 | |
| 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); |
| 51 | |
| 52 | return message; |
| 53 | } |
| 54 | |
| 55 | } |