blob: 14894f26557bb6894b8e3c7ef35b6f7269fe2cd8 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present 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
Brian O'Connorabafb502014-12-02 22:26:20 -080017package org.onosproject.openflow.controller.impl;
tom7ef8ff92014-09-17 13:08:06 -070018
19
Jimmy Jine9b7a022016-08-12 16:56:48 -070020import io.netty.buffer.ByteBuf;
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070021import io.netty.channel.ChannelHandlerContext;
22import io.netty.handler.codec.ByteToMessageDecoder;
23
24import static org.slf4j.LoggerFactory.getLogger;
25
26import java.util.List;
27
tom7ef8ff92014-09-17 13:08:06 -070028import org.projectfloodlight.openflow.protocol.OFFactories;
29import org.projectfloodlight.openflow.protocol.OFMessage;
30import org.projectfloodlight.openflow.protocol.OFMessageReader;
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070031import org.slf4j.Logger;
tom7ef8ff92014-09-17 13:08:06 -070032
33/**
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070034 * Decode an openflow message from a netty channel, for use in a netty pipeline.
tom7ef8ff92014-09-17 13:08:06 -070035 */
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070036public 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
tom7ef8ff92014-09-17 13:08:06 -070047
48 @Override
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070049 protected void decode(ChannelHandlerContext ctx,
50 ByteBuf byteBuf,
51 List<Object> out) throws Exception {
52
53 if (!ctx.channel().isActive()) {
tom7ef8ff92014-09-17 13:08:06 -070054 // In testing, I see decode being called AFTER decode last.
55 // This check avoids that from reading corrupted frames
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070056 return;
tom7ef8ff92014-09-17 13:08:06 -070057 }
58
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070059 // Note that a single call to readFrom results in reading a single
tom7ef8ff92014-09-17 13:08:06 -070060 // 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 Jine9b7a022016-08-12 16:56:48 -070067
Jimmy Jine9b7a022016-08-12 16:56:48 -070068 OFMessage message = reader.readFrom(byteBuf);
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070069 while (message != null) {
70 out.add(message);
71 message = reader.readFrom(byteBuf);
Jimmy Jine9b7a022016-08-12 16:56:48 -070072 }
tom7ef8ff92014-09-17 13:08:06 -070073 }
74
75}