Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | package org.onosproject.bgpio.types; |
| 18 | |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.onlab.packet.Ip4Address; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 22 | import org.onosproject.bgpio.exceptions.BgpParseException; |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 23 | import org.onosproject.bgpio.protocol.BgpEvpnNlri; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 24 | import org.onosproject.bgpio.protocol.BgpLSNlri; |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 25 | import org.onosproject.bgpio.protocol.evpn.BgpEvpnNlriImpl; |
| 26 | import org.onosproject.bgpio.protocol.evpn.BgpEvpnRouteType2Nlri; |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 27 | import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecNlri; |
Priyanka B | e07ea4d | 2015-11-21 21:28:10 +0530 | [diff] [blame] | 28 | import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4; |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 29 | import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4; |
| 30 | import org.onosproject.bgpio.protocol.linkstate.BgpPrefixIPv4LSNlriVer4; |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 31 | import org.onosproject.bgpio.util.Constants; |
| 32 | import org.onosproject.bgpio.util.Validation; |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 36 | import java.net.InetAddress; |
| 37 | import java.util.LinkedList; |
| 38 | import java.util.List; |
| 39 | import java.util.ListIterator; |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * Provides Implementation of MpReach Nlri BGP Path Attribute. |
| 43 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 44 | public class MpReachNlri implements BgpValueType { |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 45 | |
| 46 | private static final Logger log = LoggerFactory.getLogger(MpReachNlri.class); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 47 | public static final byte MPREACHNLRI_TYPE = 14; |
| 48 | public static final byte LINK_NLRITYPE = 2; |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 49 | public static final byte FLAGS = (byte) 0x90; |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 50 | private boolean isMpReachNlri = false; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 51 | private final List<BgpLSNlri> mpReachNlri; |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 52 | private final int length; |
| 53 | private final short afi; |
| 54 | private final byte safi; |
| 55 | private final Ip4Address ipNextHop; |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 56 | private BgpFlowSpecNlri bgpFlowSpecNlri; |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 57 | private List<BgpEvpnNlri> evpnNlri; |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Constructor to initialize parameters. |
| 61 | * |
| 62 | * @param mpReachNlri MpReach Nlri attribute |
| 63 | * @param afi address family identifier |
| 64 | * @param safi subsequent address family identifier |
| 65 | * @param ipNextHop nexthop IpAddress |
| 66 | * @param length of MpReachNlri |
| 67 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 68 | public MpReachNlri(List<BgpLSNlri> mpReachNlri, short afi, byte safi, Ip4Address ipNextHop, int length) { |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 69 | this.mpReachNlri = mpReachNlri; |
| 70 | this.isMpReachNlri = true; |
| 71 | this.ipNextHop = ipNextHop; |
| 72 | this.afi = afi; |
| 73 | this.safi = safi; |
| 74 | this.length = length; |
| 75 | } |
| 76 | |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 77 | /** |
| 78 | * Constructor to initialize parameters. |
| 79 | * |
| 80 | * @param bgpFlowSpecNlri bgpFlowSpecNlri |
| 81 | * @param afi afi |
| 82 | * @param safi safi |
| 83 | */ |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 84 | public MpReachNlri(BgpFlowSpecNlri bgpFlowSpecNlri, short afi, byte safi) { |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 85 | this.mpReachNlri = null; |
| 86 | this.isMpReachNlri = true; |
| 87 | this.length = 0; |
| 88 | this.ipNextHop = null; |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 89 | this.bgpFlowSpecNlri = bgpFlowSpecNlri; |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 90 | this.afi = afi; |
| 91 | this.safi = safi; |
| 92 | } |
| 93 | |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 94 | /** |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 95 | * Constructor to initialize parameters. |
| 96 | * |
| 97 | * @param evpnNlri evpnNlri |
| 98 | * @param afi afi |
| 99 | * @param safi safi |
| 100 | * @param ipNextHop IP Nexthop |
| 101 | */ |
| 102 | public MpReachNlri(List<BgpEvpnNlri> evpnNlri, short afi, byte safi, Ip4Address ipNextHop) { |
| 103 | this.mpReachNlri = null; |
| 104 | this.length = 42; |
| 105 | this.ipNextHop = ipNextHop; |
| 106 | this.isMpReachNlri = true; |
| 107 | this.evpnNlri = evpnNlri; |
| 108 | this.afi = afi; |
| 109 | this.safi = safi; |
| 110 | } |
| 111 | |
| 112 | /** |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 113 | * Returns whether MpReachNlri is present. |
| 114 | * |
| 115 | * @return whether MpReachNlri is present |
| 116 | */ |
| 117 | public boolean isMpReachNlriSet() { |
| 118 | return this.isMpReachNlri; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns list of MpReach Nlri. |
| 123 | * |
| 124 | * @return list of MpReach Nlri |
| 125 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 126 | public List<BgpLSNlri> mpReachNlri() { |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 127 | return this.mpReachNlri; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Returns length of MpReachNlri. |
| 132 | * |
| 133 | * @return length of MpReachNlri |
| 134 | */ |
| 135 | public int mpReachNlriLen() { |
| 136 | return this.length; |
| 137 | } |
| 138 | |
| 139 | /** |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 140 | * Returns BGP flow specification info. |
| 141 | * |
| 142 | * @return BGP flow specification info |
| 143 | */ |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 144 | public BgpFlowSpecNlri bgpFlowSpecNlri() { |
| 145 | return this.bgpFlowSpecNlri; |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 149 | * Returns BGP Evpn info. |
| 150 | * |
| 151 | * @return BGP Evpn info |
| 152 | */ |
| 153 | public List<BgpEvpnNlri> bgpEvpnNlri() { |
| 154 | return this.evpnNlri; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Returns afi. |
| 159 | * |
| 160 | * @return afi |
| 161 | */ |
| 162 | public short getAfi() { |
| 163 | return this.afi; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Returns safi. |
| 168 | * |
| 169 | * @return safi |
| 170 | */ |
| 171 | public byte getSafi() { |
| 172 | return this.safi(); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Returns mpReachNlri details type. |
| 177 | * |
| 178 | * @return type |
| 179 | */ |
| 180 | public BgpNlriType getNlriDetailsType() { |
| 181 | if ((this.afi == Constants.AFI_VALUE) |
| 182 | && (this.safi == Constants.SAFI_VALUE) |
| 183 | || (this.afi == Constants.AFI_VALUE) |
| 184 | && (this.safi == Constants.VPN_SAFI_VALUE)) { |
| 185 | return BgpNlriType.LINK_STATE; |
| 186 | } |
| 187 | |
| 188 | if ((afi == Constants.AFI_FLOWSPEC_VALUE) |
| 189 | && ((safi == Constants.SAFI_FLOWSPEC_VALUE) |
| 190 | || (safi == Constants.VPN_SAFI_FLOWSPEC_VALUE))) { |
| 191 | return BgpNlriType.FLOW_SPEC; |
| 192 | } |
| 193 | |
| 194 | if ((afi == Constants.AFI_EVPN_VALUE) |
| 195 | && (safi == Constants.SAFI_EVPN_VALUE)) { |
| 196 | return BgpNlriType.EVPN; |
| 197 | } |
| 198 | |
| 199 | return null; |
| 200 | } |
| 201 | |
| 202 | /** |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 203 | * Reads from ChannelBuffer and parses MpReachNlri. |
| 204 | * |
| 205 | * @param cb channelBuffer |
| 206 | * @return object of MpReachNlri |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 207 | * @throws BgpParseException while parsing MpReachNlri |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 208 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 209 | public static MpReachNlri read(ChannelBuffer cb) throws BgpParseException { |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 210 | ChannelBuffer tempBuf = cb.copy(); |
| 211 | Validation parseFlags = Validation.parseAttributeHeader(cb); |
| 212 | int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : |
| 213 | parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_BYTE; |
| 214 | ChannelBuffer data = tempBuf.readBytes(len); |
| 215 | |
| 216 | if (cb.readableBytes() < parseFlags.getLength()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 217 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 218 | parseFlags.getLength()); |
| 219 | } |
| 220 | if (!parseFlags.getFirstBit() && parseFlags.getSecondBit() && parseFlags.getThirdBit()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 221 | throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 222 | } |
| 223 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 224 | BgpLSNlri bgpLSNlri = null; |
| 225 | List<BgpLSNlri> mpReachNlri = new LinkedList<>(); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 226 | ChannelBuffer tempCb = cb.readBytes(parseFlags.getLength()); |
| 227 | short afi = 0; |
| 228 | byte safi = 0; |
| 229 | Ip4Address ipNextHop = null; |
| 230 | while (tempCb.readableBytes() > 0) { |
| 231 | afi = tempCb.readShort(); |
| 232 | safi = tempCb.readByte(); |
| 233 | |
| 234 | //Supporting for AFI 16388 / SAFI 71 and VPN AFI 16388 / SAFI 128 |
| 235 | if ((afi == Constants.AFI_VALUE) && (safi == Constants.SAFI_VALUE) || (afi == Constants.AFI_VALUE) |
| 236 | && (safi == Constants.VPN_SAFI_VALUE)) { |
| 237 | byte nextHopLen = tempCb.readByte(); |
Vidyashree Rama | 7ba6b4c | 2015-11-30 17:20:22 +0530 | [diff] [blame] | 238 | InetAddress ipAddress = Validation.toInetAddress(nextHopLen, tempCb); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 239 | if (ipAddress.isMulticastAddress()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 240 | throw new BgpParseException("Multicast not supported"); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 241 | } |
| 242 | ipNextHop = Ip4Address.valueOf(ipAddress); |
| 243 | byte reserved = tempCb.readByte(); |
| 244 | |
| 245 | while (tempCb.readableBytes() > 0) { |
| 246 | short nlriType = tempCb.readShort(); |
| 247 | short totNlriLen = tempCb.readShort(); |
| 248 | if (tempCb.readableBytes() < totNlriLen) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 249 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 250 | BgpErrorType.ATTRIBUTE_LENGTH_ERROR, totNlriLen); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 251 | } |
| 252 | tempBuf = tempCb.readBytes(totNlriLen); |
| 253 | switch (nlriType) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 254 | case BgpNodeLSNlriVer4.NODE_NLRITYPE: |
| 255 | bgpLSNlri = BgpNodeLSNlriVer4.read(tempBuf, afi, safi); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 256 | break; |
Priyanka B | e07ea4d | 2015-11-21 21:28:10 +0530 | [diff] [blame] | 257 | case BgpLinkLsNlriVer4.LINK_NLRITYPE: |
| 258 | bgpLSNlri = BgpLinkLsNlriVer4.read(tempBuf, afi, safi); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 259 | break; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 260 | case BgpPrefixIPv4LSNlriVer4.PREFIX_IPV4_NLRITYPE: |
| 261 | bgpLSNlri = BgpPrefixIPv4LSNlriVer4.read(tempBuf, afi, safi); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 262 | break; |
| 263 | default: |
| 264 | log.debug("nlriType not supported" + nlriType); |
| 265 | } |
| 266 | mpReachNlri.add(bgpLSNlri); |
| 267 | } |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 268 | } else if ((afi == Constants.AFI_FLOWSPEC_VALUE) |
| 269 | && ((safi == Constants.SAFI_FLOWSPEC_VALUE) |
| 270 | || (safi == Constants.VPN_SAFI_FLOWSPEC_VALUE))) { |
| 271 | List<BgpValueType> flowSpecComponents = new LinkedList<>(); |
Shashikanth VH | e3a73bc | 2016-02-22 20:11:31 +0530 | [diff] [blame] | 272 | RouteDistinguisher routeDistinguisher = null; |
Shashikanth VH | 5826066 | 2016-02-13 01:12:02 +0530 | [diff] [blame] | 273 | if (tempCb.readableBytes() > 0) { |
| 274 | BgpValueType flowSpecComponent = null; |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 275 | |
Shashikanth VH | 5826066 | 2016-02-13 01:12:02 +0530 | [diff] [blame] | 276 | byte nextHopLen = tempCb.readByte(); |
| 277 | if (nextHopLen > 0) { |
| 278 | InetAddress ipAddress = Validation.toInetAddress(nextHopLen, tempCb); |
| 279 | if (ipAddress.isMulticastAddress()) { |
| 280 | throw new BgpParseException("Multicast not supported"); |
| 281 | } |
| 282 | ipNextHop = Ip4Address.valueOf(ipAddress); |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 283 | } |
Shashikanth VH | 5826066 | 2016-02-13 01:12:02 +0530 | [diff] [blame] | 284 | |
| 285 | byte reserved = tempCb.readByte(); |
Shashikanth VH | e3a73bc | 2016-02-22 20:11:31 +0530 | [diff] [blame] | 286 | |
| 287 | if (safi == Constants.VPN_SAFI_FLOWSPEC_VALUE) { |
| 288 | routeDistinguisher = new RouteDistinguisher(); |
| 289 | routeDistinguisher = RouteDistinguisher.read(tempCb); |
| 290 | } |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 291 | while (tempCb.readableBytes() > 0) { |
subhasis777 | 55e43a8 | 2017-02-12 07:46:09 +0530 | [diff] [blame] | 292 | short totNlriLen = tempCb.getUnsignedByte(tempCb.readerIndex()); |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 293 | if (totNlriLen >= BgpFlowSpecNlri.FLOW_SPEC_LEN) { |
| 294 | if (tempCb.readableBytes() < 2) { |
| 295 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 296 | BgpErrorType.ATTRIBUTE_LENGTH_ERROR, totNlriLen); |
| 297 | } |
| 298 | totNlriLen = tempCb.readShort(); |
| 299 | } else { |
| 300 | totNlriLen = tempCb.readByte(); |
Shashikanth VH | 5826066 | 2016-02-13 01:12:02 +0530 | [diff] [blame] | 301 | } |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 302 | if (tempCb.readableBytes() < totNlriLen) { |
| 303 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 304 | BgpErrorType.ATTRIBUTE_LENGTH_ERROR, totNlriLen); |
| 305 | } |
| 306 | tempBuf = tempCb.readBytes(totNlriLen); |
| 307 | while (tempBuf.readableBytes() > 0) { |
| 308 | short type = tempBuf.readByte(); |
| 309 | switch (type) { |
| 310 | case Constants.BGP_FLOWSPEC_DST_PREFIX: |
| 311 | flowSpecComponent = BgpFsDestinationPrefix.read(tempBuf); |
| 312 | break; |
| 313 | case Constants.BGP_FLOWSPEC_SRC_PREFIX: |
| 314 | flowSpecComponent = BgpFsSourcePrefix.read(tempBuf); |
| 315 | break; |
| 316 | case Constants.BGP_FLOWSPEC_IP_PROTO: |
| 317 | flowSpecComponent = BgpFsIpProtocol.read(tempBuf); |
| 318 | break; |
| 319 | case Constants.BGP_FLOWSPEC_PORT: |
| 320 | flowSpecComponent = BgpFsPortNum.read(tempBuf); |
| 321 | break; |
| 322 | case Constants.BGP_FLOWSPEC_DST_PORT: |
| 323 | flowSpecComponent = BgpFsDestinationPortNum.read(tempBuf); |
| 324 | break; |
| 325 | case Constants.BGP_FLOWSPEC_SRC_PORT: |
| 326 | flowSpecComponent = BgpFsSourcePortNum.read(tempBuf); |
| 327 | break; |
| 328 | case Constants.BGP_FLOWSPEC_ICMP_TP: |
| 329 | flowSpecComponent = BgpFsIcmpType.read(tempBuf); |
| 330 | break; |
| 331 | case Constants.BGP_FLOWSPEC_ICMP_CD: |
| 332 | flowSpecComponent = BgpFsIcmpCode.read(tempBuf); |
| 333 | break; |
| 334 | case Constants.BGP_FLOWSPEC_TCP_FLAGS: |
| 335 | flowSpecComponent = BgpFsTcpFlags.read(tempBuf); |
| 336 | break; |
| 337 | case Constants.BGP_FLOWSPEC_PCK_LEN: |
| 338 | flowSpecComponent = BgpFsPacketLength.read(tempBuf); |
| 339 | break; |
| 340 | case Constants.BGP_FLOWSPEC_DSCP: |
| 341 | flowSpecComponent = BgpFsDscpValue.read(tempBuf); |
| 342 | break; |
| 343 | case Constants.BGP_FLOWSPEC_FRAGMENT: |
| 344 | flowSpecComponent = BgpFsFragment.read(tempBuf); |
| 345 | break; |
| 346 | default: |
| 347 | log.debug("flow spec type not supported" + type); |
| 348 | break; |
| 349 | } |
| 350 | flowSpecComponents.add(flowSpecComponent); |
| 351 | } |
Shashikanth VH | 5826066 | 2016-02-13 01:12:02 +0530 | [diff] [blame] | 352 | } |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 353 | } |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 354 | BgpFlowSpecNlri flowSpecDetails = new BgpFlowSpecNlri(flowSpecComponents); |
Shashikanth VH | e3a73bc | 2016-02-22 20:11:31 +0530 | [diff] [blame] | 355 | flowSpecDetails.setRouteDistinguiher(routeDistinguisher); |
| 356 | return new MpReachNlri(flowSpecDetails, afi, safi); |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 357 | } else if ((afi == Constants.AFI_EVPN_VALUE) |
| 358 | && (safi == Constants.SAFI_EVPN_VALUE)) { |
| 359 | |
| 360 | List<BgpEvpnNlri> eVpnComponents = null; |
| 361 | |
| 362 | byte nextHopLen = tempCb.readByte(); |
| 363 | InetAddress ipAddress = Validation.toInetAddress(nextHopLen, |
| 364 | tempCb); |
| 365 | if (ipAddress.isMulticastAddress()) { |
| 366 | throw new BgpParseException("Multicast not supported"); |
| 367 | } |
| 368 | ipNextHop = Ip4Address.valueOf(ipAddress); |
| 369 | byte reserved = tempCb.readByte(); |
| 370 | while (tempCb.readableBytes() > 0) { |
| 371 | BgpEvpnNlri eVpnComponent = BgpEvpnNlriImpl.read(tempCb); |
| 372 | eVpnComponents = new LinkedList<>(); |
| 373 | eVpnComponents.add(eVpnComponent); |
| 374 | log.info("=====evpn Component is {} ======", eVpnComponent); |
| 375 | } |
| 376 | |
| 377 | return new MpReachNlri(eVpnComponents, afi, safi, ipNextHop); |
| 378 | |
| 379 | |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 380 | } else { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 381 | throw new BgpParseException("Not Supporting afi " + afi + "safi " + safi); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | return new MpReachNlri(mpReachNlri, afi, safi, ipNextHop, parseFlags.getLength()); |
| 385 | } |
| 386 | |
| 387 | @Override |
| 388 | public short getType() { |
| 389 | return MPREACHNLRI_TYPE; |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Returns AFI. |
| 394 | * |
| 395 | * @return AFI |
| 396 | */ |
| 397 | public short afi() { |
| 398 | return this.afi; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Returns Nexthop IpAddress. |
| 403 | * |
| 404 | * @return Nexthop IpAddress |
| 405 | */ |
| 406 | public Ip4Address nexthop4() { |
| 407 | return this.ipNextHop; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Returns SAFI. |
| 412 | * |
| 413 | * @return SAFI |
| 414 | */ |
| 415 | public byte safi() { |
| 416 | return this.safi; |
| 417 | } |
| 418 | |
| 419 | @Override |
| 420 | public int write(ChannelBuffer cb) { |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 421 | int iLenStartIndex = cb.writerIndex(); |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 422 | |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 423 | if ((afi == Constants.AFI_FLOWSPEC_VALUE) && ((safi == Constants.SAFI_FLOWSPEC_VALUE) |
| 424 | || (safi == Constants.VPN_SAFI_FLOWSPEC_VALUE))) { |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 425 | List<BgpValueType> flowSpec = bgpFlowSpecNlri.flowSpecComponents(); |
| 426 | ListIterator<BgpValueType> listIterator = flowSpec.listIterator(); |
| 427 | boolean isAllFlowTypesIdentical = true; |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 428 | |
| 429 | cb.writeByte(FLAGS); |
| 430 | cb.writeByte(MPREACHNLRI_TYPE); |
| 431 | |
| 432 | int mpReachDataIndx = cb.writerIndex(); |
| 433 | cb.writeShort(0); |
| 434 | |
| 435 | cb.writeShort(afi); |
| 436 | cb.writeByte(safi); |
| 437 | //next hop address |
| 438 | cb.writeByte(0); |
| 439 | //sub network points of attachment |
| 440 | cb.writeByte(0); |
| 441 | |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 442 | if (bgpFlowSpecNlri.routeDistinguisher() != null) { |
| 443 | cb.writeLong(bgpFlowSpecNlri.routeDistinguisher().getRouteDistinguisher()); |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 444 | } |
| 445 | |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 446 | BgpValueType tlv1 = null; |
| 447 | if (listIterator.hasNext()) { |
| 448 | tlv1 = listIterator.next(); |
| 449 | } |
Ray Milkey | ae4e5ed | 2018-01-17 15:24:52 -0800 | [diff] [blame^] | 450 | while (tlv1 != null && listIterator.hasNext()) { |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 451 | BgpValueType tlv = listIterator.next(); |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 452 | if (tlv.getType() != tlv1.getType()) { |
| 453 | isAllFlowTypesIdentical = false; |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 454 | break; |
| 455 | } |
| 456 | } |
| 457 | |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 458 | if (isAllFlowTypesIdentical) { |
| 459 | BgpFlowSpecNlri.updateBufferIdenticalFlowTypes(cb, bgpFlowSpecNlri()); |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 460 | } else { |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 461 | BgpFlowSpecNlri.updateBufferNonIdenticalFlowTypes(cb, bgpFlowSpecNlri()); |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 462 | } |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 463 | int fsNlriLen = cb.writerIndex() - mpReachDataIndx; |
| 464 | cb.setShort(mpReachDataIndx, (short) (fsNlriLen - 2)); |
| 465 | |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 466 | } else if ((afi == Constants.AFI_EVPN_VALUE) |
| 467 | && (safi == Constants.SAFI_EVPN_VALUE)) { |
| 468 | |
| 469 | cb.writeByte(FLAGS); |
| 470 | cb.writeByte(MPREACHNLRI_TYPE); |
| 471 | |
| 472 | int mpReachDataIndex = cb.writerIndex(); |
| 473 | cb.writeShort(0); |
| 474 | cb.writeShort(afi); |
| 475 | cb.writeByte(safi); |
| 476 | // ip address length |
| 477 | cb.writeByte(0x04); |
| 478 | cb.writeInt(ipNextHop.toInt()); |
| 479 | //sub network points of attachment |
| 480 | cb.writeByte(0); |
| 481 | |
| 482 | for (BgpEvpnNlri element : evpnNlri) { |
| 483 | short routeType = element.getType(); |
| 484 | switch (routeType) { |
| 485 | case Constants.BGP_EVPN_MAC_IP_ADVERTISEMENT: |
| 486 | cb.writeByte(element.getType()); |
| 487 | int iSpecStartIndex = cb.writerIndex(); |
| 488 | cb.writeByte(0); |
| 489 | BgpEvpnRouteType2Nlri macIpAdvNlri = (BgpEvpnRouteType2Nlri) element |
| 490 | .getNlri(); |
| 491 | macIpAdvNlri.write(cb); |
| 492 | cb.setByte(iSpecStartIndex, (byte) (cb.writerIndex() |
| 493 | - iSpecStartIndex - 1)); |
| 494 | //ChannelBuffer temcb = cb.copy(); |
| 495 | break; |
| 496 | case Constants.BGP_EVPN_ETHERNET_AUTO_DISCOVERY: |
| 497 | break; |
| 498 | case Constants.BGP_EVPN_INCLUSIVE_MULTICASE_ETHERNET: |
| 499 | break; |
| 500 | case Constants.BGP_EVPN_ETHERNET_SEGMENT: |
| 501 | break; |
| 502 | default: |
| 503 | break; |
| 504 | } |
| 505 | } |
| 506 | int evpnNlriLen = cb.writerIndex() - mpReachDataIndex; |
| 507 | cb.setShort(mpReachDataIndex, (short) (evpnNlriLen - 2)); |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | return cb.writerIndex() - iLenStartIndex; |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | @Override |
| 514 | public String toString() { |
Shashikanth VH | 510a8d8 | 2016-02-12 12:32:43 +0530 | [diff] [blame] | 515 | return MoreObjects.toStringHelper(getClass()).omitNullValues() |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 516 | .add("mpReachNlri", mpReachNlri) |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 517 | .add("bgpFlowSpecNlri", bgpFlowSpecNlri) |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 518 | .add("afi", afi) |
| 519 | .add("safi", safi) |
| 520 | .add("ipNextHop", ipNextHop) |
| 521 | .add("length", length) |
| 522 | .toString(); |
| 523 | } |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 524 | |
| 525 | @Override |
| 526 | public int compareTo(Object o) { |
| 527 | // TODO Auto-generated method stub |
| 528 | return 0; |
| 529 | } |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 530 | } |