2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.bgpio.protocol.ver4;
18 import org.jboss.netty.buffer.ChannelBuffer;
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;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.google.common.base.MoreObjects;
33 * A NOTIFICATION message is sent when an error condition is detected. The BGP connection is closed immediately after it
36 class BGPNotificationMsgVer4 implements BGPNotificationMsg {
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 private static final Logger log = LoggerFactory.getLogger(BGPNotificationMsgVer4.class);
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;
53 static final BGPType MSG_TYPE = BGPType.NOTIFICATION;
54 static final byte DEFAULT_ERRORSUBCODE = 0;
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 };
59 static final byte MESSAGE_TYPE = 3;
60 static final BGPHeader DEFAULT_MESSAGE_HEADER = new BGPHeader(MARKER, BGPHeader.DEFAULT_HEADER_LENGTH,
63 private byte errorCode;
64 private byte errorSubCode;
66 private BGPHeader bgpHeader;
67 public static final BGPNotificationMsgVer4.Reader READER = new Reader();
72 public BGPNotificationMsgVer4() {
73 this.bgpHeader = null;
76 this.errorSubCode = 0;
80 * Constructor to initialize parameters.
82 * @param bgpHeader BGP Header in notification message
83 * @param errorCode error code
84 * @param errorSubCode error subcode
87 public BGPNotificationMsgVer4(BGPHeader bgpHeader, byte errorCode, byte errorSubCode, byte[] data) {
88 this.bgpHeader = bgpHeader;
90 this.errorCode = errorCode;
91 this.errorSubCode = errorSubCode;
95 * Reader reads BGP Notification Message from the channel buffer.
97 static class Reader implements BGPMessageReader<BGPNotificationMsg> {
99 public BGPNotificationMsg readFrom(ChannelBuffer cb, BGPHeader bgpHeader) throws BGPParseException {
102 if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) {
103 throw new BGPParseException("Not enough readable bytes");
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);
111 return new BGPNotificationMsgVer4(bgpHeader, errorCode, errorSubCode, data);
116 * Builder class for BGP notification message.
118 static class Builder implements BGPNotificationMsg.Builder {
119 private byte errorCode;
120 private byte errorSubCode;
122 private BGPHeader bgpHeader;
123 private boolean isErrorCodeSet = false;
124 private boolean isErrorSubCodeSet = false;
125 private boolean isBGPHeaderSet = false;
128 public BGPNotificationMsg build() throws BGPParseException {
129 BGPHeader bgpHeader = this.isBGPHeaderSet ? this.bgpHeader : DEFAULT_MESSAGE_HEADER;
130 if (!this.isErrorCodeSet) {
131 throw new BGPParseException("Error code must be present");
134 byte errorSubCode = this.isErrorSubCodeSet ? this.errorSubCode : DEFAULT_ERRORSUBCODE;
135 return new BGPNotificationMsgVer4(bgpHeader, this.errorCode, errorSubCode, this.data);
139 public Builder setErrorCode(byte errorCode) {
140 this.errorCode = errorCode;
141 this.isErrorCodeSet = true;
146 public Builder setErrorSubCode(byte errorSubCode) {
147 this.errorSubCode = errorSubCode;
148 this.isErrorSubCodeSet = true;
153 public Builder setData(byte[] data) {
159 public Builder setHeader(BGPHeader bgpMsgHeader) {
160 this.bgpHeader = bgpMsgHeader;
166 public BGPVersion getVersion() {
167 return BGPVersion.BGP_4;
171 public BGPType getType() {
172 return BGPType.NOTIFICATION;
176 public void writeTo(ChannelBuffer cb) throws BGPParseException {
177 WRITER.write(cb, this);
180 static final Writer WRITER = new Writer();
183 * Writer writes BGP notification message to channel buffer.
185 static class Writer implements BGPMessageWriter<BGPNotificationMsgVer4> {
187 public void write(ChannelBuffer cb, BGPNotificationMsgVer4 message) throws BGPParseException {
188 int msgStartIndex = cb.writerIndex();
189 int headerLenIndex = message.bgpHeader.write(cb);
190 if (headerLenIndex <= 0) {
191 throw new BGPParseException(BGPErrorType.MESSAGE_HEADER_ERROR, (byte) 0, null);
193 cb.writeByte(message.errorCode);
194 cb.writeByte(message.errorSubCode);
195 cb.writeBytes(message.data);
197 //Update message length field in notification message
198 int length = cb.writerIndex() - msgStartIndex;
199 cb.setShort(headerLenIndex, (short) length);
200 message.bgpHeader.setLength((short) length);
205 public byte getErrorCode() {
206 return this.errorCode;
210 * Sets errorcode with specified errorcode.
212 * @param errorCode field
214 public void setErrorCode(byte errorCode) {
215 this.errorCode = errorCode;
219 public byte getErrorSubCode() {
220 return this.errorSubCode;
224 * Sets error subcode with specified errorSubCode.
226 * @param errorSubCode field
228 public void setErrorSubCode(byte errorSubCode) {
229 this.errorSubCode = errorSubCode;
233 public byte[] getData() {
238 * Sets error data with specified data.
242 public void setData(byte[] data) {
247 public BGPHeader getHeader() {
248 return this.bgpHeader;
252 public String toString() {
253 return MoreObjects.toStringHelper(getClass())
255 .add("bgpHeader", bgpHeader)
257 .add("errorCode", errorCode)
258 .add("errorSubCode", errorSubCode)