Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
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 | |
| 19 | import java.util.List; |
| 20 | |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| 22 | import org.jboss.netty.buffer.ChannelBuffers; |
| 23 | import org.jboss.netty.channel.Channel; |
| 24 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 25 | import org.jboss.netty.handler.codec.oneone.OneToOneEncoder; |
| 26 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 27 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Encode an openflow message for output into a ChannelBuffer, for use in a |
| 30 | * netty pipeline. |
| 31 | */ |
| 32 | public class OFMessageEncoder extends OneToOneEncoder { |
| 33 | |
| 34 | @Override |
| 35 | protected Object encode(ChannelHandlerContext ctx, Channel channel, |
| 36 | Object msg) throws Exception { |
| 37 | if (!(msg instanceof List)) { |
| 38 | return msg; |
| 39 | } |
| 40 | |
| 41 | @SuppressWarnings("unchecked") |
| 42 | List<OFMessage> msglist = (List<OFMessage>) msg; |
| 43 | /* XXX S can't get length of OFMessage in loxigen's openflowj?? |
| 44 | int size = 0; |
| 45 | for (OFMessage ofm : msglist) { |
| 46 | size += ofm.getLengthU(); |
| 47 | }*/ |
| 48 | |
| 49 | ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); |
| 50 | |
| 51 | for (OFMessage ofm : msglist) { |
Charles Chan | e64d375 | 2015-10-21 13:03:58 -0700 | [diff] [blame] | 52 | if (ofm != null) { |
| 53 | ofm.writeTo(buf); |
| 54 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 55 | } |
| 56 | return buf; |
| 57 | } |
| 58 | |
| 59 | } |