Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.bgp.controller.impl; |
| 17 | |
| 18 | import java.util.List; |
| 19 | |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.jboss.netty.buffer.ChannelBuffers; |
| 22 | import org.jboss.netty.channel.Channel; |
| 23 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 24 | import org.jboss.netty.handler.codec.oneone.OneToOneEncoder; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 25 | import org.onosproject.bgpio.protocol.BgpMessage; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | /** |
| 30 | * Encode an bgp message for output into a ChannelBuffer, for use in a |
| 31 | * netty pipeline. |
| 32 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 33 | public class BgpMessageEncoder extends OneToOneEncoder { |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 34 | private static final Logger log = LoggerFactory.getLogger(BgpMessageEncoder.class); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 35 | |
| 36 | @Override |
| 37 | protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { |
| 38 | log.debug("BGPMessageEncoder::encode"); |
| 39 | if (!(msg instanceof List)) { |
| 40 | log.debug("Invalid msg."); |
| 41 | return msg; |
| 42 | } |
| 43 | |
| 44 | @SuppressWarnings("unchecked") |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 45 | List<BgpMessage> msglist = (List<BgpMessage>) msg; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 46 | |
| 47 | ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); |
| 48 | |
| 49 | log.debug("SENDING MESSAGE"); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 50 | for (BgpMessage pm : msglist) { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 51 | pm.writeTo(buf); |
| 52 | } |
| 53 | |
| 54 | HexDump.dump(buf); |
| 55 | |
| 56 | return buf; |
| 57 | } |
| 58 | } |