blob: dfaf66519b6fa93f9d9338e5a6ef3cb8afe74898 [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;
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;
Shashikanth VHf04ab492016-02-10 11:35:11 +0530295 private boolean isIpV4UnicastCapabilityTlvSet = true;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530296 private boolean isLargeAsCapabilityTlvSet = false;
297 private boolean isLsCapabilityTlvSet = false;
Shashikanth VH44967ec2016-02-04 19:46:16 +0530298 private boolean isFlowSpecCapabilityTlvSet = false;
299 private boolean isVpnFlowSpecCapabilityTlvSet = false;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530300
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530301 LinkedList<BgpValueType> capabilityTlv = new LinkedList<>();
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530302
303 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530304 public BgpOpenMsg build() throws BgpParseException {
305 BgpHeader bgpMsgHeader = this.isHeaderSet ? this.bgpMsgHeader : DEFAULT_OPEN_HEADER;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530306 short holdTime = this.isHoldTimeSet ? this.holdTime : DEFAULT_HOLD_TIME;
307
308 if (!this.isAsNumSet) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530309 throw new BgpParseException("BGP AS number is not set (mandatory)");
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530310 }
311
312 if (!this.isBgpIdSet) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530313 throw new BgpParseException("BGPID is not set (mandatory)");
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530314 }
315
Shashikanth VHf04ab492016-02-10 11:35:11 +0530316 if (this.isIpV4UnicastCapabilityTlvSet) {
317 BgpValueType tlv;
318 tlv = new MultiProtocolExtnCapabilityTlv((short) Constants.AFI_IPV4_UNICAST, RES,
319 (byte) Constants.SAFI_IPV4_UNICAST);
320 this.capabilityTlv.add(tlv);
321 }
322
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530323 if (this.isLargeAsCapabilityTlvSet) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530324 BgpValueType tlv;
Vidyashree Ramaafbbff92015-11-02 14:45:17 +0530325 int value = this.asNumber;
326 tlv = new FourOctetAsNumCapabilityTlv(value);
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530327 this.capabilityTlv.add(tlv);
328 }
329
330 if (this.isLsCapabilityTlvSet) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530331 BgpValueType tlv;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530332 tlv = new MultiProtocolExtnCapabilityTlv(AFI, RES, SAFI);
333 this.capabilityTlv.add(tlv);
334 }
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530335
Shashikanth VH44967ec2016-02-04 19:46:16 +0530336 if (this.isFlowSpecCapabilityTlvSet) {
337 BgpValueType tlv;
338 tlv = new MultiProtocolExtnCapabilityTlv(Constants.AFI_FLOWSPEC_VALUE,
339 RES, Constants.SAFI_FLOWSPEC_VALUE);
340 this.capabilityTlv.add(tlv);
341 }
342
343 if (this.isVpnFlowSpecCapabilityTlvSet) {
344 BgpValueType tlv;
345 tlv = new MultiProtocolExtnCapabilityTlv(Constants.AFI_FLOWSPEC_VALUE,
346 RES, Constants.VPN_SAFI_FLOWSPEC_VALUE);
347 this.capabilityTlv.add(tlv);
348 }
349
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530350 return new BgpOpenMsgVer4(bgpMsgHeader, PACKET_VERSION, this.asNumber, holdTime, this.bgpId,
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530351 this.capabilityTlv);
352 }
353
354 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530355 public Builder setHeader(BgpHeader bgpMsgHeader) {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530356 this.bgpMsgHeader = bgpMsgHeader;
357 return this;
358 }
359
360 @Override
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530361 public Builder setHoldTime(short holdTime) {
362 this.holdTime = holdTime;
363 this.isHoldTimeSet = true;
364 return this;
365 }
366
367 @Override
368 public Builder setAsNumber(short asNumber) {
369 this.asNumber = asNumber;
370 this.isAsNumSet = true;
371 return this;
372 }
373
374 @Override
375 public Builder setBgpId(int bgpId) {
376 this.bgpId = bgpId;
377 this.isBgpIdSet = true;
378 return this;
379 }
380
381 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530382 public Builder setCapabilityTlv(LinkedList<BgpValueType> capabilityTlv) {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530383 this.capabilityTlv = capabilityTlv;
384 return this;
385 }
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530386
387 @Override
388 public Builder setLargeAsCapabilityTlv(boolean isLargeAsCapabilitySet) {
389 this.isLargeAsCapabilityTlvSet = isLargeAsCapabilitySet;
390 return this;
391 }
392
393 @Override
394 public Builder setLsCapabilityTlv(boolean isLsCapabilitySet) {
395 this.isLsCapabilityTlvSet = isLsCapabilitySet;
396 return this;
397 }
Shashikanth VH44967ec2016-02-04 19:46:16 +0530398
399 @Override
400 public Builder setFlowSpecCapabilityTlv(boolean isFlowSpecCapabilitySet) {
401 this.isFlowSpecCapabilityTlvSet = isFlowSpecCapabilitySet;
402 return this;
403 }
404
405 @Override
406 public Builder setVpnFlowSpecCapabilityTlv(boolean isVpnFlowSpecCapabilitySet) {
407 this.isVpnFlowSpecCapabilityTlvSet = isVpnFlowSpecCapabilitySet;
408 return this;
409 }
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530410 }
411
412 @Override
413 public void writeTo(ChannelBuffer cb) {
414 try {
415 WRITER.write(cb, this);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530416 } catch (BgpParseException e) {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530417 log.debug("[writeTo] Error: " + e.toString());
418 }
419 }
420
421 public static final Writer WRITER = new Writer();
422
423 /**
424 * Writer class for writing BGP open message to channel buffer.
425 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530426 public static class Writer implements BgpMessageWriter<BgpOpenMsgVer4> {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530427
428 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530429 public void write(ChannelBuffer cb, BgpOpenMsgVer4 message) throws BgpParseException {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530430
431 int optParaLen = 0;
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530432 int as4num = 0;
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530433
434 int startIndex = cb.writerIndex();
435
436 // write common header and get msg length index
437 int msgLenIndex = message.bgpMsgHeader.write(cb);
438
439 if (msgLenIndex <= 0) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530440 throw new BgpParseException("Unable to write message header.");
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530441 }
442
443 // write version in 1-octet
444 cb.writeByte(message.version);
445
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530446 // get as4num if LS Capability is set
447 if (message.isLargeAsCapabilitySet) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530448 LinkedList<BgpValueType> capabilityTlv = message
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530449 .getCapabilityTlv();
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530450 ListIterator<BgpValueType> listIterator = capabilityTlv
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530451 .listIterator();
452
453 while (listIterator.hasNext()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530454 BgpValueType tlv = listIterator.next();
Vidyashree Ramaf49b22c2015-10-13 15:30:24 +0530455 if (tlv.getType() == FOUR_OCTET_AS_NUM_CAPA_TYPE) {
456 as4num = ((FourOctetAsNumCapabilityTlv) tlv).getInt();
457 break;
458 }
459 }
460 }
461
462 if ((message.isLargeAsCapabilitySet) && (as4num > 65535)) {
463 // write As number as AS_TRANS
464 cb.writeShort(AS_TRANS);
465 } else {
466 // write AS number in next 2-octet
467 cb.writeShort(message.asNumber);
468 }
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530469
470 // write HoldTime in next 2-octet
471 cb.writeShort(message.holdTime);
472
473 // write BGP Identifier in next 4-octet
474 cb.writeInt(message.bgpId);
475
476 // store the index of Optional parameter length
477 int optParaLenIndex = cb.writerIndex();
478
479 // set optional parameter length as 0
480 cb.writeByte(0);
481
482 // Pack capability TLV
483 optParaLen = message.packCapabilityTlv(cb, message);
484
485 if (optParaLen != 0) {
486 // Update optional parameter length
487 cb.setByte(optParaLenIndex, (byte) (optParaLen + 2)); //+2 for optional parameter type.
488 }
489
490 // write OPEN Object Length
491 int length = cb.writerIndex() - startIndex;
492 cb.setShort(msgLenIndex, (short) length);
493 message.bgpMsgHeader.setLength((short) length);
494 }
495 }
496
497 /**
498 * returns length of capability tlvs.
499 *
500 * @param cb of type channel buffer
501 * @param message of type BGPOpenMsgVer4
502 * @return capParaLen of open message
503 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530504 protected int packCapabilityTlv(ChannelBuffer cb, BgpOpenMsgVer4 message) {
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530505 int startIndex = cb.writerIndex();
506 int capParaLen = 0;
507 int capParaLenIndex = 0;
508
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530509 LinkedList<BgpValueType> capabilityTlv = message.capabilityTlv;
510 ListIterator<BgpValueType> listIterator = capabilityTlv.listIterator();
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530511
512 if (listIterator.hasNext()) {
513 // Set optional parameter type as 2
514 cb.writeByte(OPT_PARA_TYPE_CAPABILITY);
515
516 // Store the index of capability parameter length and update length at the end
517 capParaLenIndex = cb.writerIndex();
518
519 // Set capability parameter length as 0
520 cb.writeByte(0);
521
522 // Update the startIndex to know the length of capability tlv
523 startIndex = cb.writerIndex();
524 }
525
526 while (listIterator.hasNext()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530527 BgpValueType tlv = listIterator.next();
Vidyashree Rama6d4f84e2015-09-21 20:01:02 +0530528 if (tlv == null) {
529 log.debug("Warning: tlv is null from CapabilityTlv list");
530 continue;
531 }
532 tlv.write(cb);
533 }
534
535 capParaLen = cb.writerIndex() - startIndex;
536
537 if (capParaLen != 0) {
538 // Update capability parameter length
539 cb.setByte(capParaLenIndex, (byte) capParaLen);
540 }
541 return capParaLen;
542 }
543
544 @Override
545 public String toString() {
546 return MoreObjects.toStringHelper(getClass())
547 .add("bgpMsgHeader", bgpMsgHeader)
548 .add("version", version)
549 .add("holdTime", holdTime)
550 .add("asNumber", asNumber)
551 .add("bgpId", bgpId)
552 .add("capabilityTlv", capabilityTlv)
553 .toString();
554 }
555}