senthil | dc9af17 | 2024-03-22 16:52:33 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2024-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.bgpmonitoring.type; |
| 18 | |
| 19 | |
| 20 | import org.jboss.netty.buffer.ChannelBuffers; |
| 21 | import com.google.common.base.MoreObjects; |
| 22 | import org.onlab.packet.Deserializer; |
| 23 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 24 | import org.onosproject.bgpio.protocol.BgpMessage; |
| 25 | import org.onosproject.bgpio.protocol.ver4.BgpMessageVer4; |
| 26 | import org.onosproject.bgpio.types.BgpHeader; |
| 27 | import org.onosproject.bgpmonitoring.RouteMirroringMessage; |
| 28 | import org.onosproject.bgpmonitoring.PerPeer; |
| 29 | import org.onosproject.bgpmonitoring.MirroringType; |
| 30 | import org.onosproject.bgpmonitoring.BmpParseException; |
| 31 | |
| 32 | import java.util.Objects; |
| 33 | import java.util.function.BiPredicate; |
| 34 | import java.nio.ByteBuffer; |
| 35 | |
| 36 | import static com.google.common.base.Preconditions.checkState; |
| 37 | |
| 38 | /** |
| 39 | * Route Mirroring messages are used for verbatim duplication of |
| 40 | * messages as received. A possible use for mirroring is exact |
| 41 | * mirroring of one or more monitored BGP sessions, without state |
| 42 | * compression. Another possible use is the mirroring of messages that |
| 43 | * have been treated-as-withdraw [RFC7606], for debugging purposes. |
| 44 | * Mirrored messages may be sampled, or may be lossless. The Messages |
| 45 | * Lost Information code is provided to allow losses to be indicated. |
| 46 | * <p> |
| 47 | * Following the common BMP header and per-peer header is a set of TLVs |
| 48 | * that contain information about a message or set of messages. Each |
| 49 | * TLV comprises a 2-byte type code, a 2-byte length field, and a |
| 50 | * variable-length value. Inclusion of any given TLV is OPTIONAL; |
| 51 | * however, at least one TLV SHOULD be included, otherwise there's no |
| 52 | * point in sending the message. Defined TLVs are as follows: |
| 53 | * <p> |
| 54 | * Type = 0: BGP Message. A BGP PDU. This PDU may or may not be an |
| 55 | * Update message. If the BGP Message TLV occurs in the Route |
| 56 | * Mirroring message, it MUST occur last in the list of TLVs. |
| 57 | * <p> |
| 58 | * Type = 1: Information. A 2-byte code that provides information |
| 59 | * about the mirrored message or message stream. Defined codes are: |
| 60 | */ |
| 61 | public final class BmpRouteMirroring extends RouteMirroringMessage { |
| 62 | |
| 63 | /* |
| 64 | 0 1 2 3 |
| 65 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 66 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 67 | | Mirroring Type | Mirroring Length | |
| 68 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 69 | | Route Mirroring (variable) | |
| 70 | ~ ~ |
| 71 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 72 | */ |
| 73 | |
| 74 | |
| 75 | private PerPeer perPeer; |
| 76 | |
| 77 | private MirroringType mirroringType; |
| 78 | |
| 79 | private int mirroringLength; |
| 80 | |
| 81 | private BgpMessage bgpMessage; |
| 82 | |
| 83 | private BmpRouteMirroring(Builder builder) { |
| 84 | this.perPeer = builder.perPeer; |
| 85 | this.mirroringType = builder.mirroringType; |
| 86 | this.mirroringLength = builder.mirroringLength; |
| 87 | this.bgpMessage = builder.bgpMessage; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns Bgp route mirroring type. |
| 92 | * |
| 93 | * @return Bgp route mirroring type |
| 94 | */ |
| 95 | @Override |
| 96 | public MirroringType getMirroringType() { |
| 97 | return mirroringType; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns Bgp route mirroring length. |
| 102 | * |
| 103 | * @return Bgp route mirroring length |
| 104 | */ |
| 105 | @Override |
| 106 | public int getMirroringLength() { |
| 107 | return mirroringLength; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns Bgp message. |
| 112 | * |
| 113 | * @return Bgp message |
| 114 | */ |
| 115 | @Override |
| 116 | public BgpMessage getBgpMessage() { |
| 117 | return bgpMessage; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns BMP Header of BMP Message. |
| 122 | * |
| 123 | * @return BMP Header of BMP Message |
| 124 | */ |
| 125 | @Override |
| 126 | public PerPeer getPerPeer() { |
| 127 | return perPeer; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | @Override |
| 132 | public boolean equals(Object o) { |
| 133 | if (this == o) { |
| 134 | return true; |
| 135 | } |
| 136 | if (o == null || getClass() != o.getClass()) { |
| 137 | return false; |
| 138 | } |
| 139 | BmpRouteMirroring that = (BmpRouteMirroring) o; |
| 140 | return mirroringType == that.mirroringType && |
| 141 | mirroringLength == that.mirroringLength && |
| 142 | Objects.equals(perPeer, that.perPeer) && |
| 143 | Objects.equals(bgpMessage, that.bgpMessage); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public int hashCode() { |
| 148 | return Objects.hash(perPeer, mirroringType, mirroringLength, bgpMessage); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /** |
| 153 | * Data deserializer function for bmp route mirroring message. |
| 154 | * |
| 155 | * @return data deserializer function |
| 156 | */ |
| 157 | public static Deserializer<BmpRouteMirroring> deserializer() { |
| 158 | return (data, offset, length) -> { |
| 159 | BiPredicate<ByteBuffer, Integer> isValidBuffer = (b, l) |
| 160 | -> b.hasRemaining() && b.remaining() >= l; |
| 161 | |
| 162 | ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 163 | if (!isValidBuffer.test(bb, ROUTE_MIRRORING_HEADER_MIN_LENGTH + |
| 164 | PerPeerPacket.PEER_HEADER_MIN_LENGTH)) { |
| 165 | throw new BmpParseException("Invalid bmp route mirroring message buffer size."); |
| 166 | } |
| 167 | byte[] perPeerBytes = new byte[PerPeerPacket.PEER_HEADER_MIN_LENGTH]; |
| 168 | bb.get(perPeerBytes); |
| 169 | |
| 170 | Builder builder = new Builder() |
| 171 | .perPeer(PerPeerPacket.deserializer().deserialize(perPeerBytes, |
| 172 | 0, PerPeerPacket.PEER_HEADER_MIN_LENGTH)) |
| 173 | .mirroringType(MirroringType.getType((int) bb.getShort())) |
| 174 | .mirroringLength((int) bb.getShort()); |
| 175 | if (builder.mirroringType != MirroringType.BGP_MESSAGE) { |
| 176 | throw new BmpParseException("Not supported mirroring type"); |
| 177 | } |
| 178 | |
| 179 | byte[] routeBytes = new byte[bb.remaining()]; |
| 180 | bb.get(routeBytes); |
| 181 | try { |
| 182 | builder.bgpMessage(BgpMessageVer4.READER.readFrom(ChannelBuffers.wrappedBuffer(routeBytes), |
| 183 | new BgpHeader())); |
| 184 | } catch (BgpParseException ex) { |
| 185 | throw new BmpParseException(ex); |
| 186 | } |
| 187 | return builder.build(); |
| 188 | }; |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public String toString() { |
| 193 | return MoreObjects.toStringHelper(getClass()) |
| 194 | .add("perPeer", perPeer) |
| 195 | .add("mirroringType", mirroringType) |
| 196 | .add("mirroringLength", mirroringLength) |
| 197 | .add("bgpMessage", bgpMessage) |
| 198 | .toString(); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Builder for BMP route mirroring message. |
| 203 | */ |
| 204 | private static class Builder { |
| 205 | |
| 206 | private PerPeer perPeer; |
| 207 | |
| 208 | private MirroringType mirroringType; |
| 209 | |
| 210 | private int mirroringLength; |
| 211 | |
| 212 | private BgpMessage bgpMessage; |
| 213 | |
| 214 | /** |
| 215 | * Setter bmp per peer header. |
| 216 | * |
| 217 | * @param perPeer bmp per peer header. |
| 218 | * @return this class builder. |
| 219 | */ |
| 220 | public Builder perPeer(PerPeer perPeer) { |
| 221 | this.perPeer = perPeer; |
| 222 | return this; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Setter bmp mirroring type. |
| 227 | * |
| 228 | * @param mirroringType bmp mirroring type. |
| 229 | * @return this class builder. |
| 230 | */ |
| 231 | public Builder mirroringType(MirroringType mirroringType) { |
| 232 | this.mirroringType = mirroringType; |
| 233 | return this; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Setter bmp mirroring length. |
| 238 | * |
| 239 | * @param mirroringLength bmp mirroring length. |
| 240 | * @return this class builder. |
| 241 | */ |
| 242 | public Builder mirroringLength(int mirroringLength) { |
| 243 | this.mirroringLength = mirroringLength; |
| 244 | return this; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Setter bpg message. |
| 249 | * |
| 250 | * @param bgpMessage bpg message. |
| 251 | * @return this class builder. |
| 252 | */ |
| 253 | public Builder bgpMessage(BgpMessage bgpMessage) { |
| 254 | this.bgpMessage = bgpMessage; |
| 255 | return this; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Checks arguments for bgp route mirroring message. |
| 260 | */ |
| 261 | private void checkArguments() { |
| 262 | checkState(perPeer != null, "Invalid bmp statistics per peer buffer."); |
| 263 | checkState(bgpMessage != null, "Invalid bgp message."); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Builds BMP route mirroring message. |
| 268 | * |
| 269 | * @return BMP route mirroring message. |
| 270 | */ |
| 271 | public BmpRouteMirroring build() { |
| 272 | checkArguments(); |
| 273 | return new BmpRouteMirroring(this); |
| 274 | } |
| 275 | } |
| 276 | } |