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