BUG-9079 Make PCEP session recoverable from exception
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PCEPSessionImplTest.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 org.junit.After;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.mockito.Matchers;
16 import org.mockito.Mockito;
17 import org.opendaylight.protocol.pcep.PCEPSession;
18 import org.opendaylight.protocol.pcep.TerminationReason;
19 import org.opendaylight.protocol.pcep.impl.spi.Util;
20 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.LocalPref;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.Messages;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.PeerPref;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.messages.ErrorMessages;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcerr;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcreq;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcreqBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.CloseMessage;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObject;
30
31 public class PCEPSessionImplTest extends AbstractPCEPSessionTest {
32
33     private PCEPSessionImpl session;
34
35     @Before
36     public void setup() {
37         this.session = new PCEPSessionImpl(this.listener, 0, this.channel, this.openMsg.getOpenMessage().getOpen(), this.openMsg.getOpenMessage().getOpen());
38         this.session.sessionUp();
39     }
40
41     @After
42     public void tearDown() {
43         this.session.close();
44     }
45
46     @Test
47     public void testPcepSessionImpl() throws InterruptedException {
48         Assert.assertTrue(this.listener.up);
49
50         this.session.handleMessage(this.kaMsg);
51         Assert.assertEquals(1, this.session.getMessages().getReceivedMsgCount().intValue());
52
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());
58
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();
64
65         this.session.resetStats();
66         Assert.assertEquals(0, this.session.getMessages().getReceivedMsgCount().longValue());
67     }
68
69     @Test
70     public void testAttemptSecondSession() {
71         this.session.handleMessage(this.openMsg);
72         Assert.assertEquals(1, this.session.getMessages().getReceivedMsgCount().intValue());
73         Assert.assertEquals(1, this.msgsSend.size());
74         Assert.assertTrue(this.msgsSend.get(0) instanceof Pcerr);
75         final Pcerr pcErr = (Pcerr) this.msgsSend.get(0);
76         final ErrorObject errorObj = pcErr.getPcerrMessage().getErrors().get(0).getErrorObject();
77         Assert.assertEquals(PCEPErrors.ATTEMPT_2ND_SESSION, PCEPErrors.forValue(errorObj.getType(), errorObj.getValue()));
78     }
79
80     @Test
81     public void testClosedByNode() {
82         this.session.handleMessage(this.closeMsg);
83         Mockito.verify(this.channel).close();
84     }
85
86     @Test
87     public void testCapabilityNotSupported() {
88         this.session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
89         Assert.assertEquals(2, this.msgsSend.size());
90         Assert.assertTrue(this.msgsSend.get(0) instanceof Pcerr);
91         final Pcerr pcErr = (Pcerr) this.msgsSend.get(0);
92         final ErrorObject errorObj = pcErr.getPcerrMessage().getErrors().get(0).getErrorObject();
93         Assert.assertEquals(PCEPErrors.CAPABILITY_NOT_SUPPORTED, PCEPErrors.forValue(errorObj.getType(), errorObj.getValue()));
94         Assert.assertEquals(1, this.session.getMessages().getUnknownMsgReceived().intValue());
95         // exceeded max. unknown messages count - terminate session
96         Assert.assertTrue(this.msgsSend.get(1) instanceof CloseMessage);
97         final CloseMessage closeMsg = (CloseMessage) this.msgsSend.get(1);
98         Assert.assertEquals(TerminationReason.TOO_MANY_UNKNOWN_MSGS, TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason()));
99         Mockito.verify(this.channel, Mockito.times(1)).close();
100     }
101
102     @Test
103     public void testEndoOfInput() {
104         Assert.assertTrue(this.listener.up);
105         this.session.endOfInput();
106         Assert.assertFalse(this.listener.up);
107     }
108
109     @Test
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, TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason()));
116         Mockito.verify(this.channel, Mockito.times(1)).close();
117     }
118
119     @Test
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().shortValue());
126         Assert.assertEquals(KEEP_ALIVE, peerPref.getKeepalive().shortValue());
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().shortValue());
131         Assert.assertEquals(KEEP_ALIVE, localPref.getKeepalive().shortValue());
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().shortValue());
141         Assert.assertEquals(PCEPErrors.LSP_RSVP_ERROR.getErrorValue(), errMsgs.getLastReceivedError().getErrorValue().shortValue());
142
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().shortValue());
152         Assert.assertEquals(PCEPErrors.UNKNOWN_PLSP_ID.getErrorValue(), errMsgs2.getLastSentError().getErrorValue().shortValue());
153     }
154
155     @Test
156     public void testExceptionCaught() throws Exception {
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());
162     }
163
164     @Test
165     public void testSessionRecoveryOnException() throws Exception {
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(Matchers.any());
170         Mockito.verify(this.session, Mockito.never()).sendMessage(Matchers.any());
171         Mockito.verify(this.session, Mockito.never()).closeChannel();
172         try {
173             this.session.sessionUp();
174             Assert.fail();  // expect the exception to be populated
175         } catch (final RuntimeException ignored) {}
176         Assert.assertFalse(this.listener.up);
177         Mockito.verify(this.session).handleException(Matchers.any());
178         Mockito.verify(this.session).sendMessage(Matchers.any(CloseMessage.class));
179         Mockito.verify(this.session).closeChannel();
180     }
181
182     private static class SimpleExceptionSessionListener extends SimpleSessionListener {
183         @Override
184         public synchronized void onSessionUp(final PCEPSession session) {
185             super.onSessionUp(session);
186             throw new RuntimeException("Mocked runtime exception.");
187         }
188     }
189 }