652e0a12301e3e838e96c892edc29ea7b364890d
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPSessionStats.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.protocol.bgp.rib.impl;
10
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.BgpParameters;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.OptionalCapabilities;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.CParameters;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.As4BytesCase;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.GracefulRestartCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.MultiprotocolCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.multiprotocol._case.MultiprotocolCapability;
46
47 final class BGPSessionStats {
48     private final Stopwatch sessionStopwatch;
49     private final BgpSessionState stats;
50     private final TotalMsgs totalMsgs = new TotalMsgs();
51     private final KeepAliveMsgs kaMsgs = new KeepAliveMsgs();
52     private final UpdateMsgs updMsgs = new UpdateMsgs();
53     private final ErrorMsgs errMsgs = new ErrorMsgs();
54
55     public BGPSessionStats(final Open remoteOpen, final int holdTimerValue, final int keepAlive, final Channel channel,
56             final Optional<BGPSessionPreferences> localPreferences, final Collection<BgpTableType> tableTypes) {
57         this.sessionStopwatch = Stopwatch.createUnstarted();
58         this.stats = new BgpSessionState();
59         this.stats.setHoldtimeCurrent(holdTimerValue);
60         this.stats.setKeepaliveCurrent(keepAlive);
61         this.stats.setPeerPreferences(setPeerPref(remoteOpen, channel, tableTypes));
62         this.stats.setSpeakerPreferences(setSpeakerPref(channel, localPreferences));
63         initMsgs();
64     }
65
66     private void initMsgs() {
67         this.totalMsgs.setReceived(new Received());
68         this.totalMsgs.setSent(new Sent());
69         this.kaMsgs.setReceived(new Received());
70         this.kaMsgs.setSent(new Sent());
71         this.updMsgs.setReceived(new Received());
72         this.updMsgs.setSent(new Sent());
73         this.errMsgs.setErrorReceived(new ErrorReceived());
74         this.errMsgs.setErrorSent(new ErrorSent());
75     }
76
77     public void startSessionStopwatch() {
78         this.sessionStopwatch.start();
79     }
80
81     public void updateSentMsgTotal() {
82         updateSentMsg(this.totalMsgs.getSent());
83     }
84
85     public void updateReceivedMsgTotal() {
86         updateReceivedMsg(this.totalMsgs.getReceived());
87     }
88
89     public void updateReceivedMsgKA() {
90         updateReceivedMsg(this.kaMsgs.getReceived());
91     }
92
93     public void updateSentMsgKA() {
94         updateSentMsg(this.kaMsgs.getSent());
95     }
96
97     public void updateSentMsgUpd() {
98         updateSentMsg(this.updMsgs.getSent());
99     }
100
101     public void updateReceivedMsgUpd() {
102         updateReceivedMsg(this.updMsgs.getReceived());
103     }
104
105     public void updateReceivedMsgErr(final Notify error) {
106         Preconditions.checkNotNull(error);
107         final ErrorReceived received = this.errMsgs.getErrorReceived();
108         received.setCount(received.getCount() + 1);
109         received.setTimestamp(StatisticsUtil.getCurrentTimestampInSeconds());
110         received.setCode(error.getErrorCode());
111         received.setSubCode(error.getErrorSubcode());
112     }
113
114     public void updateSentMsgErr(final Notify error) {
115         Preconditions.checkNotNull(error);
116         final ErrorSent sent = this.errMsgs.getErrorSent();
117         sent.setCount(sent.getCount() + 1);
118         sent.setTimestamp(StatisticsUtil.getCurrentTimestampInSeconds());
119         sent.setCode(error.getErrorCode());
120         sent.setSubCode(error.getErrorSubcode());
121     }
122
123     public BgpSessionState getBgpSessionState(final State state) {
124         Preconditions.checkNotNull(state);
125         final MessagesStats msgs = new MessagesStats();
126         msgs.setTotalMsgs(this.totalMsgs);
127         msgs.setErrorMsgs(this.errMsgs);
128         msgs.setKeepAliveMsgs(this.kaMsgs);
129         msgs.setUpdateMsgs(this.updMsgs);
130         this.stats.setSessionDuration(StatisticsUtil.formatElapsedTime(this.sessionStopwatch.elapsed(TimeUnit.SECONDS)));
131         this.stats.setSessionState(state.toString());
132         this.stats.setMessagesStats(msgs);
133         return this.stats;
134     }
135
136     public void resetStats() {
137         initMsgs();
138     }
139
140     private static void updateReceivedMsg(final Received received) {
141         Preconditions.checkNotNull(received);
142         received.setCount(received.getCount() + 1);
143         received.setTimestamp(StatisticsUtil.getCurrentTimestampInSeconds());
144     }
145
146     private static void updateSentMsg(final Sent sent) {
147         Preconditions.checkNotNull(sent);
148         sent.setCount(sent.getCount() + 1);
149         sent.setTimestamp(StatisticsUtil.getCurrentTimestampInSeconds());
150     }
151
152     private static AdvertizedTableTypes addTableType(final BgpTableType type) {
153         Preconditions.checkNotNull(type);
154         final AdvertizedTableTypes att = new AdvertizedTableTypes();
155         att.setAfi(type.getAfi().getSimpleName());
156         att.setSafi(type.getSafi().getSimpleName());
157         return att;
158     }
159
160     private static SpeakerPreferences setSpeakerPref(final Channel channel, final Optional<BGPSessionPreferences> localPreferences) {
161         Preconditions.checkNotNull(channel);
162         final SpeakerPreferences pref = new SpeakerPreferences();
163         final InetSocketAddress isa = (InetSocketAddress) channel.localAddress();
164         pref.setAddress(isa.getAddress().getHostAddress());
165         pref.setPort(isa.getPort());
166         final List<AdvertizedTableTypes> tt = new ArrayList<>();
167         if (localPreferences.isPresent()) {
168             final BGPSessionPreferences localPref = localPreferences.get();
169             pref.setBgpId(localPref.getBgpId().getValue());
170             pref.setAs(localPref.getMyAs().getValue());
171             pref.setHoldtime(localPref.getHoldTime());
172             if (localPref.getParams() != null) {
173                 for (final BgpParameters param : localPref.getParams()) {
174                     for (final OptionalCapabilities capa : param.getOptionalCapabilities()) {
175                         final CParameters cp = capa.getCParameters();
176                         if (cp instanceof MultiprotocolCase) {
177                             final MultiprotocolCapability mc = ((MultiprotocolCase) cp).getMultiprotocolCapability();
178                             final AdvertizedTableTypes att = new AdvertizedTableTypes();
179                             att.setAfi(mc.getAfi().getSimpleName());
180                             att.setSafi(mc.getSafi().getSimpleName());
181                             tt.add(att);
182                         }
183                         if (cp instanceof As4BytesCase) {
184                             pref.setFourOctetAsCapability(((As4BytesCase) cp).getAs4BytesCapability() != null);
185                         }
186                         if (capa.getCParameters() instanceof GracefulRestartCase) {
187                             pref.setGrCapability(((GracefulRestartCase) capa.getCParameters()).getGracefulRestartCapability() != null);
188                         }
189                     }
190                 }
191             }
192         }
193         pref.setAdvertizedTableTypes(tt);
194         return pref;
195     }
196
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));
210         }
211         if (remoteOpen.getBgpParameters() != null) {
212             for (final BgpParameters param : remoteOpen.getBgpParameters()) {
213                 for (final OptionalCapabilities capa : param.getOptionalCapabilities()) {
214                     if (capa.getCParameters() instanceof As4BytesCase) {
215                         pref.setFourOctetAsCapability(((As4BytesCase) capa.getCParameters()).getAs4BytesCapability() != null);
216                     }
217                     if (capa.getCParameters() instanceof GracefulRestartCase) {
218                         pref.setGrCapability(((GracefulRestartCase) capa.getCParameters()).getGracefulRestartCapability() != null);
219                     }
220                 }
221
222             }
223         }
224         pref.setAdvertizedTableTypes(tt);
225         return pref;
226     }
227 }