blob: f66966f6f549399ad1e78f9007acc49c5e7a79eb [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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
tom9c94c5b2014-09-17 13:14:42 -070017package org.onlab.onos.openflow.controller.impl;
tom7ef8ff92014-09-17 13:08:06 -070018
19import java.util.List;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.jboss.netty.buffer.ChannelBuffers;
23import org.jboss.netty.channel.Channel;
24import org.jboss.netty.channel.ChannelHandlerContext;
25import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
26import org.projectfloodlight.openflow.protocol.OFMessage;
27
28
29/**
30 * Encode an openflow message for output into a ChannelBuffer, for use in a
31 * netty pipeline.
32 */
33public class OFMessageEncoder extends OneToOneEncoder {
34
35 @Override
36 protected Object encode(ChannelHandlerContext ctx, Channel channel,
37 Object msg) throws Exception {
38 if (!(msg instanceof List)) {
39 return msg;
40 }
41
42 @SuppressWarnings("unchecked")
43 List<OFMessage> msglist = (List<OFMessage>) msg;
44 /* XXX S can't get length of OFMessage in loxigen's openflowj??
45 int size = 0;
46 for (OFMessage ofm : msglist) {
47 size += ofm.getLengthU();
48 }*/
49
50 ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
51
52 for (OFMessage ofm : msglist) {
53 ofm.writeTo(buf);
54 }
55 return buf;
56 }
57
58}