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
8 package org.opendaylight.protocol.pcep.impl;
10 import org.junit.After;
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.mockito.ArgumentMatchers;
15 import org.mockito.Mockito;
16 import org.opendaylight.protocol.pcep.PCEPSession;
17 import org.opendaylight.protocol.pcep.TerminationReason;
18 import org.opendaylight.protocol.pcep.impl.spi.Util;
19 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Pcerr;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Pcreq;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.PcreqBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.error.messages.grouping.ErrorMessages;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.pcep.session.state.LocalPref;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.pcep.session.state.Messages;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.pcep.session.state.PeerPref;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.CloseMessage;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcep.error.object.ErrorObject;
30 public class PCEPSessionImplTest extends AbstractPCEPSessionTest {
32 private PCEPSessionImpl session;
36 this.session = new PCEPSessionImpl(this.listener, 0, this.channel, this.openMsg.getOpenMessage().getOpen(),
37 this.openMsg.getOpenMessage().getOpen());
38 this.session.sessionUp();
42 public void tearDown() {
47 public void testPcepSessionImpl() {
48 Assert.assertTrue(this.listener.up);
50 this.session.handleMessage(this.kaMsg);
51 Assert.assertEquals(1, this.session.getMessages().getReceivedMsgCount().intValue());
53 this.session.handleMessage(new PcreqBuilder().build());
54 Assert.assertEquals(2, this.session.getMessages().getReceivedMsgCount().intValue());
55 Assert.assertEquals(1, this.listener.messages.size());
56 Assert.assertTrue(this.listener.messages.get(0) instanceof Pcreq);
57 Assert.assertEquals(2, this.session.getMessages().getReceivedMsgCount().intValue());
59 this.session.handleMessage(this.closeMsg);
60 Assert.assertEquals(3, this.session.getMessages().getReceivedMsgCount().intValue());
61 Assert.assertEquals(1, this.listener.messages.size());
62 Assert.assertTrue(this.channel.isActive());
63 Mockito.verify(this.channel, Mockito.times(1)).close();
67 public void testAttemptSecondSession() {
68 this.session.handleMessage(this.openMsg);
69 Assert.assertEquals(1, this.session.getMessages().getReceivedMsgCount().intValue());
70 Assert.assertEquals(1, this.msgsSend.size());
71 Assert.assertTrue(this.msgsSend.get(0) instanceof Pcerr);
72 final Pcerr pcErr = (Pcerr) this.msgsSend.get(0);
73 final ErrorObject errorObj = pcErr.getPcerrMessage().getErrors().get(0).getErrorObject();
75 PCEPErrors.ATTEMPT_2ND_SESSION, PCEPErrors.forValue(errorObj.getType(), errorObj.getValue()));
79 public void testClosedByNode() {
80 this.session.handleMessage(this.closeMsg);
81 Mockito.verify(this.channel).close();
85 public void testCapabilityNotSupported() {
86 this.session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
87 Assert.assertEquals(2, this.msgsSend.size());
88 Assert.assertTrue(this.msgsSend.get(0) instanceof Pcerr);
89 final Pcerr pcErr = (Pcerr) this.msgsSend.get(0);
90 final ErrorObject errorObj = pcErr.getPcerrMessage().getErrors().get(0).getErrorObject();
92 PCEPErrors.CAPABILITY_NOT_SUPPORTED, PCEPErrors.forValue(errorObj.getType(), errorObj.getValue()));
93 Assert.assertEquals(1, this.session.getMessages().getUnknownMsgReceived().intValue());
94 // exceeded max. unknown messages count - terminate session
95 Assert.assertTrue(this.msgsSend.get(1) instanceof CloseMessage);
96 final CloseMessage closeMsg = (CloseMessage) this.msgsSend.get(1);
97 Assert.assertEquals(TerminationReason.TOO_MANY_UNKNOWN_MSGS,
98 TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason().toJava()));
99 Mockito.verify(this.channel, Mockito.times(1)).close();
103 public void testEndoOfInput() {
104 Assert.assertTrue(this.listener.up);
105 this.session.endOfInput();
106 Assert.assertFalse(this.listener.up);
110 public void testCloseSessionWithReason() {
111 this.session.close(TerminationReason.UNKNOWN);
112 Assert.assertEquals(1, this.msgsSend.size());
113 Assert.assertTrue(this.msgsSend.get(0) instanceof CloseMessage);
114 final CloseMessage closeMsg = (CloseMessage) this.msgsSend.get(0);
115 Assert.assertEquals(TerminationReason.UNKNOWN,
116 TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason().toJava()));
117 Mockito.verify(this.channel, Mockito.times(1)).close();
121 public void testSessionStatistics() {
122 this.session.handleMessage(Util.createErrorMessage(PCEPErrors.LSP_RSVP_ERROR, null));
123 Assert.assertEquals(this.ipAddress, this.session.getPeerPref().getIpAddress());
124 final PeerPref peerPref = this.session.getPeerPref();
125 Assert.assertEquals(this.ipAddress, peerPref.getIpAddress());
126 Assert.assertEquals(DEADTIMER, peerPref.getDeadtimer());
127 Assert.assertEquals(KEEP_ALIVE, peerPref.getKeepalive());
128 Assert.assertEquals(0, peerPref.getSessionId().intValue());
129 final LocalPref localPref = this.session.getLocalPref();
130 Assert.assertEquals(this.ipAddress, localPref.getIpAddress());
131 Assert.assertEquals(DEADTIMER, localPref.getDeadtimer());
132 Assert.assertEquals(KEEP_ALIVE, localPref.getKeepalive());
133 Assert.assertEquals(0, localPref.getSessionId().intValue());
134 final Messages msgs = this.session.getMessages();
135 Assert.assertEquals(1, msgs.getReceivedMsgCount().longValue());
136 Assert.assertEquals(0, msgs.getSentMsgCount().longValue());
137 Assert.assertEquals(0, msgs.getUnknownMsgReceived().longValue());
138 final ErrorMessages errMsgs = msgs.getErrorMessages();
139 Assert.assertEquals(1, errMsgs.getReceivedErrorMsgCount().intValue());
140 Assert.assertEquals(0, errMsgs.getSentErrorMsgCount().intValue());
141 Assert.assertEquals(PCEPErrors.LSP_RSVP_ERROR.getErrorType(), errMsgs.getLastReceivedError().getErrorType());
142 Assert.assertEquals(PCEPErrors.LSP_RSVP_ERROR.getErrorValue(), errMsgs.getLastReceivedError().getErrorValue());
144 this.session.sendMessage(Util.createErrorMessage(PCEPErrors.UNKNOWN_PLSP_ID, null));
145 final Messages msgs2 = this.session.getMessages();
146 Assert.assertEquals(1, msgs2.getReceivedMsgCount().longValue());
147 Assert.assertEquals(1, msgs2.getSentMsgCount().longValue());
148 Assert.assertEquals(0, msgs2.getUnknownMsgReceived().longValue());
149 final ErrorMessages errMsgs2 = msgs2.getErrorMessages();
150 Assert.assertEquals(1, errMsgs2.getReceivedErrorMsgCount().intValue());
151 Assert.assertEquals(1, errMsgs2.getSentErrorMsgCount().intValue());
152 Assert.assertEquals(PCEPErrors.UNKNOWN_PLSP_ID.getErrorType(), errMsgs2.getLastSentError().getErrorType());
153 Assert.assertEquals(PCEPErrors.UNKNOWN_PLSP_ID.getErrorValue(), errMsgs2.getLastSentError().getErrorValue());
157 public void testExceptionCaught() {
158 Assert.assertFalse(this.session.isClosed());
159 Assert.assertTrue(this.listener.up);
160 this.session.exceptionCaught(null, new Throwable("PCEP exception."));
161 Assert.assertFalse(this.listener.up);
162 Assert.assertTrue(this.session.isClosed());
166 @SuppressWarnings({"checkstyle:IllegalCatch","checkstyle:EmptyBlock"})
167 public void testSessionRecoveryOnException() {
168 this.listener = new SimpleExceptionSessionListener();
169 this.session = Mockito.spy(new PCEPSessionImpl(this.listener, 0, this.channel,
170 this.openMsg.getOpenMessage().getOpen(), this.openMsg.getOpenMessage().getOpen()));
171 Mockito.verify(this.session, Mockito.never()).handleException(ArgumentMatchers.any());
172 Mockito.verify(this.session, Mockito.never()).sendMessage(ArgumentMatchers.any());
173 Mockito.verify(this.session, Mockito.never()).closeChannel();
175 this.session.sessionUp();
176 Assert.fail(); // expect the exception to be populated
177 } catch (final RuntimeException ignored) {
179 Assert.assertFalse(this.listener.up);
180 Mockito.verify(this.session).handleException(ArgumentMatchers.any());
181 Mockito.verify(this.session).sendMessage(ArgumentMatchers.any(CloseMessage.class));
182 Mockito.verify(this.session).closeChannel();
185 private static class SimpleExceptionSessionListener extends SimpleSessionListener {
187 public synchronized void onSessionUp(final PCEPSession session) {
188 super.onSessionUp(session);
189 throw new RuntimeException("Mocked runtime exception.");