Priyanka B | 3b69554 | 2015-10-14 17:16:27 +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 | */ |
| 16 | |
| 17 | package org.onosproject.bgpio.types; |
| 18 | |
| 19 | import java.net.InetAddress; |
| 20 | import java.util.LinkedList; |
| 21 | import java.util.List; |
| 22 | |
| 23 | import org.jboss.netty.buffer.ChannelBuffer; |
| 24 | import org.onlab.packet.Ip4Address; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 25 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 26 | import org.onosproject.bgpio.protocol.BgpLSNlri; |
| 27 | import org.onosproject.bgpio.protocol.linkstate.BgpPrefixIPv4LSNlriVer4; |
| 28 | import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4; |
Priyanka B | e07ea4d | 2015-11-21 21:28:10 +0530 | [diff] [blame] | 29 | import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4; |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 30 | import org.onosproject.bgpio.util.Constants; |
| 31 | import org.onosproject.bgpio.util.Validation; |
| 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | import com.google.common.base.MoreObjects; |
| 36 | |
| 37 | /* |
| 38 | * Provides Implementation of MpReach Nlri BGP Path Attribute. |
| 39 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 40 | public class MpReachNlri implements BgpValueType { |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 41 | |
| 42 | private static final Logger log = LoggerFactory.getLogger(MpReachNlri.class); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 43 | public static final byte MPREACHNLRI_TYPE = 14; |
| 44 | public static final byte LINK_NLRITYPE = 2; |
| 45 | |
| 46 | private boolean isMpReachNlri = false; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 47 | private final List<BgpLSNlri> mpReachNlri; |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 48 | private final int length; |
| 49 | private final short afi; |
| 50 | private final byte safi; |
| 51 | private final Ip4Address ipNextHop; |
| 52 | |
| 53 | /** |
| 54 | * Constructor to initialize parameters. |
| 55 | * |
| 56 | * @param mpReachNlri MpReach Nlri attribute |
| 57 | * @param afi address family identifier |
| 58 | * @param safi subsequent address family identifier |
| 59 | * @param ipNextHop nexthop IpAddress |
| 60 | * @param length of MpReachNlri |
| 61 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 62 | 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] | 63 | this.mpReachNlri = mpReachNlri; |
| 64 | this.isMpReachNlri = true; |
| 65 | this.ipNextHop = ipNextHop; |
| 66 | this.afi = afi; |
| 67 | this.safi = safi; |
| 68 | this.length = length; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns whether MpReachNlri is present. |
| 73 | * |
| 74 | * @return whether MpReachNlri is present |
| 75 | */ |
| 76 | public boolean isMpReachNlriSet() { |
| 77 | return this.isMpReachNlri; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns list of MpReach Nlri. |
| 82 | * |
| 83 | * @return list of MpReach Nlri |
| 84 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 85 | public List<BgpLSNlri> mpReachNlri() { |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 86 | return this.mpReachNlri; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns length of MpReachNlri. |
| 91 | * |
| 92 | * @return length of MpReachNlri |
| 93 | */ |
| 94 | public int mpReachNlriLen() { |
| 95 | return this.length; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Reads from ChannelBuffer and parses MpReachNlri. |
| 100 | * |
| 101 | * @param cb channelBuffer |
| 102 | * @return object of MpReachNlri |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 103 | * @throws BgpParseException while parsing MpReachNlri |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 104 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 105 | public static MpReachNlri read(ChannelBuffer cb) throws BgpParseException { |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 106 | ChannelBuffer tempBuf = cb.copy(); |
| 107 | Validation parseFlags = Validation.parseAttributeHeader(cb); |
| 108 | int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : |
| 109 | parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_BYTE; |
| 110 | ChannelBuffer data = tempBuf.readBytes(len); |
| 111 | |
| 112 | if (cb.readableBytes() < parseFlags.getLength()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 113 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 114 | parseFlags.getLength()); |
| 115 | } |
| 116 | if (!parseFlags.getFirstBit() && parseFlags.getSecondBit() && parseFlags.getThirdBit()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 117 | throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 118 | } |
| 119 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 120 | BgpLSNlri bgpLSNlri = null; |
| 121 | List<BgpLSNlri> mpReachNlri = new LinkedList<>(); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 122 | ChannelBuffer tempCb = cb.readBytes(parseFlags.getLength()); |
| 123 | short afi = 0; |
| 124 | byte safi = 0; |
| 125 | Ip4Address ipNextHop = null; |
| 126 | while (tempCb.readableBytes() > 0) { |
| 127 | afi = tempCb.readShort(); |
| 128 | safi = tempCb.readByte(); |
| 129 | |
| 130 | //Supporting for AFI 16388 / SAFI 71 and VPN AFI 16388 / SAFI 128 |
| 131 | if ((afi == Constants.AFI_VALUE) && (safi == Constants.SAFI_VALUE) || (afi == Constants.AFI_VALUE) |
| 132 | && (safi == Constants.VPN_SAFI_VALUE)) { |
| 133 | byte nextHopLen = tempCb.readByte(); |
Priyanka B | e07ea4d | 2015-11-21 21:28:10 +0530 | [diff] [blame] | 134 | InetAddress ipAddress = Validation.toInetAddress(nextHopLen, cb); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 135 | if (ipAddress.isMulticastAddress()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 136 | throw new BgpParseException("Multicast not supported"); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 137 | } |
| 138 | ipNextHop = Ip4Address.valueOf(ipAddress); |
| 139 | byte reserved = tempCb.readByte(); |
| 140 | |
| 141 | while (tempCb.readableBytes() > 0) { |
| 142 | short nlriType = tempCb.readShort(); |
| 143 | short totNlriLen = tempCb.readShort(); |
| 144 | if (tempCb.readableBytes() < totNlriLen) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 145 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 146 | BgpErrorType.ATTRIBUTE_LENGTH_ERROR, totNlriLen); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 147 | } |
| 148 | tempBuf = tempCb.readBytes(totNlriLen); |
| 149 | switch (nlriType) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 150 | case BgpNodeLSNlriVer4.NODE_NLRITYPE: |
| 151 | bgpLSNlri = BgpNodeLSNlriVer4.read(tempBuf, afi, safi); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 152 | break; |
Priyanka B | e07ea4d | 2015-11-21 21:28:10 +0530 | [diff] [blame] | 153 | case BgpLinkLsNlriVer4.LINK_NLRITYPE: |
| 154 | bgpLSNlri = BgpLinkLsNlriVer4.read(tempBuf, afi, safi); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 155 | break; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 156 | case BgpPrefixIPv4LSNlriVer4.PREFIX_IPV4_NLRITYPE: |
| 157 | bgpLSNlri = BgpPrefixIPv4LSNlriVer4.read(tempBuf, afi, safi); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 158 | break; |
| 159 | default: |
| 160 | log.debug("nlriType not supported" + nlriType); |
| 161 | } |
| 162 | mpReachNlri.add(bgpLSNlri); |
| 163 | } |
| 164 | } else { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 165 | throw new BgpParseException("Not Supporting afi " + afi + "safi " + safi); |
Priyanka B | 3b69554 | 2015-10-14 17:16:27 +0530 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | return new MpReachNlri(mpReachNlri, afi, safi, ipNextHop, parseFlags.getLength()); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public short getType() { |
| 173 | return MPREACHNLRI_TYPE; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Returns AFI. |
| 178 | * |
| 179 | * @return AFI |
| 180 | */ |
| 181 | public short afi() { |
| 182 | return this.afi; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Returns Nexthop IpAddress. |
| 187 | * |
| 188 | * @return Nexthop IpAddress |
| 189 | */ |
| 190 | public Ip4Address nexthop4() { |
| 191 | return this.ipNextHop; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Returns SAFI. |
| 196 | * |
| 197 | * @return SAFI |
| 198 | */ |
| 199 | public byte safi() { |
| 200 | return this.safi; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public int write(ChannelBuffer cb) { |
| 205 | //Not to be Implemented as of now |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public String toString() { |
| 211 | return MoreObjects.toStringHelper(getClass()) |
| 212 | .add("mpReachNlri", mpReachNlri) |
| 213 | .add("afi", afi) |
| 214 | .add("safi", safi) |
| 215 | .add("ipNextHop", ipNextHop) |
| 216 | .add("length", length) |
| 217 | .toString(); |
| 218 | } |
| 219 | } |