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.bgpcep.pcep.topology.provider;
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;
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;
39 private long minReplyTime = 0;
40 private long maxReplyTime = 0;
41 private long totalTime = 0;
42 private long reqCount = 0;
44 public SessionListenerState() {
45 this.sessionUpDuration = new Stopwatch();
46 this.capa = new PeerCapabilities();
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();
56 public void processRequestStats(final long duration) {
57 if (this.minReplyTime == 0) {
58 this.minReplyTime = duration;
60 if (duration < this.minReplyTime) {
61 this.minReplyTime = duration;
64 if (duration > this.maxReplyTime) {
65 this.maxReplyTime = duration;
67 this.totalTime += duration;
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);
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;
93 public ReplyTime getReplyTime() {
94 final ReplyTime time = new ReplyTime();
96 if (this.reqCount != 0) {
97 avg = this.totalTime / this.reqCount;
99 time.setAverageTime(avg);
100 time.setMaxTime(this.maxReplyTime);
101 time.setMinTime(this.minReplyTime);
105 public PeerCapabilities getPeerCapabilities() {
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)));
119 public void setPeerCapabilities(final PeerCapabilities capabilities) {
120 this.capa = Preconditions.checkNotNull(capabilities);
123 public void updateLastReceivedRptMsg() {
124 this.lastReceivedRptMsgTimestamp = System.currentTimeMillis();
125 this.receivedRptMsgCount++;
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++;
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());
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());
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());
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)));