blob: fb54e8b2396f01e01bf561beaf05c9ac2a2d3902 [file] [log] [blame]
Pavlin Radoslavov80f3e182014-12-15 10:46:18 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
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.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24/**
25 * A class for preparing BGP messages.
26 */
27final class BgpMessage {
28 private static final Logger log =
29 LoggerFactory.getLogger(BgpMessage.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 BgpMessage() {
38 }
39
40 /**
41 * Prepares BGP message.
42 *
43 * @param type the BGP message type
44 * @param payload the message payload to transmit (BGP header excluded)
45 * @return the message to transmit (BGP header included)
46 */
47 static ChannelBuffer prepareBgpMessage(int type, ChannelBuffer payload) {
48 ChannelBuffer message =
49 ChannelBuffers.buffer(BgpConstants.BGP_HEADER_LENGTH +
50 payload.readableBytes());
51
52 // Write the marker
53 for (int i = 0; i < BgpConstants.BGP_HEADER_MARKER_LENGTH; i++) {
54 message.writeByte(0xff);
55 }
56
57 // Write the rest of the BGP header
58 message.writeShort(BgpConstants.BGP_HEADER_LENGTH +
59 payload.readableBytes());
60 message.writeByte(type);
61
62 // Write the payload
63 message.writeBytes(payload);
64 return message;
65 }
Pavlin Radoslavov278cdde2014-12-16 14:09:31 -080066
67 /**
68 * An exception indicating a parsing error of the BGP message.
69 */
70 static final class BgpParseException extends Exception {
71 /**
72 * Default constructor.
73 */
74 private BgpParseException() {
75 super();
76 }
77
78 /**
79 * Constructor for a specific exception details message.
80 *
81 * @param message the message with the exception details
82 */
83 BgpParseException(String message) {
84 super(message);
85 }
86 }
Pavlin Radoslavov80f3e182014-12-15 10:46:18 -080087}