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 static java.util.Objects.requireNonNull;
13 import com.google.common.base.Stopwatch;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.atomic.LongAdder;
16 import org.opendaylight.controller.config.yang.pcep.topology.provider.ErrorMessages;
17 import org.opendaylight.controller.config.yang.pcep.topology.provider.LastReceivedError;
18 import org.opendaylight.controller.config.yang.pcep.topology.provider.LastSentError;
19 import org.opendaylight.controller.config.yang.pcep.topology.provider.LocalPref;
20 import org.opendaylight.controller.config.yang.pcep.topology.provider.Messages;
21 import org.opendaylight.controller.config.yang.pcep.topology.provider.PeerCapabilities;
22 import org.opendaylight.controller.config.yang.pcep.topology.provider.PeerPref;
23 import org.opendaylight.controller.config.yang.pcep.topology.provider.ReplyTime;
24 import org.opendaylight.controller.config.yang.pcep.topology.provider.SessionState;
25 import org.opendaylight.controller.config.yang.pcep.topology.provider.StatefulMessages;
26 import org.opendaylight.protocol.pcep.PCEPSession;
27 import org.opendaylight.protocol.util.StatisticsUtil;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Pcinitiate;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Pcupd;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
32 final class SessionListenerState {
33 private final LongAdder lastReceivedRptMsgTimestamp = new LongAdder();
34 private final LongAdder receivedRptMsgCount = new LongAdder();
35 private final LongAdder sentUpdMsgCount = new LongAdder();
36 private final LongAdder sentInitMsgCount = new LongAdder();
37 private PeerCapabilities capa;
38 private LocalPref localPref;
39 private PeerPref peerPref;
40 private final Stopwatch sessionUpDuration;
42 private final LongAdder minReplyTime = new LongAdder();
43 private final LongAdder maxReplyTime = new LongAdder();
44 private final LongAdder totalTime = new LongAdder();
45 private final LongAdder reqCount = new LongAdder();
47 public SessionListenerState() {
48 this.sessionUpDuration = Stopwatch.createUnstarted();
49 this.capa = new PeerCapabilities();
52 public void init(final PCEPSession session) {
53 requireNonNull(session);
54 this.localPref = getLocalPref(session.getLocalPref());
55 this.peerPref = getPeerPref(session.getPeerPref());
56 this.sessionUpDuration.start();
59 public synchronized void processRequestStats(final long duration) {
60 if (this.minReplyTime.longValue() == 0) {
61 this.minReplyTime.reset();
62 this.minReplyTime.add(duration);
63 } else if (duration < this.minReplyTime.longValue()) {
64 this.minReplyTime.reset();
65 this.minReplyTime.add(duration);
67 if (duration > this.maxReplyTime.longValue()) {
68 this.maxReplyTime.reset();
69 this.maxReplyTime.add(duration);
71 this.totalTime.add(duration);
72 this.reqCount.increment();
75 public synchronized StatefulMessages getStatefulMessages() {
76 final StatefulMessages msgs = new StatefulMessages();
77 msgs.setLastReceivedRptMsgTimestamp(this.lastReceivedRptMsgTimestamp.longValue());
78 msgs.setReceivedRptMsgCount(this.receivedRptMsgCount.longValue());
79 msgs.setSentInitMsgCount(this.sentInitMsgCount.longValue());
80 msgs.setSentUpdMsgCount(this.sentUpdMsgCount.longValue());
84 public synchronized void resetStats(final PCEPSession session) {
85 requireNonNull(session);
86 this.receivedRptMsgCount.reset();
87 this.sentInitMsgCount.reset();
88 this.sentUpdMsgCount.reset();
89 this.lastReceivedRptMsgTimestamp.reset();
90 this.maxReplyTime.reset();
91 this.minReplyTime.reset();
92 this.totalTime.reset();
93 this.reqCount.reset();
97 public synchronized ReplyTime getReplyTime() {
98 final ReplyTime time = new ReplyTime();
100 if (this.reqCount.longValue() != 0) {
101 avg = Math.round((double)this.totalTime.longValue()/this.reqCount.longValue());
103 time.setAverageTime(avg);
104 time.setMaxTime(this.maxReplyTime.longValue());
105 time.setMinTime(this.minReplyTime.longValue());
109 public PeerCapabilities getPeerCapabilities() {
113 public SessionState getSessionState(final PCEPSession session) {
114 requireNonNull(session);
115 final SessionState state = new SessionState();
116 state.setLocalPref(this.localPref);
117 state.setPeerPref(this.peerPref);
118 state.setMessages(getMessageStats(session.getMessages()));
119 state.setSessionDuration(StatisticsUtil.formatElapsedTime(this.sessionUpDuration.elapsed(TimeUnit.SECONDS)));
123 public void setPeerCapabilities(final PeerCapabilities capabilities) {
124 this.capa = requireNonNull(capabilities);
127 public void updateLastReceivedRptMsg() {
128 this.lastReceivedRptMsgTimestamp.reset();
129 this.lastReceivedRptMsgTimestamp.add(StatisticsUtil.getCurrentTimestampInSeconds());
130 this.receivedRptMsgCount.increment();
133 public void updateStatefulSentMsg(final Message msg) {
134 if (msg instanceof Pcinitiate) {
135 this.sentInitMsgCount.increment();
136 } else if (msg instanceof Pcupd) {
137 this.sentUpdMsgCount.increment();
141 private static LocalPref getLocalPref(final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
142 .controller.pcep.stats.rev141006.pcep.session.state.LocalPref localPref) {
143 final LocalPref local = new LocalPref();
144 local.setDeadtimer(localPref.getDeadtimer());
145 local.setIpAddress(localPref.getIpAddress());
146 local.setKeepalive(localPref.getKeepalive());
147 local.setSessionId(localPref.getSessionId());
151 private static PeerPref getPeerPref(final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
152 .controller.pcep.stats.rev141006.pcep.session.state.PeerPref peerPref) {
153 final PeerPref peer = new PeerPref();
154 peer.setDeadtimer(peerPref.getDeadtimer());
155 peer.setIpAddress(peerPref.getIpAddress());
156 peer.setKeepalive(peerPref.getKeepalive());
157 peer.setSessionId(peerPref.getSessionId());
161 private static Messages getMessageStats(final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
162 .controller.pcep.stats.rev141006.pcep.session.state.Messages messages) {
163 final LastReceivedError lastReceivedError = new LastReceivedError();
164 lastReceivedError.setErrorType(messages.getErrorMessages().getLastReceivedError().getErrorType());
165 lastReceivedError.setErrorValue(messages.getErrorMessages().getLastReceivedError().getErrorValue());
166 final LastSentError lastSentError = new LastSentError();
167 lastSentError.setErrorType(messages.getErrorMessages().getLastSentError().getErrorType());
168 lastSentError.setErrorValue(messages.getErrorMessages().getLastSentError().getErrorValue());
169 final ErrorMessages errMsgs = new ErrorMessages();
170 errMsgs.setLastReceivedError(lastReceivedError);
171 errMsgs.setLastSentError(lastSentError);
172 errMsgs.setReceivedErrorMsgCount(messages.getErrorMessages().getReceivedErrorMsgCount());
173 errMsgs.setSentErrorMsgCount(messages.getErrorMessages().getSentErrorMsgCount());
174 final Messages msgs = new Messages();
175 msgs.setErrorMessages(errMsgs);
176 msgs.setLastSentMsgTimestamp(messages.getLastSentMsgTimestamp());
177 msgs.setReceivedMsgCount(messages.getReceivedMsgCount());
178 msgs.setSentMsgCount(messages.getSentMsgCount());
179 msgs.setUnknownMsgReceived(msgs.getUnknownMsgReceived());