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 | package org.onosproject.bgpio.protocol.evpn; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.jboss.netty.buffer.ChannelBuffer; |
| 20 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 21 | import org.onosproject.bgpio.protocol.BgpEvpnNlri; |
| 22 | import org.onosproject.bgpio.types.BgpErrorType; |
| 23 | import org.onosproject.bgpio.util.Constants; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | /** |
| 28 | * Implementation of Evpn NLRI. |
| 29 | */ |
| 30 | public class BgpEvpnNlriImpl implements BgpEvpnNlri { |
| 31 | |
| 32 | /* |
| 33 | * REFERENCE : RFC 7432 BGP MPLS-Based Ethernet VPN |
| 34 | +-----------------------------------+ |
| 35 | | Route Type (1 octet) | |
| 36 | +-----------------------------------+ |
| 37 | | Length (1 octet) | |
| 38 | +-----------------------------------+ |
| 39 | | Route Type specific (variable) | |
| 40 | +-----------------------------------+ |
| 41 | |
| 42 | Figure : The format of the EVPN NLRI |
| 43 | */ |
| 44 | |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame^] | 45 | private static final Logger log = LoggerFactory |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 46 | .getLogger(BgpEvpnNlriImpl.class); |
| 47 | |
| 48 | // total length of route type and length |
| 49 | public static final short TYPE_AND_LEN = 2; |
| 50 | private byte routeType; |
| 51 | private BgpEvpnNlriData routeTypeSpec; |
| 52 | |
| 53 | /** |
| 54 | * Resets parameters. |
| 55 | */ |
| 56 | public BgpEvpnNlriImpl() { |
| 57 | this.routeType = Constants.BGP_EVPN_MAC_IP_ADVERTISEMENT; |
| 58 | this.routeTypeSpec = null; |
| 59 | |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Constructor to initialize parameters for BGP EvpnNlri. |
| 64 | * |
| 65 | * @param routeType route Type |
| 66 | * @param routeTypeSpefic route type specific |
| 67 | */ |
| 68 | public BgpEvpnNlriImpl(byte routeType, BgpEvpnNlriData routeTypeSpefic) { |
| 69 | this.routeType = routeType; |
| 70 | this.routeTypeSpec = routeTypeSpefic; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Reads from channelBuffer and parses Evpn Nlri. |
| 75 | * |
| 76 | * @param cb ChannelBuffer |
| 77 | * @return object of BgpEvpnNlriVer4 |
| 78 | * @throws BgpParseException while parsing Bgp Evpn Nlri |
| 79 | */ |
| 80 | public static BgpEvpnNlriImpl read(ChannelBuffer cb) |
| 81 | throws BgpParseException { |
| 82 | |
| 83 | BgpEvpnNlriData routeNlri = null; |
| 84 | |
| 85 | if (cb.readableBytes() > 0) { |
| 86 | ChannelBuffer tempBuf = cb.copy(); |
| 87 | byte type = cb.readByte(); |
| 88 | byte length = cb.readByte(); |
| 89 | if (cb.readableBytes() < length) { |
| 90 | throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 91 | BgpErrorType.OPTIONAL_ATTRIBUTE_ERROR, |
| 92 | tempBuf.readBytes(cb.readableBytes() |
| 93 | + TYPE_AND_LEN)); |
| 94 | } |
| 95 | ChannelBuffer tempCb = cb.readBytes(length); |
| 96 | switch (type) { |
| 97 | case BgpEvpnRouteType2Nlri.TYPE: |
| 98 | routeNlri = BgpEvpnRouteType2Nlri.read(tempCb); |
| 99 | break; |
| 100 | default: |
| 101 | log.info("Discarding, EVPN route type {}", type); |
| 102 | throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 103 | BgpErrorType.MISSING_WELLKNOWN_ATTRIBUTE, null); |
| 104 | // break; |
| 105 | } |
| 106 | return new BgpEvpnNlriImpl(type, routeNlri); |
| 107 | } else { |
| 108 | return new BgpEvpnNlriImpl(); |
| 109 | } |
| 110 | |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public BgpEvpnNlriData getNlri() { |
| 115 | return routeTypeSpec; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public BgpEvpnRouteType getRouteType() { |
| 120 | switch (routeType) { |
| 121 | case Constants.BGP_EVPN_ETHERNET_AUTO_DISCOVERY: |
| 122 | return BgpEvpnRouteType.ETHERNET_AUTO_DISCOVERY; |
| 123 | case Constants.BGP_EVPN_MAC_IP_ADVERTISEMENT: |
| 124 | return BgpEvpnRouteType.MAC_IP_ADVERTISEMENT; |
| 125 | case Constants.BGP_EVPN_INCLUSIVE_MULTICASE_ETHERNET: |
| 126 | return BgpEvpnRouteType.INCLUSIVE_MULTICASE_ETHERNET; |
| 127 | case Constants.BGP_EVPN_ETHERNET_SEGMENT: |
| 128 | return BgpEvpnRouteType.ETHERNET_SEGMENT; |
| 129 | default: |
| 130 | return null; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public String toString() { |
| 136 | return MoreObjects.toStringHelper(getClass()).omitNullValues() |
| 137 | .add("routeType", routeType) |
| 138 | .add("routeTypeSpefic ", routeTypeSpec).toString(); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public short getType() { |
| 143 | return routeType; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | @Override |
| 148 | public int write(ChannelBuffer cb) { |
| 149 | int iLenStartIndex = cb.writerIndex(); |
| 150 | cb.writeByte(routeType); |
| 151 | int iSpecStartIndex = cb.writerIndex(); |
| 152 | cb.writeByte(0); |
| 153 | switch (routeType) { |
| 154 | case BgpEvpnRouteType2Nlri.TYPE: |
| 155 | ((BgpEvpnRouteType2Nlri) routeTypeSpec).write(cb); |
| 156 | break; |
| 157 | default: |
| 158 | break; |
| 159 | } |
| 160 | cb.setByte(iSpecStartIndex, |
| 161 | (short) (cb.writerIndex() - iSpecStartIndex + 1)); |
| 162 | return cb.writerIndex() - iLenStartIndex; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public int compareTo(Object o) { |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | } |
| 172 | |