bc02caee38a92584be98da6eb0a7d9349fa3fc89
[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.Deque;
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.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.opendaylight.controller.test.sal.binding.it.TestHelper;
24 import org.opendaylight.openflowjava.protocol.impl.clients.ClientEvent;
25 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioHandler;
26 import org.opendaylight.openflowjava.protocol.impl.clients.SimpleClient;
27 import org.opendaylight.openflowjava.protocol.impl.clients.SleepEvent;
28 import org.opendaylight.openflowplugin.openflow.md.core.ThreadPoolLoggingExecutor;
29 import org.opendaylight.openflowplugin.openflow.md.core.sal.OpenflowPluginProvider;
30 import org.ops4j.pax.exam.Configuration;
31 import org.ops4j.pax.exam.Option;
32 import org.ops4j.pax.exam.junit.PaxExam;
33 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
34 import org.ops4j.pax.exam.spi.reactors.PerClass;
35 import org.ops4j.pax.exam.util.Filter;
36 import org.osgi.framework.BundleContext;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * covers basic handshake scenarios
42  */
43 @RunWith(PaxExam.class)
44 @ExamReactorStrategy(PerClass.class)
45 public class OFPluginToLibraryTest {
46
47     private static final Logger LOG = LoggerFactory
48             .getLogger(OFPluginToLibraryTest.class);
49
50     private final ArrayBlockingQueue<Runnable> SCENARIO_POOL_QUEUE = new ArrayBlockingQueue<>(1);
51
52     @Inject @Filter(timeout=60000)
53     OpenflowPluginProvider openflowPluginProvider;
54
55     @Inject @Filter(timeout=60000)
56     BundleContext ctx;
57
58     private SimpleClient switchSim;
59     private ThreadPoolLoggingExecutor scenarioPool;
60
61     /**
62      * test setup
63      * @throws InterruptedException
64      */
65     @Before
66     public void setUp() throws InterruptedException {
67         LOG.debug("openflowPluginProvider: "+openflowPluginProvider);
68         switchSim = createSimpleClient();
69         scenarioPool = new ThreadPoolLoggingExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, SCENARIO_POOL_QUEUE, "scenario");
70     }
71
72     /**
73      * test tear down
74      */
75     @After
76     public void tearDown() {
77         SimulatorAssistant.waitForSwitchSimulatorOn(switchSim);
78         SimulatorAssistant.tearDownSwitchSimulatorAfterScenario(switchSim, scenarioPool, getFailSafeTimeout());
79     }
80
81     /**
82      * test basic integration with OFLib running the handshake
83      * @throws Exception
84      */
85     @Test
86     public void handshakeOk1() throws Exception {
87         LOG.debug("handshakeOk1 integration test");
88
89         switchSim.setSecuredClient(false);
90         Deque<ClientEvent> handshakeScenario = ScenarioFactory.createHandshakeScenarioVBM(
91                 ScenarioFactory.VERSION_BITMAP_13, (short) 0, ScenarioFactory.VERSION_BITMAP_10_13, true);
92
93         ScenarioHandler scenario = new ScenarioHandler(handshakeScenario);
94         switchSim.setScenarioHandler(scenario);
95         scenarioPool.execute(switchSim);
96     }
97
98     /**
99      * test basic integration with OFLib running the handshake (with version bitmap)
100      * @throws Exception
101      */
102     @Test
103     public void handshakeOk2() throws Exception {
104         LOG.debug("handshakeOk2 integration test");
105
106         switchSim = createSimpleClient();
107         switchSim.setSecuredClient(false);
108         Deque<ClientEvent> handshakeScenario = ScenarioFactory.createHandshakeScenario(
109                 (short) 0, ScenarioFactory.VERSION_BITMAP_10_13);
110
111         ScenarioHandler scenario = new ScenarioHandler(handshakeScenario);
112         switchSim.setScenarioHandler(scenario);
113         scenarioPool.execute(switchSim);
114     }
115
116     /**
117      * test basic integration with OFLib running the handshake:
118      * creating auxiliary connection without primary connection -- FAIL
119      * @throws Exception
120      */
121     @Test
122     public void handshakeFail1() throws Exception {
123         LOG.debug("handshakeFail1 integration test");
124
125         switchSim = createSimpleClient();
126         switchSim.setSecuredClient(false);
127         Deque<ClientEvent> handshakeScenario = ScenarioFactory.createHandshakeScenario((short) 1,
128                 ScenarioFactory.VERSION_BITMAP_10_13);
129
130         ScenarioHandler scenario = new ScenarioHandler(handshakeScenario);
131         switchSim.setScenarioHandler(scenario);
132         scenarioPool.execute(switchSim);
133     }
134
135     /**
136      * test basic integration with OFLib running the handshake
137      * adding 5s wait as first event of switch -- FAIL
138      * @throws Exception
139      */
140     @Test
141     public void handshakeFail2() throws Exception {
142         LOG.debug("handshakeFail2 integration test");
143         LOG.debug("openflowPluginProvider: "+openflowPluginProvider);
144
145         switchSim = createSimpleClient();
146         switchSim.setSecuredClient(false);
147         Deque<ClientEvent> handshakeScenario = ScenarioFactory.createHandshakeScenario((short) 0,
148                 ScenarioFactory.VERSION_BITMAP_10_13);
149         handshakeScenario.addFirst(new SleepEvent(5000));
150         ScenarioHandler scenario = new ScenarioHandler(handshakeScenario);
151         switchSim.setScenarioHandler(scenario);
152         scenarioPool.execute(switchSim);
153     }
154
155     /**
156      * test with MLX running OF10 and OFP running OF13/OF10
157      *
158      * MLX issues an OFPT_ERROR on the version compatability MLX issues a second
159      * HELLO after the second OFP HELLO
160      *
161      * @throws Exception
162      */
163     @Test
164     public void handshakeOkNoVBM_OF10_TwoHello() throws Exception {
165         LOG.debug("handshakeOkMLX10 integration test");
166         LOG.debug("openflowPluginProvider: " + openflowPluginProvider);
167
168         switchSim = createSimpleClient();
169         switchSim.setSecuredClient(false);
170         Deque<ClientEvent> handshakeScenario = ScenarioFactory
171                 .createHandshakeScenarioNoVBM_OF10_TwoHello();
172         // handshakeScenario.setElementAt(new SleepEvent(5000),
173         // handshakeScenario
174         // .size());
175
176         ScenarioHandler scenario = new ScenarioHandler(handshakeScenario);
177         switchSim.setScenarioHandler(scenario);
178         scenarioPool.execute(switchSim);
179     }
180
181     /**
182      * test with Mininet running OF10 and OFP running OF13/OF10
183      *
184      * Mininet issues an OFPT_ERROR on the version compatability Mininet doesn't
185      * issue a second HELLO
186      *
187      * @throws Exception
188      */
189     @Test
190     public void handshakeOkNoVBM_OF10_SingleHello() throws Exception {
191         LOG.debug("handshakeOkMLX10 integration test");
192         LOG.debug("openflowPluginProvider: " + openflowPluginProvider);
193
194         switchSim = createSimpleClient();
195         switchSim.setSecuredClient(false);
196         Deque<ClientEvent> handshakeScenario = ScenarioFactory
197                 .createHandshakeScenarioNOVBM_OF10_OneHello();
198
199         ScenarioHandler scenario = new ScenarioHandler(handshakeScenario);
200         switchSim.setScenarioHandler(scenario);
201         scenarioPool.execute(switchSim);
202     }
203
204     /**
205      * @return
206      */
207     private static SimpleClient createSimpleClient() {
208         return new SimpleClient("localhost", 6653);
209     }
210
211     /**
212      * @return timeout for case of failure
213      */
214     private static long getFailSafeTimeout() {
215         return 20000;
216     }
217
218
219     /**
220      * @return bundle options
221      */
222     @Configuration
223     public Option[] config() {
224         LOG.info("configuring...");
225         return options(
226                 systemProperty("osgi.console").value("2401"),
227                 systemProperty("osgi.bundles.defaultStartLevel").value("4"),
228                 systemProperty("pax.exam.osgi.unresolved.fail").value("true"),
229
230                 OFPaxOptionsAssistant.osgiConsoleBundles(),
231                 OFPaxOptionsAssistant.loggingBudles(),
232                 OFPaxOptionsAssistant.ofPluginBundles());
233     }
234
235 }