f52d27e57c21af58380fd49a3f0cbbfac34f3d5e
[onosfw.git] /
1 /*
2  * Copyright 2014 Open Networking Laboratory
3  *
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
7  *
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.
15  */
16
17 package org.onosproject.openflow.controller.impl;
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 }