blob: 292872f1319496719e1c295babcde4432ce6c570 [file] [log] [blame]
Pavlin Radoslavov80f3e182014-12-15 10:46:18 -08001/*
2 * Copyright 2014 Open Networking Laboratory
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 */
16package org.onosproject.sdnip.bgp;
17
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 }
65}