blob: 90cf0a055cdbe467053ffb0a2a5f4167511d003d [file] [log] [blame]
senthil33ed8212024-03-22 16:49:21 +05301/*
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
17package org.onosproject.bgpmonitoring.type;
18
19import com.google.common.base.MoreObjects;
20import org.jboss.netty.buffer.ChannelBuffers;
21
22import org.onosproject.bgpio.exceptions.BgpParseException;
23import org.onosproject.bgpio.protocol.BgpMessage;
24import org.onosproject.bgpio.protocol.ver4.BgpMessageVer4;
25import org.onosproject.bgpio.types.BgpHeader;
26import org.onosproject.bgpmonitoring.RouteMonitoringMessage;
27import org.onosproject.bgpmonitoring.BmpParseException;
28import org.onosproject.bgpmonitoring.PerPeer;
29import org.onlab.packet.Deserializer;
30import java.util.Objects;
31import java.util.function.BiPredicate;
32import java.nio.ByteBuffer;
33
34import static com.google.common.base.Preconditions.checkState;
35
36/**
37 * Used to provide an initial dump of all routes received from a peer.
38 * As well as an ongoing mechanism that sends the incremental routes
39 * advertised and withdrawn by a peer to the monitoring station.
40 * <p>
41 * Route Monitoring messages are used for initial synchronization of the
42 * ADJ-RIBs-In. They are also used for ongoing monitoring of the
43 * ADJ-RIB-In state. Route monitoring messages are state-compressed.
44 * <p>
45 * Route Mirroring messages are used for verbatim duplication of
46 * messages as received. A possible use for mirroring is exact
47 * mirroring of one or more monitored BGP sessions, without state
48 * compression. Another possible use is the mirroring of messages that
49 * have been treated-as-withdraw [RFC7606], for debugging purposes.
50 * Mirrored messages may be sampled, or may be lossless. The Messages
51 * Lost Information code is provided to allow losses to be indicated.
52 * Following the common BMP header and per-peer header is a set of TLVs
53 * that contain information about a message or set of messages. Each
54 * TLV comprises a 2-byte type code, a 2-byte length field, and a
55 * variable-length value. Inclusion of any given TLV is OPTIONAL;
56 * however, at least one TLV SHOULD be included, otherwise there's no
57 * point in sending the message.
58 */
59public final class BmpRouteMonitoringMessage extends RouteMonitoringMessage {
60
61
62 private PerPeer perPeer;
63
64 private BgpMessage bgpMessage;
65
66 private BmpRouteMonitoringMessage(Builder builder) {
67 this.perPeer = builder.perPeer;
68 this.bgpMessage = builder.bgpMessage;
69 }
70
71 /**
72 * Returns Bgp message.
73 *
74 * @return Bgp message
75 */
76 @Override
77 public BgpMessage getBgpMessage() {
78 return bgpMessage;
79 }
80
81 /**
82 * Returns BMP Header of BMP Message.
83 *
84 * @return BMP Header of BMP Message
85 */
86 @Override
87 public PerPeer getPerPeer() {
88 return perPeer;
89 }
90
91
92 @Override
93 public boolean equals(Object o) {
94 if (this == o) {
95 return true;
96 }
97 if (o == null || getClass() != o.getClass()) {
98 return false;
99 }
100 BmpRouteMonitoringMessage that = (BmpRouteMonitoringMessage) o;
101 return Objects.equals(perPeer, that.perPeer) &&
102 Objects.equals(bgpMessage, that.bgpMessage);
103 }
104
105 @Override
106 public int hashCode() {
107 return Objects.hash(perPeer, bgpMessage);
108 }
109
110 /**
111 * Data deserializer function for BMP route monitoring.
112 *
113 * @return data deserializer function
114 */
115 public static Deserializer<BmpRouteMonitoringMessage> deserializer() {
116 return (data, offset, length) -> {
117 BiPredicate<ByteBuffer, Integer> isValidBuffer = (b, l)
118 -> b.hasRemaining() && b.remaining() >= l;
119
120 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
121 if (!isValidBuffer.test(bb, ROUTE_MONITORING_HEADER_MIN_LENGTH +
122 PerPeerPacket.PEER_HEADER_MIN_LENGTH)) {
123 throw new BmpParseException("Invalid bmp route monitoring message buffer size.");
124 }
125
126 byte[] perPeerBytes = new byte[PerPeerPacket.PEER_HEADER_MIN_LENGTH];
127 bb.get(perPeerBytes);
128
129 byte[] routeBytes = new byte[bb.remaining()];
130 bb.get(routeBytes);
131 Builder builder = new Builder()
132 .perPeer(PerPeerPacket.deserializer().deserialize(perPeerBytes,
133 0, PerPeerPacket.PEER_HEADER_MIN_LENGTH));
134 try {
135 builder.bgpMessage(BgpMessageVer4.READER.readFrom(ChannelBuffers.wrappedBuffer(routeBytes),
136 new BgpHeader()));
137 } catch (BgpParseException ex) {
138 throw new BmpParseException(ex);
139 }
140 return builder.build();
141
142 };
143 }
144
145 @Override
146 public String toString() {
147 return MoreObjects.toStringHelper(getClass())
148 .add("perPeer", perPeer)
149 .add("bgpMessage", bgpMessage)
150 .toString();
151 }
152
153 /**
154 * Builder for BMP route monitoring message.
155 */
156 private static class Builder {
157
158 private PerPeer perPeer;
159
160 private BgpMessage bgpMessage;
161
162 /**
163 * Setter bmp per peer header.
164 *
165 * @param perPeer bmp per peer header.
166 * @return this class builder.
167 */
168 public Builder perPeer(PerPeer perPeer) {
169 this.perPeer = perPeer;
170 return this;
171 }
172
173 /**
174 * Setter bpg message.
175 *
176 * @param bgpMessage bpg message.
177 * @return this class builder.
178 */
179 public Builder bgpMessage(BgpMessage bgpMessage) {
180 this.bgpMessage = bgpMessage;
181 return this;
182 }
183
184 /**
185 * Checks arguments for bgp route monitoring message.
186 */
187 private void checkArguments() {
188 checkState(perPeer != null, "Invalid bmp statistics per peer buffer.");
189 checkState(bgpMessage != null, "Invalid bgp message.");
190 }
191
192 /**
193 * Builds BMP route monitoring message.
194 *
195 * @return BMP route monitoring message.
196 */
197 public BmpRouteMonitoringMessage build() {
198 checkArguments();
199 return new BmpRouteMonitoringMessage(this);
200 }
201
202 }
203
204}