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