blob: 727301f594c73bb533b7388cd3d6a27e077d8a63 [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
19import java.util.List;
20
Jimmy Jine9b7a022016-08-12 16:56:48 -070021import io.netty.buffer.ByteBuf;
22import io.netty.buffer.Unpooled;
tom7ef8ff92014-09-17 13:08:06 -070023import org.jboss.netty.buffer.ChannelBuffer;
24import org.jboss.netty.buffer.ChannelBuffers;
25import org.jboss.netty.channel.Channel;
26import org.jboss.netty.channel.ChannelHandlerContext;
27import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
28import org.projectfloodlight.openflow.protocol.OFMessage;
29
tom7ef8ff92014-09-17 13:08:06 -070030/**
31 * Encode an openflow message for output into a ChannelBuffer, for use in a
32 * netty pipeline.
33 */
34public class OFMessageEncoder extends OneToOneEncoder {
35
36 @Override
37 protected Object encode(ChannelHandlerContext ctx, Channel channel,
38 Object msg) throws Exception {
39 if (!(msg instanceof List)) {
40 return msg;
41 }
42
43 @SuppressWarnings("unchecked")
44 List<OFMessage> msglist = (List<OFMessage>) msg;
45 /* XXX S can't get length of OFMessage in loxigen's openflowj??
46 int size = 0;
47 for (OFMessage ofm : msglist) {
48 size += ofm.getLengthU();
49 }*/
50
Jimmy Jine9b7a022016-08-12 16:56:48 -070051 ByteBuf bb = Unpooled.buffer();
tom7ef8ff92014-09-17 13:08:06 -070052
53 for (OFMessage ofm : msglist) {
Charles Chane64d3752015-10-21 13:03:58 -070054 if (ofm != null) {
Jimmy Jine9b7a022016-08-12 16:56:48 -070055 ofm.writeTo(bb);
Charles Chane64d3752015-10-21 13:03:58 -070056 }
tom7ef8ff92014-09-17 13:08:06 -070057 }
Jimmy Jine9b7a022016-08-12 16:56:48 -070058
59 ChannelBuffer buf = ChannelBuffers.wrappedBuffer(bb.nioBuffer());
60
tom7ef8ff92014-09-17 13:08:06 -070061 return buf;
62 }
63
64}