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 static java.util.Objects.requireNonNull;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Stopwatch;
15 import io.netty.channel.Channel;
16 import java.net.InetSocketAddress;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.concurrent.TimeUnit;
21 import java.util.stream.Collectors;
22 import javax.annotation.Nonnull;
23 import org.opendaylight.controller.config.api.IdentityAttributeRef;
24 import org.opendaylight.controller.config.yang.bgp.rib.impl.AdvertisedAddPathTableTypes;
25 import org.opendaylight.controller.config.yang.bgp.rib.impl.AdvertizedTableTypes;
26 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpSessionState;
27 import org.opendaylight.controller.config.yang.bgp.rib.impl.ErrorMsgs;
28 import org.opendaylight.controller.config.yang.bgp.rib.impl.ErrorReceived;
29 import org.opendaylight.controller.config.yang.bgp.rib.impl.ErrorReceivedTotal;
30 import org.opendaylight.controller.config.yang.bgp.rib.impl.ErrorSent;
31 import org.opendaylight.controller.config.yang.bgp.rib.impl.ErrorSentTotal;
32 import org.opendaylight.controller.config.yang.bgp.rib.impl.KeepAliveMsgs;
33 import org.opendaylight.controller.config.yang.bgp.rib.impl.LocalPeerPreferences;
34 import org.opendaylight.controller.config.yang.bgp.rib.impl.MessagesStats;
35 import org.opendaylight.controller.config.yang.bgp.rib.impl.Received;
36 import org.opendaylight.controller.config.yang.bgp.rib.impl.RemotePeerPreferences;
37 import org.opendaylight.controller.config.yang.bgp.rib.impl.RouteRefreshMsgs;
38 import org.opendaylight.controller.config.yang.bgp.rib.impl.Sent;
39 import org.opendaylight.controller.config.yang.bgp.rib.impl.TotalMsgs;
40 import org.opendaylight.controller.config.yang.bgp.rib.impl.UpdateMsgs;
41 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl;
42 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
43 import org.opendaylight.protocol.util.StatisticsUtil;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Timestamp;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.ZeroBasedCounter32;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParameters;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.RouteRefresh;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.MultiprotocolCapability;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.add.path.capability.AddressFamilies;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
62 import org.opendaylight.yangtools.yang.binding.Notification;
63 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
64 import org.opendaylight.yangtools.yang.common.QName;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
68 public final class BGPSessionStatsImpl implements BGPSessionStats {
69 private static final Logger LOG = LoggerFactory.getLogger(BGPSessionStatsImpl.class);
71 private final Stopwatch sessionStopwatch;
72 private final BgpSessionState stats;
73 private final BGPSessionImpl session;
74 private final TotalMsgs totalMsgs = new TotalMsgs();
75 private final KeepAliveMsgs kaMsgs = new KeepAliveMsgs();
76 private final UpdateMsgs updMsgs = new UpdateMsgs();
77 private final RouteRefreshMsgs rrMsgs = new RouteRefreshMsgs();
78 private final ErrorMsgs errMsgs = new ErrorMsgs();
79 private final ErrorSentTotal errMsgsSentTotal = new ErrorSentTotal();
80 private final ErrorReceivedTotal errMsgsRecvTotal = new ErrorReceivedTotal();
81 private static final ZeroBasedCounter32 INITIAL_COUNTER = new ZeroBasedCounter32(0L);
83 public BGPSessionStatsImpl(@Nonnull final BGPSessionImpl session, @Nonnull final Open remoteOpen, final int holdTimerValue, final int keepAlive, @Nonnull final Channel channel,
84 @Nonnull final Optional<BGPSessionPreferences> localPreferences, @Nonnull final Collection<BgpTableType> tableTypes, @Nonnull final List<AddressFamilies> addPathTypes) {
85 this.session = session;
86 this.sessionStopwatch = Stopwatch.createUnstarted();
87 this.stats = new BgpSessionState();
88 this.stats.setHoldtimeCurrent(holdTimerValue);
89 this.stats.setKeepaliveCurrent(keepAlive);
90 this.stats.setLocalPeerPreferences(setLocalPeerPref(remoteOpen, channel, tableTypes, addPathTypes));
91 this.stats.setRemotePeerPreferences(setRemotePeerPref(channel, localPreferences));
92 this.errMsgs.setErrorReceivedTotal(this.errMsgsRecvTotal);
93 this.errMsgs.setErrorSentTotal(this.errMsgsSentTotal);
94 this.errMsgs.setErrorReceived(new ArrayList<>());
95 this.errMsgs.setErrorSent(new ArrayList<>());
99 private static Received newReceivedInstance() {
100 final Received recv = new Received();
101 recv.setCount(INITIAL_COUNTER);
105 private static Sent newSentInstance() {
106 final Sent sent = new Sent();
107 sent.setCount(INITIAL_COUNTER);
111 private static void updateReceivedMsg(final Received received) {
112 requireNonNull(received);
113 final long count = received.getCount() == null ? 0L : received.getCount().getValue();
114 received.setCount(new ZeroBasedCounter32(count + 1));
115 received.setTimestamp(new Timestamp(StatisticsUtil.getCurrentTimestampInSeconds()));
118 private static void updateSentMsg(final Sent sent) {
119 requireNonNull(sent);
120 final long count = sent.getCount() == null ? 0L : sent.getCount().getValue();
121 sent.setCount(new ZeroBasedCounter32(count + 1));
122 sent.setTimestamp(new Timestamp(StatisticsUtil.getCurrentTimestampInSeconds()));
125 private static AdvertizedTableTypes addTableType(final BgpTableType type) {
126 requireNonNull(type);
127 final AdvertizedTableTypes att = new AdvertizedTableTypes();
128 final QName afi = BindingReflections.findQName(type.getAfi()).intern();
129 final QName safi = BindingReflections.findQName(type.getSafi()).intern();
130 att.setAfi(new IdentityAttributeRef(afi.toString()));
131 att.setSafi(new IdentityAttributeRef(safi.toString()));
135 private static AdvertisedAddPathTableTypes addAddPathTableType(final AddressFamilies addressFamilies) {
136 requireNonNull(addressFamilies);
137 final AdvertisedAddPathTableTypes att = new AdvertisedAddPathTableTypes();
138 att.setAfi(new IdentityAttributeRef(BindingReflections.findQName(addressFamilies.getAfi()).intern().toString()));
139 att.setSafi(new IdentityAttributeRef(BindingReflections.findQName(addressFamilies.getSafi()).intern().toString()));
140 att.setSendReceive(addressFamilies.getSendReceive());
144 private static RemotePeerPreferences setRemotePeerPref(final Channel channel, final Optional<BGPSessionPreferences> localPreferences) {
145 requireNonNull(channel);
146 final RemotePeerPreferences pref = new RemotePeerPreferences();
147 final InetSocketAddress isa = (InetSocketAddress) channel.localAddress();
148 pref.setHost(IpAddressBuilder.getDefaultInstance(isa.getAddress().getHostAddress()));
149 pref.setPort(new PortNumber(isa.getPort()));
150 final List<AdvertizedTableTypes> tt = new ArrayList<>();
151 final List<AdvertisedAddPathTableTypes> attList = new ArrayList<>();
152 if (localPreferences.isPresent()) {
153 final BGPSessionPreferences localPref = localPreferences.get();
154 pref.setBgpId(localPref.getBgpId());
155 pref.setAs(localPref.getMyAs());
156 pref.setHoldtimer(localPref.getHoldTime());
157 if (localPref.getParams() != null) {
158 for (final BgpParameters param : localPref.getParams()) {
159 for (final OptionalCapabilities capa : param.getOptionalCapabilities()) {
160 final CParameters cParam = capa.getCParameters();
161 final CParameters1 capabilities = cParam.getAugmentation(CParameters1.class);
162 if (capabilities != null) {
163 final MultiprotocolCapability mc = capabilities.getMultiprotocolCapability();
165 final AdvertizedTableTypes att = new AdvertizedTableTypes();
166 att.setAfi(new IdentityAttributeRef(BindingReflections.findQName(mc.getAfi()).intern().toString()));
167 att.setSafi(new IdentityAttributeRef(BindingReflections.findQName(mc.getSafi()).intern().toString()));
170 if (capabilities.getGracefulRestartCapability() != null) {
171 pref.setGrCapability(true);
173 if (capabilities.getAddPathCapability() != null) {
174 final List<AdvertisedAddPathTableTypes> addPathTableTypeList = capabilities.getAddPathCapability()
175 .getAddressFamilies()
177 .map(BGPSessionStatsImpl::addAddPathTableType)
178 .collect(Collectors.toList());
179 attList.addAll(addPathTableTypeList);
181 if (capabilities.getRouteRefreshCapability() != null) {
182 pref.setRouteRefreshCapability(true);
185 if (cParam.getAs4BytesCapability() != null) {
186 pref.setFourOctetAsCapability(true);
188 if (cParam.getBgpExtendedMessageCapability() != null) {
189 pref.setBgpExtendedMessageCapability(true);
195 pref.setAdvertizedTableTypes(tt);
196 pref.setAdvertisedAddPathTableTypes(attList);
200 private static LocalPeerPreferences setLocalPeerPref(final Open remoteOpen, final Channel channel, final Collection<BgpTableType> tableTypes,
201 final List<AddressFamilies> addPathTypes) {
202 requireNonNull(remoteOpen);
203 requireNonNull(channel);
204 final LocalPeerPreferences pref = new LocalPeerPreferences();
205 final InetSocketAddress isa = (InetSocketAddress) channel.remoteAddress();
206 pref.setHost(IpAddressBuilder.getDefaultInstance(isa.getAddress().getHostAddress()));
207 pref.setPort(new PortNumber(isa.getPort()));
208 pref.setBgpId(new BgpId(remoteOpen.getBgpIdentifier()));
209 pref.setAs(new AsNumber(remoteOpen.getMyAsNumber().longValue()));
210 pref.setHoldtimer(remoteOpen.getHoldTimer());
212 final List<AdvertizedTableTypes> tt = tableTypes.stream().map(BGPSessionStatsImpl::addTableType).collect(Collectors.toList());
213 final List<AdvertisedAddPathTableTypes> addPathTableTypeList = addPathTypes.stream().map(BGPSessionStatsImpl::addAddPathTableType)
214 .collect(Collectors.toList());
216 if (remoteOpen.getBgpParameters() != null) {
217 for (final BgpParameters param : remoteOpen.getBgpParameters()) {
218 for (final OptionalCapabilities capa : param.getOptionalCapabilities()) {
219 final CParameters cParam = capa.getCParameters();
220 final CParameters1 capabilities = cParam.getAugmentation(CParameters1.class);
221 if (cParam.getAs4BytesCapability() != null) {
222 pref.setFourOctetAsCapability(true);
224 if (capabilities != null) {
225 if (capabilities.getGracefulRestartCapability() != null) {
226 pref.setGrCapability(true);
228 if (capabilities.getRouteRefreshCapability() != null) {
229 pref.setRouteRefreshCapability(true);
232 if (cParam.getBgpExtendedMessageCapability() != null) {
233 pref.setBgpExtendedMessageCapability(true);
239 pref.setAdvertizedTableTypes(tt);
240 pref.setAdvertisedAddPathTableTypes(addPathTableTypeList);
244 private void initMsgs() {
245 this.totalMsgs.setReceived(newReceivedInstance());
246 this.totalMsgs.setSent(newSentInstance());
247 this.kaMsgs.setReceived(newReceivedInstance());
248 this.kaMsgs.setSent(newSentInstance());
249 this.updMsgs.setReceived(newReceivedInstance());
250 this.updMsgs.setSent(newSentInstance());
251 this.rrMsgs.setReceived(newReceivedInstance());
252 this.rrMsgs.setSent(newSentInstance());
253 this.errMsgsSentTotal.setCount(INITIAL_COUNTER);
254 this.errMsgsRecvTotal.setCount(INITIAL_COUNTER);
255 this.errMsgs.getErrorSent().clear();
256 this.errMsgs.getErrorReceived().clear();
259 public void startSessionStopwatch() {
260 this.sessionStopwatch.start();
263 private void updateSentMsgTotal() {
264 updateSentMsg(this.totalMsgs.getSent());
267 private void updateReceivedMsgTotal() {
268 updateReceivedMsg(this.totalMsgs.getReceived());
271 private void updateReceivedMsgKA() {
272 updateReceivedMsg(this.kaMsgs.getReceived());
275 public void updateSentMsgKA() {
276 updateSentMsg(this.kaMsgs.getSent());
279 private void updateSentMsgUpd() {
280 updateSentMsg(this.updMsgs.getSent());
283 private void updateReceivedMsgUpd() {
284 updateReceivedMsg(this.updMsgs.getReceived());
287 private void updateSentMsgRR() {
288 updateSentMsg(this.rrMsgs.getSent());
291 private void updateReceivedMsgRR() {
292 updateReceivedMsg(this.rrMsgs.getReceived());
295 private void updateReceivedMsgErr(@Nonnull final Notify error) {
296 requireNonNull(error);
297 final List<ErrorReceived> errList = this.errMsgs.getErrorReceived();
298 ErrorReceived received = null;
299 for (ErrorReceived err : errList) {
300 if (err.getErrorCode().equals(error.getErrorCode()) && err.getErrorSubcode().equals(error.getErrorSubcode())) {
305 if (null == received) {
306 received = new ErrorReceived();
307 received.setErrorCode(error.getErrorCode());
308 received.setErrorSubcode(error.getErrorSubcode());
309 received.setCount(INITIAL_COUNTER);
310 errList.add(received);
312 received.setCount(new ZeroBasedCounter32(received.getCount().getValue() + 1));
313 final Timestamp curTimestamp = new Timestamp(StatisticsUtil.getCurrentTimestampInSeconds());
314 received.setTimestamp(curTimestamp);
315 this.errMsgsRecvTotal.setCount(new ZeroBasedCounter32(this.errMsgsRecvTotal.getCount().getValue() + 1));
316 this.errMsgsRecvTotal.setTimestamp(curTimestamp);
319 private void updateSentMsgErr(@Nonnull final Notify error) {
320 requireNonNull(error);
321 final List<ErrorSent> errList = this.errMsgs.getErrorSent();
322 ErrorSent sent = null;
323 for (ErrorSent err : errList) {
324 if (err.getErrorCode().equals(error.getErrorCode()) && err.getErrorSubcode().equals(error.getErrorSubcode())) {
330 sent = new ErrorSent();
331 sent.setErrorCode(error.getErrorCode());
332 sent.setErrorSubcode(error.getErrorSubcode());
333 sent.setCount(INITIAL_COUNTER);
336 sent.setCount(new ZeroBasedCounter32(sent.getCount().getValue() + 1));
337 final Timestamp curTimestamp = new Timestamp(StatisticsUtil.getCurrentTimestampInSeconds());
338 sent.setTimestamp(curTimestamp);
339 this.errMsgsSentTotal.setCount(new ZeroBasedCounter32(this.errMsgsSentTotal.getCount().getValue() + 1));
340 this.errMsgsSentTotal.setTimestamp(curTimestamp);
344 public BgpSessionState getBgpSessionState() {
345 final MessagesStats msgs = new MessagesStats();
346 msgs.setTotalMsgs(this.totalMsgs);
347 msgs.setErrorMsgs(this.errMsgs);
348 msgs.setKeepAliveMsgs(this.kaMsgs);
349 msgs.setUpdateMsgs(this.updMsgs);
350 msgs.setRouteRefreshMsgs(this.rrMsgs);
351 this.stats.setSessionDuration(StatisticsUtil.formatElapsedTime(this.sessionStopwatch.elapsed(TimeUnit.SECONDS)));
352 this.stats.setSessionState(this.session.getState().toString());
353 this.stats.setMessagesStats(msgs);
358 public void resetBgpSessionStats() {
362 public void updateReceivedMsg(Notification msg) {
363 LOG.trace("Updating received BGP session message count..");
364 this.updateReceivedMsgTotal();
365 if (msg instanceof Notify) {
366 this.updateReceivedMsgErr((Notify) msg);
367 } else if (msg instanceof Keepalive) {
368 this.updateReceivedMsgKA();
369 } else if (msg instanceof RouteRefresh) {
370 this.updateReceivedMsgRR();
371 } else if (msg instanceof Update) {
372 this.updateReceivedMsgUpd();
376 public void updateSentMsg(Notification msg) {
377 LOG.trace("Updating sent BGP session message count..");
378 this.updateSentMsgTotal();
379 if (msg instanceof Update) {
380 this.updateSentMsgUpd();
381 } else if (msg instanceof Notify) {
382 this.updateSentMsgErr((Notify) msg);
383 } else if (msg instanceof RouteRefresh) {
384 this.updateSentMsgRR();