Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 16 | package org.onosproject.bgpio.protocol.ver4; |
| 17 | |
| 18 | import org.jboss.netty.buffer.ChannelBuffer; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 19 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 20 | import org.onosproject.bgpio.protocol.BgpMessageReader; |
| 21 | import org.onosproject.bgpio.protocol.BgpMessageWriter; |
| 22 | import org.onosproject.bgpio.protocol.BgpNotificationMsg; |
| 23 | import org.onosproject.bgpio.protocol.BgpType; |
| 24 | import org.onosproject.bgpio.protocol.BgpVersion; |
| 25 | import org.onosproject.bgpio.types.BgpErrorType; |
| 26 | import org.onosproject.bgpio.types.BgpHeader; |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import com.google.common.base.MoreObjects; |
| 31 | |
| 32 | /** |
| 33 | * A NOTIFICATION message is sent when an error condition is detected. The BGP connection is closed immediately after it |
| 34 | * is sent. |
| 35 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 36 | class BgpNotificationMsgVer4 implements BgpNotificationMsg { |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | 0 1 2 3 |
| 40 | 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 |
| 41 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 42 | | Error code | Error subcode | Data (variable) | |
| 43 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 44 | REFERENCE : RFC 4271 |
| 45 | */ |
| 46 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 47 | private static final Logger log = LoggerFactory.getLogger(BgpNotificationMsgVer4.class); |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 48 | |
| 49 | static final byte PACKET_VERSION = 4; |
| 50 | //BGPHeader(19) + Error code(1) + Error subcode(1) |
| 51 | static final int TOTAL_MESSAGE_MIN_LENGTH = 21; |
| 52 | static final int PACKET_MINIMUM_LENGTH = 2; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 53 | static final BgpType MSG_TYPE = BgpType.NOTIFICATION; |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 54 | static final byte DEFAULT_ERRORSUBCODE = 0; |
Priyanka B | 715854a | 2015-10-27 16:11:53 +0530 | [diff] [blame] | 55 | static final byte[] MARKER = {(byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 56 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 57 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 58 | (byte) 0xff, (byte) 0xff, (byte) 0xff }; |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 59 | static final byte MESSAGE_TYPE = 3; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 60 | static final BgpHeader DEFAULT_MESSAGE_HEADER = new BgpHeader(MARKER, BgpHeader.DEFAULT_HEADER_LENGTH, |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 61 | MESSAGE_TYPE); |
| 62 | |
| 63 | private byte errorCode; |
| 64 | private byte errorSubCode; |
| 65 | private byte[] data; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 66 | private BgpHeader bgpHeader; |
| 67 | public static final BgpNotificationMsgVer4.Reader READER = new Reader(); |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 68 | |
| 69 | /** |
Priyanka B | 715854a | 2015-10-27 16:11:53 +0530 | [diff] [blame] | 70 | * Initialize fields. |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 71 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 72 | public BgpNotificationMsgVer4() { |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 73 | this.bgpHeader = null; |
| 74 | this.data = null; |
| 75 | this.errorCode = 0; |
| 76 | this.errorSubCode = 0; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Constructor to initialize parameters. |
| 81 | * |
| 82 | * @param bgpHeader BGP Header in notification message |
| 83 | * @param errorCode error code |
| 84 | * @param errorSubCode error subcode |
| 85 | * @param data field |
| 86 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 87 | public BgpNotificationMsgVer4(BgpHeader bgpHeader, byte errorCode, byte errorSubCode, byte[] data) { |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 88 | this.bgpHeader = bgpHeader; |
| 89 | this.data = data; |
| 90 | this.errorCode = errorCode; |
| 91 | this.errorSubCode = errorSubCode; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Reader reads BGP Notification Message from the channel buffer. |
| 96 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 97 | static class Reader implements BgpMessageReader<BgpNotificationMsg> { |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 98 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 99 | public BgpNotificationMsg readFrom(ChannelBuffer cb, BgpHeader bgpHeader) throws BgpParseException { |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 100 | byte errorCode; |
| 101 | byte errorSubCode; |
| 102 | if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 103 | throw new BgpParseException("Not enough readable bytes"); |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 104 | } |
| 105 | errorCode = cb.readByte(); |
| 106 | errorSubCode = cb.readByte(); |
| 107 | //Message Length = 21 + Data Length |
| 108 | int dataLength = bgpHeader.getLength() - TOTAL_MESSAGE_MIN_LENGTH; |
| 109 | byte[] data = new byte[dataLength]; |
| 110 | cb.readBytes(data, 0, dataLength); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 111 | return new BgpNotificationMsgVer4(bgpHeader, errorCode, errorSubCode, data); |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Builder class for BGP notification message. |
| 117 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 118 | static class Builder implements BgpNotificationMsg.Builder { |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 119 | private byte errorCode; |
| 120 | private byte errorSubCode; |
| 121 | private byte[] data; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 122 | private BgpHeader bgpHeader; |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 123 | private boolean isErrorCodeSet = false; |
| 124 | private boolean isErrorSubCodeSet = false; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 125 | private boolean isBgpHeaderSet = false; |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 126 | |
| 127 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 128 | public BgpNotificationMsg build() throws BgpParseException { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 129 | BgpHeader bgpHeader = this.isBgpHeaderSet ? this.bgpHeader : DEFAULT_MESSAGE_HEADER; |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 130 | if (!this.isErrorCodeSet) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 131 | throw new BgpParseException("Error code must be present"); |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | byte errorSubCode = this.isErrorSubCodeSet ? this.errorSubCode : DEFAULT_ERRORSUBCODE; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 135 | return new BgpNotificationMsgVer4(bgpHeader, this.errorCode, errorSubCode, this.data); |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public Builder setErrorCode(byte errorCode) { |
| 140 | this.errorCode = errorCode; |
| 141 | this.isErrorCodeSet = true; |
| 142 | return this; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public Builder setErrorSubCode(byte errorSubCode) { |
| 147 | this.errorSubCode = errorSubCode; |
| 148 | this.isErrorSubCodeSet = true; |
| 149 | return this; |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public Builder setData(byte[] data) { |
Shashikanth VH | 447c6b0 | 2015-11-25 21:25:35 +0530 | [diff] [blame] | 154 | if (data != null) { |
| 155 | this.data = data; |
| 156 | } |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 157 | return this; |
| 158 | } |
| 159 | |
| 160 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 161 | public Builder setHeader(BgpHeader bgpMsgHeader) { |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 162 | this.bgpHeader = bgpMsgHeader; |
| 163 | return this; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 168 | public BgpVersion getVersion() { |
| 169 | return BgpVersion.BGP_4; |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 173 | public BgpType getType() { |
| 174 | return BgpType.NOTIFICATION; |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 178 | public void writeTo(ChannelBuffer cb) throws BgpParseException { |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 179 | WRITER.write(cb, this); |
| 180 | } |
| 181 | |
| 182 | static final Writer WRITER = new Writer(); |
| 183 | |
| 184 | /** |
| 185 | * Writer writes BGP notification message to channel buffer. |
| 186 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 187 | static class Writer implements BgpMessageWriter<BgpNotificationMsgVer4> { |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 188 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 189 | public void write(ChannelBuffer cb, BgpNotificationMsgVer4 message) throws BgpParseException { |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 190 | int msgStartIndex = cb.writerIndex(); |
| 191 | int headerLenIndex = message.bgpHeader.write(cb); |
| 192 | if (headerLenIndex <= 0) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 193 | throw new BgpParseException(BgpErrorType.MESSAGE_HEADER_ERROR, (byte) 0, null); |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 194 | } |
| 195 | cb.writeByte(message.errorCode); |
| 196 | cb.writeByte(message.errorSubCode); |
Shashikanth VH | 1ecbe7b | 2015-12-02 22:54:23 +0530 | [diff] [blame] | 197 | if (message.data != null) { |
| 198 | cb.writeBytes(message.data); |
| 199 | } |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 200 | |
| 201 | //Update message length field in notification message |
| 202 | int length = cb.writerIndex() - msgStartIndex; |
| 203 | cb.setShort(headerLenIndex, (short) length); |
| 204 | message.bgpHeader.setLength((short) length); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public byte getErrorCode() { |
| 210 | return this.errorCode; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Sets errorcode with specified errorcode. |
| 215 | * |
| 216 | * @param errorCode field |
| 217 | */ |
| 218 | public void setErrorCode(byte errorCode) { |
| 219 | this.errorCode = errorCode; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public byte getErrorSubCode() { |
| 224 | return this.errorSubCode; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Sets error subcode with specified errorSubCode. |
| 229 | * |
| 230 | * @param errorSubCode field |
| 231 | */ |
| 232 | public void setErrorSubCode(byte errorSubCode) { |
| 233 | this.errorSubCode = errorSubCode; |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public byte[] getData() { |
| 238 | return this.data; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Sets error data with specified data. |
| 243 | * |
| 244 | * @param data field |
| 245 | */ |
| 246 | public void setData(byte[] data) { |
| 247 | this.data = data; |
| 248 | } |
| 249 | |
| 250 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 251 | public BgpHeader getHeader() { |
Priyanka B | 9aaa311 | 2015-10-09 17:19:33 +0530 | [diff] [blame] | 252 | return this.bgpHeader; |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public String toString() { |
| 257 | return MoreObjects.toStringHelper(getClass()) |
| 258 | .omitNullValues() |
| 259 | .add("bgpHeader", bgpHeader) |
| 260 | .add("data", data) |
| 261 | .add("errorCode", errorCode) |
| 262 | .add("errorSubCode", errorSubCode) |
| 263 | .toString(); |
| 264 | } |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 265 | } |