Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 3 | * |
| 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 Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 16 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 17 | package org.onosproject.routing.bgp; |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 18 | |
| 19 | import org.jboss.netty.buffer.ChannelBuffer; |
| 20 | import org.jboss.netty.buffer.ChannelBuffers; |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
| 24 | /** |
| 25 | * A class for preparing BGP messages. |
| 26 | */ |
| 27 | final 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 Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 66 | |
| 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 Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 87 | } |