2 * Copyright (c) 2013 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
8 package org.opendaylight.protocol.pcep.impl;
10 import io.netty.channel.Channel;
11 import io.netty.util.Timeout;
12 import io.netty.util.Timer;
13 import io.netty.util.TimerTask;
15 import java.io.IOException;
16 import java.net.InetSocketAddress;
17 import java.util.ArrayList;
18 import java.util.Date;
19 import java.util.LinkedList;
20 import java.util.List;
21 import java.util.Queue;
22 import java.util.concurrent.TimeUnit;
24 import org.opendaylight.protocol.framework.AbstractProtocolSession;
25 import org.opendaylight.protocol.pcep.PCEPCloseTermination;
26 import org.opendaylight.protocol.pcep.PCEPErrors;
27 import org.opendaylight.protocol.pcep.PCEPMessage;
28 import org.opendaylight.protocol.pcep.PCEPSession;
29 import org.opendaylight.protocol.pcep.PCEPSessionListener;
30 import org.opendaylight.protocol.pcep.PCEPTlv;
31 import org.opendaylight.protocol.pcep.message.PCEPCloseMessage;
32 import org.opendaylight.protocol.pcep.message.PCEPErrorMessage;
33 import org.opendaylight.protocol.pcep.message.PCEPKeepAliveMessage;
34 import org.opendaylight.protocol.pcep.message.PCEPOpenMessage;
35 import org.opendaylight.protocol.pcep.object.PCEPCloseObject;
36 import org.opendaylight.protocol.pcep.object.PCEPCloseObject.Reason;
37 import org.opendaylight.protocol.pcep.object.PCEPErrorObject;
38 import org.opendaylight.protocol.pcep.object.PCEPOpenObject;
39 import org.opendaylight.protocol.pcep.tlv.NodeIdentifierTlv;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.google.common.annotations.VisibleForTesting;
44 import com.google.common.base.Objects;
45 import com.google.common.base.Objects.ToStringHelper;
46 import com.google.common.base.Preconditions;
49 * Implementation of PCEPSession. (Not final for testing.)
51 class PCEPSessionImpl extends AbstractProtocolSession<PCEPMessage> implements PCEPSession, PCEPSessionRuntimeMXBean {
53 * System.nanoTime value about when was sent the last message Protected to be updated also in tests.
55 protected volatile long lastMessageSentAt;
58 * System.nanoTime value about when was received the last message
60 private long lastMessageReceivedAt;
63 * Protected for testing.
65 protected int maxUnknownMessages;
67 protected final Queue<Long> unknownMessagesTimes = new LinkedList<Long>();
69 private final PCEPSessionListener listener;
72 * Open Object with session characteristics that were accepted by another PCE (sent from this session).
74 private final PCEPOpenObject localOpen;
77 * Open Object with session characteristics for this session (sent from another PCE).
79 private final PCEPOpenObject remoteOpen;
81 private static final Logger logger = LoggerFactory.getLogger(PCEPSessionImpl.class);
84 * Timer object grouping FSM Timers
86 private final Timer stateTimer;
88 private int sentMsgCount = 0;
90 private int receivedMsgCount = 0;
92 // True if the listener should not be notified about events
93 private boolean closed = false;
95 private final Channel channel;
97 PCEPSessionImpl(final Timer timer, final PCEPSessionListener listener, final int maxUnknownMessages,
98 final Channel channel, final PCEPOpenObject localOpen, final PCEPOpenObject remoteOpen) {
99 this.listener = Preconditions.checkNotNull(listener);
100 this.stateTimer = Preconditions.checkNotNull(timer);
101 this.channel = Preconditions.checkNotNull(channel);
102 this.localOpen = Preconditions.checkNotNull(localOpen);
103 this.remoteOpen = Preconditions.checkNotNull(remoteOpen);
104 this.lastMessageReceivedAt = System.nanoTime();
106 if (this.maxUnknownMessages != 0) {
107 this.maxUnknownMessages = maxUnknownMessages;
110 if (getDeadTimerValue() != 0) {
111 stateTimer.newTimeout(new TimerTask() {
113 public void run(final Timeout timeout) throws Exception {
116 }, getDeadTimerValue(), TimeUnit.SECONDS);
119 if (getKeepAliveTimerValue() != 0) {
120 stateTimer.newTimeout(new TimerTask() {
122 public void run(final Timeout timeout) throws Exception {
123 handleKeepaliveTimer();
125 }, getKeepAliveTimerValue(), TimeUnit.SECONDS);
128 logger.debug("Session started.");
132 * If DeadTimer expires, the session ends. If a message (whichever) was received during this period, the DeadTimer
133 * will be rescheduled by DEAD_TIMER_VALUE + the time that has passed from the start of the DeadTimer to the time at
134 * which the message was received. If the session was closed by the time this method starts to execute (the session
135 * state will become IDLE), that rescheduling won't occur.
137 private synchronized void handleDeadTimer() {
138 final long ct = System.nanoTime();
140 final long nextDead = this.lastMessageReceivedAt + TimeUnit.SECONDS.toNanos(getDeadTimerValue());
142 if (this.channel.isActive()) {
143 if (ct >= nextDead) {
144 logger.debug("DeadTimer expired. " + new Date());
145 this.terminate(Reason.EXP_DEADTIMER);
147 stateTimer.newTimeout(new TimerTask() {
149 public void run(final Timeout timeout) throws Exception {
152 }, nextDead - ct, TimeUnit.NANOSECONDS);
158 * If KeepAlive Timer expires, sends KeepAlive message. If a message (whichever) was send during this period, the
159 * KeepAlive Timer will be rescheduled by KEEP_ALIVE_TIMER_VALUE + the time that has passed from the start of the
160 * KeepAlive timer to the time at which the message was sent. If the session was closed by the time this method
161 * starts to execute (the session state will become IDLE), that rescheduling won't occur.
163 private synchronized void handleKeepaliveTimer() {
164 final long ct = System.nanoTime();
166 long nextKeepalive = this.lastMessageSentAt + TimeUnit.SECONDS.toNanos(getKeepAliveTimerValue());
168 if (channel.isActive()) {
169 if (ct >= nextKeepalive) {
170 this.sendMessage(new PCEPKeepAliveMessage());
171 nextKeepalive = this.lastMessageSentAt + TimeUnit.SECONDS.toNanos(getKeepAliveTimerValue());
174 this.stateTimer.newTimeout(new TimerTask() {
176 public void run(final Timeout timeout) throws Exception {
177 handleKeepaliveTimer();
179 }, nextKeepalive - ct, TimeUnit.NANOSECONDS);
184 * Sends message to serialization.
186 * @param msg to be sent
189 public void sendMessage(final PCEPMessage msg) {
191 this.channel.writeAndFlush(msg);
192 this.lastMessageSentAt = System.nanoTime();
193 if (!(msg instanceof PCEPKeepAliveMessage)) {
194 logger.debug("Sent message: " + msg);
197 } catch (final Exception e) {
198 logger.warn("Message {} was not sent.", msg, e);
203 * Closes PCEP session without sending a Close message, as the channel is no longer active.
206 public void close() {
207 logger.trace("Closing session: {}", this);
208 this.channel.close();
212 * Closes PCEP session, cancels all timers, returns to state Idle, sends the Close Message. KeepAlive and DeadTimer
213 * are cancelled if the state of the session changes to IDLE. This method is used to close the PCEP session from
214 * inside the session or from the listener, therefore the parent of this session should be informed.
217 public synchronized void close(final PCEPCloseObject.Reason reason) {
218 logger.debug("Closing session: {}", this);
220 this.sendMessage(new PCEPCloseMessage(new PCEPCloseObject(reason)));
221 this.channel.close();
224 private synchronized void terminate(final PCEPCloseObject.Reason reason) {
225 this.listener.onSessionTerminated(this, new PCEPCloseTermination(reason));
227 this.sendMessage(new PCEPCloseMessage(new PCEPCloseObject(reason)));
232 public synchronized void endOfInput() {
234 this.listener.onSessionDown(this, new IOException("End of input detected. Close the session."));
239 private void sendErrorMessage(final PCEPErrors value) {
240 this.sendErrorMessage(value, null);
244 * Sends PCEP Error Message with one PCEPError and Open Object.
249 private void sendErrorMessage(final PCEPErrors value, final PCEPOpenObject open) {
250 final PCEPErrorObject error = new PCEPErrorObject(value);
251 final List<PCEPErrorObject> errors = new ArrayList<PCEPErrorObject>();
253 this.sendMessage(new PCEPErrorMessage(open, errors, null));
257 * The fact, that a message is malformed, comes from parser. In case of unrecognized message a particular error is
258 * sent (CAPABILITY_NOT_SUPPORTED) and the method checks if the MAX_UNKNOWN_MSG per minute wasn't overstepped.
259 * Second, any other error occurred that is specified by rfc. In this case, the an error message is generated and
262 * @param error documented error in RFC5440 or draft
265 public void handleMalformedMessage(final PCEPErrors error) {
266 final long ct = System.nanoTime();
267 this.sendErrorMessage(error);
268 if (error == PCEPErrors.CAPABILITY_NOT_SUPPORTED) {
269 this.unknownMessagesTimes.add(ct);
270 while (ct - this.unknownMessagesTimes.peek() > 60 * 1E9) {
271 this.unknownMessagesTimes.poll();
273 if (this.unknownMessagesTimes.size() > this.maxUnknownMessages) {
274 this.terminate(Reason.TOO_MANY_UNKNOWN_MSG);
280 * Handles incoming message. If the session is up, it notifies the user. The user is notified about every message
283 * @param msg incoming message
286 public void handleMessage(final PCEPMessage msg) {
287 // Update last reception time
288 this.lastMessageReceivedAt = System.nanoTime();
289 this.receivedMsgCount++;
291 // Internal message handling. The user does not see these messages
292 if (msg instanceof PCEPKeepAliveMessage) {
293 // Do nothing, the timer has been already reset
294 } else if (msg instanceof PCEPOpenMessage) {
295 this.sendErrorMessage(PCEPErrors.ATTEMPT_2ND_SESSION);
296 } else if (msg instanceof PCEPCloseMessage) {
298 * Session is up, we are reporting all messages to user. One notable
299 * exception is CLOSE message, which needs to be converted into a
300 * session DOWN event.
304 // This message needs to be handled by the user
305 this.listener.onMessage(this, msg);
310 * @return the sentMsgCount
314 public Integer getSentMsgCount() {
315 return this.sentMsgCount;
319 * @return the receivedMsgCount
323 public Integer getReceivedMsgCount() {
324 return this.receivedMsgCount;
328 public Integer getDeadTimerValue() {
329 return remoteOpen.getDeadTimerValue();
333 public Integer getKeepAliveTimerValue() {
334 return localOpen.getKeepAliveTimerValue();
338 public String getPeerAddress() {
339 InetSocketAddress a = (InetSocketAddress) channel.remoteAddress();
340 return a.getHostName();
344 public void tearDown() {
349 public String getNodeIdentifier() {
350 for (PCEPTlv tlv : this.remoteOpen.getTlvs()) {
351 if (tlv instanceof NodeIdentifierTlv) {
352 return tlv.toString();
359 public final String toString() {
360 return addToStringAttributes(Objects.toStringHelper(this)).toString();
363 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
364 toStringHelper.add("localOpen", localOpen);
365 toStringHelper.add("remoteOpen", remoteOpen);
366 return toStringHelper;
370 protected void sessionUp() {
371 listener.onSessionUp(this);