Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +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.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; |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onosproject.ospf.controller.OspfMessage; |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 24 | import org.onosproject.ospf.protocol.ospfpacket.OspfMessageReader; |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 25 | import org.onosproject.ospf.protocol.util.OspfUtil; |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import java.util.LinkedList; |
| 30 | import java.util.List; |
| 31 | |
| 32 | /** |
| 33 | * Decodes an OSPF message from a Channel, for use in a netty pipeline. |
| 34 | */ |
| 35 | public class OspfMessageDecoder extends FrameDecoder { |
| 36 | |
| 37 | private static final Logger log = LoggerFactory.getLogger(OspfMessageDecoder.class); |
| 38 | |
| 39 | @Override |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 40 | protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { |
| 41 | log.debug("OspfMessageDecoder::Message received <:> length {}", buffer.readableBytes()); |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 42 | if (!channel.isConnected()) { |
| 43 | log.info("Channel is not connected."); |
| 44 | return null; |
| 45 | } |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 46 | OspfMessageReader messageReader = new OspfMessageReader(); |
| 47 | List<OspfMessage> ospfMessageList = new LinkedList<>(); |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 48 | while (buffer.readableBytes() >= OspfUtil.MINIMUM_FRAME_LEN) { |
| 49 | ChannelBuffer ospfDataBuffer = buffer.readBytes(OspfUtil.MINIMUM_FRAME_LEN); |
| 50 | int readableBytes = ospfDataBuffer.readableBytes(); |
| 51 | OspfMessage message = messageReader.readFromBuffer(ospfDataBuffer); |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 52 | if (message != null) { |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 53 | if (ospfDataBuffer.readableBytes() >= OspfUtil.METADATA_LEN) { |
| 54 | ospfDataBuffer.readerIndex(readableBytes - OspfUtil.METADATA_LEN); |
| 55 | log.debug("IsisMessageDecoder::Reading metadata <:> length {}", ospfDataBuffer.readableBytes()); |
| 56 | |
| 57 | |
| 58 | int interfaceIndex = ospfDataBuffer.readByte(); |
| 59 | byte[] sourceIpBytes = new byte[OspfUtil.FOUR_BYTES]; |
| 60 | ospfDataBuffer.readBytes(sourceIpBytes, 0, OspfUtil.FOUR_BYTES); |
| 61 | Ip4Address sourceIP = Ip4Address.valueOf(sourceIpBytes); |
| 62 | |
| 63 | message.setSourceIp(sourceIP); |
| 64 | message.setInterfaceIndex(interfaceIndex); |
| 65 | } |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 66 | ospfMessageList.add(message); |
| 67 | } |
| 68 | } |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 69 | return (!ospfMessageList.isEmpty()) ? ospfMessageList : null; |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 70 | } |
| 71 | } |