SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +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 | */ |
harikrushna-Huawei | 6ecfc77 | 2017-04-10 18:22:00 +0530 | [diff] [blame^] | 16 | package org.onosproject.pcep.server.impl; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 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; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 25 | import org.onosproject.pcepio.exceptions.PcepOutOfBoundMessageException; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 26 | import org.onosproject.pcepio.protocol.PcepFactories; |
| 27 | import org.onosproject.pcepio.protocol.PcepMessage; |
| 28 | import org.onosproject.pcepio.protocol.PcepMessageReader; |
| 29 | import org.onosproject.pcepio.util.HexDump; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | /** |
| 34 | * Decode an pcep message from a Channel, for use in a netty pipeline. |
| 35 | */ |
| 36 | public class PcepMessageDecoder extends FrameDecoder { |
| 37 | |
| 38 | protected static final Logger log = LoggerFactory.getLogger(PcepMessageDecoder.class); |
| 39 | |
| 40 | @Override |
| 41 | protected Object decode(ChannelHandlerContext ctx, Channel channel, |
| 42 | ChannelBuffer buffer) throws Exception { |
| 43 | log.debug("Message received."); |
| 44 | if (!channel.isConnected()) { |
| 45 | log.info("Channel is not connected."); |
| 46 | // In testing, I see decode being called AFTER decode last. |
| 47 | // This check avoids that from reading corrupted frames |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | HexDump.pcepHexDump(buffer); |
| 52 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 53 | // Buffer can contain multiple messages, also may contain out of bound message. |
| 54 | // Read the message one by one from buffer and parse it. If it encountered out of bound message, |
| 55 | // then mark the reader index and again take the next chunk of messages from the channel |
| 56 | // and parse again from the marked reader index. |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 57 | PcepMessageReader<PcepMessage> reader = PcepFactories.getGenericReader(); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 58 | List<PcepMessage> msgList = (List<PcepMessage>) ctx.getAttachment(); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 59 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 60 | if (msgList == null) { |
| 61 | msgList = new LinkedList<>(); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 62 | } |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 63 | |
| 64 | try { |
| 65 | while (buffer.readableBytes() > 0) { |
| 66 | buffer.markReaderIndex(); |
| 67 | PcepMessage message = reader.readFrom(buffer); |
| 68 | msgList.add(message); |
| 69 | } |
| 70 | ctx.setAttachment(null); |
| 71 | return msgList; |
| 72 | } catch (PcepOutOfBoundMessageException e) { |
| 73 | log.debug("PCEP message decode error"); |
| 74 | buffer.resetReaderIndex(); |
| 75 | buffer.discardReadBytes(); |
| 76 | ctx.setAttachment(msgList); |
| 77 | } |
| 78 | return null; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 79 | } |
| 80 | } |