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; |
Shashikanth VH | 44967ec | 2016-02-04 19:46:16 +0530 | [diff] [blame] | 34 | import org.onosproject.bgpio.util.Constants; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
| 38 | import com.google.common.base.MoreObjects; |
| 39 | |
| 40 | /** |
| 41 | * Provides BGP open message. |
| 42 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 43 | public class BgpOpenMsgVer4 implements BgpOpenMsg { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 44 | |
| 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 65 | protected static final Logger log = LoggerFactory.getLogger(BgpOpenMsgVer4.class); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 66 | |
| 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 Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 72 | public static final short AS_TRANS = 23456; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 73 | public static final int OPT_PARA_TYPE_CAPABILITY = 2; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 74 | public static final BgpType MSG_TYPE = BgpType.OPEN; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 75 | 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 Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 79 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 82 | public static final BgpHeader DEFAULT_OPEN_HEADER = new BgpHeader(MARKER, |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 83 | (short) OPEN_MSG_MINIMUM_LENGTH, (byte) 0X01); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 84 | private BgpHeader bgpMsgHeader; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 85 | private byte version; |
| 86 | private short asNumber; |
| 87 | private short holdTime; |
| 88 | private int bgpId; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 89 | private boolean isLargeAsCapabilitySet; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 90 | private LinkedList<BgpValueType> capabilityTlv; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 91 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 92 | public static final BgpOpenMsgVer4.Reader READER = new Reader(); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * reset variables. |
| 96 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 97 | public BgpOpenMsgVer4() { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 98 | 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 Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 109 | * @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 Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 115 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 116 | public BgpOpenMsgVer4(BgpHeader bgpMsgHeader, byte version, short asNumber, short holdTime, |
| 117 | int bgpId, LinkedList<BgpValueType> capabilityTlv) { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 118 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 127 | public BgpHeader getHeader() { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 128 | return this.bgpMsgHeader; |
| 129 | } |
| 130 | |
| 131 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 132 | public BgpVersion getVersion() { |
| 133 | return BgpVersion.BGP_4; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 137 | public BgpType getType() { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 138 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 157 | public LinkedList<BgpValueType> getCapabilityTlv() { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 158 | return this.capabilityTlv; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Reader class for reading BGP open message from channel buffer. |
| 163 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 164 | public static class Reader implements BgpMessageReader<BgpOpenMsg> { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 165 | |
| 166 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 167 | public BgpOpenMsg readFrom(ChannelBuffer cb, BgpHeader bgpHeader) throws BgpParseException { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 168 | |
| 169 | byte version; |
| 170 | short holdTime; |
| 171 | short asNumber; |
| 172 | int bgpId; |
| 173 | byte optParaLen = 0; |
| 174 | byte optParaType; |
| 175 | byte capParaLen = 0; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 176 | LinkedList<BgpValueType> capabilityTlv = new LinkedList<>(); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 177 | |
| 178 | if (cb.readableBytes() < OPEN_MSG_MINIMUM_LENGTH) { |
| 179 | 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] | 180 | Validation.validateLen(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.BAD_MESSAGE_LENGTH, |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 181 | cb.readableBytes()); |
| 182 | } |
| 183 | |
| 184 | // Read version |
| 185 | version = cb.readByte(); |
| 186 | if (version != PACKET_VERSION) { |
| 187 | log.error("[readFrom] Invalid version: " + version); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 188 | throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, |
| 189 | BgpErrorType.UNSUPPORTED_VERSION_NUMBER, null); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 190 | } |
| 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 213 | throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, (byte) 0, null); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 214 | } |
| 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 222 | throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, |
| 223 | BgpErrorType.UNSUPPORTED_OPTIONAL_PARAMETER, null); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 224 | } |
| 225 | } |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 226 | return new BgpOpenMsgVer4(bgpHeader, version, asNumber, holdTime, bgpId, capabilityTlv); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Parsing capabilities. |
| 232 | * |
| 233 | * @param cb of type channel buffer |
| 234 | * @return capabilityTlv of open message |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 235 | * @throws BgpParseException while parsing capabilities |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 236 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 237 | protected static LinkedList<BgpValueType> parseCapabilityTlv(ChannelBuffer cb) throws BgpParseException { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 238 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 239 | LinkedList<BgpValueType> capabilityTlv = new LinkedList<>(); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 240 | |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 241 | while (cb.readableBytes() > 0) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 242 | BgpValueType tlv; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 243 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 250 | throw new BgpParseException("Invalid length received for FourOctetAsNumCapabilityTlv."); |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 251 | } |
| 252 | if (length > cb.readableBytes()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 253 | throw new BgpParseException("Four octet as num tlv length" |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 254 | + " 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 262 | throw new BgpParseException("Invalid length received for MultiProtocolExtnCapabilityTlv."); |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 263 | } |
| 264 | if (length > cb.readableBytes()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 265 | throw new BgpParseException("BGP LS tlv length is more than readableBytes."); |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 266 | } |
| 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 Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 279 | return capabilityTlv; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Builder class for BGP open message. |
| 284 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 285 | static class Builder implements BgpOpenMsg.Builder { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 286 | |
| 287 | private boolean isHeaderSet = false; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 288 | private BgpHeader bgpMsgHeader; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 289 | private boolean isHoldTimeSet = false; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 290 | private short holdTime; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 291 | private boolean isAsNumSet = false; |
| 292 | private short asNumber; |
| 293 | private boolean isBgpIdSet = false; |
| 294 | private int bgpId; |
Shashikanth VH | f04ab49 | 2016-02-10 11:35:11 +0530 | [diff] [blame] | 295 | private boolean isIpV4UnicastCapabilityTlvSet = true; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 296 | private boolean isLargeAsCapabilityTlvSet = false; |
| 297 | private boolean isLsCapabilityTlvSet = false; |
Shashikanth VH | 44967ec | 2016-02-04 19:46:16 +0530 | [diff] [blame] | 298 | private boolean isFlowSpecCapabilityTlvSet = false; |
| 299 | private boolean isVpnFlowSpecCapabilityTlvSet = false; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 300 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 301 | LinkedList<BgpValueType> capabilityTlv = new LinkedList<>(); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 302 | |
| 303 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 304 | public BgpOpenMsg build() throws BgpParseException { |
| 305 | BgpHeader bgpMsgHeader = this.isHeaderSet ? this.bgpMsgHeader : DEFAULT_OPEN_HEADER; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 306 | short holdTime = this.isHoldTimeSet ? this.holdTime : DEFAULT_HOLD_TIME; |
| 307 | |
| 308 | if (!this.isAsNumSet) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 309 | throw new BgpParseException("BGP AS number is not set (mandatory)"); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | if (!this.isBgpIdSet) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 313 | throw new BgpParseException("BGPID is not set (mandatory)"); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 314 | } |
| 315 | |
Shashikanth VH | f04ab49 | 2016-02-10 11:35:11 +0530 | [diff] [blame] | 316 | 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 Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 323 | if (this.isLargeAsCapabilityTlvSet) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 324 | BgpValueType tlv; |
Vidyashree Rama | afbbff9 | 2015-11-02 14:45:17 +0530 | [diff] [blame] | 325 | int value = this.asNumber; |
| 326 | tlv = new FourOctetAsNumCapabilityTlv(value); |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 327 | this.capabilityTlv.add(tlv); |
| 328 | } |
| 329 | |
| 330 | if (this.isLsCapabilityTlvSet) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 331 | BgpValueType tlv; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 332 | tlv = new MultiProtocolExtnCapabilityTlv(AFI, RES, SAFI); |
| 333 | this.capabilityTlv.add(tlv); |
| 334 | } |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 335 | |
Shashikanth VH | 44967ec | 2016-02-04 19:46:16 +0530 | [diff] [blame] | 336 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 350 | return new BgpOpenMsgVer4(bgpMsgHeader, PACKET_VERSION, this.asNumber, holdTime, this.bgpId, |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 351 | this.capabilityTlv); |
| 352 | } |
| 353 | |
| 354 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 355 | public Builder setHeader(BgpHeader bgpMsgHeader) { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 356 | this.bgpMsgHeader = bgpMsgHeader; |
| 357 | return this; |
| 358 | } |
| 359 | |
| 360 | @Override |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 361 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 382 | public Builder setCapabilityTlv(LinkedList<BgpValueType> capabilityTlv) { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 383 | this.capabilityTlv = capabilityTlv; |
| 384 | return this; |
| 385 | } |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 386 | |
| 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 VH | 44967ec | 2016-02-04 19:46:16 +0530 | [diff] [blame] | 398 | |
| 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 Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public void writeTo(ChannelBuffer cb) { |
| 414 | try { |
| 415 | WRITER.write(cb, this); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 416 | } catch (BgpParseException e) { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 417 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 426 | public static class Writer implements BgpMessageWriter<BgpOpenMsgVer4> { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 427 | |
| 428 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 429 | public void write(ChannelBuffer cb, BgpOpenMsgVer4 message) throws BgpParseException { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 430 | |
| 431 | int optParaLen = 0; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 432 | int as4num = 0; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 433 | |
| 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 440 | throw new BgpParseException("Unable to write message header."); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | // write version in 1-octet |
| 444 | cb.writeByte(message.version); |
| 445 | |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 446 | // get as4num if LS Capability is set |
| 447 | if (message.isLargeAsCapabilitySet) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 448 | LinkedList<BgpValueType> capabilityTlv = message |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 449 | .getCapabilityTlv(); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 450 | ListIterator<BgpValueType> listIterator = capabilityTlv |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 451 | .listIterator(); |
| 452 | |
| 453 | while (listIterator.hasNext()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 454 | BgpValueType tlv = listIterator.next(); |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 455 | 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 Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 469 | |
| 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 504 | protected int packCapabilityTlv(ChannelBuffer cb, BgpOpenMsgVer4 message) { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 505 | int startIndex = cb.writerIndex(); |
| 506 | int capParaLen = 0; |
| 507 | int capParaLenIndex = 0; |
| 508 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 509 | LinkedList<BgpValueType> capabilityTlv = message.capabilityTlv; |
| 510 | ListIterator<BgpValueType> listIterator = capabilityTlv.listIterator(); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 511 | |
| 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 527 | BgpValueType tlv = listIterator.next(); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 528 | 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 | } |