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
9 package org.opendaylight.protocol.pcep.impl;
11 import org.junit.After;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.mockito.Mockito;
16 import org.opendaylight.protocol.pcep.TerminationReason;
17 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.LocalPref;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.Messages;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.PeerPref;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.stats.rev141006.pcep.session.state.messages.ErrorMessages;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.CloseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcerr;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcreq;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcreqBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.CloseMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObject;
29 public class PCEPSessionImplTest extends AbstractPCEPSessionTest {
31 private PCEPSessionImpl session;
35 this.session = new PCEPSessionImpl(this.listener, 0, this.channel, this.openMsg.getOpenMessage().getOpen(), this.openMsg.getOpenMessage().getOpen());
36 this.session.sessionUp();
40 public void tearDown() {
45 public void testPcepSessionImpl() throws InterruptedException {
46 Assert.assertTrue(this.listener.up);
48 this.session.handleMessage(this.kaMsg);
49 Assert.assertEquals(1, this.session.getMessages().getReceivedMsgCount().intValue());
51 this.session.handleMessage(new PcreqBuilder().build());
52 Assert.assertEquals(2, this.session.getMessages().getReceivedMsgCount().intValue());
53 Assert.assertEquals(1, this.listener.messages.size());
54 Assert.assertTrue(this.listener.messages.get(0) instanceof Pcreq);
55 Assert.assertEquals(2, this.session.getMessages().getReceivedMsgCount().intValue());
57 this.session.handleMessage(new CloseBuilder().build());
58 Assert.assertEquals(3, this.session.getMessages().getReceivedMsgCount().intValue());
59 Assert.assertEquals(1, this.listener.messages.size());
60 Assert.assertTrue(this.channel.isActive());
61 Mockito.verify(this.channel, Mockito.times(1)).close();
63 this.session.resetStats();
64 Assert.assertEquals(0, this.session.getMessages().getReceivedMsgCount().longValue());
68 public void testAttemptSecondSession() {
69 this.session.handleMessage(this.openMsg);
70 Assert.assertEquals(1, this.session.getMessages().getReceivedMsgCount().intValue());
71 Assert.assertEquals(1, this.msgsSend.size());
72 Assert.assertTrue(this.msgsSend.get(0) instanceof Pcerr);
73 final Pcerr pcErr = (Pcerr) this.msgsSend.get(0);
74 final ErrorObject errorObj = pcErr.getPcerrMessage().getErrors().get(0).getErrorObject();
75 Assert.assertEquals(PCEPErrors.ATTEMPT_2ND_SESSION, PCEPErrors.forValue(errorObj.getType(), errorObj.getValue()));
79 public void testCapabilityNotSupported() {
80 this.session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
81 Assert.assertEquals(2, this.msgsSend.size());
82 Assert.assertTrue(this.msgsSend.get(0) instanceof Pcerr);
83 final Pcerr pcErr = (Pcerr) this.msgsSend.get(0);
84 final ErrorObject errorObj = pcErr.getPcerrMessage().getErrors().get(0).getErrorObject();
85 Assert.assertEquals(PCEPErrors.CAPABILITY_NOT_SUPPORTED, PCEPErrors.forValue(errorObj.getType(), errorObj.getValue()));
86 Assert.assertEquals(1, this.session.getMessages().getUnknownMsgReceived().intValue());
87 // exceeded max. unknown messages count - terminate session
88 Assert.assertTrue(this.msgsSend.get(1) instanceof CloseMessage);
89 final CloseMessage closeMsg = (CloseMessage) this.msgsSend.get(1);
90 Assert.assertEquals(TerminationReason.TOO_MANY_UNKNOWN_MSGS, TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason()));
91 Mockito.verify(this.channel, Mockito.times(1)).close();
95 public void testEndoOfInput() {
96 Assert.assertTrue(this.listener.up);
97 this.session.endOfInput();
98 Assert.assertFalse(this.listener.up);
102 public void testCloseSessionWithReason() {
103 this.session.close(TerminationReason.UNKNOWN);
104 Assert.assertEquals(1, this.msgsSend.size());
105 Assert.assertTrue(this.msgsSend.get(0) instanceof CloseMessage);
106 final CloseMessage closeMsg = (CloseMessage) this.msgsSend.get(0);
107 Assert.assertEquals(TerminationReason.UNKNOWN, TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason()));
108 Mockito.verify(this.channel, Mockito.times(1)).close();
112 public void testSessionStatistics() {
113 this.session.handleMessage(Util.createErrorMessage(PCEPErrors.LSP_RSVP_ERROR, null));
114 Assert.assertEquals(IP_ADDRESS, this.session.getPeerPref().getIpAddress());
115 final PeerPref peerPref = this.session.getPeerPref();
116 Assert.assertEquals(IP_ADDRESS, peerPref.getIpAddress());
117 Assert.assertEquals(DEADTIMER, peerPref.getDeadtimer().shortValue());
118 Assert.assertEquals(KEEP_ALIVE, peerPref.getKeepalive().shortValue());
119 Assert.assertEquals(0, peerPref.getSessionId().intValue());
120 final LocalPref localPref = this.session.getLocalPref();
121 Assert.assertEquals(IP_ADDRESS, localPref.getIpAddress());
122 Assert.assertEquals(DEADTIMER, localPref.getDeadtimer().shortValue());
123 Assert.assertEquals(KEEP_ALIVE, localPref.getKeepalive().shortValue());
124 Assert.assertEquals(0, localPref.getSessionId().intValue());
125 final Messages msgs = this.session.getMessages();
126 Assert.assertEquals(1, msgs.getReceivedMsgCount().longValue());
127 Assert.assertEquals(0, msgs.getSentMsgCount().longValue());
128 Assert.assertEquals(0, msgs.getUnknownMsgReceived().longValue());
129 final ErrorMessages errMsgs = msgs.getErrorMessages();
130 Assert.assertEquals(1, errMsgs.getReceivedErrorMsgCount().intValue());
131 Assert.assertEquals(0, errMsgs.getSentErrorMsgCount().intValue());
132 Assert.assertEquals(PCEPErrors.LSP_RSVP_ERROR.getErrorType(), errMsgs.getLastReceivedError().getErrorType().shortValue());
133 Assert.assertEquals(PCEPErrors.LSP_RSVP_ERROR.getErrorValue(), errMsgs.getLastReceivedError().getErrorValue().shortValue());
135 this.session.sendMessage(Util.createErrorMessage(PCEPErrors.UNKNOWN_PLSP_ID, null));
136 final Messages msgs2 = this.session.getMessages();
137 Assert.assertEquals(1, msgs2.getReceivedMsgCount().longValue());
138 Assert.assertEquals(1, msgs2.getSentMsgCount().longValue());
139 Assert.assertEquals(0, msgs2.getUnknownMsgReceived().longValue());
140 final ErrorMessages errMsgs2 = msgs2.getErrorMessages();
141 Assert.assertEquals(1, errMsgs2.getReceivedErrorMsgCount().intValue());
142 Assert.assertEquals(1, errMsgs2.getSentErrorMsgCount().intValue());
143 Assert.assertEquals(PCEPErrors.UNKNOWN_PLSP_ID.getErrorType(), errMsgs2.getLastSentError().getErrorType().shortValue());
144 Assert.assertEquals(PCEPErrors.UNKNOWN_PLSP_ID.getErrorValue(), errMsgs2.getLastSentError().getErrorValue().shortValue());