Bug-2081: PCEP statistics
[bgpcep.git] / pcep / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / SessionListenerState.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.bgpcep.pcep.topology.provider;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Stopwatch;
13 import java.util.concurrent.TimeUnit;
14 import org.opendaylight.controller.config.yang.pcep.topology.provider.ErrorMessages;
15 import org.opendaylight.controller.config.yang.pcep.topology.provider.LastReceivedError;
16 import org.opendaylight.controller.config.yang.pcep.topology.provider.LastSentError;
17 import org.opendaylight.controller.config.yang.pcep.topology.provider.LocalPref;
18 import org.opendaylight.controller.config.yang.pcep.topology.provider.Messages;
19 import org.opendaylight.controller.config.yang.pcep.topology.provider.PeerCapabilities;
20 import org.opendaylight.controller.config.yang.pcep.topology.provider.PeerPref;
21 import org.opendaylight.controller.config.yang.pcep.topology.provider.ReplyTime;
22 import org.opendaylight.controller.config.yang.pcep.topology.provider.SessionState;
23 import org.opendaylight.controller.config.yang.pcep.topology.provider.StatefulMessages;
24 import org.opendaylight.protocol.pcep.PCEPSession;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Pcinitiate;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Pcupd;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
28
29 final class SessionListenerState {
30     private long lastReceivedRptMsgTimestamp = 0;
31     private long receivedRptMsgCount = 0;
32     private long sentUpdMsgCount = 0;
33     private long sentInitMsgCount = 0;
34     private PeerCapabilities capa;
35     private LocalPref localPref;
36     private PeerPref peerPref;
37     private final Stopwatch sessionUpDuration;
38
39     private long minReplyTime = 0;
40     private long maxReplyTime = 0;
41     private long totalTime = 0;
42     private long reqCount = 0;
43
44     public SessionListenerState() {
45         this.sessionUpDuration = new Stopwatch();
46         this.capa = new PeerCapabilities();
47     }
48
49     public void init(final PCEPSession session) {
50         Preconditions.checkNotNull(session);
51         this.localPref = getLocalPref(session.getLocalPref());
52         this.peerPref = getPeerPref(session.getPeerPref());
53         this.sessionUpDuration.start();
54     }
55
56     public void processRequestStats(final long duration) {
57         if (this.minReplyTime == 0) {
58             this.minReplyTime = duration;
59         } else {
60             if (duration < this.minReplyTime) {
61                 this.minReplyTime = duration;
62             }
63         }
64         if (duration > this.maxReplyTime) {
65             this.maxReplyTime = duration;
66         }
67         this.totalTime += duration;
68         this.reqCount++;
69     }
70
71     public StatefulMessages getStatefulMessages() {
72         final StatefulMessages msgs = new StatefulMessages();
73         msgs.setLastReceivedRptMsgTimestamp(TimeUnit.MILLISECONDS.toSeconds(this.lastReceivedRptMsgTimestamp));
74         msgs.setReceivedRptMsgCount(this.receivedRptMsgCount);
75         msgs.setSentInitMsgCount(this.sentInitMsgCount);
76         msgs.setSentUpdMsgCount(this.sentUpdMsgCount);
77         return msgs;
78     }
79
80     public void resetStats(final PCEPSession session) {
81         Preconditions.checkNotNull(session);
82         this.receivedRptMsgCount = 0;
83         this.sentInitMsgCount = 0;
84         this.sentUpdMsgCount = 0;
85         this.lastReceivedRptMsgTimestamp = 0;
86         this.maxReplyTime = 0;
87         this.minReplyTime = 0;
88         this.totalTime = 0;
89         this.reqCount = 0;
90         session.resetStats();
91     }
92
93     public ReplyTime getReplyTime() {
94         final ReplyTime time = new ReplyTime();
95         long avg = 0;
96         if (this.reqCount != 0) {
97             avg = this.totalTime / this.reqCount;
98         }
99         time.setAverageTime(avg);
100         time.setMaxTime(this.maxReplyTime);
101         time.setMinTime(this.minReplyTime);
102         return time;
103     }
104
105     public PeerCapabilities getPeerCapabilities() {
106         return this.capa;
107     }
108
109     public SessionState getSessionState(final PCEPSession session) {
110         Preconditions.checkNotNull(session);
111         final SessionState state = new SessionState();
112         state.setLocalPref(this.localPref);
113         state.setPeerPref(this.peerPref);
114         state.setMessages(getMessageStats(session.getMessages()));
115         state.setSessionDuration(formatElapsedTime(this.sessionUpDuration.elapsed(TimeUnit.SECONDS)));
116         return state;
117     }
118
119     public void setPeerCapabilities(final PeerCapabilities capabilities) {
120         this.capa = Preconditions.checkNotNull(capabilities);
121     }
122
123     public void updateLastReceivedRptMsg() {
124         this.lastReceivedRptMsgTimestamp = System.currentTimeMillis();
125         this.receivedRptMsgCount++;
126     }
127
128     public void updateStatefulSentMsg(final Message msg) {
129         if (msg instanceof Pcinitiate || msg instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Pcinitiate) {
130             this.sentInitMsgCount++;
131         } else if (msg instanceof Pcupd || msg instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Pcupd) {
132             this.sentUpdMsgCount++;
133         }
134     }
135
136     private static LocalPref getLocalPref(final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.LocalPref localPref) {
137         final LocalPref local = new LocalPref();
138         local.setDeadtimer(localPref.getDeadtimer());
139         local.setIpAddress(localPref.getIpAddress());
140         local.setKeepalive(localPref.getKeepalive());
141         local.setSessionId(localPref.getSessionId());
142         return local;
143     }
144
145     private static PeerPref getPeerPref(final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.PeerPref peerPref) {
146         final PeerPref peer = new PeerPref();
147         peer.setDeadtimer(peerPref.getDeadtimer());
148         peer.setIpAddress(peerPref.getIpAddress());
149         peer.setKeepalive(peerPref.getKeepalive());
150         peer.setSessionId(peerPref.getSessionId());
151         return peer;
152     }
153
154     private static Messages getMessageStats(final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.Messages messages) {
155         final LastReceivedError lastReceivedError = new LastReceivedError();
156         lastReceivedError.setErrorType(messages.getErrorMessages().getLastReceivedError().getErrorType());
157         lastReceivedError.setErrorValue(messages.getErrorMessages().getLastReceivedError().getErrorValue());
158         final LastSentError lastSentError = new LastSentError();
159         lastSentError.setErrorType(messages.getErrorMessages().getLastSentError().getErrorType());
160         lastSentError.setErrorValue(messages.getErrorMessages().getLastSentError().getErrorValue());
161         final ErrorMessages errMsgs = new ErrorMessages();
162         errMsgs.setLastReceivedError(lastReceivedError);
163         errMsgs.setLastSentError(lastSentError);
164         errMsgs.setReceivedErrorMsgCount(messages.getErrorMessages().getReceivedErrorMsgCount());
165         errMsgs.setSentErrorMsgCount(messages.getErrorMessages().getSentErrorMsgCount());
166         final Messages msgs = new Messages();
167         msgs.setErrorMessages(errMsgs);
168         msgs.setLastSentMsgTimestamp(messages.getLastSentMsgTimestamp());
169         msgs.setReceivedMsgCount(messages.getReceivedMsgCount());
170         msgs.setSentMsgCount(messages.getSentMsgCount());
171         msgs.setUnknownMsgReceived(msgs.getUnknownMsgReceived());
172         return msgs;
173     }
174
175     private static String formatElapsedTime(final long seconds) {
176         return String.format("%2d:%02d:%02d:%02d",
177                 TimeUnit.SECONDS.toDays(seconds),
178                 TimeUnit.SECONDS.toHours(seconds) - TimeUnit.DAYS.toHours(TimeUnit.SECONDS.toDays(seconds)),
179                 TimeUnit.SECONDS.toMinutes(seconds) - TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(seconds)),
180                 seconds - TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(seconds)));
181     }
182 }