blob: e9e919cf41fe3b2f051f3cf0eddf1a23eedc52b9 [file] [log] [blame]
Satish Ke107e662015-09-21 19:00:17 +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.bgp.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;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053025import org.onosproject.bgpio.protocol.BgpMessage;
Satish Ke107e662015-09-21 19:00:17 +053026import org.onlab.util.HexDump;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053027import org.onosproject.bgpio.protocol.BgpFactories;
28import org.onosproject.bgpio.protocol.BgpMessageReader;
29import org.onosproject.bgpio.types.BgpHeader;
Satish Ke107e662015-09-21 19:00:17 +053030import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33/**
34 * Decode an bgp message from a Channel, for use in a netty pipeline.
35 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053036public class BgpMessageDecoder extends FrameDecoder {
Satish Ke107e662015-09-21 19:00:17 +053037
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053038 protected static final Logger log = LoggerFactory.getLogger(BgpMessageDecoder.class);
Satish Ke107e662015-09-21 19:00:17 +053039
40 @Override
41 protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
Satish Ke107e662015-09-21 19:00:17 +053042 log.debug("MESSAGE IS RECEIVED.");
43 if (!channel.isConnected()) {
44 log.info("Channel is not connected.");
45 return null;
46 }
47
48 HexDump.dump(buffer);
49
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053050 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
Shashikanth VH7a2fe652015-12-28 14:53:44 +053051 List<BgpMessage> msgList = (List<BgpMessage>) ctx.getAttachment();
Shashikanth VH45c3ece2015-11-04 19:00:19 +053052
Shashikanth VH7a2fe652015-12-28 14:53:44 +053053 if (msgList == null) {
54 msgList = new LinkedList<>();
Shashikanth VH45c3ece2015-11-04 19:00:19 +053055 }
Shashikanth VH7a2fe652015-12-28 14:53:44 +053056
57 try {
58 while (buffer.readableBytes() > 0) {
59 buffer.markReaderIndex();
60 BgpHeader bgpHeader = new BgpHeader();
61 BgpMessage message = reader.readFrom(buffer, bgpHeader);
62 msgList.add(message);
63 }
64
65 return msgList;
66 } catch (Exception e) {
67 log.debug("Bgp protocol message decode error");
68 buffer.resetReaderIndex();
69 buffer.discardReadBytes();
70 ctx.setAttachment(msgList);
71 }
72 return null;
Satish Ke107e662015-09-21 19:00:17 +053073 }
74}