033324a078fdb2079efa2116b8e2bac8aa6152b4
[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.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.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;
28
29 public class PCEPSessionImplTest extends AbstractPCEPSessionTest {
30
31     private PCEPSessionImpl session;
32
33     @Before
34     public void setup() {
35         this.session = new PCEPSessionImpl(this.listener, 0, this.channel, this.openMsg.getOpenMessage().getOpen(), this.openMsg.getOpenMessage().getOpen());
36         this.session.sessionUp();
37     }
38
39     @After
40     public void tearDown() {
41         this.session.close();
42     }
43
44     @Test
45     public void testPcepSessionImpl() throws InterruptedException {
46         Assert.assertTrue(this.listener.up);
47
48         this.session.handleMessage(this.kaMsg);
49         Assert.assertEquals(1, this.session.getMessages().getReceivedMsgCount().intValue());
50
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());
56
57         this.session.handleMessage(this.closeMsg);
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();
62
63         this.session.resetStats();
64         Assert.assertEquals(0, this.session.getMessages().getReceivedMsgCount().longValue());
65     }
66
67     @Test
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()));
76     }
77
78     @Test
79     public void testClosedByNode() {
80         this.session.handleMessage(this.closeMsg);
81         Mockito.verify(this.channel).close();
82     }
83
84     @Test
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();
91         Assert.assertEquals(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, TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason()));
97         Mockito.verify(this.channel, Mockito.times(1)).close();
98     }
99
100     @Test
101     public void testEndoOfInput() {
102         Assert.assertTrue(this.listener.up);
103         this.session.endOfInput();
104         Assert.assertFalse(this.listener.up);
105     }
106
107     @Test
108     public void testCloseSessionWithReason() {
109         this.session.close(TerminationReason.UNKNOWN);
110         Assert.assertEquals(1, this.msgsSend.size());
111         Assert.assertTrue(this.msgsSend.get(0) instanceof CloseMessage);
112         final CloseMessage closeMsg = (CloseMessage) this.msgsSend.get(0);
113         Assert.assertEquals(TerminationReason.UNKNOWN, TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason()));
114         Mockito.verify(this.channel, Mockito.times(1)).close();
115     }
116
117     @Test
118     public void testSessionStatistics() {
119         this.session.handleMessage(Util.createErrorMessage(PCEPErrors.LSP_RSVP_ERROR, null));
120         Assert.assertEquals(this.ipAddress, this.session.getPeerPref().getIpAddress());
121         final PeerPref peerPref = this.session.getPeerPref();
122         Assert.assertEquals(this.ipAddress, peerPref.getIpAddress());
123         Assert.assertEquals(DEADTIMER, peerPref.getDeadtimer().shortValue());
124         Assert.assertEquals(KEEP_ALIVE, peerPref.getKeepalive().shortValue());
125         Assert.assertEquals(0, peerPref.getSessionId().intValue());
126         final LocalPref localPref = this.session.getLocalPref();
127         Assert.assertEquals(this.ipAddress, localPref.getIpAddress());
128         Assert.assertEquals(DEADTIMER, localPref.getDeadtimer().shortValue());
129         Assert.assertEquals(KEEP_ALIVE, localPref.getKeepalive().shortValue());
130         Assert.assertEquals(0, localPref.getSessionId().intValue());
131         final Messages msgs = this.session.getMessages();
132         Assert.assertEquals(1, msgs.getReceivedMsgCount().longValue());
133         Assert.assertEquals(0, msgs.getSentMsgCount().longValue());
134         Assert.assertEquals(0, msgs.getUnknownMsgReceived().longValue());
135         final ErrorMessages errMsgs = msgs.getErrorMessages();
136         Assert.assertEquals(1, errMsgs.getReceivedErrorMsgCount().intValue());
137         Assert.assertEquals(0, errMsgs.getSentErrorMsgCount().intValue());
138         Assert.assertEquals(PCEPErrors.LSP_RSVP_ERROR.getErrorType(), errMsgs.getLastReceivedError().getErrorType().shortValue());
139         Assert.assertEquals(PCEPErrors.LSP_RSVP_ERROR.getErrorValue(), errMsgs.getLastReceivedError().getErrorValue().shortValue());
140
141         this.session.sendMessage(Util.createErrorMessage(PCEPErrors.UNKNOWN_PLSP_ID, null));
142         final Messages msgs2 = this.session.getMessages();
143         Assert.assertEquals(1, msgs2.getReceivedMsgCount().longValue());
144         Assert.assertEquals(1, msgs2.getSentMsgCount().longValue());
145         Assert.assertEquals(0, msgs2.getUnknownMsgReceived().longValue());
146         final ErrorMessages errMsgs2 = msgs2.getErrorMessages();
147         Assert.assertEquals(1, errMsgs2.getReceivedErrorMsgCount().intValue());
148         Assert.assertEquals(1, errMsgs2.getSentErrorMsgCount().intValue());
149         Assert.assertEquals(PCEPErrors.UNKNOWN_PLSP_ID.getErrorType(), errMsgs2.getLastSentError().getErrorType().shortValue());
150         Assert.assertEquals(PCEPErrors.UNKNOWN_PLSP_ID.getErrorValue(), errMsgs2.getLastSentError().getErrorValue().shortValue());
151     }
152 }