Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 1 | /* |
| 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 Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 16 | package org.onosproject.bgpio.protocol.ver4; |
| 17 | |
| 18 | import java.util.LinkedList; |
| 19 | import java.util.ListIterator; |
| 20 | |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 22 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 23 | import org.onosproject.bgpio.protocol.BgpMessageReader; |
| 24 | import org.onosproject.bgpio.protocol.BgpMessageWriter; |
| 25 | import org.onosproject.bgpio.protocol.BgpOpenMsg; |
| 26 | import org.onosproject.bgpio.protocol.BgpType; |
| 27 | import org.onosproject.bgpio.protocol.BgpVersion; |
| 28 | import org.onosproject.bgpio.types.BgpErrorType; |
| 29 | import org.onosproject.bgpio.types.BgpHeader; |
| 30 | import org.onosproject.bgpio.types.BgpValueType; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 31 | import org.onosproject.bgpio.types.FourOctetAsNumCapabilityTlv; |
| 32 | import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 33 | import org.onosproject.bgpio.util.Validation; |
| 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
| 37 | import com.google.common.base.MoreObjects; |
| 38 | |
| 39 | /** |
| 40 | * Provides BGP open message. |
| 41 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 42 | public class BgpOpenMsgVer4 implements BgpOpenMsg { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | 0 1 2 3 |
| 46 | 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 |
| 47 | +-+-+-+-+-+-+-+-+ |
| 48 | | Version | |
| 49 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 50 | | My Autonomous System | |
| 51 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 52 | | Hold Time | |
| 53 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 54 | | BGP Identifier | |
| 55 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 56 | | Opt Parm Len | |
| 57 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 58 | | Optional Parameters (variable) | |
| 59 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 60 | OPEN Message Format |
| 61 | REFERENCE : RFC 4271 |
| 62 | */ |
| 63 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 64 | protected static final Logger log = LoggerFactory.getLogger(BgpOpenMsgVer4.class); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 65 | |
| 66 | public static final byte PACKET_VERSION = 4; |
| 67 | public static final int OPEN_MSG_MINIMUM_LENGTH = 10; |
| 68 | public static final int MSG_HEADER_LENGTH = 19; |
| 69 | public static final int MARKER_LENGTH = 16; |
| 70 | public static final int DEFAULT_HOLD_TIME = 120; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 71 | public static final short AS_TRANS = 23456; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 72 | public static final int OPT_PARA_TYPE_CAPABILITY = 2; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 73 | public static final BgpType MSG_TYPE = BgpType.OPEN; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 74 | public static final short AFI = 16388; |
| 75 | public static final byte SAFI = 71; |
| 76 | public static final byte RES = 0; |
| 77 | public static final int FOUR_OCTET_AS_NUM_CAPA_TYPE = 65; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 78 | public static final byte[] MARKER = new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 79 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 80 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 81 | public static final BgpHeader DEFAULT_OPEN_HEADER = new BgpHeader(MARKER, |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 82 | (short) OPEN_MSG_MINIMUM_LENGTH, (byte) 0X01); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 83 | private BgpHeader bgpMsgHeader; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 84 | private byte version; |
| 85 | private short asNumber; |
| 86 | private short holdTime; |
| 87 | private int bgpId; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 88 | private boolean isLargeAsCapabilitySet; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 89 | private LinkedList<BgpValueType> capabilityTlv; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 90 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 91 | public static final BgpOpenMsgVer4.Reader READER = new Reader(); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * reset variables. |
| 95 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 96 | public BgpOpenMsgVer4() { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 97 | this.bgpMsgHeader = null; |
| 98 | this.version = 0; |
| 99 | this.holdTime = 0; |
| 100 | this.asNumber = 0; |
| 101 | this.bgpId = 0; |
| 102 | this.capabilityTlv = null; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Constructor to initialize all variables of BGP Open message. |
| 107 | * |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 108 | * @param bgpMsgHeader BGP Header in open message |
| 109 | * @param version BGP version in open message |
| 110 | * @param holdTime hold time in open message |
| 111 | * @param asNumber AS number in open message |
| 112 | * @param bgpId BGP identifier in open message |
| 113 | * @param capabilityTlv capabilities in open message |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 114 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 115 | public BgpOpenMsgVer4(BgpHeader bgpMsgHeader, byte version, short asNumber, short holdTime, |
| 116 | int bgpId, LinkedList<BgpValueType> capabilityTlv) { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 117 | this.bgpMsgHeader = bgpMsgHeader; |
| 118 | this.version = version; |
| 119 | this.asNumber = asNumber; |
| 120 | this.holdTime = holdTime; |
| 121 | this.bgpId = bgpId; |
| 122 | this.capabilityTlv = capabilityTlv; |
| 123 | } |
| 124 | |
| 125 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 126 | public BgpHeader getHeader() { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 127 | return this.bgpMsgHeader; |
| 128 | } |
| 129 | |
| 130 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 131 | public BgpVersion getVersion() { |
| 132 | return BgpVersion.BGP_4; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 136 | public BgpType getType() { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 137 | return MSG_TYPE; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public short getHoldTime() { |
| 142 | return this.holdTime; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public short getAsNumber() { |
| 147 | return this.asNumber; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public int getBgpId() { |
| 152 | return this.bgpId; |
| 153 | } |
| 154 | |
| 155 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 156 | public LinkedList<BgpValueType> getCapabilityTlv() { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 157 | return this.capabilityTlv; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Reader class for reading BGP open message from channel buffer. |
| 162 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 163 | public static class Reader implements BgpMessageReader<BgpOpenMsg> { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 164 | |
| 165 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 166 | public BgpOpenMsg readFrom(ChannelBuffer cb, BgpHeader bgpHeader) throws BgpParseException { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 167 | |
| 168 | byte version; |
| 169 | short holdTime; |
| 170 | short asNumber; |
| 171 | int bgpId; |
| 172 | byte optParaLen = 0; |
| 173 | byte optParaType; |
| 174 | byte capParaLen = 0; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 175 | LinkedList<BgpValueType> capabilityTlv = new LinkedList<>(); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 176 | |
| 177 | if (cb.readableBytes() < OPEN_MSG_MINIMUM_LENGTH) { |
| 178 | log.error("[readFrom] Invalid length: Packet size is less than the minimum length "); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 179 | Validation.validateLen(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.BAD_MESSAGE_LENGTH, |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 180 | cb.readableBytes()); |
| 181 | } |
| 182 | |
| 183 | // Read version |
| 184 | version = cb.readByte(); |
| 185 | if (version != PACKET_VERSION) { |
| 186 | log.error("[readFrom] Invalid version: " + version); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 187 | throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, |
| 188 | BgpErrorType.UNSUPPORTED_VERSION_NUMBER, null); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // Read AS number |
| 192 | asNumber = cb.readShort(); |
| 193 | |
| 194 | // Read Hold timer |
| 195 | holdTime = cb.readShort(); |
| 196 | |
| 197 | // Read BGP Identifier |
| 198 | bgpId = cb.readInt(); |
| 199 | |
| 200 | // Read optional parameter length |
| 201 | optParaLen = cb.readByte(); |
| 202 | |
| 203 | // Read Capabilities if optional parameter length is greater than 0 |
| 204 | if (optParaLen != 0) { |
| 205 | // Read optional parameter type |
| 206 | optParaType = cb.readByte(); |
| 207 | |
| 208 | // Read optional parameter length |
| 209 | capParaLen = cb.readByte(); |
| 210 | |
| 211 | if (cb.readableBytes() < capParaLen) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 212 | throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, (byte) 0, null); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | ChannelBuffer capaCb = cb.readBytes(capParaLen); |
| 216 | |
| 217 | // Parse capabilities only if optional parameter type is 2 |
| 218 | if ((optParaType == OPT_PARA_TYPE_CAPABILITY) && (capParaLen != 0)) { |
| 219 | capabilityTlv = parseCapabilityTlv(capaCb); |
| 220 | } else { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 221 | throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, |
| 222 | BgpErrorType.UNSUPPORTED_OPTIONAL_PARAMETER, null); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 223 | } |
| 224 | } |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 225 | return new BgpOpenMsgVer4(bgpHeader, version, asNumber, holdTime, bgpId, capabilityTlv); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Parsing capabilities. |
| 231 | * |
| 232 | * @param cb of type channel buffer |
| 233 | * @return capabilityTlv of open message |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 234 | * @throws BgpParseException while parsing capabilities |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 235 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 236 | protected static LinkedList<BgpValueType> parseCapabilityTlv(ChannelBuffer cb) throws BgpParseException { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 237 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 238 | LinkedList<BgpValueType> capabilityTlv = new LinkedList<>(); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 239 | |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 240 | while (cb.readableBytes() > 0) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 241 | BgpValueType tlv; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 242 | short type = cb.readByte(); |
| 243 | short length = cb.readByte(); |
| 244 | |
| 245 | switch (type) { |
| 246 | case FourOctetAsNumCapabilityTlv.TYPE: |
| 247 | log.debug("FourOctetAsNumCapabilityTlv"); |
| 248 | if (FourOctetAsNumCapabilityTlv.LENGTH != length) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 249 | throw new BgpParseException("Invalid length received for FourOctetAsNumCapabilityTlv."); |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 250 | } |
| 251 | if (length > cb.readableBytes()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 252 | throw new BgpParseException("Four octet as num tlv length" |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 253 | + " is more than readableBytes."); |
| 254 | } |
| 255 | int as4Num = cb.readInt(); |
| 256 | tlv = new FourOctetAsNumCapabilityTlv(as4Num); |
| 257 | break; |
| 258 | case MultiProtocolExtnCapabilityTlv.TYPE: |
| 259 | log.debug("MultiProtocolExtnCapabilityTlv"); |
| 260 | if (MultiProtocolExtnCapabilityTlv.LENGTH != length) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 261 | throw new BgpParseException("Invalid length received for MultiProtocolExtnCapabilityTlv."); |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 262 | } |
| 263 | if (length > cb.readableBytes()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 264 | throw new BgpParseException("BGP LS tlv length is more than readableBytes."); |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 265 | } |
| 266 | short afi = cb.readShort(); |
| 267 | byte res = cb.readByte(); |
| 268 | byte safi = cb.readByte(); |
| 269 | tlv = new MultiProtocolExtnCapabilityTlv(afi, res, safi); |
| 270 | break; |
| 271 | default: |
| 272 | log.debug("Warning: Unsupported TLV: " + type); |
| 273 | cb.skipBytes(length); |
| 274 | continue; |
| 275 | } |
| 276 | capabilityTlv.add(tlv); |
| 277 | } |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 278 | return capabilityTlv; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Builder class for BGP open message. |
| 283 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 284 | static class Builder implements BgpOpenMsg.Builder { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 285 | |
| 286 | private boolean isHeaderSet = false; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 287 | private BgpHeader bgpMsgHeader; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 288 | private boolean isHoldTimeSet = false; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 289 | private short holdTime; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 290 | private boolean isAsNumSet = false; |
| 291 | private short asNumber; |
| 292 | private boolean isBgpIdSet = false; |
| 293 | private int bgpId; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 294 | private boolean isLargeAsCapabilityTlvSet = false; |
| 295 | private boolean isLsCapabilityTlvSet = false; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 296 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 297 | LinkedList<BgpValueType> capabilityTlv = new LinkedList<>(); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 298 | |
| 299 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 300 | public BgpOpenMsg build() throws BgpParseException { |
| 301 | BgpHeader bgpMsgHeader = this.isHeaderSet ? this.bgpMsgHeader : DEFAULT_OPEN_HEADER; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 302 | short holdTime = this.isHoldTimeSet ? this.holdTime : DEFAULT_HOLD_TIME; |
| 303 | |
| 304 | if (!this.isAsNumSet) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 305 | throw new BgpParseException("BGP AS number is not set (mandatory)"); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | if (!this.isBgpIdSet) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 309 | throw new BgpParseException("BGPID is not set (mandatory)"); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 310 | } |
| 311 | |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 312 | if (this.isLargeAsCapabilityTlvSet) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 313 | BgpValueType tlv; |
Vidyashree Rama | afbbff9 | 2015-11-02 14:45:17 +0530 | [diff] [blame] | 314 | int value = this.asNumber; |
| 315 | tlv = new FourOctetAsNumCapabilityTlv(value); |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 316 | this.capabilityTlv.add(tlv); |
| 317 | } |
| 318 | |
| 319 | if (this.isLsCapabilityTlvSet) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 320 | BgpValueType tlv; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 321 | tlv = new MultiProtocolExtnCapabilityTlv(AFI, RES, SAFI); |
| 322 | this.capabilityTlv.add(tlv); |
| 323 | } |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 324 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 325 | return new BgpOpenMsgVer4(bgpMsgHeader, PACKET_VERSION, this.asNumber, holdTime, this.bgpId, |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 326 | this.capabilityTlv); |
| 327 | } |
| 328 | |
| 329 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 330 | public Builder setHeader(BgpHeader bgpMsgHeader) { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 331 | this.bgpMsgHeader = bgpMsgHeader; |
| 332 | return this; |
| 333 | } |
| 334 | |
| 335 | @Override |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 336 | public Builder setHoldTime(short holdTime) { |
| 337 | this.holdTime = holdTime; |
| 338 | this.isHoldTimeSet = true; |
| 339 | return this; |
| 340 | } |
| 341 | |
| 342 | @Override |
| 343 | public Builder setAsNumber(short asNumber) { |
| 344 | this.asNumber = asNumber; |
| 345 | this.isAsNumSet = true; |
| 346 | return this; |
| 347 | } |
| 348 | |
| 349 | @Override |
| 350 | public Builder setBgpId(int bgpId) { |
| 351 | this.bgpId = bgpId; |
| 352 | this.isBgpIdSet = true; |
| 353 | return this; |
| 354 | } |
| 355 | |
| 356 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 357 | public Builder setCapabilityTlv(LinkedList<BgpValueType> capabilityTlv) { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 358 | this.capabilityTlv = capabilityTlv; |
| 359 | return this; |
| 360 | } |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 361 | |
| 362 | @Override |
| 363 | public Builder setLargeAsCapabilityTlv(boolean isLargeAsCapabilitySet) { |
| 364 | this.isLargeAsCapabilityTlvSet = isLargeAsCapabilitySet; |
| 365 | return this; |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public Builder setLsCapabilityTlv(boolean isLsCapabilitySet) { |
| 370 | this.isLsCapabilityTlvSet = isLsCapabilitySet; |
| 371 | return this; |
| 372 | } |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public void writeTo(ChannelBuffer cb) { |
| 377 | try { |
| 378 | WRITER.write(cb, this); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 379 | } catch (BgpParseException e) { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 380 | log.debug("[writeTo] Error: " + e.toString()); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | public static final Writer WRITER = new Writer(); |
| 385 | |
| 386 | /** |
| 387 | * Writer class for writing BGP open message to channel buffer. |
| 388 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 389 | public static class Writer implements BgpMessageWriter<BgpOpenMsgVer4> { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 390 | |
| 391 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 392 | public void write(ChannelBuffer cb, BgpOpenMsgVer4 message) throws BgpParseException { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 393 | |
| 394 | int optParaLen = 0; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 395 | int as4num = 0; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 396 | |
| 397 | int startIndex = cb.writerIndex(); |
| 398 | |
| 399 | // write common header and get msg length index |
| 400 | int msgLenIndex = message.bgpMsgHeader.write(cb); |
| 401 | |
| 402 | if (msgLenIndex <= 0) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 403 | throw new BgpParseException("Unable to write message header."); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | // write version in 1-octet |
| 407 | cb.writeByte(message.version); |
| 408 | |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 409 | // get as4num if LS Capability is set |
| 410 | if (message.isLargeAsCapabilitySet) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 411 | LinkedList<BgpValueType> capabilityTlv = message |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 412 | .getCapabilityTlv(); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 413 | ListIterator<BgpValueType> listIterator = capabilityTlv |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 414 | .listIterator(); |
| 415 | |
| 416 | while (listIterator.hasNext()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 417 | BgpValueType tlv = listIterator.next(); |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 418 | if (tlv.getType() == FOUR_OCTET_AS_NUM_CAPA_TYPE) { |
| 419 | as4num = ((FourOctetAsNumCapabilityTlv) tlv).getInt(); |
| 420 | break; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | if ((message.isLargeAsCapabilitySet) && (as4num > 65535)) { |
| 426 | // write As number as AS_TRANS |
| 427 | cb.writeShort(AS_TRANS); |
| 428 | } else { |
| 429 | // write AS number in next 2-octet |
| 430 | cb.writeShort(message.asNumber); |
| 431 | } |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 432 | |
| 433 | // write HoldTime in next 2-octet |
| 434 | cb.writeShort(message.holdTime); |
| 435 | |
| 436 | // write BGP Identifier in next 4-octet |
| 437 | cb.writeInt(message.bgpId); |
| 438 | |
| 439 | // store the index of Optional parameter length |
| 440 | int optParaLenIndex = cb.writerIndex(); |
| 441 | |
| 442 | // set optional parameter length as 0 |
| 443 | cb.writeByte(0); |
| 444 | |
| 445 | // Pack capability TLV |
| 446 | optParaLen = message.packCapabilityTlv(cb, message); |
| 447 | |
| 448 | if (optParaLen != 0) { |
| 449 | // Update optional parameter length |
| 450 | cb.setByte(optParaLenIndex, (byte) (optParaLen + 2)); //+2 for optional parameter type. |
| 451 | } |
| 452 | |
| 453 | // write OPEN Object Length |
| 454 | int length = cb.writerIndex() - startIndex; |
| 455 | cb.setShort(msgLenIndex, (short) length); |
| 456 | message.bgpMsgHeader.setLength((short) length); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * returns length of capability tlvs. |
| 462 | * |
| 463 | * @param cb of type channel buffer |
| 464 | * @param message of type BGPOpenMsgVer4 |
| 465 | * @return capParaLen of open message |
| 466 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 467 | protected int packCapabilityTlv(ChannelBuffer cb, BgpOpenMsgVer4 message) { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 468 | int startIndex = cb.writerIndex(); |
| 469 | int capParaLen = 0; |
| 470 | int capParaLenIndex = 0; |
| 471 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 472 | LinkedList<BgpValueType> capabilityTlv = message.capabilityTlv; |
| 473 | ListIterator<BgpValueType> listIterator = capabilityTlv.listIterator(); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 474 | |
| 475 | if (listIterator.hasNext()) { |
| 476 | // Set optional parameter type as 2 |
| 477 | cb.writeByte(OPT_PARA_TYPE_CAPABILITY); |
| 478 | |
| 479 | // Store the index of capability parameter length and update length at the end |
| 480 | capParaLenIndex = cb.writerIndex(); |
| 481 | |
| 482 | // Set capability parameter length as 0 |
| 483 | cb.writeByte(0); |
| 484 | |
| 485 | // Update the startIndex to know the length of capability tlv |
| 486 | startIndex = cb.writerIndex(); |
| 487 | } |
| 488 | |
| 489 | while (listIterator.hasNext()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 490 | BgpValueType tlv = listIterator.next(); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 491 | if (tlv == null) { |
| 492 | log.debug("Warning: tlv is null from CapabilityTlv list"); |
| 493 | continue; |
| 494 | } |
| 495 | tlv.write(cb); |
| 496 | } |
| 497 | |
| 498 | capParaLen = cb.writerIndex() - startIndex; |
| 499 | |
| 500 | if (capParaLen != 0) { |
| 501 | // Update capability parameter length |
| 502 | cb.setByte(capParaLenIndex, (byte) capParaLen); |
| 503 | } |
| 504 | return capParaLen; |
| 505 | } |
| 506 | |
| 507 | @Override |
| 508 | public String toString() { |
| 509 | return MoreObjects.toStringHelper(getClass()) |
| 510 | .add("bgpMsgHeader", bgpMsgHeader) |
| 511 | .add("version", version) |
| 512 | .add("holdTime", holdTime) |
| 513 | .add("asNumber", asNumber) |
| 514 | .add("bgpId", bgpId) |
| 515 | .add("capabilityTlv", capabilityTlv) |
| 516 | .toString(); |
| 517 | } |
| 518 | } |