blob: 5672eff60884e77eb3e4e6e7d125535ab988258c [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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
tom9c94c5b2014-09-17 13:14:42 -070017package org.onlab.onos.openflow.controller.impl;
tom7ef8ff92014-09-17 13:08:06 -070018
19
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.jboss.netty.channel.Channel;
22import org.jboss.netty.channel.ChannelHandlerContext;
23import org.jboss.netty.handler.codec.frame.FrameDecoder;
24import org.projectfloodlight.openflow.protocol.OFFactories;
25import org.projectfloodlight.openflow.protocol.OFMessage;
26import org.projectfloodlight.openflow.protocol.OFMessageReader;
27
28/**
29 * Decode an openflow message from a Channel, for use in a netty pipeline.
30 */
31public 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}