BUG: 4831
[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.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;
44
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();
52
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));
61         initMsgs();
62     }
63
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());
73     }
74
75     public void startSessionStopwatch() {
76         this.sessionStopwatch.start();
77     }
78
79     public void updateSentMsgTotal() {
80         updateSentMsg(this.totalMsgs.getSent());
81     }
82
83     public void updateReceivedMsgTotal() {
84         updateReceivedMsg(this.totalMsgs.getReceived());
85     }
86
87     public void updateReceivedMsgKA() {
88         updateReceivedMsg(this.kaMsgs.getReceived());
89     }
90
91     public void updateSentMsgKA() {
92         updateSentMsg(this.kaMsgs.getSent());
93     }
94
95     public void updateSentMsgUpd() {
96         updateSentMsg(this.updMsgs.getSent());
97     }
98
99     public void updateReceivedMsgUpd() {
100         updateReceivedMsg(this.updMsgs.getReceived());
101     }
102
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());
110     }
111
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());
119     }
120
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);
131         return this.stats;
132     }
133
134     public void resetStats() {
135         initMsgs();
136     }
137
138     private static void updateReceivedMsg(final Received received) {
139         Preconditions.checkNotNull(received);
140         received.setCount(received.getCount() + 1);
141         received.setTimestamp(StatisticsUtil.getCurrentTimestampInSeconds());
142     }
143
144     private static void updateSentMsg(final Sent sent) {
145         Preconditions.checkNotNull(sent);
146         sent.setCount(sent.getCount() + 1);
147         sent.setTimestamp(StatisticsUtil.getCurrentTimestampInSeconds());
148     }
149
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());
155         return att;
156     }
157
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();
176                             if (mc != null) {
177                                 final AdvertizedTableTypes att = new AdvertizedTableTypes();
178                                 att.setAfi(mc.getAfi().getSimpleName());
179                                 att.setSafi(mc.getSafi().getSimpleName());
180                                 tt.add(att);
181                             }
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);
187                             }
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                     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);
222                     }
223                 }
224
225             }
226         }
227         pref.setAdvertizedTableTypes(tt);
228         return pref;
229     }
230 }