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