BUG-837: IT unstable
[openflowplugin.git] / openflowplugin-it / src / test / java / org / opendaylight / openflowplugin / openflow / md / it / OFPluginToLibraryTest.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.openflowplugin.openflow.md.it;
9
10 import static org.ops4j.pax.exam.CoreOptions.options;
11 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
12
13 import java.util.Stack;
14 import java.util.concurrent.ArrayBlockingQueue;
15 import java.util.concurrent.TimeUnit;
16
17 import javax.inject.Inject;
18
19 import org.junit.After;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.opendaylight.controller.test.sal.binding.it.TestHelper;
25 import org.opendaylight.openflowjava.protocol.impl.clients.ClientEvent;
26 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioHandler;
27 import org.opendaylight.openflowjava.protocol.impl.clients.SimpleClient;
28 import org.opendaylight.openflowjava.protocol.impl.clients.SleepEvent;
29 import org.opendaylight.openflowplugin.openflow.md.core.ThreadPoolLoggingExecutor;
30 import org.opendaylight.openflowplugin.openflow.md.core.sal.OpenflowPluginProvider;
31 import org.ops4j.pax.exam.Configuration;
32 import org.ops4j.pax.exam.Option;
33 import org.ops4j.pax.exam.junit.PaxExam;
34 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
35 import org.ops4j.pax.exam.spi.reactors.PerClass;
36 import org.ops4j.pax.exam.util.Filter;
37 import org.osgi.framework.BundleContext;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * covers basic handshake scenarios
43  */
44 @RunWith(PaxExam.class)
45 @ExamReactorStrategy(PerClass.class)
46 public class OFPluginToLibraryTest {
47
48     private static final Logger LOG = LoggerFactory
49             .getLogger(OFPluginToLibraryTest.class);
50
51     private static final ArrayBlockingQueue<Runnable> SCENARIO_POOL_QUEUE = new ArrayBlockingQueue<>(1);
52
53     @Inject @Filter(timeout=20000)
54     OpenflowPluginProvider openflowPluginProvider;
55
56     @Inject
57     BundleContext ctx;
58
59     private SimpleClient switchSim;
60     private ThreadPoolLoggingExecutor scenarioPool;
61
62     /**
63      * test setup
64      * @throws InterruptedException
65      */
66     @Before
67     public void setUp() throws InterruptedException {
68         LOG.debug("openflowPluginProvider: "+openflowPluginProvider);
69         scenarioPool = new ThreadPoolLoggingExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, SCENARIO_POOL_QUEUE, "scenario");
70         //FIXME: plugin should provide service exposing startup result via future
71         Thread.sleep(5000);
72     }
73
74     /**
75      * test tear down
76      */
77     @After
78     public void tearDown() {
79         try {
80             LOG.debug("tearing down simulator");
81             switchSim.getScenarioDone().get(getFailSafeTimeout(), TimeUnit.MILLISECONDS);
82         } catch (Exception e) {
83             String msg = "waiting for scenario to finish failed: "+e.getMessage();
84             LOG.error(msg, e);
85             Assert.fail(msg);
86         } finally {
87             scenarioPool.shutdownNow();
88             SCENARIO_POOL_QUEUE.clear();
89         }
90
91         try {
92             LOG.debug("checking if simulator succeeded to connect to controller");
93             boolean simulatorWasOnline = switchSim.getIsOnlineFuture().get(100, TimeUnit.MILLISECONDS);
94             Assert.assertTrue("simulator failed to connect to controller", simulatorWasOnline);
95         } catch (Exception e) {
96             String message = "simulator probably failed to connect to controller";
97             LOG.error(message, e);
98             Assert.fail(message);
99         }
100     }
101
102     /**
103      * test basic integration with OFLib running the handshake
104      * @throws Exception
105      */
106     @Test
107     public void handshakeOk1() throws Exception {
108         LOG.debug("handshakeOk1 integration test");
109
110         switchSim = createSimpleClient();
111         switchSim.setSecuredClient(false);
112         Stack<ClientEvent> handshakeScenario = ScenarioFactory.createHandshakeScenarioVBM(
113                 ScenarioFactory.VERSION_BITMAP_13, (short) 0, ScenarioFactory.VERSION_BITMAP_10_13);
114
115         ScenarioHandler scenario = new ScenarioHandler(handshakeScenario);
116         switchSim.setScenarioHandler(scenario);
117         scenarioPool.execute(switchSim);
118     }
119
120     /**
121      * test basic integration with OFLib running the handshake (with version bitmap)
122      * @throws Exception
123      */
124     @Test
125     public void handshakeOk2() throws Exception {
126         LOG.debug("handshakeOk2 integration test");
127
128         switchSim = createSimpleClient();
129         switchSim.setSecuredClient(false);
130         Stack<ClientEvent> handshakeScenario = ScenarioFactory.createHandshakeScenario(
131                 (short) 0, ScenarioFactory.VERSION_BITMAP_10_13);
132
133         ScenarioHandler scenario = new ScenarioHandler(handshakeScenario);
134         switchSim.setScenarioHandler(scenario);
135         scenarioPool.execute(switchSim);
136     }
137
138     /**
139      * test basic integration with OFLib running the handshake:
140      * creating auxiliary connection without primary connection -- FAIL
141      * @throws Exception
142      */
143     @Test
144     public void handshakeFail1() throws Exception {
145         LOG.debug("handshakeFail1 integration test");
146
147         switchSim = createSimpleClient();
148         switchSim.setSecuredClient(false);
149         Stack<ClientEvent> handshakeScenario = ScenarioFactory.createHandshakeScenario((short) 1,
150                 ScenarioFactory.VERSION_BITMAP_10_13);
151
152         ScenarioHandler scenario = new ScenarioHandler(handshakeScenario);
153         switchSim.setScenarioHandler(scenario);
154         scenarioPool.execute(switchSim);
155     }
156
157     /**
158      * test basic integration with OFLib running the handshake
159      * adding 5s wait as first event of switch -- FAIL
160      * @throws Exception
161      */
162     @Test
163     public void handshakeFail2() throws Exception {
164         LOG.debug("handshakeFail2 integration test");
165         LOG.debug("openflowPluginProvider: "+openflowPluginProvider);
166
167         switchSim = createSimpleClient();
168         switchSim.setSecuredClient(false);
169         Stack<ClientEvent> handshakeScenario = ScenarioFactory.createHandshakeScenario((short) 0,
170                 ScenarioFactory.VERSION_BITMAP_10_13);
171         handshakeScenario.setElementAt(new SleepEvent(5000), 0);
172
173         ScenarioHandler scenario = new ScenarioHandler(handshakeScenario);
174         switchSim.setScenarioHandler(scenario);
175         scenarioPool.execute(switchSim);
176     }
177
178     /**
179      * test with MLX running OF10 and OFP running OF13/OF10
180      *
181      * MLX issues an OFPT_ERROR on the version compatability MLX issues a second
182      * HELLO after the second OFP HELLO
183      *
184      * @throws Exception
185      */
186     @Test
187     public void handshakeOkNoVBM_OF10_TwoHello() throws Exception {
188         LOG.debug("handshakeOkMLX10 integration test");
189         LOG.debug("openflowPluginProvider: " + openflowPluginProvider);
190
191         switchSim = createSimpleClient();
192         switchSim.setSecuredClient(false);
193         Stack<ClientEvent> handshakeScenario = ScenarioFactory
194                 .createHandshakeScenarioNoVBM_OF10_TwoHello();
195         // handshakeScenario.setElementAt(new SleepEvent(5000),
196         // handshakeScenario
197         // .size());
198
199         ScenarioHandler scenario = new ScenarioHandler(handshakeScenario);
200         switchSim.setScenarioHandler(scenario);
201         scenarioPool.execute(switchSim);
202     }
203
204     /**
205      * test with Mininet running OF10 and OFP running OF13/OF10
206      *
207      * Mininet issues an OFPT_ERROR on the version compatability Mininet doesn't
208      * issue a second HELLO
209      *
210      * @throws Exception
211      */
212     @Test
213     public void handshakeOkNoVBM_OF10_SingleHello() throws Exception {
214         LOG.debug("handshakeOkMLX10 integration test");
215         LOG.debug("openflowPluginProvider: " + openflowPluginProvider);
216
217         switchSim = createSimpleClient();
218         switchSim.setSecuredClient(false);
219         Stack<ClientEvent> handshakeScenario = ScenarioFactory
220                 .createHandshakeScenarioNOVBM_OF10_OneHello();
221
222         ScenarioHandler scenario = new ScenarioHandler(handshakeScenario);
223         switchSim.setScenarioHandler(scenario);
224         scenarioPool.execute(switchSim);
225     }
226
227     /**
228      * @return
229      */
230     private static SimpleClient createSimpleClient() {
231         return new SimpleClient("localhost", 6653);
232     }
233
234     /**
235      * @return timeout for case of failure
236      */
237     private static long getFailSafeTimeout() {
238         return 20000;
239     }
240
241
242     /**
243      * @return bundle options
244      */
245     @Configuration
246     public Option[] config() {
247         LOG.info("configuring...");
248         return options(
249                 systemProperty("osgi.console").value("2401"),
250                 systemProperty("osgi.bundles.defaultStartLevel").value("4"),
251                 systemProperty("pax.exam.osgi.unresolved.fail").value("true"),
252
253                 OFPaxOptionsAssistant.osgiConsoleBundles(),
254                 OFPaxOptionsAssistant.loggingBudles(),
255
256                 TestHelper.junitAndMockitoBundles(),
257                 TestHelper.mdSalCoreBundles(),
258                 TestHelper.configMinumumBundles(),
259                 TestHelper.baseModelBundles(),
260                 TestHelper.flowCapableModelBundles(),
261
262                 OFPaxOptionsAssistant.ofPluginBundles());
263     }
264
265 }