blob: 1d7f5b6dbb3df252f0c08475585eeb37ebff6e15 [file] [log] [blame]
Pavlin Radoslavov80f3e182014-12-15 10:46:18 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Pavlin Radoslavov80f3e182014-12-15 10:46:18 -08003 *
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 */
Jonathan Hart41349e92015-02-09 14:14:02 -080016package org.onosproject.routing.bgp;
Pavlin Radoslavov80f3e182014-12-15 10:46:18 -080017
18import org.jboss.netty.buffer.ChannelBuffer;
19import org.jboss.netty.buffer.ChannelBuffers;
20import org.jboss.netty.channel.ChannelHandlerContext;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24/**
25 * A class for handling BGP KEEPALIVE messages.
26 */
27final class BgpKeepalive {
28 private static final Logger log =
29 LoggerFactory.getLogger(BgpKeepalive.class);
30
31 /**
32 * Default constructor.
33 * <p>
34 * The constructor is private to prevent creating an instance of
35 * this utility class.
36 */
37 private BgpKeepalive() {
38 }
39
40 /**
41 * Processes BGP KEEPALIVE message.
42 *
43 * @param bgpSession the BGP Session to use
44 * @param ctx the Channel Handler Context
45 * @param message the message to process
46 */
47 static void processBgpKeepalive(BgpSession bgpSession,
48 ChannelHandlerContext ctx,
49 ChannelBuffer message) {
50 if (message.readableBytes() + BgpConstants.BGP_HEADER_LENGTH !=
51 BgpConstants.BGP_KEEPALIVE_EXPECTED_LENGTH) {
52 log.debug("BGP RX KEEPALIVE Error from {}: " +
53 "Invalid total message length {}. Expected {}",
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -080054 bgpSession.remoteInfo().address(),
Pavlin Radoslavov80f3e182014-12-15 10:46:18 -080055 message.readableBytes() + BgpConstants.BGP_HEADER_LENGTH,
56 BgpConstants.BGP_KEEPALIVE_EXPECTED_LENGTH);
57 //
58 // ERROR: Bad Message Length
59 //
60 // Send NOTIFICATION and close the connection
61 ChannelBuffer txMessage =
62 BgpNotification.prepareBgpNotificationBadMessageLength(
63 message.readableBytes() + BgpConstants.BGP_HEADER_LENGTH);
64 ctx.getChannel().write(txMessage);
65 bgpSession.closeSession(ctx);
66 return;
67 }
68
69 //
70 // Parse the KEEPALIVE message: nothing to do
71 //
72 log.trace("BGP RX KEEPALIVE message from {}",
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -080073 bgpSession.remoteInfo().address());
Pavlin Radoslavov80f3e182014-12-15 10:46:18 -080074
75 // Start the Session Timeout timer
76 bgpSession.restartSessionTimeoutTimer(ctx);
77 }
78
79 /**
80 * Prepares BGP KEEPALIVE message.
81 *
82 * @return the message to transmit (BGP header included)
83 */
84 static ChannelBuffer prepareBgpKeepalive() {
85 ChannelBuffer message =
86 ChannelBuffers.buffer(BgpConstants.BGP_MESSAGE_MAX_LENGTH);
87
88 //
89 // Prepare the KEEPALIVE message payload: nothing to do
90 //
91 return BgpMessage.prepareBgpMessage(BgpConstants.BGP_TYPE_KEEPALIVE,
92 message);
93 }
94}