blob: d6c4b59425a4c06b60459088b2195f68b2e9d82d [file] [log] [blame]
Satish Ke107e662015-09-21 19:00:17 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Satish Ke107e662015-09-21 19:00:17 +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 */
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;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053026import org.onosproject.bgpio.protocol.BgpFactories;
27import org.onosproject.bgpio.protocol.BgpMessageReader;
28import org.onosproject.bgpio.types.BgpHeader;
Satish Ke107e662015-09-21 19:00:17 +053029import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32/**
33 * Decode an bgp message from a Channel, for use in a netty pipeline.
34 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053035public class BgpMessageDecoder extends FrameDecoder {
Satish Ke107e662015-09-21 19:00:17 +053036
Ray Milkey9c9cde42018-01-12 14:22:06 -080037 private static final Logger log = LoggerFactory.getLogger(BgpMessageDecoder.class);
Satish Ke107e662015-09-21 19:00:17 +053038
39 @Override
40 protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
Satish Ke107e662015-09-21 19:00:17 +053041 log.debug("MESSAGE IS RECEIVED.");
42 if (!channel.isConnected()) {
43 log.info("Channel is not connected.");
44 return null;
45 }
46
47 HexDump.dump(buffer);
48
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053049 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
Shashikanth VH7a2fe652015-12-28 14:53:44 +053050 List<BgpMessage> msgList = (List<BgpMessage>) ctx.getAttachment();
Shashikanth VH45c3ece2015-11-04 19:00:19 +053051
Shashikanth VH7a2fe652015-12-28 14:53:44 +053052 if (msgList == null) {
53 msgList = new LinkedList<>();
Shashikanth VH45c3ece2015-11-04 19:00:19 +053054 }
Shashikanth VH7a2fe652015-12-28 14:53:44 +053055
56 try {
57 while (buffer.readableBytes() > 0) {
58 buffer.markReaderIndex();
59 BgpHeader bgpHeader = new BgpHeader();
60 BgpMessage message = reader.readFrom(buffer, bgpHeader);
61 msgList.add(message);
62 }
Priyanka B4c3b4512016-07-22 11:41:49 +053063 ctx.setAttachment(null);
Shashikanth VH7a2fe652015-12-28 14:53:44 +053064 return msgList;
65 } catch (Exception e) {
66 log.debug("Bgp protocol message decode error");
67 buffer.resetReaderIndex();
68 buffer.discardReadBytes();
69 ctx.setAttachment(msgList);
70 }
71 return null;
Satish Ke107e662015-09-21 19:00:17 +053072 }
73}