BUG-2661:Sonar issue
[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.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         Deque<ClientEvent> handshakeScenario = ScenarioFactory.createHandshakeScenarioVBM(
113                 ScenarioFactory.VERSION_BITMAP_13, (short) 0, ScenarioFactory.VERSION_BITMAP_10_13, true);
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         Deque<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         Deque<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         Deque<ClientEvent> handshakeScenario = ScenarioFactory.createHandshakeScenario((short) 0,
170                 ScenarioFactory.VERSION_BITMAP_10_13);
171         handshakeScenario.addFirst(new SleepEvent(5000));
172         ScenarioHandler scenario = new ScenarioHandler(handshakeScenario);
173         switchSim.setScenarioHandler(scenario);
174         scenarioPool.execute(switchSim);
175     }
176
177     /**
178      * test with MLX running OF10 and OFP running OF13/OF10
179      *
180      * MLX issues an OFPT_ERROR on the version compatability MLX issues a second
181      * HELLO after the second OFP HELLO
182      *
183      * @throws Exception
184      */
185     @Test
186     public void handshakeOkNoVBM_OF10_TwoHello() throws Exception {
187         LOG.debug("handshakeOkMLX10 integration test");
188         LOG.debug("openflowPluginProvider: " + openflowPluginProvider);
189
190         switchSim = createSimpleClient();
191         switchSim.setSecuredClient(false);
192         Deque<ClientEvent> handshakeScenario = ScenarioFactory
193                 .createHandshakeScenarioNoVBM_OF10_TwoHello();
194         // handshakeScenario.setElementAt(new SleepEvent(5000),
195         // handshakeScenario
196         // .size());
197
198         ScenarioHandler scenario = new ScenarioHandler(handshakeScenario);
199         switchSim.setScenarioHandler(scenario);
200         scenarioPool.execute(switchSim);
201     }
202
203     /**
204      * test with Mininet running OF10 and OFP running OF13/OF10
205      *
206      * Mininet issues an OFPT_ERROR on the version compatability Mininet doesn't
207      * issue a second HELLO
208      *
209      * @throws Exception
210      */
211     @Test
212     public void handshakeOkNoVBM_OF10_SingleHello() throws Exception {
213         LOG.debug("handshakeOkMLX10 integration test");
214         LOG.debug("openflowPluginProvider: " + openflowPluginProvider);
215
216         switchSim = createSimpleClient();
217         switchSim.setSecuredClient(false);
218         Deque<ClientEvent> handshakeScenario = ScenarioFactory
219                 .createHandshakeScenarioNOVBM_OF10_OneHello();
220
221         ScenarioHandler scenario = new ScenarioHandler(handshakeScenario);
222         switchSim.setScenarioHandler(scenario);
223         scenarioPool.execute(switchSim);
224     }
225
226     /**
227      * @return
228      */
229     private static SimpleClient createSimpleClient() {
230         return new SimpleClient("localhost", 6653);
231     }
232
233     /**
234      * @return timeout for case of failure
235      */
236     private static long getFailSafeTimeout() {
237         return 20000;
238     }
239
240
241     /**
242      * @return bundle options
243      */
244     @Configuration
245     public Option[] config() {
246         LOG.info("configuring...");
247         return options(
248                 systemProperty("osgi.console").value("2401"),
249                 systemProperty("osgi.bundles.defaultStartLevel").value("4"),
250                 systemProperty("pax.exam.osgi.unresolved.fail").value("true"),
251
252                 OFPaxOptionsAssistant.osgiConsoleBundles(),
253                 OFPaxOptionsAssistant.loggingBudles(),
254
255                 TestHelper.junitAndMockitoBundles(),
256                 TestHelper.mdSalCoreBundles(),
257                 TestHelper.configMinumumBundles(),
258                 TestHelper.baseModelBundles(),
259                 TestHelper.flowCapableModelBundles(),
260
261                 OFPaxOptionsAssistant.ofPluginBundles());
262     }
263
264 }