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