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