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.LinkedList; |
| 19 | import java.util.List; |
| 20 | |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| 22 | import org.jboss.netty.channel.Channel; |
| 23 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 24 | import org.jboss.netty.handler.codec.frame.FrameDecoder; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 25 | import org.onosproject.bgpio.protocol.BgpMessage; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 26 | import org.onosproject.bgpio.protocol.BgpFactories; |
| 27 | import org.onosproject.bgpio.protocol.BgpMessageReader; |
| 28 | import org.onosproject.bgpio.types.BgpHeader; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | /** |
| 33 | * Decode an bgp message from a Channel, for use in a netty pipeline. |
| 34 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 35 | public class BgpMessageDecoder extends FrameDecoder { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 36 | |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame^] | 37 | private static final Logger log = LoggerFactory.getLogger(BgpMessageDecoder.class); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 38 | |
| 39 | @Override |
| 40 | protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 41 | log.debug("MESSAGE IS RECEIVED."); |
| 42 | if (!channel.isConnected()) { |
| 43 | log.info("Channel is not connected."); |
| 44 | return null; |
| 45 | } |
| 46 | |
| 47 | HexDump.dump(buffer); |
| 48 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 49 | BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader(); |
Shashikanth VH | 7a2fe65 | 2015-12-28 14:53:44 +0530 | [diff] [blame] | 50 | List<BgpMessage> msgList = (List<BgpMessage>) ctx.getAttachment(); |
Shashikanth VH | 45c3ece | 2015-11-04 19:00:19 +0530 | [diff] [blame] | 51 | |
Shashikanth VH | 7a2fe65 | 2015-12-28 14:53:44 +0530 | [diff] [blame] | 52 | if (msgList == null) { |
| 53 | msgList = new LinkedList<>(); |
Shashikanth VH | 45c3ece | 2015-11-04 19:00:19 +0530 | [diff] [blame] | 54 | } |
Shashikanth VH | 7a2fe65 | 2015-12-28 14:53:44 +0530 | [diff] [blame] | 55 | |
| 56 | try { |
| 57 | while (buffer.readableBytes() > 0) { |
| 58 | buffer.markReaderIndex(); |
| 59 | BgpHeader bgpHeader = new BgpHeader(); |
| 60 | BgpMessage message = reader.readFrom(buffer, bgpHeader); |
| 61 | msgList.add(message); |
| 62 | } |
Priyanka B | 4c3b451 | 2016-07-22 11:41:49 +0530 | [diff] [blame] | 63 | ctx.setAttachment(null); |
Shashikanth VH | 7a2fe65 | 2015-12-28 14:53:44 +0530 | [diff] [blame] | 64 | return msgList; |
| 65 | } catch (Exception e) { |
| 66 | log.debug("Bgp protocol message decode error"); |
| 67 | buffer.resetReaderIndex(); |
| 68 | buffer.discardReadBytes(); |
| 69 | ctx.setAttachment(msgList); |
| 70 | } |
| 71 | return null; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 72 | } |
| 73 | } |