Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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.ospf.controller.impl; |
| 17 | |
| 18 | import org.jboss.netty.buffer.ChannelBuffer; |
| 19 | import org.jboss.netty.channel.Channel; |
| 20 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 21 | import org.jboss.netty.handler.codec.frame.FrameDecoder; |
| 22 | import org.onosproject.ospf.protocol.ospfpacket.OspfMessage; |
| 23 | import org.onosproject.ospf.protocol.ospfpacket.OspfMessageReader; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import java.util.LinkedList; |
| 28 | import java.util.List; |
| 29 | |
| 30 | /** |
| 31 | * Decodes an OSPF message from a Channel, for use in a netty pipeline. |
| 32 | */ |
| 33 | public class OspfMessageDecoder extends FrameDecoder { |
| 34 | |
| 35 | private static final Logger log = LoggerFactory.getLogger(OspfMessageDecoder.class); |
| 36 | |
| 37 | @Override |
| 38 | protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer channelBuffer) throws Exception { |
| 39 | log.debug("OspfMessageDecoder::Message received <:> length {}", channelBuffer.readableBytes()); |
| 40 | log.debug("channelBuffer.readableBytes - decode {}", channelBuffer.readableBytes()); |
| 41 | if (!channel.isConnected()) { |
| 42 | log.info("Channel is not connected."); |
| 43 | return null; |
| 44 | } |
| 45 | |
| 46 | OspfMessageReader messageReader = new OspfMessageReader(); |
| 47 | List<OspfMessage> ospfMessageList = new LinkedList<>(); |
| 48 | |
| 49 | while (channelBuffer.readableBytes() > 0) { |
| 50 | OspfMessage message = messageReader.readFromBuffer(channelBuffer); |
| 51 | if (message != null) { |
| 52 | ospfMessageList.add(message); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return ospfMessageList; |
| 57 | } |
| 58 | } |