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