SureshBR | 25058b7 | 2015-08-13 13:05:06 +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.pcep.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; |
| 25 | import org.onosproject.pcepio.protocol.PcepFactories; |
| 26 | import org.onosproject.pcepio.protocol.PcepMessage; |
| 27 | import org.onosproject.pcepio.protocol.PcepMessageReader; |
| 28 | import org.onosproject.pcepio.util.HexDump; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | /** |
| 33 | * Decode an pcep message from a Channel, for use in a netty pipeline. |
| 34 | */ |
| 35 | public class PcepMessageDecoder extends FrameDecoder { |
| 36 | |
| 37 | protected static final Logger log = LoggerFactory.getLogger(PcepMessageDecoder.class); |
| 38 | |
| 39 | @Override |
| 40 | protected Object decode(ChannelHandlerContext ctx, Channel channel, |
| 41 | ChannelBuffer buffer) throws Exception { |
| 42 | log.debug("Message received."); |
| 43 | if (!channel.isConnected()) { |
| 44 | log.info("Channel is not connected."); |
| 45 | // In testing, I see decode being called AFTER decode last. |
| 46 | // This check avoids that from reading corrupted frames |
| 47 | return null; |
| 48 | } |
| 49 | |
| 50 | HexDump.pcepHexDump(buffer); |
| 51 | |
| 52 | // Note that a single call to decode results in reading a single |
| 53 | // PcepMessage from the channel buffer, which is passed on to, and processed |
| 54 | // by, the controller (in PcepChannelHandler). |
| 55 | // This is different from earlier behavior (with the original pcepIO), |
| 56 | // where we parsed all the messages in the buffer, before passing on |
| 57 | // a list of the parsed messages to the controller. |
| 58 | // The performance *may or may not* not be as good as before. |
| 59 | PcepMessageReader<PcepMessage> reader = PcepFactories.getGenericReader(); |
| 60 | List<PcepMessage> msgList = new LinkedList<PcepMessage>(); |
| 61 | |
| 62 | while (buffer.readableBytes() > 0) { |
| 63 | PcepMessage message = reader.readFrom(buffer); |
| 64 | msgList.add(message); |
| 65 | } |
| 66 | return msgList; |
| 67 | } |
| 68 | } |