Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [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 | */ |
| 16 | package org.onosproject.bgpio.types; |
| 17 | |
| 18 | import org.jboss.netty.buffer.ChannelBuffer; |
| 19 | import org.slf4j.Logger; |
| 20 | import org.slf4j.LoggerFactory; |
| 21 | |
| 22 | /** |
| 23 | * Provides BGP Message Header which is common for all the Messages. |
| 24 | */ |
| 25 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 26 | public class BgpHeader { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 27 | |
| 28 | /* 0 1 2 3 |
| 29 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 30 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 31 | | | |
| 32 | + + |
| 33 | | | |
| 34 | + + |
| 35 | | Marker | |
| 36 | + + |
| 37 | | | |
| 38 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 39 | | Length | Type | |
| 40 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 41 | */ |
| 42 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 43 | protected static final Logger log = LoggerFactory.getLogger(BgpHeader.class); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 44 | |
| 45 | public static final int MARKER_LENGTH = 16; |
| 46 | public static final short DEFAULT_HEADER_LENGTH = 19; |
| 47 | |
| 48 | private byte[] marker; |
| 49 | private byte type; |
| 50 | private short length; |
| 51 | |
| 52 | /** |
| 53 | * Reset fields. |
| 54 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 55 | public BgpHeader() { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 56 | this.marker = null; |
| 57 | this.length = 0; |
| 58 | this.type = 0; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Constructors to initialize parameters. |
| 63 | * |
| 64 | * @param marker field in BGP header |
| 65 | * @param length message length |
| 66 | * @param type message type |
| 67 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 68 | public BgpHeader(byte[] marker, short length, byte type) { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 69 | this.marker = marker; |
| 70 | this.length = length; |
| 71 | this.type = type; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Sets marker field. |
| 76 | * |
| 77 | * @param value marker field |
| 78 | */ |
| 79 | public void setMarker(byte[] value) { |
| 80 | this.marker = value; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Sets message type. |
| 85 | * |
| 86 | * @param value message type |
| 87 | */ |
| 88 | public void setType(byte value) { |
| 89 | this.type = value; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Sets message length. |
| 94 | * |
| 95 | * @param value message length |
| 96 | */ |
| 97 | public void setLength(short value) { |
| 98 | this.length = value; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns message length. |
| 103 | * |
| 104 | * @return message length |
| 105 | */ |
| 106 | public short getLength() { |
| 107 | return this.length; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns message marker. |
| 112 | * |
| 113 | * @return message marker |
| 114 | */ |
| 115 | public byte[] getMarker() { |
| 116 | return this.marker; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns message type. |
| 121 | * |
| 122 | * @return message type |
| 123 | */ |
| 124 | public byte getType() { |
| 125 | return this.type; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Writes Byte stream of BGP header to channel buffer. |
| 130 | * |
| 131 | * @param cb ChannelBuffer |
| 132 | * @return length index of message header |
| 133 | */ |
| 134 | public int write(ChannelBuffer cb) { |
| 135 | |
| 136 | cb.writeBytes(getMarker(), 0, MARKER_LENGTH); |
| 137 | |
| 138 | int headerLenIndex = cb.writerIndex(); |
| 139 | cb.writeShort((short) 0); |
| 140 | cb.writeByte(type); |
| 141 | |
| 142 | return headerLenIndex; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Read from channel buffer and Returns BGP header. |
| 147 | * |
| 148 | * @param cb ChannelBuffer |
| 149 | * @return object of BGPHeader |
| 150 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 151 | public static BgpHeader read(ChannelBuffer cb) { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 152 | |
| 153 | byte[] marker = new byte[MARKER_LENGTH]; |
| 154 | byte type; |
| 155 | short length; |
| 156 | cb.readBytes(marker, 0, MARKER_LENGTH); |
| 157 | length = cb.readShort(); |
| 158 | type = cb.readByte(); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 159 | return new BgpHeader(marker, length, type); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 160 | } |
| 161 | } |