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 com.google.common.base.Preconditions;
12 import io.netty.channel.Channel;
13 import java.net.InetSocketAddress;
14 import java.util.concurrent.TimeUnit;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.LocalPref;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.LocalPrefBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.Messages;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.MessagesBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.PeerPref;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.PeerPrefBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.messages.ErrorMessagesBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.messages.error.messages.LastReceivedErrorBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.messages.error.messages.LastSentErrorBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PcerrMessage;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObject;
29 final class PCEPSessionState {
30 private long sentMsgCount = 0;
31 private long receivedMsgCount = 0;
32 private long sentErrMsgCount = 0;
33 private long receivedErrMsgCount = 0;
34 private long lastSentMsgTimestamp = 0;
35 private final PeerPref peerPref;
36 private final LocalPref localPref;
37 private final LastReceivedErrorBuilder lastReceivedErrorBuilder;
38 private final LastSentErrorBuilder lastSentErrorBuilder;
39 private final ErrorMessagesBuilder errorsBuilder;
40 private final MessagesBuilder msgsBuilder;
42 public PCEPSessionState(final Open remoteOpen, final Open localOpen, final Channel channel) {
43 Preconditions.checkNotNull(remoteOpen);
44 Preconditions.checkNotNull(localOpen);
45 Preconditions.checkNotNull(channel);
46 this.peerPref = getRemotePref(remoteOpen, channel);
47 this.localPref = getLocalPref(localOpen, channel);
48 this.lastReceivedErrorBuilder = new LastReceivedErrorBuilder();
49 this.lastSentErrorBuilder = new LastSentErrorBuilder();
50 this.errorsBuilder = new ErrorMessagesBuilder();
51 this.msgsBuilder = new MessagesBuilder();
54 public Messages getMessages(final int unknownMessagesCount) {
55 this.errorsBuilder.setReceivedErrorMsgCount(this.receivedErrMsgCount);
56 this.errorsBuilder.setSentErrorMsgCount(this.sentErrMsgCount);
57 this.errorsBuilder.setLastReceivedError(this.lastReceivedErrorBuilder.build());
58 this.errorsBuilder.setLastSentError(this.lastSentErrorBuilder.build());
59 this.msgsBuilder.setLastSentMsgTimestamp(TimeUnit.MILLISECONDS.toSeconds(this.lastSentMsgTimestamp));
60 this.msgsBuilder.setReceivedMsgCount(this.receivedMsgCount);
61 this.msgsBuilder.setSentMsgCount(this.sentMsgCount);
62 this.msgsBuilder.setUnknownMsgReceived(unknownMessagesCount);
63 this.msgsBuilder.setErrorMessages(this.errorsBuilder.build());
64 return this.msgsBuilder.build();
68 this.receivedMsgCount = 0;
69 this.sentMsgCount = 0;
70 this.receivedErrMsgCount = 0;
71 this.sentErrMsgCount = 0;
72 this.lastSentMsgTimestamp = 0;
73 this.lastReceivedErrorBuilder.setErrorType((short) 0);
74 this.lastReceivedErrorBuilder.setErrorValue((short) 0);
75 this.lastSentErrorBuilder.setErrorType((short) 0);
76 this.lastSentErrorBuilder.setErrorValue((short) 0);
79 public LocalPref getLocalPref() {
80 return this.localPref;
83 public PeerPref getPeerPref() {
87 public void setLastSentError(final Message msg) {
88 this.sentErrMsgCount++;
89 final ErrorObject errObj = getErrorObject(msg);
90 this.lastSentErrorBuilder.setErrorType(errObj.getType());
91 this.lastSentErrorBuilder.setErrorValue(errObj.getValue());
94 public void setLastReceivedError(final Message msg) {
95 final ErrorObject errObj = getErrorObject(msg);
96 this.receivedErrMsgCount++;
97 this.lastReceivedErrorBuilder.setErrorType(errObj.getType());
98 this.lastReceivedErrorBuilder.setErrorValue(errObj.getValue());
101 public void updateLastReceivedMsg() {
102 this.receivedMsgCount++;
105 public void updateLastSentMsg() {
106 this.lastSentMsgTimestamp = System.currentTimeMillis();
110 private static ErrorObject getErrorObject(final Message msg) {
111 Preconditions.checkNotNull(msg);
112 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessage errMsg =
113 ((PcerrMessage) msg).getPcerrMessage();
114 return errMsg.getErrors().get(errMsg.getErrors().size() - 1).getErrorObject();
117 private static PeerPref getRemotePref(final Open open, final Channel channel) {
118 final PeerPrefBuilder peerBuilder = new PeerPrefBuilder();
119 peerBuilder.setDeadtimer(open.getDeadTimer());
120 peerBuilder.setKeepalive(open.getKeepalive());
121 peerBuilder.setIpAddress(((InetSocketAddress) channel.remoteAddress()).getAddress().getHostAddress());
122 peerBuilder.setSessionId(open.getSessionId().intValue());
123 return peerBuilder.build();
126 private static LocalPref getLocalPref(final Open open, final Channel channel) {
127 final LocalPrefBuilder peerBuilder = new LocalPrefBuilder();
128 peerBuilder.setDeadtimer(open.getDeadTimer());
129 peerBuilder.setKeepalive(open.getKeepalive());
130 peerBuilder.setIpAddress(((InetSocketAddress) channel.localAddress()).getAddress().getHostAddress());
131 peerBuilder.setSessionId(open.getSessionId().intValue());
132 return peerBuilder.build();