72edcd73bed67e63550ad69d600d8d324f4f2dd7
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / PCEPSessionState.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.protocol.pcep.impl;
10
11 import static java.util.Objects.requireNonNull;
12
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;
29
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;
43
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();
55     }
56
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();
68     }
69
70     public LocalPref getLocalPref() {
71         return this.localPref;
72     }
73
74     private static LocalPref getLocalPref(final Open open, final Channel channel) {
75         final LocalPrefBuilder peerBuilder = new LocalPrefBuilder();
76         peerBuilder.setDeadtimer(open.getDeadTimer());
77         peerBuilder.setKeepalive(open.getKeepalive());
78         peerBuilder.setIpAddress(((InetSocketAddress) channel.localAddress()).getAddress().getHostAddress());
79         peerBuilder.setSessionId(open.getSessionId().intValue());
80         return peerBuilder.build();
81     }
82
83     PeerPref getPeerPref() {
84         return this.peerPref;
85     }
86
87     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());
92     }
93
94     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());
99     }
100
101     void updateLastReceivedMsg() {
102         this.receivedMsgCount++;
103     }
104
105     void updateLastSentMsg() {
106         this.lastSentMsgTimestamp = StatisticsUtil.getCurrentTimestampInSeconds();
107         this.sentMsgCount++;
108     }
109
110     private static ErrorObject getErrorObject(final Message msg) {
111         requireNonNull(msg);
112         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message
113                 .PcerrMessage errMsg = ((PcerrMessage) msg).getPcerrMessage();
114         return errMsg.getErrors().get(errMsg.getErrors().size() - 1).getErrorObject();
115     }
116
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();
124     }
125
126     public Open getLocalOpen() {
127         return localOpen;
128     }
129 }