blob: 2c21b73daa1a4fbf77ef6e2dbd57c704355fee4a [file] [log] [blame]
SureshBR25058b72015-08-13 13:05:06 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
SureshBR25058b72015-08-13 13:05:06 +05303 *
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-Huawei6ecfc772017-04-10 18:22:00 +053016package org.onosproject.pcep.server.impl;
SureshBR25058b72015-08-13 13:05:06 +053017
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;
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053025import org.onosproject.pcepio.exceptions.PcepOutOfBoundMessageException;
SureshBR25058b72015-08-13 13:05:06 +053026import org.onosproject.pcepio.protocol.PcepFactories;
27import org.onosproject.pcepio.protocol.PcepMessage;
28import org.onosproject.pcepio.protocol.PcepMessageReader;
29import org.onosproject.pcepio.util.HexDump;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33/**
34 * Decode an pcep message from a Channel, for use in a netty pipeline.
35 */
36public 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 Sf1bbd362016-02-25 18:19:59 +053053 // 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.
SureshBR25058b72015-08-13 13:05:06 +053057 PcepMessageReader<PcepMessage> reader = PcepFactories.getGenericReader();
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053058 List<PcepMessage> msgList = (List<PcepMessage>) ctx.getAttachment();
SureshBR25058b72015-08-13 13:05:06 +053059
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053060 if (msgList == null) {
61 msgList = new LinkedList<>();
SureshBR25058b72015-08-13 13:05:06 +053062 }
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053063
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;
SureshBR25058b72015-08-13 13:05:06 +053079 }
80}