Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.protocol.evpn; |
| 18 | |
| 19 | import com.google.common.base.MoreObjects; |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 23 | import org.onosproject.bgpio.types.BgpEvpnEsi; |
| 24 | import org.onosproject.bgpio.types.BgpEvpnLabel; |
| 25 | import org.onosproject.bgpio.types.RouteDistinguisher; |
| 26 | import org.onosproject.bgpio.util.Constants; |
| 27 | import org.onosproject.bgpio.util.Validation; |
| 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
| 31 | import java.net.InetAddress; |
| 32 | |
| 33 | public class BgpEvpnRouteType2Nlri implements BgpEvpnNlriData { |
| 34 | |
| 35 | /* |
| 36 | * REFERENCE : RFC 7432 BGP MPLS-Based Ethernet VPN |
| 37 | +---------------------------------------+ |
| 38 | | RD (8 octets) | |
| 39 | +---------------------------------------+ |
| 40 | |Ethernet Segment Identifier (10 octets)| |
| 41 | +---------------------------------------+ |
| 42 | | Ethernet Tag ID (4 octets) | |
| 43 | +---------------------------------------+ |
| 44 | | MAC Address Length (1 octet) | |
| 45 | +---------------------------------------+ |
| 46 | | MAC Address (6 octets) | |
| 47 | +---------------------------------------+ |
| 48 | | IP Address Length (1 octet) | |
| 49 | +---------------------------------------+ |
| 50 | | IP Address (0, 4, or 16 octets) | |
| 51 | +---------------------------------------+ |
| 52 | | MPLS Label1 (3 octets) | |
| 53 | +---------------------------------------+ |
| 54 | | MPLS Label2 (0 or 3 octets) | |
| 55 | +---------------------------------------+ |
| 56 | |
| 57 | Figure : A MAC/IP Advertisement route type specific EVPN NLRI |
| 58 | |
| 59 | */ |
| 60 | |
| 61 | public static final short TYPE = Constants.BGP_EVPN_MAC_IP_ADVERTISEMENT; |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame^] | 62 | private static final Logger log = LoggerFactory.getLogger(BgpEvpnRouteType2Nlri.class); |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 63 | // unit of length is bit |
| 64 | public static final short IPV4_ADDRESS_LENGTH = 32; |
| 65 | public static final short MAC_ADDRESS_LENGTH = 48; |
| 66 | private RouteDistinguisher rd; |
| 67 | private BgpEvpnEsi esi; |
| 68 | private int ethernetTagID; |
| 69 | private byte macAddressLength; |
| 70 | private MacAddress macAddress; |
| 71 | private byte ipAddressLength; |
| 72 | private InetAddress ipAddress; |
| 73 | private BgpEvpnLabel mplsLabel1; |
| 74 | private BgpEvpnLabel mplsLabel2; |
| 75 | |
| 76 | /** |
| 77 | * Resets parameters. |
| 78 | */ |
| 79 | public BgpEvpnRouteType2Nlri() { |
| 80 | this.rd = null; |
| 81 | this.esi = null; |
| 82 | this.ethernetTagID = 0; |
| 83 | this.macAddressLength = 0; |
| 84 | this.macAddress = null; |
| 85 | this.ipAddressLength = 0; |
| 86 | this.ipAddress = null; |
| 87 | this.mplsLabel1 = null; |
| 88 | this.mplsLabel2 = null; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Creates the Evpn route type 2 route. |
| 93 | * |
| 94 | * @param rd route distinguisher |
| 95 | * @param esi esi |
| 96 | * @param ethernetTagID ethernet tag id |
| 97 | * @param macAddress mac |
| 98 | * @param ipAddress ip |
| 99 | * @param mplsLabel1 label |
| 100 | * @param mplsLabel2 label |
| 101 | */ |
| 102 | public BgpEvpnRouteType2Nlri(RouteDistinguisher rd, |
| 103 | BgpEvpnEsi esi, |
| 104 | int ethernetTagID, MacAddress macAddress, |
| 105 | InetAddress ipAddress, BgpEvpnLabel mplsLabel1, |
| 106 | BgpEvpnLabel mplsLabel2) { |
| 107 | this.rd = rd; |
| 108 | this.esi = esi; |
| 109 | this.ethernetTagID = ethernetTagID; |
| 110 | this.macAddressLength = MAC_ADDRESS_LENGTH; |
| 111 | this.macAddress = macAddress; |
| 112 | if (ipAddress != null) { |
| 113 | this.ipAddressLength = IPV4_ADDRESS_LENGTH; |
| 114 | this.ipAddress = ipAddress; |
| 115 | } else { |
| 116 | this.ipAddressLength = 0; |
| 117 | this.ipAddress = null; |
| 118 | } |
| 119 | this.mplsLabel1 = mplsLabel1; |
| 120 | this.mplsLabel2 = mplsLabel2; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Reads the Evpn type 2 attributes. |
| 125 | * |
| 126 | * @param cb channel buffer |
| 127 | * @return type2 route |
| 128 | * @throws BgpParseException parse exception |
| 129 | */ |
| 130 | public static BgpEvpnRouteType2Nlri read(ChannelBuffer cb) throws BgpParseException { |
| 131 | if (cb.readableBytes() == 0) { |
| 132 | return null; |
| 133 | } |
| 134 | RouteDistinguisher rd = RouteDistinguisher.read(cb); |
| 135 | BgpEvpnEsi esi = BgpEvpnEsi.read(cb); |
| 136 | int ethernetTagID = cb.readInt(); |
| 137 | byte macAddressLength = cb.readByte(); |
| 138 | MacAddress macAddress = Validation.toMacAddress(macAddressLength / 8, cb); |
| 139 | byte ipAddressLength = cb.readByte(); |
| 140 | InetAddress ipAddress = null; |
| 141 | if (ipAddressLength > 0) { |
| 142 | ipAddress = Validation.toInetAddress(ipAddressLength / 8, cb); |
| 143 | } |
| 144 | BgpEvpnLabel mplsLabel1 = BgpEvpnLabel.read(cb); |
| 145 | BgpEvpnLabel mplsLabel2 = null; |
| 146 | if (cb.readableBytes() > 0) { |
| 147 | mplsLabel2 = BgpEvpnLabel.read(cb); |
| 148 | } |
| 149 | |
| 150 | return new BgpEvpnRouteType2Nlri(rd, esi, ethernetTagID, macAddress, |
| 151 | ipAddress, mplsLabel1, |
| 152 | mplsLabel2); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public int write(ChannelBuffer cb) { |
| 157 | int iLenStartIndex = cb.writerIndex(); |
| 158 | cb.writeLong(rd.getRouteDistinguisher()); |
| 159 | esi.write(cb); |
| 160 | cb.writeInt(ethernetTagID); |
| 161 | cb.writeByte(macAddressLength); |
| 162 | cb.writeBytes(macAddress.toBytes()); |
| 163 | cb.writeByte(ipAddressLength); |
| 164 | if (ipAddressLength > 0) { |
| 165 | cb.writeBytes(ipAddress.getAddress()); |
| 166 | } |
| 167 | mplsLabel1.write(cb); |
| 168 | if (mplsLabel2 != null) { |
| 169 | mplsLabel2.write(cb); |
| 170 | } |
| 171 | return cb.writerIndex() - iLenStartIndex; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Returns the rd. |
| 176 | * |
| 177 | * @return rd route distinguisher |
| 178 | */ |
| 179 | public RouteDistinguisher getRouteDistinguisher() { |
| 180 | return rd; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Returns the esi. |
| 185 | * |
| 186 | * @return esi ethernet segment identifier |
| 187 | */ |
| 188 | public BgpEvpnEsi getEthernetSegmentidentifier() { |
| 189 | return esi; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Returns the ethernet tag id. |
| 194 | * |
| 195 | * @return macAddress macadress |
| 196 | */ |
| 197 | public int getEthernetTagID() { |
| 198 | return ethernetTagID; |
| 199 | } |
| 200 | |
| 201 | public MacAddress getMacAddress() { |
| 202 | return macAddress; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Returns the ip address. |
| 207 | * |
| 208 | * @return ipAddress ipaddress |
| 209 | */ |
| 210 | public InetAddress getIpAddress() { |
| 211 | return ipAddress; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Returns the mpls label. |
| 216 | * |
| 217 | * @return mplsLabel1 mpls label |
| 218 | */ |
| 219 | public BgpEvpnLabel getMplsLable1() { |
| 220 | return mplsLabel1; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Returns the mpls label. |
| 225 | * |
| 226 | * @return mplsLabel2 mpls label |
| 227 | */ |
| 228 | public BgpEvpnLabel getMplsLable2() { |
| 229 | return mplsLabel2; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Set the rd. |
| 234 | * |
| 235 | * @param rd route distinguisher |
| 236 | */ |
| 237 | public void setRouteDistinguisher(RouteDistinguisher rd) { |
| 238 | this.rd = rd; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Set the ESI. |
| 243 | * |
| 244 | * @param esi esi |
| 245 | */ |
| 246 | public void setEthernetSegmentidentifier(BgpEvpnEsi esi) { |
| 247 | this.esi = esi; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Set the ethernet tag id. |
| 252 | * |
| 253 | * @param ethernetTagID ethernet tag id. |
| 254 | */ |
| 255 | public void setEthernetTagID(int ethernetTagID) { |
| 256 | this.ethernetTagID = ethernetTagID; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Set the mac address. |
| 261 | * |
| 262 | * @param macAddress mac |
| 263 | */ |
| 264 | public void setMacAddress(MacAddress macAddress) { |
| 265 | this.macAddress = macAddress; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Set the ip address. |
| 270 | * |
| 271 | * @param ipAddress ip |
| 272 | */ |
| 273 | public void setIpAddress(InetAddress ipAddress) { |
| 274 | this.ipAddress = ipAddress; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Set the mpls label. |
| 279 | * |
| 280 | * @param mplsLabel1 label |
| 281 | */ |
| 282 | public void setMplsLable1(BgpEvpnLabel mplsLabel1) { |
| 283 | this.mplsLabel1 = mplsLabel1; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Set the mpls label. |
| 288 | * |
| 289 | * @param mplsLabel2 label |
| 290 | */ |
| 291 | public void setMplsLable2(BgpEvpnLabel mplsLabel2) { |
| 292 | this.mplsLabel2 = mplsLabel2; |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public BgpEvpnRouteType getType() { |
| 297 | return BgpEvpnRouteType.MAC_IP_ADVERTISEMENT; |
| 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public String toString() { |
| 302 | return MoreObjects.toStringHelper(getClass()).omitNullValues() |
| 303 | .add("rd ", rd) |
| 304 | .add("esi", esi) |
| 305 | .add("ethernetTagID", ethernetTagID) |
| 306 | .add("macAddressLength", macAddressLength) |
| 307 | .add("macAddress ", macAddress) |
| 308 | .add("ipAddressLength", ipAddressLength) |
| 309 | .add("ipAddress", ipAddress) |
| 310 | .add("mplsLabel1 ", mplsLabel1) |
| 311 | .add("mplsLabel2", mplsLabel2).toString(); |
| 312 | } |
| 313 | |
| 314 | } |