Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 20 | import io.netty.buffer.ByteBuf; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 21 | import io.netty.channel.ChannelHandlerContext; |
| 22 | import io.netty.handler.codec.ByteToMessageDecoder; |
| 23 | |
| 24 | import static org.slf4j.LoggerFactory.getLogger; |
| 25 | |
| 26 | import java.util.List; |
| 27 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 28 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 29 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 30 | import org.projectfloodlight.openflow.protocol.OFMessageReader; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 31 | import org.slf4j.Logger; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 34 | * Decode an openflow message from a netty channel, for use in a netty pipeline. |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 35 | */ |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 36 | public final class OFMessageDecoder extends ByteToMessageDecoder { |
| 37 | |
| 38 | private static final Logger log = getLogger(OFMessageDecoder.class); |
| 39 | |
| 40 | public static OFMessageDecoder getInstance() { |
| 41 | // not Sharable |
| 42 | return new OFMessageDecoder(); |
| 43 | } |
| 44 | |
| 45 | private OFMessageDecoder() {} |
| 46 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 47 | |
| 48 | @Override |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 49 | protected void decode(ChannelHandlerContext ctx, |
| 50 | ByteBuf byteBuf, |
| 51 | List<Object> out) throws Exception { |
| 52 | |
| 53 | if (!ctx.channel().isActive()) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 54 | // In testing, I see decode being called AFTER decode last. |
| 55 | // This check avoids that from reading corrupted frames |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 56 | return; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 59 | // Note that a single call to readFrom results in reading a single |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 60 | // OFMessage from the channel buffer, which is passed on to, and processed |
| 61 | // by, the controller (in OFChannelHandler). |
| 62 | // This is different from earlier behavior (with the original openflowj), |
| 63 | // where we parsed all the messages in the buffer, before passing on |
| 64 | // a list of the parsed messages to the controller. |
| 65 | // The performance *may or may not* not be as good as before. |
| 66 | OFMessageReader<OFMessage> reader = OFFactories.getGenericReader(); |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 67 | |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 68 | OFMessage message = reader.readFrom(byteBuf); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 69 | while (message != null) { |
| 70 | out.add(message); |
| 71 | message = reader.readFrom(byteBuf); |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 72 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | } |