blob: c541a9b6baaca2ee889dc12f250515fcc1670a5a [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Jimmy Jine9b7a022016-08-12 16:56:48 -070019import io.netty.buffer.ByteBuf;
20import io.netty.buffer.Unpooled;
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070021import io.netty.channel.ChannelHandler.Sharable;
22import io.netty.channel.ChannelHandlerContext;
23import io.netty.channel.ChannelOutboundHandlerAdapter;
24import io.netty.channel.ChannelPromise;
25import io.netty.handler.codec.EncoderException;
26import static org.slf4j.LoggerFactory.getLogger;
27
tom7ef8ff92014-09-17 13:08:06 -070028import org.projectfloodlight.openflow.protocol.OFMessage;
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070029import org.slf4j.Logger;
tom7ef8ff92014-09-17 13:08:06 -070030
tom7ef8ff92014-09-17 13:08:06 -070031/**
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070032 * Encode an openflow message for output into a netty channel, for use in a
tom7ef8ff92014-09-17 13:08:06 -070033 * netty pipeline.
34 */
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070035@Sharable
36public final class OFMessageEncoder extends ChannelOutboundHandlerAdapter {
tom7ef8ff92014-09-17 13:08:06 -070037
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070038 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
tom7ef8ff92014-09-17 13:08:06 -070056 @Override
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070057 public void write(ChannelHandlerContext ctx,
58 Object msg,
Ray Milkey986a47a2018-01-25 11:38:51 -080059 ChannelPromise promise) {
tom7ef8ff92014-09-17 13:08:06 -070060
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070061 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();
tom7ef8ff92014-09-17 13:08:06 -070067
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070068 encode(ctx, ofmsgs, buf);
tom7ef8ff92014-09-17 13:08:06 -070069
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070070 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 Chane64d3752015-10-21 13:03:58 -070092 }
tom7ef8ff92014-09-17 13:08:06 -070093 }
tom7ef8ff92014-09-17 13:08:06 -070094 }
95
96}