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.protocol.pcep.impl;
11 import static java.util.Objects.requireNonNull;
13 import io.netty.channel.Channel;
14 import java.net.InetSocketAddress;
15 import org.opendaylight.protocol.util.StatisticsUtil;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.error.messages.grouping.ErrorMessagesBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.error.messages.grouping.error.messages.LastReceivedErrorBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.error.messages.grouping.error.messages.LastSentErrorBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.pcep.session.state.LocalPref;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.pcep.session.state.LocalPrefBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.pcep.session.state.Messages;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.pcep.session.state.MessagesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.pcep.session.state.PeerPref;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.pcep.session.state.PeerPrefBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.PcerrMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.Open;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcep.error.object.ErrorObject;
30 final class PCEPSessionState {
31 private final Open localOpen;
32 private long sentMsgCount = 0;
33 private long receivedMsgCount = 0;
34 private long sentErrMsgCount = 0;
35 private long receivedErrMsgCount = 0;
36 private long lastSentMsgTimestamp = 0;
37 private final PeerPref peerPref;
38 private final LocalPref localPref;
39 private final LastReceivedErrorBuilder lastReceivedErrorBuilder;
40 private final LastSentErrorBuilder lastSentErrorBuilder;
41 private final ErrorMessagesBuilder errorsBuilder;
42 private final MessagesBuilder msgsBuilder;
44 PCEPSessionState(final Open remoteOpen, final Open localOpen, final Channel channel) {
45 requireNonNull(remoteOpen);
46 requireNonNull(localOpen);
47 requireNonNull(channel);
48 this.localOpen = localOpen;
49 this.peerPref = getRemotePref(remoteOpen, channel);
50 this.localPref = getLocalPref(localOpen, channel);
51 this.lastReceivedErrorBuilder = new LastReceivedErrorBuilder();
52 this.lastSentErrorBuilder = new LastSentErrorBuilder();
53 this.errorsBuilder = new ErrorMessagesBuilder();
54 this.msgsBuilder = new MessagesBuilder();
57 Messages getMessages(final int unknownMessagesCount) {
58 this.errorsBuilder.setReceivedErrorMsgCount(this.receivedErrMsgCount);
59 this.errorsBuilder.setSentErrorMsgCount(this.sentErrMsgCount);
60 this.errorsBuilder.setLastReceivedError(this.lastReceivedErrorBuilder.build());
61 this.errorsBuilder.setLastSentError(this.lastSentErrorBuilder.build());
62 this.msgsBuilder.setLastSentMsgTimestamp(this.lastSentMsgTimestamp);
63 this.msgsBuilder.setReceivedMsgCount(this.receivedMsgCount);
64 this.msgsBuilder.setSentMsgCount(this.sentMsgCount);
65 this.msgsBuilder.setUnknownMsgReceived(unknownMessagesCount);
66 this.msgsBuilder.setErrorMessages(this.errorsBuilder.build());
67 return this.msgsBuilder.build();
70 public LocalPref getLocalPref() {
71 return this.localPref;
74 PeerPref getPeerPref() {
78 void setLastSentError(final Message msg) {
79 this.sentErrMsgCount++;
80 final ErrorObject errObj = getErrorObject(msg);
81 this.lastSentErrorBuilder.setErrorType(errObj.getType());
82 this.lastSentErrorBuilder.setErrorValue(errObj.getValue());
85 void setLastReceivedError(final Message msg) {
86 final ErrorObject errObj = getErrorObject(msg);
87 this.receivedErrMsgCount++;
88 this.lastReceivedErrorBuilder.setErrorType(errObj.getType());
89 this.lastReceivedErrorBuilder.setErrorValue(errObj.getValue());
92 void updateLastReceivedMsg() {
93 this.receivedMsgCount++;
96 void updateLastSentMsg() {
97 this.lastSentMsgTimestamp = StatisticsUtil.getCurrentTimestampInSeconds();
101 private static ErrorObject getErrorObject(final Message msg) {
103 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message
104 .PcerrMessage errMsg = ((PcerrMessage) msg).getPcerrMessage();
105 return errMsg.getErrors().get(errMsg.getErrors().size() - 1).getErrorObject();
108 private static PeerPref getRemotePref(final Open open, final Channel channel) {
109 final PeerPrefBuilder peerBuilder = new PeerPrefBuilder();
110 peerBuilder.setDeadtimer(open.getDeadTimer());
111 peerBuilder.setKeepalive(open.getKeepalive());
112 peerBuilder.setIpAddress(((InetSocketAddress) channel.remoteAddress()).getAddress().getHostAddress());
113 peerBuilder.setSessionId(open.getSessionId().intValue());
114 return peerBuilder.build();
117 private static LocalPref getLocalPref(final Open open, final Channel channel) {
118 final LocalPrefBuilder peerBuilder = new LocalPrefBuilder();
119 peerBuilder.setDeadtimer(open.getDeadTimer());
120 peerBuilder.setKeepalive(open.getKeepalive());
121 peerBuilder.setIpAddress(((InetSocketAddress) channel.localAddress()).getAddress().getHostAddress());
122 peerBuilder.setSessionId(open.getSessionId().intValue());
123 return peerBuilder.build();
126 public Open getLocalOpen() {