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 | |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 19 | import io.netty.buffer.ByteBuf; |
| 20 | import io.netty.buffer.Unpooled; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 21 | import io.netty.channel.ChannelHandler.Sharable; |
| 22 | import io.netty.channel.ChannelHandlerContext; |
| 23 | import io.netty.channel.ChannelOutboundHandlerAdapter; |
| 24 | import io.netty.channel.ChannelPromise; |
| 25 | import io.netty.handler.codec.EncoderException; |
| 26 | import static org.slf4j.LoggerFactory.getLogger; |
| 27 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 28 | import org.projectfloodlight.openflow.protocol.OFMessage; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 29 | import org.slf4j.Logger; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 30 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 31 | /** |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 32 | * Encode an openflow message for output into a netty channel, for use in a |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 33 | * netty pipeline. |
| 34 | */ |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 35 | @Sharable |
| 36 | public final class OFMessageEncoder extends ChannelOutboundHandlerAdapter { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 37 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 38 | private static final Logger log = getLogger(OFMessageEncoder.class); |
| 39 | |
| 40 | private static final OFMessageEncoder INSTANCE = new OFMessageEncoder(); |
| 41 | |
| 42 | public static OFMessageEncoder getInstance() { |
| 43 | return INSTANCE; |
| 44 | } |
| 45 | |
| 46 | private OFMessageEncoder() {} |
| 47 | |
| 48 | protected final void encode(ChannelHandlerContext ctx, |
| 49 | Iterable<OFMessage> msgs, |
| 50 | ByteBuf out) throws Exception { |
| 51 | |
| 52 | msgs.forEach(msg -> msg.writeTo(out)); |
| 53 | } |
| 54 | |
| 55 | // MessageToByteEncoder without dependency to TypeParameterMatcher |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 56 | @Override |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 57 | public void write(ChannelHandlerContext ctx, |
| 58 | Object msg, |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame^] | 59 | ChannelPromise promise) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 60 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 61 | ByteBuf buf = null; |
| 62 | try { |
| 63 | if (msg instanceof Iterable) { |
| 64 | @SuppressWarnings("unchecked") |
| 65 | Iterable<OFMessage> ofmsgs = (Iterable<OFMessage>) msg; |
| 66 | buf = ctx.alloc().ioBuffer(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 67 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 68 | encode(ctx, ofmsgs, buf); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 69 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 70 | if (buf.isReadable()) { |
| 71 | ctx.write(buf, promise); |
| 72 | } else { |
| 73 | log.warn("NOTHING WAS WRITTEN for {}", msg); |
| 74 | buf.release(); |
| 75 | ctx.write(Unpooled.EMPTY_BUFFER, promise); |
| 76 | } |
| 77 | buf = null; |
| 78 | |
| 79 | } else { |
| 80 | log.warn("Attempted to encode unexpected message: {}", msg); |
| 81 | ctx.write(msg, promise); |
| 82 | } |
| 83 | } catch (EncoderException e) { |
| 84 | log.error("EncoderException handling {}", msg, e); |
| 85 | throw e; |
| 86 | } catch (Throwable e) { |
| 87 | log.error("Exception handling {}", msg, e); |
| 88 | throw new EncoderException(e); |
| 89 | } finally { |
| 90 | if (buf != null) { |
| 91 | buf.release(); |
Charles Chan | e64d375 | 2015-10-21 13:03:58 -0700 | [diff] [blame] | 92 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 93 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | } |