2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.protocol.bgp.rib.impl.stats.peer;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Stopwatch;
14 import io.netty.channel.Channel;
15 import java.net.InetSocketAddress;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.concurrent.TimeUnit;
20 import java.util.stream.Collectors;
21 import javax.annotation.Nonnull;
22 import org.opendaylight.controller.config.api.IdentityAttributeRef;
23 import org.opendaylight.controller.config.yang.bgp.rib.impl.AdvertisedAddPathTableTypes;
24 import org.opendaylight.controller.config.yang.bgp.rib.impl.AdvertizedTableTypes;
25 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpSessionState;
26 import org.opendaylight.controller.config.yang.bgp.rib.impl.ErrorMsgs;
27 import org.opendaylight.controller.config.yang.bgp.rib.impl.ErrorReceived;
28 import org.opendaylight.controller.config.yang.bgp.rib.impl.ErrorReceivedTotal;
29 import org.opendaylight.controller.config.yang.bgp.rib.impl.ErrorSent;
30 import org.opendaylight.controller.config.yang.bgp.rib.impl.ErrorSentTotal;
31 import org.opendaylight.controller.config.yang.bgp.rib.impl.KeepAliveMsgs;
32 import org.opendaylight.controller.config.yang.bgp.rib.impl.LocalPeerPreferences;
33 import org.opendaylight.controller.config.yang.bgp.rib.impl.MessagesStats;
34 import org.opendaylight.controller.config.yang.bgp.rib.impl.Received;
35 import org.opendaylight.controller.config.yang.bgp.rib.impl.RemotePeerPreferences;
36 import org.opendaylight.controller.config.yang.bgp.rib.impl.RouteRefreshMsgs;
37 import org.opendaylight.controller.config.yang.bgp.rib.impl.Sent;
38 import org.opendaylight.controller.config.yang.bgp.rib.impl.TotalMsgs;
39 import org.opendaylight.controller.config.yang.bgp.rib.impl.UpdateMsgs;
40 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl;
41 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
42 import org.opendaylight.protocol.util.StatisticsUtil;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Timestamp;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.ZeroBasedCounter32;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParameters;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.RouteRefresh;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.MultiprotocolCapability;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.add.path.capability.AddressFamilies;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
61 import org.opendaylight.yangtools.yang.binding.Notification;
62 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
63 import org.opendaylight.yangtools.yang.common.QName;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
67 public final class BGPSessionStatsImpl implements BGPSessionStats {
68 private static final Logger LOG = LoggerFactory.getLogger(BGPSessionStatsImpl.class);
70 private final Stopwatch sessionStopwatch;
71 private final BgpSessionState stats;
72 private final BGPSessionImpl session;
73 private final TotalMsgs totalMsgs = new TotalMsgs();
74 private final KeepAliveMsgs kaMsgs = new KeepAliveMsgs();
75 private final UpdateMsgs updMsgs = new UpdateMsgs();
76 private final RouteRefreshMsgs rrMsgs = new RouteRefreshMsgs();
77 private final ErrorMsgs errMsgs = new ErrorMsgs();
78 private final ErrorSentTotal errMsgsSentTotal = new ErrorSentTotal();
79 private final ErrorReceivedTotal errMsgsRecvTotal = new ErrorReceivedTotal();
80 private static final ZeroBasedCounter32 INITIAL_COUNTER = new ZeroBasedCounter32(0L);
82 public BGPSessionStatsImpl(@Nonnull final BGPSessionImpl session, @Nonnull final Open remoteOpen, final int holdTimerValue, final int keepAlive, @Nonnull final Channel channel,
83 @Nonnull final Optional<BGPSessionPreferences> localPreferences, @Nonnull final Collection<BgpTableType> tableTypes, @Nonnull final List<AddressFamilies> addPathTypes) {
84 this.session = session;
85 this.sessionStopwatch = Stopwatch.createUnstarted();
86 this.stats = new BgpSessionState();
87 this.stats.setHoldtimeCurrent(holdTimerValue);
88 this.stats.setKeepaliveCurrent(keepAlive);
89 this.stats.setLocalPeerPreferences(setLocalPeerPref(remoteOpen, channel, tableTypes, addPathTypes));
90 this.stats.setRemotePeerPreferences(setRemotePeerPref(channel, localPreferences));
91 this.errMsgs.setErrorReceivedTotal(this.errMsgsRecvTotal);
92 this.errMsgs.setErrorSentTotal(this.errMsgsSentTotal);
93 this.errMsgs.setErrorReceived(new ArrayList<>());
94 this.errMsgs.setErrorSent(new ArrayList<>());
98 private static Received newReceivedInstance() {
99 final Received recv = new Received();
100 recv.setCount(INITIAL_COUNTER);
104 private static Sent newSentInstance() {
105 final Sent sent = new Sent();
106 sent.setCount(INITIAL_COUNTER);
110 private static void updateReceivedMsg(final Received received) {
111 Preconditions.checkNotNull(received);
112 final long count = received.getCount() == null ? 0L : received.getCount().getValue();
113 received.setCount(new ZeroBasedCounter32(count + 1));
114 received.setTimestamp(new Timestamp(StatisticsUtil.getCurrentTimestampInSeconds()));
117 private static void updateSentMsg(final Sent sent) {
118 Preconditions.checkNotNull(sent);
119 final long count = sent.getCount() == null ? 0L : sent.getCount().getValue();
120 sent.setCount(new ZeroBasedCounter32(count + 1));
121 sent.setTimestamp(new Timestamp(StatisticsUtil.getCurrentTimestampInSeconds()));
124 private static AdvertizedTableTypes addTableType(final BgpTableType type) {
125 Preconditions.checkNotNull(type);
126 final AdvertizedTableTypes att = new AdvertizedTableTypes();
127 final QName afi = BindingReflections.findQName(type.getAfi()).intern();
128 final QName safi = BindingReflections.findQName(type.getSafi()).intern();
129 att.setAfi(new IdentityAttributeRef(afi.toString()));
130 att.setSafi(new IdentityAttributeRef(safi.toString()));
134 private static AdvertisedAddPathTableTypes addAddPathTableType(final AddressFamilies addressFamilies) {
135 Preconditions.checkNotNull(addressFamilies);
136 final AdvertisedAddPathTableTypes att = new AdvertisedAddPathTableTypes();
137 att.setAfi(new IdentityAttributeRef(BindingReflections.findQName(addressFamilies.getAfi()).intern().toString()));
138 att.setSafi(new IdentityAttributeRef(BindingReflections.findQName(addressFamilies.getSafi()).intern().toString()));
139 att.setSendReceive(addressFamilies.getSendReceive());
143 private static RemotePeerPreferences setRemotePeerPref(final Channel channel, final Optional<BGPSessionPreferences> localPreferences) {
144 Preconditions.checkNotNull(channel);
145 final RemotePeerPreferences pref = new RemotePeerPreferences();
146 final InetSocketAddress isa = (InetSocketAddress) channel.localAddress();
147 pref.setHost(IpAddressBuilder.getDefaultInstance(isa.getAddress().getHostAddress()));
148 pref.setPort(new PortNumber(isa.getPort()));
149 final List<AdvertizedTableTypes> tt = new ArrayList<>();
150 final List<AdvertisedAddPathTableTypes> attList = new ArrayList<>();
151 if (localPreferences.isPresent()) {
152 final BGPSessionPreferences localPref = localPreferences.get();
153 pref.setBgpId(localPref.getBgpId());
154 pref.setAs(localPref.getMyAs());
155 pref.setHoldtimer(localPref.getHoldTime());
156 if (localPref.getParams() != null) {
157 for (final BgpParameters param : localPref.getParams()) {
158 for (final OptionalCapabilities capa : param.getOptionalCapabilities()) {
159 final CParameters cParam = capa.getCParameters();
160 final CParameters1 capabilities = cParam.getAugmentation(CParameters1.class);
161 if (capabilities != null) {
162 final MultiprotocolCapability mc = capabilities.getMultiprotocolCapability();
164 final AdvertizedTableTypes att = new AdvertizedTableTypes();
165 att.setAfi(new IdentityAttributeRef(BindingReflections.findQName(mc.getAfi()).intern().toString()));
166 att.setSafi(new IdentityAttributeRef(BindingReflections.findQName(mc.getSafi()).intern().toString()));
169 if (capabilities.getGracefulRestartCapability() != null) {
170 pref.setGrCapability(true);
172 if (capabilities.getAddPathCapability() != null) {
173 // FIXME: add path capability is deprecated, replaced by addPathTableTypes
174 pref.setAddPathCapability(true);
176 final List<AdvertisedAddPathTableTypes> addPathTableTypeList = capabilities.getAddPathCapability()
177 .getAddressFamilies()
179 .map(BGPSessionStatsImpl::addAddPathTableType)
180 .collect(Collectors.toList());
181 attList.addAll(addPathTableTypeList);
183 if (capabilities.getRouteRefreshCapability() != null) {
184 pref.setRouteRefreshCapability(true);
187 if (cParam.getAs4BytesCapability() != null) {
188 pref.setFourOctetAsCapability(true);
190 if (cParam.getBgpExtendedMessageCapability() != null) {
191 pref.setBgpExtendedMessageCapability(true);
197 pref.setAdvertizedTableTypes(tt);
198 pref.setAdvertisedAddPathTableTypes(attList);
202 private static LocalPeerPreferences setLocalPeerPref(final Open remoteOpen, final Channel channel, final Collection<BgpTableType> tableTypes,
203 final List<AddressFamilies> addPathTypes) {
204 Preconditions.checkNotNull(remoteOpen);
205 Preconditions.checkNotNull(channel);
206 final LocalPeerPreferences pref = new LocalPeerPreferences();
207 final InetSocketAddress isa = (InetSocketAddress) channel.remoteAddress();
208 pref.setHost(IpAddressBuilder.getDefaultInstance(isa.getAddress().getHostAddress()));
209 pref.setPort(new PortNumber(isa.getPort()));
210 pref.setBgpId(new BgpId(remoteOpen.getBgpIdentifier()));
211 pref.setAs(new AsNumber(remoteOpen.getMyAsNumber().longValue()));
212 pref.setHoldtimer(remoteOpen.getHoldTimer());
214 final List<AdvertizedTableTypes> tt = tableTypes.stream().map(BGPSessionStatsImpl::addTableType).collect(Collectors.toList());
215 final List<AdvertisedAddPathTableTypes> addPathTableTypeList = addPathTypes.stream().map(BGPSessionStatsImpl::addAddPathTableType)
216 .collect(Collectors.toList());
218 if (remoteOpen.getBgpParameters() != null) {
219 for (final BgpParameters param : remoteOpen.getBgpParameters()) {
220 for (final OptionalCapabilities capa : param.getOptionalCapabilities()) {
221 final CParameters cParam = capa.getCParameters();
222 final CParameters1 capabilities = cParam.getAugmentation(CParameters1.class);
223 if (cParam.getAs4BytesCapability() != null) {
224 pref.setFourOctetAsCapability(true);
226 if (capabilities != null) {
227 if (capabilities.getGracefulRestartCapability() != null) {
228 pref.setGrCapability(true);
230 // FIXME: add-path-capability is deprecated in Boron
231 if (capabilities.getAddPathCapability() != null) {
232 pref.setAddPathCapability(true);
234 if (capabilities.getRouteRefreshCapability() != null) {
235 pref.setRouteRefreshCapability(true);
238 if (cParam.getBgpExtendedMessageCapability() != null) {
239 pref.setBgpExtendedMessageCapability(true);
245 pref.setAdvertizedTableTypes(tt);
246 pref.setAdvertisedAddPathTableTypes(addPathTableTypeList);
250 private void initMsgs() {
251 this.totalMsgs.setReceived(newReceivedInstance());
252 this.totalMsgs.setSent(newSentInstance());
253 this.kaMsgs.setReceived(newReceivedInstance());
254 this.kaMsgs.setSent(newSentInstance());
255 this.updMsgs.setReceived(newReceivedInstance());
256 this.updMsgs.setSent(newSentInstance());
257 this.rrMsgs.setReceived(newReceivedInstance());
258 this.rrMsgs.setSent(newSentInstance());
259 this.errMsgsSentTotal.setCount(INITIAL_COUNTER);
260 this.errMsgsRecvTotal.setCount(INITIAL_COUNTER);
261 this.errMsgs.getErrorSent().clear();
262 this.errMsgs.getErrorReceived().clear();
265 public void startSessionStopwatch() {
266 this.sessionStopwatch.start();
269 private void updateSentMsgTotal() {
270 updateSentMsg(this.totalMsgs.getSent());
273 private void updateReceivedMsgTotal() {
274 updateReceivedMsg(this.totalMsgs.getReceived());
277 private void updateReceivedMsgKA() {
278 updateReceivedMsg(this.kaMsgs.getReceived());
281 public void updateSentMsgKA() {
282 updateSentMsg(this.kaMsgs.getSent());
285 private void updateSentMsgUpd() {
286 updateSentMsg(this.updMsgs.getSent());
289 private void updateReceivedMsgUpd() {
290 updateReceivedMsg(this.updMsgs.getReceived());
293 private void updateSentMsgRR() {
294 updateSentMsg(this.rrMsgs.getSent());
297 private void updateReceivedMsgRR() {
298 updateReceivedMsg(this.rrMsgs.getReceived());
301 private void updateReceivedMsgErr(@Nonnull final Notify error) {
302 Preconditions.checkNotNull(error);
303 final List<ErrorReceived> errList = this.errMsgs.getErrorReceived();
304 ErrorReceived received = null;
305 for (ErrorReceived err : errList) {
306 if (err.getErrorCode().equals(error.getErrorCode()) && err.getErrorSubcode().equals(error.getErrorSubcode())) {
311 if (null == received) {
312 received = new ErrorReceived();
313 received.setErrorCode(error.getErrorCode());
314 received.setErrorSubcode(error.getErrorSubcode());
315 received.setCount(INITIAL_COUNTER);
316 errList.add(received);
318 received.setCount(new ZeroBasedCounter32(received.getCount().getValue() + 1));
319 final Timestamp curTimestamp = new Timestamp(StatisticsUtil.getCurrentTimestampInSeconds());
320 received.setTimestamp(curTimestamp);
321 this.errMsgsRecvTotal.setCount(new ZeroBasedCounter32(this.errMsgsRecvTotal.getCount().getValue() + 1));
322 this.errMsgsRecvTotal.setTimestamp(curTimestamp);
325 private void updateSentMsgErr(@Nonnull final Notify error) {
326 Preconditions.checkNotNull(error);
327 final List<ErrorSent> errList = this.errMsgs.getErrorSent();
328 ErrorSent sent = null;
329 for (ErrorSent err : errList) {
330 if (err.getErrorCode().equals(error.getErrorCode()) && err.getErrorSubcode().equals(error.getErrorSubcode())) {
336 sent = new ErrorSent();
337 sent.setErrorCode(error.getErrorCode());
338 sent.setErrorSubcode(error.getErrorSubcode());
339 sent.setCount(INITIAL_COUNTER);
342 sent.setCount(new ZeroBasedCounter32(sent.getCount().getValue() + 1));
343 final Timestamp curTimestamp = new Timestamp(StatisticsUtil.getCurrentTimestampInSeconds());
344 sent.setTimestamp(curTimestamp);
345 this.errMsgsSentTotal.setCount(new ZeroBasedCounter32(this.errMsgsSentTotal.getCount().getValue() + 1));
346 this.errMsgsSentTotal.setTimestamp(curTimestamp);
350 public BgpSessionState getBgpSessionState() {
351 final MessagesStats msgs = new MessagesStats();
352 msgs.setTotalMsgs(this.totalMsgs);
353 msgs.setErrorMsgs(this.errMsgs);
354 msgs.setKeepAliveMsgs(this.kaMsgs);
355 msgs.setUpdateMsgs(this.updMsgs);
356 msgs.setRouteRefreshMsgs(this.rrMsgs);
357 this.stats.setSessionDuration(StatisticsUtil.formatElapsedTime(this.sessionStopwatch.elapsed(TimeUnit.SECONDS)));
358 this.stats.setSessionState(this.session.getState().toString());
359 this.stats.setMessagesStats(msgs);
364 public void resetBgpSessionStats() {
368 public void updateReceivedMsg(Notification msg) {
369 LOG.trace("Updating received BGP session message count..");
370 this.updateReceivedMsgTotal();
371 if (msg instanceof Notify) {
372 this.updateReceivedMsgErr((Notify) msg);
373 } else if (msg instanceof Keepalive) {
374 this.updateReceivedMsgKA();
375 } else if (msg instanceof RouteRefresh) {
376 this.updateReceivedMsgRR();
377 } else if (msg instanceof Update) {
378 this.updateReceivedMsgUpd();
382 public void updateSentMsg(Notification msg) {
383 LOG.trace("Updating sent BGP session message count..");
384 this.updateSentMsgTotal();
385 if (msg instanceof Update) {
386 this.updateSentMsgUpd();
387 } else if (msg instanceof Notify) {
388 this.updateSentMsgErr((Notify) msg);
389 } else if (msg instanceof RouteRefresh) {
390 this.updateSentMsgRR();