blob: febffe1aefc05dbe9132ec3ed39c3aceb7f18c24 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
tom7ef8ff92014-09-17 13:08:06 -07009 *
Thomas Vachuska781d18b2014-10-27 10:31:25 -070010 * http://www.apache.org/licenses/LICENSE-2.0
tom7ef8ff92014-09-17 13:08:06 -070011 *
Thomas Vachuska781d18b2014-10-27 10:31:25 -070012 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
tom7ef8ff92014-09-17 13:08:06 -070019
tom9c94c5b2014-09-17 13:14:42 -070020package org.onlab.onos.openflow.controller.impl;
tom7ef8ff92014-09-17 13:08:06 -070021
22import java.util.List;
23
24import org.jboss.netty.buffer.ChannelBuffer;
25import org.jboss.netty.buffer.ChannelBuffers;
26import org.jboss.netty.channel.Channel;
27import org.jboss.netty.channel.ChannelHandlerContext;
28import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
29import org.projectfloodlight.openflow.protocol.OFMessage;
30
31
32/**
33 * Encode an openflow message for output into a ChannelBuffer, for use in a
34 * netty pipeline.
35 */
36public class OFMessageEncoder extends OneToOneEncoder {
37
38 @Override
39 protected Object encode(ChannelHandlerContext ctx, Channel channel,
40 Object msg) throws Exception {
41 if (!(msg instanceof List)) {
42 return msg;
43 }
44
45 @SuppressWarnings("unchecked")
46 List<OFMessage> msglist = (List<OFMessage>) msg;
47 /* XXX S can't get length of OFMessage in loxigen's openflowj??
48 int size = 0;
49 for (OFMessage ofm : msglist) {
50 size += ofm.getLengthU();
51 }*/
52
53 ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
54
55 for (OFMessage ofm : msglist) {
56 ofm.writeTo(buf);
57 }
58 return buf;
59 }
60
61}