Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 9 | * |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame^] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 11 | * |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame^] | 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 19 | |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 20 | package org.onlab.onos.openflow.controller.impl; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 21 | |
| 22 | |
| 23 | import org.jboss.netty.buffer.ChannelBuffer; |
| 24 | import org.jboss.netty.channel.Channel; |
| 25 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 26 | import org.jboss.netty.handler.codec.frame.FrameDecoder; |
| 27 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 28 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 29 | import org.projectfloodlight.openflow.protocol.OFMessageReader; |
| 30 | |
| 31 | /** |
| 32 | * Decode an openflow message from a Channel, for use in a netty pipeline. |
| 33 | */ |
| 34 | public class OFMessageDecoder extends FrameDecoder { |
| 35 | |
| 36 | @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. |
| 41 | // This check avoids that from reading corrupted frames |
| 42 | return null; |
| 43 | } |
| 44 | |
| 45 | // 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(); |
| 53 | OFMessage message = reader.readFrom(buffer); |
| 54 | |
| 55 | return message; |
| 56 | } |
| 57 | |
| 58 | } |