blob: 78ff2c249f177bb89568919d95212806c808383f [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
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
tom7ef8ff92014-09-17 13:08:06 -07009 *
Thomas Vachuska781d18b2014-10-27 10:31:25 -070010 * http://www.apache.org/licenses/LICENSE-2.0
tom7ef8ff92014-09-17 13:08:06 -070011 *
Thomas Vachuska781d18b2014-10-27 10:31:25 -070012 * 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 */
tom7ef8ff92014-09-17 13:08:06 -070019
tom9c94c5b2014-09-17 13:14:42 -070020package org.onlab.onos.openflow.controller.impl;
tom7ef8ff92014-09-17 13:08:06 -070021
22
23import org.jboss.netty.buffer.ChannelBuffer;
24import org.jboss.netty.channel.Channel;
25import org.jboss.netty.channel.ChannelHandlerContext;
26import org.jboss.netty.handler.codec.frame.FrameDecoder;
27import org.projectfloodlight.openflow.protocol.OFFactories;
28import org.projectfloodlight.openflow.protocol.OFMessage;
29import org.projectfloodlight.openflow.protocol.OFMessageReader;
30
31/**
32 * Decode an openflow message from a Channel, for use in a netty pipeline.
33 */
34public 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}