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();
74 Assert.assertEquals(PCEPErrors.ATTEMPT_2ND_SESSION, PCEPErrors.forValue(errorObj.getType(), errorObj.getValue()));
78 public void testClosedByNode() {
79 this.session.handleMessage(this.closeMsg);
80 Mockito.verify(this.channel).close();
84 public void testCapabilityNotSupported() {
85 this.session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
86 Assert.assertEquals(2, this.msgsSend.size());
87 Assert.assertTrue(this.msgsSend.get(0) instanceof Pcerr);
88 final Pcerr pcErr = (Pcerr) this.msgsSend.get(0);
89 final ErrorObject errorObj = pcErr.getPcerrMessage().getErrors().get(0).getErrorObject();
91 PCEPErrors.CAPABILITY_NOT_SUPPORTED, PCEPErrors.forValue(errorObj.getType(), errorObj.getValue()));
92 Assert.assertEquals(1, this.session.getMessages().getUnknownMsgReceived().intValue());
93 // exceeded max. unknown messages count - terminate session
94 Assert.assertTrue(this.msgsSend.get(1) instanceof CloseMessage);
95 final CloseMessage closeMsg = (CloseMessage) this.msgsSend.get(1);
96 Assert.assertEquals(TerminationReason.TOO_MANY_UNKNOWN_MSGS,
97 TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason().toJava()));
98 Mockito.verify(this.channel, Mockito.times(1)).close();
102 public void testEndoOfInput() {
103 Assert.assertTrue(this.listener.up);
104 this.session.endOfInput();
105 Assert.assertFalse(this.listener.up);
109 public void testCloseSessionWithReason() {
110 this.session.close(TerminationReason.UNKNOWN);
111 Assert.assertEquals(1, this.msgsSend.size());
112 Assert.assertTrue(this.msgsSend.get(0) instanceof CloseMessage);
113 final CloseMessage closeMsg = (CloseMessage) this.msgsSend.get(0);
114 Assert.assertEquals(TerminationReason.UNKNOWN,
115 TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason().toJava()));
116 Mockito.verify(this.channel, Mockito.times(1)).close();
120 public void testSessionStatistics() {
121 this.session.handleMessage(Util.createErrorMessage(PCEPErrors.LSP_RSVP_ERROR, null));
122 Assert.assertEquals(this.ipAddress, this.session.getPeerPref().getIpAddress());
123 final PeerPref peerPref = this.session.getPeerPref();
124 Assert.assertEquals(this.ipAddress, peerPref.getIpAddress());
125 Assert.assertEquals(DEADTIMER, peerPref.getDeadtimer());
126 Assert.assertEquals(KEEP_ALIVE, peerPref.getKeepalive());
127 Assert.assertEquals(0, peerPref.getSessionId().intValue());
128 final LocalPref localPref = this.session.getLocalPref();
129 Assert.assertEquals(this.ipAddress, localPref.getIpAddress());
130 Assert.assertEquals(DEADTIMER, localPref.getDeadtimer());
131 Assert.assertEquals(KEEP_ALIVE, localPref.getKeepalive());
132 Assert.assertEquals(0, localPref.getSessionId().intValue());
133 final Messages msgs = this.session.getMessages();
134 Assert.assertEquals(1, msgs.getReceivedMsgCount().longValue());
135 Assert.assertEquals(0, msgs.getSentMsgCount().longValue());
136 Assert.assertEquals(0, msgs.getUnknownMsgReceived().longValue());
137 final ErrorMessages errMsgs = msgs.getErrorMessages();
138 Assert.assertEquals(1, errMsgs.getReceivedErrorMsgCount().intValue());
139 Assert.assertEquals(0, errMsgs.getSentErrorMsgCount().intValue());
140 Assert.assertEquals(PCEPErrors.LSP_RSVP_ERROR.getErrorType(), errMsgs.getLastReceivedError().getErrorType());
141 Assert.assertEquals(PCEPErrors.LSP_RSVP_ERROR.getErrorValue(), errMsgs.getLastReceivedError().getErrorValue());
143 this.session.sendMessage(Util.createErrorMessage(PCEPErrors.UNKNOWN_PLSP_ID, null));
144 final Messages msgs2 = this.session.getMessages();
145 Assert.assertEquals(1, msgs2.getReceivedMsgCount().longValue());
146 Assert.assertEquals(1, msgs2.getSentMsgCount().longValue());
147 Assert.assertEquals(0, msgs2.getUnknownMsgReceived().longValue());
148 final ErrorMessages errMsgs2 = msgs2.getErrorMessages();
149 Assert.assertEquals(1, errMsgs2.getReceivedErrorMsgCount().intValue());
150 Assert.assertEquals(1, errMsgs2.getSentErrorMsgCount().intValue());
151 Assert.assertEquals(PCEPErrors.UNKNOWN_PLSP_ID.getErrorType(), errMsgs2.getLastSentError().getErrorType());
152 Assert.assertEquals(PCEPErrors.UNKNOWN_PLSP_ID.getErrorValue(), errMsgs2.getLastSentError().getErrorValue());
156 public void testExceptionCaught() {
157 Assert.assertFalse(this.session.isClosed());
158 Assert.assertTrue(this.listener.up);
159 this.session.exceptionCaught(null, new Throwable("PCEP exception."));
160 Assert.assertFalse(this.listener.up);
161 Assert.assertTrue(this.session.isClosed());
165 public void testSessionRecoveryOnException() {
166 this.listener = new SimpleExceptionSessionListener();
167 this.session = Mockito.spy(new PCEPSessionImpl(this.listener, 0, this.channel,
168 this.openMsg.getOpenMessage().getOpen(), this.openMsg.getOpenMessage().getOpen()));
169 Mockito.verify(this.session, Mockito.never()).handleException(ArgumentMatchers.any());
170 Mockito.verify(this.session, Mockito.never()).sendMessage(ArgumentMatchers.any());
171 Mockito.verify(this.session, Mockito.never()).closeChannel();
173 this.session.sessionUp();
174 Assert.fail(); // expect the exception to be populated
175 } catch (final RuntimeException ignored) {
177 Assert.assertFalse(this.listener.up);
178 Mockito.verify(this.session).handleException(ArgumentMatchers.any());
179 Mockito.verify(this.session).sendMessage(ArgumentMatchers.any(CloseMessage.class));
180 Mockito.verify(this.session).closeChannel();
183 private static class SimpleExceptionSessionListener extends SimpleSessionListener {
185 public synchronized void onSessionUp(final PCEPSession session) {
186 super.onSessionUp(session);
187 throw new RuntimeException("Mocked runtime exception.");