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