blob: 7243e21aaf155687c79ed0cf8966ead392a65dab [file] [log] [blame]
Priyanka B9aaa3112015-10-09 17:19:33 +05301/*
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 */
16package org.onosproject.bgpio.protocol.ver4;
17
18import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053019import org.onosproject.bgpio.exceptions.BgpParseException;
20import org.onosproject.bgpio.protocol.BgpMessageReader;
21import org.onosproject.bgpio.protocol.BgpMessageWriter;
22import org.onosproject.bgpio.protocol.BgpNotificationMsg;
23import org.onosproject.bgpio.protocol.BgpType;
24import org.onosproject.bgpio.protocol.BgpVersion;
25import org.onosproject.bgpio.types.BgpErrorType;
26import org.onosproject.bgpio.types.BgpHeader;
Priyanka B9aaa3112015-10-09 17:19:33 +053027import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import 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 VH5dd8dbe2015-11-26 13:22:18 +053036class BgpNotificationMsgVer4 implements BgpNotificationMsg {
Priyanka B9aaa3112015-10-09 17:19:33 +053037
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 VH5dd8dbe2015-11-26 13:22:18 +053047 private static final Logger log = LoggerFactory.getLogger(BgpNotificationMsgVer4.class);
Priyanka B9aaa3112015-10-09 17:19:33 +053048
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 VH5dd8dbe2015-11-26 13:22:18 +053053 static final BgpType MSG_TYPE = BgpType.NOTIFICATION;
Priyanka B9aaa3112015-10-09 17:19:33 +053054 static final byte DEFAULT_ERRORSUBCODE = 0;
Priyanka B715854a2015-10-27 16:11:53 +053055 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 B9aaa3112015-10-09 17:19:33 +053059 static final byte MESSAGE_TYPE = 3;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053060 static final BgpHeader DEFAULT_MESSAGE_HEADER = new BgpHeader(MARKER, BgpHeader.DEFAULT_HEADER_LENGTH,
Priyanka B9aaa3112015-10-09 17:19:33 +053061 MESSAGE_TYPE);
62
63 private byte errorCode;
64 private byte errorSubCode;
65 private byte[] data;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053066 private BgpHeader bgpHeader;
67 public static final BgpNotificationMsgVer4.Reader READER = new Reader();
Priyanka B9aaa3112015-10-09 17:19:33 +053068
69 /**
Priyanka B715854a2015-10-27 16:11:53 +053070 * Initialize fields.
Priyanka B9aaa3112015-10-09 17:19:33 +053071 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053072 public BgpNotificationMsgVer4() {
Priyanka B9aaa3112015-10-09 17:19:33 +053073 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 VH5dd8dbe2015-11-26 13:22:18 +053087 public BgpNotificationMsgVer4(BgpHeader bgpHeader, byte errorCode, byte errorSubCode, byte[] data) {
Priyanka B9aaa3112015-10-09 17:19:33 +053088 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 VH5dd8dbe2015-11-26 13:22:18 +053097 static class Reader implements BgpMessageReader<BgpNotificationMsg> {
Priyanka B9aaa3112015-10-09 17:19:33 +053098 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053099 public BgpNotificationMsg readFrom(ChannelBuffer cb, BgpHeader bgpHeader) throws BgpParseException {
Priyanka B9aaa3112015-10-09 17:19:33 +0530100 byte errorCode;
101 byte errorSubCode;
102 if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530103 throw new BgpParseException("Not enough readable bytes");
Priyanka B9aaa3112015-10-09 17:19:33 +0530104 }
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 VH5dd8dbe2015-11-26 13:22:18 +0530111 return new BgpNotificationMsgVer4(bgpHeader, errorCode, errorSubCode, data);
Priyanka B9aaa3112015-10-09 17:19:33 +0530112 }
113 }
114
115 /**
116 * Builder class for BGP notification message.
117 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530118 static class Builder implements BgpNotificationMsg.Builder {
Priyanka B9aaa3112015-10-09 17:19:33 +0530119 private byte errorCode;
120 private byte errorSubCode;
121 private byte[] data;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530122 private BgpHeader bgpHeader;
Priyanka B9aaa3112015-10-09 17:19:33 +0530123 private boolean isErrorCodeSet = false;
124 private boolean isErrorSubCodeSet = false;
125 private boolean isBGPHeaderSet = false;
126
127 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530128 public BgpNotificationMsg build() throws BgpParseException {
129 BgpHeader bgpHeader = this.isBGPHeaderSet ? this.bgpHeader : DEFAULT_MESSAGE_HEADER;
Priyanka B9aaa3112015-10-09 17:19:33 +0530130 if (!this.isErrorCodeSet) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530131 throw new BgpParseException("Error code must be present");
Priyanka B9aaa3112015-10-09 17:19:33 +0530132 }
133
134 byte errorSubCode = this.isErrorSubCodeSet ? this.errorSubCode : DEFAULT_ERRORSUBCODE;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530135 return new BgpNotificationMsgVer4(bgpHeader, this.errorCode, errorSubCode, this.data);
Priyanka B9aaa3112015-10-09 17:19:33 +0530136 }
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 VH447c6b02015-11-25 21:25:35 +0530154 if (data != null) {
155 this.data = data;
156 }
Priyanka B9aaa3112015-10-09 17:19:33 +0530157 return this;
158 }
159
160 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530161 public Builder setHeader(BgpHeader bgpMsgHeader) {
Priyanka B9aaa3112015-10-09 17:19:33 +0530162 this.bgpHeader = bgpMsgHeader;
163 return this;
164 }
165 }
166
167 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530168 public BgpVersion getVersion() {
169 return BgpVersion.BGP_4;
Priyanka B9aaa3112015-10-09 17:19:33 +0530170 }
171
172 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530173 public BgpType getType() {
174 return BgpType.NOTIFICATION;
Priyanka B9aaa3112015-10-09 17:19:33 +0530175 }
176
177 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530178 public void writeTo(ChannelBuffer cb) throws BgpParseException {
Priyanka B9aaa3112015-10-09 17:19:33 +0530179 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 VH5dd8dbe2015-11-26 13:22:18 +0530187 static class Writer implements BgpMessageWriter<BgpNotificationMsgVer4> {
Priyanka B9aaa3112015-10-09 17:19:33 +0530188 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530189 public void write(ChannelBuffer cb, BgpNotificationMsgVer4 message) throws BgpParseException {
Priyanka B9aaa3112015-10-09 17:19:33 +0530190 int msgStartIndex = cb.writerIndex();
191 int headerLenIndex = message.bgpHeader.write(cb);
192 if (headerLenIndex <= 0) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530193 throw new BgpParseException(BgpErrorType.MESSAGE_HEADER_ERROR, (byte) 0, null);
Priyanka B9aaa3112015-10-09 17:19:33 +0530194 }
195 cb.writeByte(message.errorCode);
196 cb.writeByte(message.errorSubCode);
Shashikanth VH1ecbe7b2015-12-02 22:54:23 +0530197 if (message.data != null) {
198 cb.writeBytes(message.data);
199 }
Priyanka B9aaa3112015-10-09 17:19:33 +0530200
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 VH5dd8dbe2015-11-26 13:22:18 +0530251 public BgpHeader getHeader() {
Priyanka B9aaa3112015-10-09 17:19:33 +0530252 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 }
265}