blob: 2cc9f7adcddd61761475f4e3d809a6f6fa7df3d1 [file] [log] [blame]
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +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 */
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;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053035import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
38import com.google.common.base.MoreObjects;
39
40/**
41 * Provides BGP open message.
42 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053043public class BgpOpenMsgVer4 implements BgpOpenMsg {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053044
45 /*
46 0 1 2 3
47 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
48 +-+-+-+-+-+-+-+-+
49 | Version |
50 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 | My Autonomous System |
52 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 | Hold Time |
54 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 | BGP Identifier |
56 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 | Opt Parm Len |
58 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59 | Optional Parameters (variable) |
60 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 OPEN Message Format
62 REFERENCE : RFC 4271
63 */
64
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053065 protected static final Logger log = LoggerFactory.getLogger(BgpOpenMsgVer4.class);
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053066
67 public static final byte PACKET_VERSION = 4;
68 public static final int OPEN_MSG_MINIMUM_LENGTH = 10;
69 public static final int MSG_HEADER_LENGTH = 19;
70 public static final int MARKER_LENGTH = 16;
71 public static final int DEFAULT_HOLD_TIME = 120;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +053072 public static final short AS_TRANS = 23456;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053073 public static final int OPT_PARA_TYPE_CAPABILITY = 2;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053074 public static final BgpType MSG_TYPE = BgpType.OPEN;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +053075 public static final short AFI = 16388;
76 public static final byte SAFI = 71;
77 public static final byte RES = 0;
78 public static final int FOUR_OCTET_AS_NUM_CAPA_TYPE = 65;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053079 public static final byte[] MARKER = new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
80 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
81 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff};
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053082 public static final BgpHeader DEFAULT_OPEN_HEADER = new BgpHeader(MARKER,
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053083 (short) OPEN_MSG_MINIMUM_LENGTH, (byte) 0X01);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053084 private BgpHeader bgpMsgHeader;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053085 private byte version;
86 private short asNumber;
87 private short holdTime;
88 private int bgpId;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +053089 private boolean isLargeAsCapabilitySet;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053090 private LinkedList<BgpValueType> capabilityTlv;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053091
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053092 public static final BgpOpenMsgVer4.Reader READER = new Reader();
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053093
94 /**
95 * reset variables.
96 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053097 public BgpOpenMsgVer4() {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +053098 this.bgpMsgHeader = null;
99 this.version = 0;
100 this.holdTime = 0;
101 this.asNumber = 0;
102 this.bgpId = 0;
103 this.capabilityTlv = null;
104 }
105
106 /**
107 * Constructor to initialize all variables of BGP Open message.
108 *
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530109 * @param bgpMsgHeader BGP Header in open message
110 * @param version BGP version in open message
111 * @param holdTime hold time in open message
112 * @param asNumber AS number in open message
113 * @param bgpId BGP identifier in open message
114 * @param capabilityTlv capabilities in open message
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530115 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530116 public BgpOpenMsgVer4(BgpHeader bgpMsgHeader, byte version, short asNumber, short holdTime,
117 int bgpId, LinkedList<BgpValueType> capabilityTlv) {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530118 this.bgpMsgHeader = bgpMsgHeader;
119 this.version = version;
120 this.asNumber = asNumber;
121 this.holdTime = holdTime;
122 this.bgpId = bgpId;
123 this.capabilityTlv = capabilityTlv;
124 }
125
126 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530127 public BgpHeader getHeader() {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530128 return this.bgpMsgHeader;
129 }
130
131 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530132 public BgpVersion getVersion() {
133 return BgpVersion.BGP_4;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530134 }
135
136 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530137 public BgpType getType() {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530138 return MSG_TYPE;
139 }
140
141 @Override
142 public short getHoldTime() {
143 return this.holdTime;
144 }
145
146 @Override
147 public short getAsNumber() {
148 return this.asNumber;
149 }
150
151 @Override
152 public int getBgpId() {
153 return this.bgpId;
154 }
155
156 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530157 public LinkedList<BgpValueType> getCapabilityTlv() {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530158 return this.capabilityTlv;
159 }
160
161 /**
162 * Reader class for reading BGP open message from channel buffer.
163 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530164 public static class Reader implements BgpMessageReader<BgpOpenMsg> {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530165
166 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530167 public BgpOpenMsg readFrom(ChannelBuffer cb, BgpHeader bgpHeader) throws BgpParseException {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530168
169 byte version;
170 short holdTime;
171 short asNumber;
172 int bgpId;
173 byte optParaLen = 0;
174 byte optParaType;
175 byte capParaLen = 0;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530176 LinkedList<BgpValueType> capabilityTlv = new LinkedList<>();
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530177
178 if (cb.readableBytes() < OPEN_MSG_MINIMUM_LENGTH) {
179 log.error("[readFrom] Invalid length: Packet size is less than the minimum length ");
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530180 Validation.validateLen(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.BAD_MESSAGE_LENGTH,
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530181 cb.readableBytes());
182 }
183
184 // Read version
185 version = cb.readByte();
186 if (version != PACKET_VERSION) {
187 log.error("[readFrom] Invalid version: " + version);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530188 throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR,
189 BgpErrorType.UNSUPPORTED_VERSION_NUMBER, null);
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530190 }
191
192 // Read AS number
193 asNumber = cb.readShort();
194
195 // Read Hold timer
196 holdTime = cb.readShort();
197
198 // Read BGP Identifier
199 bgpId = cb.readInt();
200
201 // Read optional parameter length
202 optParaLen = cb.readByte();
203
204 // Read Capabilities if optional parameter length is greater than 0
205 if (optParaLen != 0) {
206 // Read optional parameter type
207 optParaType = cb.readByte();
208
209 // Read optional parameter length
210 capParaLen = cb.readByte();
211
212 if (cb.readableBytes() < capParaLen) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530213 throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, (byte) 0, null);
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530214 }
215
216 ChannelBuffer capaCb = cb.readBytes(capParaLen);
217
218 // Parse capabilities only if optional parameter type is 2
219 if ((optParaType == OPT_PARA_TYPE_CAPABILITY) && (capParaLen != 0)) {
220 capabilityTlv = parseCapabilityTlv(capaCb);
221 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530222 throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR,
223 BgpErrorType.UNSUPPORTED_OPTIONAL_PARAMETER, null);
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530224 }
225 }
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530226 return new BgpOpenMsgVer4(bgpHeader, version, asNumber, holdTime, bgpId, capabilityTlv);
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530227 }
228 }
229
230 /**
231 * Parsing capabilities.
232 *
233 * @param cb of type channel buffer
234 * @return capabilityTlv of open message
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530235 * @throws BgpParseException while parsing capabilities
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530236 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530237 protected static LinkedList<BgpValueType> parseCapabilityTlv(ChannelBuffer cb) throws BgpParseException {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530238
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530239 LinkedList<BgpValueType> capabilityTlv = new LinkedList<>();
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530240
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530241 while (cb.readableBytes() > 0) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530242 BgpValueType tlv;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530243 short type = cb.readByte();
244 short length = cb.readByte();
245
246 switch (type) {
247 case FourOctetAsNumCapabilityTlv.TYPE:
248 log.debug("FourOctetAsNumCapabilityTlv");
249 if (FourOctetAsNumCapabilityTlv.LENGTH != length) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530250 throw new BgpParseException("Invalid length received for FourOctetAsNumCapabilityTlv.");
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530251 }
252 if (length > cb.readableBytes()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530253 throw new BgpParseException("Four octet as num tlv length"
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530254 + " is more than readableBytes.");
255 }
256 int as4Num = cb.readInt();
257 tlv = new FourOctetAsNumCapabilityTlv(as4Num);
258 break;
259 case MultiProtocolExtnCapabilityTlv.TYPE:
260 log.debug("MultiProtocolExtnCapabilityTlv");
261 if (MultiProtocolExtnCapabilityTlv.LENGTH != length) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530262 throw new BgpParseException("Invalid length received for MultiProtocolExtnCapabilityTlv.");
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530263 }
264 if (length > cb.readableBytes()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530265 throw new BgpParseException("BGP LS tlv length is more than readableBytes.");
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530266 }
267 short afi = cb.readShort();
268 byte res = cb.readByte();
269 byte safi = cb.readByte();
270 tlv = new MultiProtocolExtnCapabilityTlv(afi, res, safi);
271 break;
272 default:
273 log.debug("Warning: Unsupported TLV: " + type);
274 cb.skipBytes(length);
275 continue;
276 }
277 capabilityTlv.add(tlv);
278 }
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530279 return capabilityTlv;
280 }
281
282 /**
283 * Builder class for BGP open message.
284 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530285 static class Builder implements BgpOpenMsg.Builder {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530286
287 private boolean isHeaderSet = false;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530288 private BgpHeader bgpMsgHeader;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530289 private boolean isHoldTimeSet = false;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530290 private short holdTime;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530291 private boolean isAsNumSet = false;
292 private short asNumber;
293 private boolean isBgpIdSet = false;
294 private int bgpId;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530295 private boolean isLargeAsCapabilityTlvSet = false;
296 private boolean isLsCapabilityTlvSet = false;
Shashikanth VH44967ec2016-02-04 19:46:16 +0530297 private boolean isFlowSpecCapabilityTlvSet = false;
298 private boolean isVpnFlowSpecCapabilityTlvSet = false;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530299
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530300 LinkedList<BgpValueType> capabilityTlv = new LinkedList<>();
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530301
302 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530303 public BgpOpenMsg build() throws BgpParseException {
304 BgpHeader bgpMsgHeader = this.isHeaderSet ? this.bgpMsgHeader : DEFAULT_OPEN_HEADER;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530305 short holdTime = this.isHoldTimeSet ? this.holdTime : DEFAULT_HOLD_TIME;
306
307 if (!this.isAsNumSet) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530308 throw new BgpParseException("BGP AS number is not set (mandatory)");
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530309 }
310
311 if (!this.isBgpIdSet) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530312 throw new BgpParseException("BGPID is not set (mandatory)");
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530313 }
314
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530315 if (this.isLargeAsCapabilityTlvSet) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530316 BgpValueType tlv;
Vidyashree Ramaafbbff92015-11-02 14:45:17 +0530317 int value = this.asNumber;
318 tlv = new FourOctetAsNumCapabilityTlv(value);
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530319 this.capabilityTlv.add(tlv);
320 }
321
322 if (this.isLsCapabilityTlvSet) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530323 BgpValueType tlv;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530324 tlv = new MultiProtocolExtnCapabilityTlv(AFI, RES, SAFI);
325 this.capabilityTlv.add(tlv);
326 }
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530327
Shashikanth VH44967ec2016-02-04 19:46:16 +0530328 if (this.isFlowSpecCapabilityTlvSet) {
329 BgpValueType tlv;
330 tlv = new MultiProtocolExtnCapabilityTlv(Constants.AFI_FLOWSPEC_VALUE,
331 RES, Constants.SAFI_FLOWSPEC_VALUE);
332 this.capabilityTlv.add(tlv);
333 }
334
335 if (this.isVpnFlowSpecCapabilityTlvSet) {
336 BgpValueType tlv;
337 tlv = new MultiProtocolExtnCapabilityTlv(Constants.AFI_FLOWSPEC_VALUE,
338 RES, Constants.VPN_SAFI_FLOWSPEC_VALUE);
339 this.capabilityTlv.add(tlv);
340 }
341
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530342 return new BgpOpenMsgVer4(bgpMsgHeader, PACKET_VERSION, this.asNumber, holdTime, this.bgpId,
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530343 this.capabilityTlv);
344 }
345
346 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530347 public Builder setHeader(BgpHeader bgpMsgHeader) {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530348 this.bgpMsgHeader = bgpMsgHeader;
349 return this;
350 }
351
352 @Override
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530353 public Builder setHoldTime(short holdTime) {
354 this.holdTime = holdTime;
355 this.isHoldTimeSet = true;
356 return this;
357 }
358
359 @Override
360 public Builder setAsNumber(short asNumber) {
361 this.asNumber = asNumber;
362 this.isAsNumSet = true;
363 return this;
364 }
365
366 @Override
367 public Builder setBgpId(int bgpId) {
368 this.bgpId = bgpId;
369 this.isBgpIdSet = true;
370 return this;
371 }
372
373 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530374 public Builder setCapabilityTlv(LinkedList<BgpValueType> capabilityTlv) {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530375 this.capabilityTlv = capabilityTlv;
376 return this;
377 }
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530378
379 @Override
380 public Builder setLargeAsCapabilityTlv(boolean isLargeAsCapabilitySet) {
381 this.isLargeAsCapabilityTlvSet = isLargeAsCapabilitySet;
382 return this;
383 }
384
385 @Override
386 public Builder setLsCapabilityTlv(boolean isLsCapabilitySet) {
387 this.isLsCapabilityTlvSet = isLsCapabilitySet;
388 return this;
389 }
Shashikanth VH44967ec2016-02-04 19:46:16 +0530390
391 @Override
392 public Builder setFlowSpecCapabilityTlv(boolean isFlowSpecCapabilitySet) {
393 this.isFlowSpecCapabilityTlvSet = isFlowSpecCapabilitySet;
394 return this;
395 }
396
397 @Override
398 public Builder setVpnFlowSpecCapabilityTlv(boolean isVpnFlowSpecCapabilitySet) {
399 this.isVpnFlowSpecCapabilityTlvSet = isVpnFlowSpecCapabilitySet;
400 return this;
401 }
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530402 }
403
404 @Override
405 public void writeTo(ChannelBuffer cb) {
406 try {
407 WRITER.write(cb, this);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530408 } catch (BgpParseException e) {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530409 log.debug("[writeTo] Error: " + e.toString());
410 }
411 }
412
413 public static final Writer WRITER = new Writer();
414
415 /**
416 * Writer class for writing BGP open message to channel buffer.
417 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530418 public static class Writer implements BgpMessageWriter<BgpOpenMsgVer4> {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530419
420 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530421 public void write(ChannelBuffer cb, BgpOpenMsgVer4 message) throws BgpParseException {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530422
423 int optParaLen = 0;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530424 int as4num = 0;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530425
426 int startIndex = cb.writerIndex();
427
428 // write common header and get msg length index
429 int msgLenIndex = message.bgpMsgHeader.write(cb);
430
431 if (msgLenIndex <= 0) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530432 throw new BgpParseException("Unable to write message header.");
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530433 }
434
435 // write version in 1-octet
436 cb.writeByte(message.version);
437
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530438 // get as4num if LS Capability is set
439 if (message.isLargeAsCapabilitySet) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530440 LinkedList<BgpValueType> capabilityTlv = message
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530441 .getCapabilityTlv();
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530442 ListIterator<BgpValueType> listIterator = capabilityTlv
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530443 .listIterator();
444
445 while (listIterator.hasNext()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530446 BgpValueType tlv = listIterator.next();
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530447 if (tlv.getType() == FOUR_OCTET_AS_NUM_CAPA_TYPE) {
448 as4num = ((FourOctetAsNumCapabilityTlv) tlv).getInt();
449 break;
450 }
451 }
452 }
453
454 if ((message.isLargeAsCapabilitySet) && (as4num > 65535)) {
455 // write As number as AS_TRANS
456 cb.writeShort(AS_TRANS);
457 } else {
458 // write AS number in next 2-octet
459 cb.writeShort(message.asNumber);
460 }
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530461
462 // write HoldTime in next 2-octet
463 cb.writeShort(message.holdTime);
464
465 // write BGP Identifier in next 4-octet
466 cb.writeInt(message.bgpId);
467
468 // store the index of Optional parameter length
469 int optParaLenIndex = cb.writerIndex();
470
471 // set optional parameter length as 0
472 cb.writeByte(0);
473
474 // Pack capability TLV
475 optParaLen = message.packCapabilityTlv(cb, message);
476
477 if (optParaLen != 0) {
478 // Update optional parameter length
479 cb.setByte(optParaLenIndex, (byte) (optParaLen + 2)); //+2 for optional parameter type.
480 }
481
482 // write OPEN Object Length
483 int length = cb.writerIndex() - startIndex;
484 cb.setShort(msgLenIndex, (short) length);
485 message.bgpMsgHeader.setLength((short) length);
486 }
487 }
488
489 /**
490 * returns length of capability tlvs.
491 *
492 * @param cb of type channel buffer
493 * @param message of type BGPOpenMsgVer4
494 * @return capParaLen of open message
495 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530496 protected int packCapabilityTlv(ChannelBuffer cb, BgpOpenMsgVer4 message) {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530497 int startIndex = cb.writerIndex();
498 int capParaLen = 0;
499 int capParaLenIndex = 0;
500
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530501 LinkedList<BgpValueType> capabilityTlv = message.capabilityTlv;
502 ListIterator<BgpValueType> listIterator = capabilityTlv.listIterator();
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530503
504 if (listIterator.hasNext()) {
505 // Set optional parameter type as 2
506 cb.writeByte(OPT_PARA_TYPE_CAPABILITY);
507
508 // Store the index of capability parameter length and update length at the end
509 capParaLenIndex = cb.writerIndex();
510
511 // Set capability parameter length as 0
512 cb.writeByte(0);
513
514 // Update the startIndex to know the length of capability tlv
515 startIndex = cb.writerIndex();
516 }
517
518 while (listIterator.hasNext()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530519 BgpValueType tlv = listIterator.next();
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530520 if (tlv == null) {
521 log.debug("Warning: tlv is null from CapabilityTlv list");
522 continue;
523 }
524 tlv.write(cb);
525 }
526
527 capParaLen = cb.writerIndex() - startIndex;
528
529 if (capParaLen != 0) {
530 // Update capability parameter length
531 cb.setByte(capParaLenIndex, (byte) capParaLen);
532 }
533 return capParaLen;
534 }
535
536 @Override
537 public String toString() {
538 return MoreObjects.toStringHelper(getClass())
539 .add("bgpMsgHeader", bgpMsgHeader)
540 .add("version", version)
541 .add("holdTime", holdTime)
542 .add("asNumber", asNumber)
543 .add("bgpId", bgpId)
544 .add("capabilityTlv", capabilityTlv)
545 .toString();
546 }
547}