blob: 431c6210f53c4fb466d2b39a608e10457cb25704 [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();
51 List<BgpMessage> msgList = new LinkedList<BgpMessage>();
Shashikanth VH45c3ece2015-11-04 19:00:19 +053052
53 while (buffer.readableBytes() > 0) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053054 BgpHeader bgpHeader = new BgpHeader();
55 BgpMessage message = reader.readFrom(buffer, bgpHeader);
Shashikanth VH45c3ece2015-11-04 19:00:19 +053056 msgList.add(message);
57 }
Satish Ke107e662015-09-21 19:00:17 +053058 return msgList;
59 }
60}