blob: b106589194287713a08a494365693cdb1764fb14 [file] [log] [blame]
SureshBR25058b72015-08-13 13:05:06 +05301/*
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 */
16package org.onosproject.pcep.controller.impl;
17
18import java.util.LinkedList;
19import java.util.List;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.jboss.netty.channel.Channel;
23import org.jboss.netty.channel.ChannelHandlerContext;
24import org.jboss.netty.handler.codec.frame.FrameDecoder;
25import org.onosproject.pcepio.protocol.PcepFactories;
26import org.onosproject.pcepio.protocol.PcepMessage;
27import org.onosproject.pcepio.protocol.PcepMessageReader;
28import org.onosproject.pcepio.util.HexDump;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32/**
33 * Decode an pcep message from a Channel, for use in a netty pipeline.
34 */
35public 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();
Sho SHIMIZU9b8274c2015-09-04 15:54:24 -070060 List<PcepMessage> msgList = new LinkedList<>();
SureshBR25058b72015-08-13 13:05:06 +053061
62 while (buffer.readableBytes() > 0) {
63 PcepMessage message = reader.readFrom(buffer);
64 msgList.add(message);
65 }
66 return msgList;
67 }
68}