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