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;
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 org.opendaylight.controller.config.yang.bgp.rib.impl.AdvertizedTableTypes;
21 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpSessionState;
22 import org.opendaylight.controller.config.yang.bgp.rib.impl.ErrorMsgs;
23 import org.opendaylight.controller.config.yang.bgp.rib.impl.ErrorReceived;
24 import org.opendaylight.controller.config.yang.bgp.rib.impl.ErrorSent;
25 import org.opendaylight.controller.config.yang.bgp.rib.impl.KeepAliveMsgs;
26 import org.opendaylight.controller.config.yang.bgp.rib.impl.MessagesStats;
27 import org.opendaylight.controller.config.yang.bgp.rib.impl.PeerPreferences;
28 import org.opendaylight.controller.config.yang.bgp.rib.impl.Received;
29 import org.opendaylight.controller.config.yang.bgp.rib.impl.Sent;
30 import org.opendaylight.controller.config.yang.bgp.rib.impl.SpeakerPreferences;
31 import org.opendaylight.controller.config.yang.bgp.rib.impl.TotalMsgs;
32 import org.opendaylight.controller.config.yang.bgp.rib.impl.UpdateMsgs;
33 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl.State;
34 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
35 import org.opendaylight.protocol.util.StatisticsUtil;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParameters;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.MultiprotocolCapability;
45 final class BGPSessionStats {
46 private final Stopwatch sessionStopwatch;
47 private final BgpSessionState stats;
48 private final TotalMsgs totalMsgs = new TotalMsgs();
49 private final KeepAliveMsgs kaMsgs = new KeepAliveMsgs();
50 private final UpdateMsgs updMsgs = new UpdateMsgs();
51 private final ErrorMsgs errMsgs = new ErrorMsgs();
53 public BGPSessionStats(final Open remoteOpen, final int holdTimerValue, final int keepAlive, final Channel channel,
54 final Optional<BGPSessionPreferences> localPreferences, final Collection<BgpTableType> tableTypes) {
55 this.sessionStopwatch = Stopwatch.createUnstarted();
56 this.stats = new BgpSessionState();
57 this.stats.setHoldtimeCurrent(holdTimerValue);
58 this.stats.setKeepaliveCurrent(keepAlive);
59 this.stats.setPeerPreferences(setPeerPref(remoteOpen, channel, tableTypes));
60 this.stats.setSpeakerPreferences(setSpeakerPref(channel, localPreferences));
64 private void initMsgs() {
65 this.totalMsgs.setReceived(new Received());
66 this.totalMsgs.setSent(new Sent());
67 this.kaMsgs.setReceived(new Received());
68 this.kaMsgs.setSent(new Sent());
69 this.updMsgs.setReceived(new Received());
70 this.updMsgs.setSent(new Sent());
71 this.errMsgs.setErrorReceived(new ErrorReceived());
72 this.errMsgs.setErrorSent(new ErrorSent());
75 public void startSessionStopwatch() {
76 this.sessionStopwatch.start();
79 public void updateSentMsgTotal() {
80 updateSentMsg(this.totalMsgs.getSent());
83 public void updateReceivedMsgTotal() {
84 updateReceivedMsg(this.totalMsgs.getReceived());
87 public void updateReceivedMsgKA() {
88 updateReceivedMsg(this.kaMsgs.getReceived());
91 public void updateSentMsgKA() {
92 updateSentMsg(this.kaMsgs.getSent());
95 public void updateSentMsgUpd() {
96 updateSentMsg(this.updMsgs.getSent());
99 public void updateReceivedMsgUpd() {
100 updateReceivedMsg(this.updMsgs.getReceived());
103 public void updateReceivedMsgErr(final Notify error) {
104 Preconditions.checkNotNull(error);
105 final ErrorReceived received = this.errMsgs.getErrorReceived();
106 received.setCount(received.getCount() + 1);
107 received.setTimestamp(StatisticsUtil.getCurrentTimestampInSeconds());
108 received.setCode(error.getErrorCode());
109 received.setSubCode(error.getErrorSubcode());
112 public void updateSentMsgErr(final Notify error) {
113 Preconditions.checkNotNull(error);
114 final ErrorSent sent = this.errMsgs.getErrorSent();
115 sent.setCount(sent.getCount() + 1);
116 sent.setTimestamp(StatisticsUtil.getCurrentTimestampInSeconds());
117 sent.setCode(error.getErrorCode());
118 sent.setSubCode(error.getErrorSubcode());
121 public BgpSessionState getBgpSessionState(final State state) {
122 Preconditions.checkNotNull(state);
123 final MessagesStats msgs = new MessagesStats();
124 msgs.setTotalMsgs(this.totalMsgs);
125 msgs.setErrorMsgs(this.errMsgs);
126 msgs.setKeepAliveMsgs(this.kaMsgs);
127 msgs.setUpdateMsgs(this.updMsgs);
128 this.stats.setSessionDuration(StatisticsUtil.formatElapsedTime(this.sessionStopwatch.elapsed(TimeUnit.SECONDS)));
129 this.stats.setSessionState(state.toString());
130 this.stats.setMessagesStats(msgs);
134 public void resetStats() {
138 private static void updateReceivedMsg(final Received received) {
139 Preconditions.checkNotNull(received);
140 received.setCount(received.getCount() + 1);
141 received.setTimestamp(StatisticsUtil.getCurrentTimestampInSeconds());
144 private static void updateSentMsg(final Sent sent) {
145 Preconditions.checkNotNull(sent);
146 sent.setCount(sent.getCount() + 1);
147 sent.setTimestamp(StatisticsUtil.getCurrentTimestampInSeconds());
150 private static AdvertizedTableTypes addTableType(final BgpTableType type) {
151 Preconditions.checkNotNull(type);
152 final AdvertizedTableTypes att = new AdvertizedTableTypes();
153 att.setAfi(type.getAfi().getSimpleName());
154 att.setSafi(type.getSafi().getSimpleName());
158 private static SpeakerPreferences setSpeakerPref(final Channel channel, final Optional<BGPSessionPreferences> localPreferences) {
159 Preconditions.checkNotNull(channel);
160 final SpeakerPreferences pref = new SpeakerPreferences();
161 final InetSocketAddress isa = (InetSocketAddress) channel.localAddress();
162 pref.setAddress(isa.getAddress().getHostAddress());
163 pref.setPort(isa.getPort());
164 final List<AdvertizedTableTypes> tt = new ArrayList<>();
165 if (localPreferences.isPresent()) {
166 final BGPSessionPreferences localPref = localPreferences.get();
167 pref.setBgpId(localPref.getBgpId().getValue());
168 pref.setAs(localPref.getMyAs().getValue());
169 pref.setHoldtime(localPref.getHoldTime());
170 if (localPref.getParams() != null) {
171 for (final BgpParameters param : localPref.getParams()) {
172 for (final OptionalCapabilities capa : param.getOptionalCapabilities()) {
173 final CParameters cParam = capa.getCParameters();
174 if(cParam.getAugmentation(CParameters1.class) != null) {
175 final MultiprotocolCapability mc = cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability();
177 final AdvertizedTableTypes att = new AdvertizedTableTypes();
178 att.setAfi(mc.getAfi().getSimpleName());
179 att.setSafi(mc.getSafi().getSimpleName());
182 pref.setGrCapability(cParam.getAugmentation(CParameters1.class).getGracefulRestartCapability() != null);
183 pref.setFourOctetAsCapability(cParam.getAs4BytesCapability() != null);
184 pref.setAddPathCapability(cParam.getAugmentation(CParameters1.class).getAddPathCapability() != null);
185 if (cParam.getBgpExtendedMessageCapability() !=null) {
186 pref.setBgpExtendedMessageCapability(true);
193 pref.setAdvertizedTableTypes(tt);
197 private static PeerPreferences setPeerPref(final Open remoteOpen, final Channel channel, final Collection<BgpTableType> tableTypes) {
198 Preconditions.checkNotNull(remoteOpen);
199 Preconditions.checkNotNull(channel);
200 final PeerPreferences pref = new PeerPreferences();
201 final InetSocketAddress isa = (InetSocketAddress) channel.remoteAddress();
202 pref.setAddress(isa.getAddress().getHostAddress());
203 pref.setPort(isa.getPort());
204 pref.setBgpId(remoteOpen.getBgpIdentifier().getValue());
205 pref.setAs(remoteOpen.getMyAsNumber().longValue());
206 pref.setHoldtime(remoteOpen.getHoldTimer());
207 final List<AdvertizedTableTypes> tt = new ArrayList<>();
208 for (final BgpTableType t : tableTypes) {
209 tt.add(addTableType(t));
211 if (remoteOpen.getBgpParameters() != null) {
212 for (final BgpParameters param : remoteOpen.getBgpParameters()) {
213 for (final OptionalCapabilities capa : param.getOptionalCapabilities()) {
214 final CParameters cParam = capa.getCParameters();
215 pref.setFourOctetAsCapability(cParam.getAs4BytesCapability() != null);
216 pref.setGrCapability(cParam.getAugmentation(CParameters1.class) != null &&
217 cParam.getAugmentation(CParameters1.class).getGracefulRestartCapability() != null);
218 pref.setAddPathCapability(cParam.getAugmentation(CParameters1.class) != null &&
219 cParam.getAugmentation(CParameters1.class).getAddPathCapability() != null);
220 if (cParam.getBgpExtendedMessageCapability() !=null) {
221 pref.setBgpExtendedMessageCapability(true);
227 pref.setAdvertizedTableTypes(tt);