55a37946632721c4f872020ea4cb0de283440ad5
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / FiniteStateMachineTest.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.protocol.pcep.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12
13 import io.netty.util.concurrent.DefaultPromise;
14 import io.netty.util.concurrent.GlobalEventExecutor;
15 import org.junit.After;
16 import org.junit.Before;
17 import org.junit.Ignore;
18 import org.junit.Test;
19 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Keepalive;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcerr;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenMessage;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.Errors;
25 import org.opendaylight.yangtools.yang.binding.Notification;
26
27 public class FiniteStateMachineTest extends AbstractPCEPSessionTest {
28
29     private DefaultPCEPSessionNegotiator serverSession;
30
31     @Before
32     public void setup() {
33         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open localPrefs = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder().setKeepalive(
34                 (short) 1).build();
35         this.serverSession = new DefaultPCEPSessionNegotiator(new DefaultPromise<PCEPSessionImpl>(GlobalEventExecutor.INSTANCE), this.channel, this.listener, (short) 1, 20, localPrefs);
36     }
37
38     /**
39      * Both PCEs accept session characteristics. Also tests KeepAliveTimer and error message and when pce attempts to
40      * establish pce session for the 2nd time.
41      *
42      * @throws Exception
43      */
44     @Test
45     public void testSessionCharsAccBoth() throws Exception {
46         this.serverSession.channelActive(null);
47         assertEquals(1, this.msgsSend.size());
48         assertTrue(this.msgsSend.get(0) instanceof Open);
49         this.serverSession.handleMessage(this.openMsg);
50         assertEquals(2, this.msgsSend.size());
51         assertTrue(this.msgsSend.get(1) instanceof Keepalive);
52         this.serverSession.handleMessage(this.kaMsg);
53         assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.FINISHED);
54     }
55
56     /**
57      * Mock PCE does not accept session characteristics the first time.
58      *
59      * @throws Exception
60      */
61     @Test
62     public void testSessionCharsAccMe() throws Exception {
63         this.serverSession.channelActive(null);
64         assertEquals(1, this.msgsSend.size());
65         assertTrue(this.msgsSend.get(0) instanceof Open);
66         final Open remote = (Open) this.msgsSend.get(0);
67         this.serverSession.handleMessage(this.openMsg);
68         assertEquals(2, this.msgsSend.size());
69         assertTrue(this.msgsSend.get(1) instanceof Keepalive);
70         this.serverSession.handleMessage(Util.createErrorMessage(PCEPErrors.NON_ACC_NEG_SESSION_CHAR, remote.getOpenMessage().getOpen()));
71         assertEquals(3, this.msgsSend.size());
72         assertTrue(this.msgsSend.get(2) instanceof Open);
73         this.serverSession.handleMessage(this.kaMsg);
74         assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.FINISHED);
75     }
76
77     /**
78      * Sending different PCEP Message than Open in session establishment phase.
79      *
80      * @throws Exception
81      */
82     @Test
83     public void testErrorOneOne() throws Exception {
84         this.serverSession.channelActive(null);
85         assertEquals(1, this.msgsSend.size());
86         assertTrue(this.msgsSend.get(0) instanceof Open);
87         this.serverSession.handleMessage(this.kaMsg);
88         for (final Notification m : this.msgsSend) {
89             if (m instanceof Pcerr) {
90                 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
91                 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
92                 assertEquals(new Short((short) 1), obj.getErrorObject().getValue());
93             }
94         }
95     }
96
97     /**
98      * KeepWaitTimer expired.
99      *
100      * @throws Exception
101      */
102     @Test
103     public void testErrorOneSeven() throws Exception {
104         this.serverSession.channelActive(null);
105         assertEquals(1, this.msgsSend.size());
106         assertTrue(this.msgsSend.get(0) instanceof Open);
107         this.serverSession.handleMessage(this.openMsg);
108         Thread.sleep(1000);
109         for (final Notification m : this.msgsSend) {
110             if (m instanceof Pcerr) {
111                 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
112                 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
113                 assertEquals(new Short((short) 7), obj.getErrorObject().getValue());
114             }
115         }
116     }
117
118     /************* Tests commented because of their long duration (tested timers) **************/
119
120     /**
121      * OpenWait timer expired.
122      *
123      * @throws InterruptedException
124      */
125     @Test
126     @Ignore
127     public void testErrorOneTwo() throws InterruptedException {
128         this.serverSession.channelActive(null);
129         assertEquals(1, this.msgsSend.size());
130         assertTrue(this.msgsSend.get(0) instanceof OpenMessage);
131         Thread.sleep(60 * 1000);
132         for (final Notification m : this.msgsSend) {
133             if (m instanceof Pcerr) {
134                 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
135                 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
136                 assertEquals(new Short((short) 2), obj.getErrorObject().getValue());
137             }
138         }
139     }
140
141     @Test
142     @Ignore
143     public void testUnknownMessage() throws InterruptedException {
144         final SimpleSessionListener client = new SimpleSessionListener();
145         final PCEPSessionImpl s = new PCEPSessionImpl(client, 5, this.channel, this.openMsg.getOpenMessage().getOpen(), this.openMsg.getOpenMessage().getOpen());
146         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
147         assertEquals(1, s.getUnknownMessagesTimes().size());
148         Thread.sleep(10000);
149         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
150         assertEquals(2, s.getUnknownMessagesTimes().size());
151         Thread.sleep(10000);
152         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
153         assertEquals(3, s.getUnknownMessagesTimes().size());
154         Thread.sleep(20000);
155         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
156         assertEquals(4, s.getUnknownMessagesTimes().size());
157         Thread.sleep(30000);
158         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
159         assertEquals(3, s.getUnknownMessagesTimes().size());
160         Thread.sleep(10000);
161         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
162         assertEquals(3, s.getUnknownMessagesTimes().size());
163         Thread.sleep(5000);
164         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
165         assertEquals(4, s.getUnknownMessagesTimes().size());
166         Thread.sleep(1000);
167         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
168         assertEquals(5, s.getUnknownMessagesTimes().size());
169         Thread.sleep(1000);
170         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
171         synchronized (client) {
172             while (client.up) {
173                 client.wait();
174             }
175         }
176         assertTrue(!client.up);
177     }
178
179     @After
180     public void tearDown() {
181     }
182 }