blob: d8900adbc5d57fa31ff4daecde5c4cbf477ca962 [file] [log] [blame]
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +05303 *
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 */
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053016package org.onosproject.bgpio.protocol.ver4;
17
18import java.util.LinkedList;
19import java.util.ListIterator;
20
21import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053022import org.onosproject.bgpio.exceptions.BgpParseException;
23import org.onosproject.bgpio.protocol.BgpMessageReader;
24import org.onosproject.bgpio.protocol.BgpMessageWriter;
25import org.onosproject.bgpio.protocol.BgpOpenMsg;
26import org.onosproject.bgpio.protocol.BgpType;
27import org.onosproject.bgpio.protocol.BgpVersion;
28import org.onosproject.bgpio.types.BgpErrorType;
29import org.onosproject.bgpio.types.BgpHeader;
30import org.onosproject.bgpio.types.BgpValueType;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +053031import org.onosproject.bgpio.types.FourOctetAsNumCapabilityTlv;
32import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053033import org.onosproject.bgpio.util.Validation;
Shashikanth VH44967ec2016-02-04 19:46:16 +053034import org.onosproject.bgpio.util.Constants;
Shashikanth VHb58cfd52016-04-21 16:45:50 +053035import org.onosproject.bgpio.types.RpdCapabilityTlv;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053036import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
39import com.google.common.base.MoreObjects;
40
41/**
42 * Provides BGP open message.
43 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053044public class BgpOpenMsgVer4 implements BgpOpenMsg {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053045
46 /*
47 0 1 2 3
48 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
49 +-+-+-+-+-+-+-+-+
50 | Version |
51 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 | My Autonomous System |
53 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 | Hold Time |
55 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 | BGP Identifier |
57 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 | Opt Parm Len |
59 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 | Optional Parameters (variable) |
61 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62 OPEN Message Format
63 REFERENCE : RFC 4271
64 */
65
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053066 protected static final Logger log = LoggerFactory.getLogger(BgpOpenMsgVer4.class);
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053067
68 public static final byte PACKET_VERSION = 4;
69 public static final int OPEN_MSG_MINIMUM_LENGTH = 10;
70 public static final int MSG_HEADER_LENGTH = 19;
71 public static final int MARKER_LENGTH = 16;
72 public static final int DEFAULT_HOLD_TIME = 120;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +053073 public static final short AS_TRANS = 23456;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053074 public static final int OPT_PARA_TYPE_CAPABILITY = 2;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053075 public static final BgpType MSG_TYPE = BgpType.OPEN;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +053076 public static final short AFI = 16388;
77 public static final byte SAFI = 71;
78 public static final byte RES = 0;
79 public static final int FOUR_OCTET_AS_NUM_CAPA_TYPE = 65;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053080 public static final byte[] MARKER = new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
81 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
82 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff};
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053083 public static final BgpHeader DEFAULT_OPEN_HEADER = new BgpHeader(MARKER,
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053084 (short) OPEN_MSG_MINIMUM_LENGTH, (byte) 0X01);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053085 private BgpHeader bgpMsgHeader;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053086 private byte version;
Shashikanth VH09792f02016-05-10 16:59:57 +053087 private long asNumber;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053088 private short holdTime;
89 private int bgpId;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +053090 private boolean isLargeAsCapabilitySet;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053091 private LinkedList<BgpValueType> capabilityTlv;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053092
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053093 public static final BgpOpenMsgVer4.Reader READER = new Reader();
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053094
95 /**
96 * reset variables.
97 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053098 public BgpOpenMsgVer4() {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053099 this.bgpMsgHeader = null;
100 this.version = 0;
101 this.holdTime = 0;
102 this.asNumber = 0;
103 this.bgpId = 0;
104 this.capabilityTlv = null;
105 }
106
107 /**
108 * Constructor to initialize all variables of BGP Open message.
109 *
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530110 * @param bgpMsgHeader BGP Header in open message
111 * @param version BGP version in open message
112 * @param holdTime hold time in open message
113 * @param asNumber AS number in open message
114 * @param bgpId BGP identifier in open message
115 * @param capabilityTlv capabilities in open message
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530116 */
Shashikanth VH09792f02016-05-10 16:59:57 +0530117 public BgpOpenMsgVer4(BgpHeader bgpMsgHeader, byte version, long asNumber, short holdTime,
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530118 int bgpId, LinkedList<BgpValueType> capabilityTlv) {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530119 this.bgpMsgHeader = bgpMsgHeader;
120 this.version = version;
121 this.asNumber = asNumber;
122 this.holdTime = holdTime;
123 this.bgpId = bgpId;
124 this.capabilityTlv = capabilityTlv;
125 }
126
127 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530128 public BgpHeader getHeader() {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530129 return this.bgpMsgHeader;
130 }
131
132 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530133 public BgpVersion getVersion() {
134 return BgpVersion.BGP_4;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530135 }
136
137 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530138 public BgpType getType() {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530139 return MSG_TYPE;
140 }
141
142 @Override
143 public short getHoldTime() {
144 return this.holdTime;
145 }
146
147 @Override
Shashikanth VH09792f02016-05-10 16:59:57 +0530148 public long getAsNumber() {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530149 return this.asNumber;
150 }
151
152 @Override
153 public int getBgpId() {
154 return this.bgpId;
155 }
156
157 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530158 public LinkedList<BgpValueType> getCapabilityTlv() {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530159 return this.capabilityTlv;
160 }
161
162 /**
163 * Reader class for reading BGP open message from channel buffer.
164 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530165 public static class Reader implements BgpMessageReader<BgpOpenMsg> {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530166
167 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530168 public BgpOpenMsg readFrom(ChannelBuffer cb, BgpHeader bgpHeader) throws BgpParseException {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530169
170 byte version;
171 short holdTime;
Shashikanth VH09792f02016-05-10 16:59:57 +0530172 long asNumber;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530173 int bgpId;
174 byte optParaLen = 0;
175 byte optParaType;
176 byte capParaLen = 0;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530177 LinkedList<BgpValueType> capabilityTlv = new LinkedList<>();
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530178
179 if (cb.readableBytes() < OPEN_MSG_MINIMUM_LENGTH) {
180 log.error("[readFrom] Invalid length: Packet size is less than the minimum length ");
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530181 Validation.validateLen(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.BAD_MESSAGE_LENGTH,
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530182 cb.readableBytes());
183 }
184
185 // Read version
186 version = cb.readByte();
187 if (version != PACKET_VERSION) {
188 log.error("[readFrom] Invalid version: " + version);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530189 throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR,
190 BgpErrorType.UNSUPPORTED_VERSION_NUMBER, null);
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530191 }
192
193 // Read AS number
Shashikanth VH09792f02016-05-10 16:59:57 +0530194 asNumber = cb.getUnsignedShort(cb.readerIndex());
195 cb.readShort();
mohamedrahil00f6f262016-11-24 20:20:41 +0530196 log.debug("AS number read");
197
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530198 // Read Hold timer
199 holdTime = cb.readShort();
mohamedrahil00f6f262016-11-24 20:20:41 +0530200 log.debug("Holding time read");
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530201
202 // Read BGP Identifier
203 bgpId = cb.readInt();
mohamedrahil00f6f262016-11-24 20:20:41 +0530204 log.debug("BGP identifier read");
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530205
206 // Read optional parameter length
207 optParaLen = cb.readByte();
mohamedrahil00f6f262016-11-24 20:20:41 +0530208 log.debug("OPtional parameter length read");
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530209
210 // Read Capabilities if optional parameter length is greater than 0
211 if (optParaLen != 0) {
212 // Read optional parameter type
213 optParaType = cb.readByte();
214
215 // Read optional parameter length
216 capParaLen = cb.readByte();
217
218 if (cb.readableBytes() < capParaLen) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530219 throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, (byte) 0, null);
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530220 }
221
222 ChannelBuffer capaCb = cb.readBytes(capParaLen);
223
224 // Parse capabilities only if optional parameter type is 2
225 if ((optParaType == OPT_PARA_TYPE_CAPABILITY) && (capParaLen != 0)) {
226 capabilityTlv = parseCapabilityTlv(capaCb);
227 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530228 throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR,
229 BgpErrorType.UNSUPPORTED_OPTIONAL_PARAMETER, null);
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530230 }
231 }
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530232 return new BgpOpenMsgVer4(bgpHeader, version, asNumber, holdTime, bgpId, capabilityTlv);
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530233 }
234 }
235
236 /**
237 * Parsing capabilities.
238 *
239 * @param cb of type channel buffer
240 * @return capabilityTlv of open message
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530241 * @throws BgpParseException while parsing capabilities
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530242 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530243 protected static LinkedList<BgpValueType> parseCapabilityTlv(ChannelBuffer cb) throws BgpParseException {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530244
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530245 LinkedList<BgpValueType> capabilityTlv = new LinkedList<>();
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530246
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530247 while (cb.readableBytes() > 0) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530248 BgpValueType tlv;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530249 short type = cb.readByte();
250 short length = cb.readByte();
251
252 switch (type) {
253 case FourOctetAsNumCapabilityTlv.TYPE:
254 log.debug("FourOctetAsNumCapabilityTlv");
255 if (FourOctetAsNumCapabilityTlv.LENGTH != length) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530256 throw new BgpParseException("Invalid length received for FourOctetAsNumCapabilityTlv.");
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530257 }
258 if (length > cb.readableBytes()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530259 throw new BgpParseException("Four octet as num tlv length"
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530260 + " is more than readableBytes.");
261 }
262 int as4Num = cb.readInt();
263 tlv = new FourOctetAsNumCapabilityTlv(as4Num);
264 break;
Shashikanth VHb58cfd52016-04-21 16:45:50 +0530265 case RpdCapabilityTlv.TYPE:
266 log.debug("RpdCapability");
267 if (RpdCapabilityTlv.LENGTH != length) {
268 throw new BgpParseException("Invalid length received for RpdCapability.");
269 }
270 if (length > cb.readableBytes()) {
mohamedrahil00f6f262016-11-24 20:20:41 +0530271 throw new BgpParseException("Four octet as num TLV length"
Shashikanth VHb58cfd52016-04-21 16:45:50 +0530272 + " is more than readableBytes.");
273 }
274 short rpdAfi = cb.readShort();
275 byte rpdAsafi = cb.readByte();
276 byte sendReceive = cb.readByte();
277 tlv = new RpdCapabilityTlv(sendReceive);
278 break;
279
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530280 case MultiProtocolExtnCapabilityTlv.TYPE:
281 log.debug("MultiProtocolExtnCapabilityTlv");
Shashikanth VHb650bfa2016-04-18 12:54:03 +0530282
Shashikanth VHb58cfd52016-04-21 16:45:50 +0530283 if (MultiProtocolExtnCapabilityTlv.LENGTH != length) {
284 throw new BgpParseException("Invalid length received for MultiProtocolExtnCapabilityTlv.");
285 }
286
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530287 if (length > cb.readableBytes()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530288 throw new BgpParseException("BGP LS tlv length is more than readableBytes.");
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530289 }
290 short afi = cb.readShort();
291 byte res = cb.readByte();
292 byte safi = cb.readByte();
Shashikanth VHb58cfd52016-04-21 16:45:50 +0530293 tlv = new MultiProtocolExtnCapabilityTlv(afi, res, safi);
294
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530295 break;
296 default:
297 log.debug("Warning: Unsupported TLV: " + type);
298 cb.skipBytes(length);
299 continue;
300 }
301 capabilityTlv.add(tlv);
302 }
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530303 return capabilityTlv;
304 }
305
306 /**
307 * Builder class for BGP open message.
308 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530309 static class Builder implements BgpOpenMsg.Builder {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530310
311 private boolean isHeaderSet = false;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530312 private BgpHeader bgpMsgHeader;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530313 private boolean isHoldTimeSet = false;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530314 private short holdTime;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530315 private boolean isAsNumSet = false;
316 private short asNumber;
317 private boolean isBgpIdSet = false;
318 private int bgpId;
Shashikanth VHf04ab492016-02-10 11:35:11 +0530319 private boolean isIpV4UnicastCapabilityTlvSet = true;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530320 private boolean isLargeAsCapabilityTlvSet = false;
321 private boolean isLsCapabilityTlvSet = false;
Shashikanth VH44967ec2016-02-04 19:46:16 +0530322 private boolean isFlowSpecCapabilityTlvSet = false;
323 private boolean isVpnFlowSpecCapabilityTlvSet = false;
Shashikanth VHb650bfa2016-04-18 12:54:03 +0530324 private boolean isFlowSpecRpdCapabilityTlvSet = false;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530325
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530326 LinkedList<BgpValueType> capabilityTlv = new LinkedList<>();
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530327
328 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530329 public BgpOpenMsg build() throws BgpParseException {
330 BgpHeader bgpMsgHeader = this.isHeaderSet ? this.bgpMsgHeader : DEFAULT_OPEN_HEADER;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530331 short holdTime = this.isHoldTimeSet ? this.holdTime : DEFAULT_HOLD_TIME;
332
333 if (!this.isAsNumSet) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530334 throw new BgpParseException("BGP AS number is not set (mandatory)");
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530335 }
336
337 if (!this.isBgpIdSet) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530338 throw new BgpParseException("BGPID is not set (mandatory)");
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530339 }
340
Shashikanth VHf04ab492016-02-10 11:35:11 +0530341 if (this.isIpV4UnicastCapabilityTlvSet) {
342 BgpValueType tlv;
343 tlv = new MultiProtocolExtnCapabilityTlv((short) Constants.AFI_IPV4_UNICAST, RES,
344 (byte) Constants.SAFI_IPV4_UNICAST);
345 this.capabilityTlv.add(tlv);
346 }
347
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530348 if (this.isLargeAsCapabilityTlvSet) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530349 BgpValueType tlv;
Vidyashree Ramaafbbff92015-11-02 14:45:17 +0530350 int value = this.asNumber;
351 tlv = new FourOctetAsNumCapabilityTlv(value);
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530352 this.capabilityTlv.add(tlv);
353 }
354
355 if (this.isLsCapabilityTlvSet) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530356 BgpValueType tlv;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530357 tlv = new MultiProtocolExtnCapabilityTlv(AFI, RES, SAFI);
358 this.capabilityTlv.add(tlv);
359 }
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530360
Shashikanth VH44967ec2016-02-04 19:46:16 +0530361 if (this.isFlowSpecCapabilityTlvSet) {
362 BgpValueType tlv;
363 tlv = new MultiProtocolExtnCapabilityTlv(Constants.AFI_FLOWSPEC_VALUE,
364 RES, Constants.SAFI_FLOWSPEC_VALUE);
365 this.capabilityTlv.add(tlv);
366 }
367
368 if (this.isVpnFlowSpecCapabilityTlvSet) {
369 BgpValueType tlv;
370 tlv = new MultiProtocolExtnCapabilityTlv(Constants.AFI_FLOWSPEC_VALUE,
371 RES, Constants.VPN_SAFI_FLOWSPEC_VALUE);
372 this.capabilityTlv.add(tlv);
373 }
374
Shashikanth VHb650bfa2016-04-18 12:54:03 +0530375 if (this.isFlowSpecRpdCapabilityTlvSet) {
376 BgpValueType tlv;
Shashikanth VHb58cfd52016-04-21 16:45:50 +0530377 tlv = new RpdCapabilityTlv(Constants.RPD_CAPABILITY_SEND_VALUE);
Shashikanth VHb650bfa2016-04-18 12:54:03 +0530378 this.capabilityTlv.add(tlv);
379 }
380
381
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530382 return new BgpOpenMsgVer4(bgpMsgHeader, PACKET_VERSION, this.asNumber, holdTime, this.bgpId,
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530383 this.capabilityTlv);
384 }
385
386 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530387 public Builder setHeader(BgpHeader bgpMsgHeader) {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530388 this.bgpMsgHeader = bgpMsgHeader;
389 return this;
390 }
391
392 @Override
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530393 public Builder setHoldTime(short holdTime) {
394 this.holdTime = holdTime;
395 this.isHoldTimeSet = true;
396 return this;
397 }
398
399 @Override
400 public Builder setAsNumber(short asNumber) {
401 this.asNumber = asNumber;
402 this.isAsNumSet = true;
403 return this;
404 }
405
406 @Override
407 public Builder setBgpId(int bgpId) {
408 this.bgpId = bgpId;
409 this.isBgpIdSet = true;
410 return this;
411 }
412
413 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530414 public Builder setCapabilityTlv(LinkedList<BgpValueType> capabilityTlv) {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530415 this.capabilityTlv = capabilityTlv;
416 return this;
417 }
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530418
419 @Override
420 public Builder setLargeAsCapabilityTlv(boolean isLargeAsCapabilitySet) {
421 this.isLargeAsCapabilityTlvSet = isLargeAsCapabilitySet;
422 return this;
423 }
424
425 @Override
426 public Builder setLsCapabilityTlv(boolean isLsCapabilitySet) {
427 this.isLsCapabilityTlvSet = isLsCapabilitySet;
428 return this;
429 }
Shashikanth VH44967ec2016-02-04 19:46:16 +0530430
431 @Override
432 public Builder setFlowSpecCapabilityTlv(boolean isFlowSpecCapabilitySet) {
433 this.isFlowSpecCapabilityTlvSet = isFlowSpecCapabilitySet;
434 return this;
435 }
436
437 @Override
438 public Builder setVpnFlowSpecCapabilityTlv(boolean isVpnFlowSpecCapabilitySet) {
439 this.isVpnFlowSpecCapabilityTlvSet = isVpnFlowSpecCapabilitySet;
440 return this;
441 }
Shashikanth VHb650bfa2016-04-18 12:54:03 +0530442
443 @Override
444 public Builder setFlowSpecRpdCapabilityTlv(boolean isFlowSpecRpdCapabilityTlvSet) {
445 this.isFlowSpecRpdCapabilityTlvSet = isFlowSpecRpdCapabilityTlvSet;
446 return this;
447 }
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530448 }
449
450 @Override
451 public void writeTo(ChannelBuffer cb) {
452 try {
453 WRITER.write(cb, this);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530454 } catch (BgpParseException e) {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530455 log.debug("[writeTo] Error: " + e.toString());
456 }
457 }
458
459 public static final Writer WRITER = new Writer();
460
461 /**
462 * Writer class for writing BGP open message to channel buffer.
463 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530464 public static class Writer implements BgpMessageWriter<BgpOpenMsgVer4> {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530465
466 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530467 public void write(ChannelBuffer cb, BgpOpenMsgVer4 message) throws BgpParseException {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530468
469 int optParaLen = 0;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530470 int as4num = 0;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530471
472 int startIndex = cb.writerIndex();
473
474 // write common header and get msg length index
475 int msgLenIndex = message.bgpMsgHeader.write(cb);
476
477 if (msgLenIndex <= 0) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530478 throw new BgpParseException("Unable to write message header.");
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530479 }
480
481 // write version in 1-octet
482 cb.writeByte(message.version);
483
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530484 // get as4num if LS Capability is set
485 if (message.isLargeAsCapabilitySet) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530486 LinkedList<BgpValueType> capabilityTlv = message
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530487 .getCapabilityTlv();
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530488 ListIterator<BgpValueType> listIterator = capabilityTlv
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530489 .listIterator();
490
491 while (listIterator.hasNext()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530492 BgpValueType tlv = listIterator.next();
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530493 if (tlv.getType() == FOUR_OCTET_AS_NUM_CAPA_TYPE) {
494 as4num = ((FourOctetAsNumCapabilityTlv) tlv).getInt();
495 break;
496 }
497 }
498 }
499
500 if ((message.isLargeAsCapabilitySet) && (as4num > 65535)) {
501 // write As number as AS_TRANS
502 cb.writeShort(AS_TRANS);
503 } else {
504 // write AS number in next 2-octet
Shashikanth VH09792f02016-05-10 16:59:57 +0530505 cb.writeShort((short) message.asNumber);
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530506 }
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530507
508 // write HoldTime in next 2-octet
509 cb.writeShort(message.holdTime);
510
511 // write BGP Identifier in next 4-octet
512 cb.writeInt(message.bgpId);
513
514 // store the index of Optional parameter length
515 int optParaLenIndex = cb.writerIndex();
516
517 // set optional parameter length as 0
518 cb.writeByte(0);
519
520 // Pack capability TLV
521 optParaLen = message.packCapabilityTlv(cb, message);
522
523 if (optParaLen != 0) {
524 // Update optional parameter length
525 cb.setByte(optParaLenIndex, (byte) (optParaLen + 2)); //+2 for optional parameter type.
526 }
527
528 // write OPEN Object Length
529 int length = cb.writerIndex() - startIndex;
530 cb.setShort(msgLenIndex, (short) length);
531 message.bgpMsgHeader.setLength((short) length);
532 }
533 }
534
535 /**
536 * returns length of capability tlvs.
537 *
538 * @param cb of type channel buffer
539 * @param message of type BGPOpenMsgVer4
540 * @return capParaLen of open message
541 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530542 protected int packCapabilityTlv(ChannelBuffer cb, BgpOpenMsgVer4 message) {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530543 int startIndex = cb.writerIndex();
544 int capParaLen = 0;
545 int capParaLenIndex = 0;
546
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530547 LinkedList<BgpValueType> capabilityTlv = message.capabilityTlv;
548 ListIterator<BgpValueType> listIterator = capabilityTlv.listIterator();
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530549
550 if (listIterator.hasNext()) {
551 // Set optional parameter type as 2
552 cb.writeByte(OPT_PARA_TYPE_CAPABILITY);
553
554 // Store the index of capability parameter length and update length at the end
555 capParaLenIndex = cb.writerIndex();
556
557 // Set capability parameter length as 0
558 cb.writeByte(0);
559
560 // Update the startIndex to know the length of capability tlv
561 startIndex = cb.writerIndex();
562 }
563
564 while (listIterator.hasNext()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530565 BgpValueType tlv = listIterator.next();
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530566 if (tlv == null) {
mohamedrahil00f6f262016-11-24 20:20:41 +0530567 log.debug("Warning: TLV is null from CapabilityTlv list");
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530568 continue;
569 }
570 tlv.write(cb);
571 }
572
573 capParaLen = cb.writerIndex() - startIndex;
574
575 if (capParaLen != 0) {
576 // Update capability parameter length
577 cb.setByte(capParaLenIndex, (byte) capParaLen);
578 }
579 return capParaLen;
580 }
581
582 @Override
583 public String toString() {
584 return MoreObjects.toStringHelper(getClass())
585 .add("bgpMsgHeader", bgpMsgHeader)
586 .add("version", version)
587 .add("holdTime", holdTime)
588 .add("asNumber", asNumber)
589 .add("bgpId", bgpId)
590 .add("capabilityTlv", capabilityTlv)
591 .toString();
592 }
593}