BUG-837: it unstable (added missing bundles)
[openflowplugin.git] / openflowplugin-it / src / test / java / org / opendaylight / openflowplugin / openflow / md / it / SalIntegrationTest.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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.ops4j.pax.exam.CoreOptions.options;
13 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.concurrent.ArrayBlockingQueue;
18 import java.util.concurrent.TimeUnit;
19
20 import javax.inject.Inject;
21
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
27 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
28 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
29 import org.opendaylight.controller.sal.binding.api.NotificationService;
30 import org.opendaylight.controller.test.sal.binding.it.TestHelper;
31 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioHandler;
32 import org.opendaylight.openflowjava.protocol.impl.clients.SimpleClient;
33 import org.opendaylight.openflowplugin.openflow.md.core.ThreadPoolLoggingExecutor;
34 import org.opendaylight.openflowplugin.openflow.md.core.sal.OpenflowPluginProvider;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener;
40 import org.ops4j.pax.exam.Configuration;
41 import org.ops4j.pax.exam.Option;
42 import org.ops4j.pax.exam.junit.PaxExam;
43 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
44 import org.ops4j.pax.exam.spi.reactors.PerClass;
45 import org.ops4j.pax.exam.util.Filter;
46 import org.osgi.framework.BundleContext;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * Exercise inventory listener ({@link OpendaylightInventoryListener#onNodeUpdated(NodeUpdated)})
52  */
53 @RunWith(PaxExam.class)
54 @ExamReactorStrategy(PerClass.class)
55 public class SalIntegrationTest {
56
57     static final Logger LOG = LoggerFactory.getLogger(SalIntegrationTest.class);
58
59     private final ArrayBlockingQueue<Runnable> SCENARIO_POOL_QUEUE = new ArrayBlockingQueue<>(1);
60     private ThreadPoolLoggingExecutor scenarioPool;
61     private SimpleClient switchSim;
62     private Runnable finalCheck;
63
64     @Inject @Filter(timeout=60*000)
65     BundleContext ctx;
66
67     @Inject @Filter(timeout=60*1000)
68     BindingAwareBroker broker;
69
70     @Inject @Filter(timeout=60*1000)
71     OpenflowPluginProvider openflowPluginProvider;
72
73     /**
74      * @return timeout for case of failure
75      */
76     static long getFailSafeTimeout() {
77         return 30000;
78     }
79
80     /**
81      * test setup
82      * @throws InterruptedException
83      */
84     @Before
85     public void setUp() throws InterruptedException {
86         switchSim = new SimpleClient("localhost", 6653);
87         scenarioPool = new ThreadPoolLoggingExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, SCENARIO_POOL_QUEUE, "scenario");
88     }
89
90     /**
91      * test tear down
92      */
93     @After
94     public void tearDown() {
95         SimulatorAssistant.waitForSwitchSimulatorOn(switchSim);
96         SimulatorAssistant.tearDownSwitchSimulatorAfterScenario(switchSim, scenarioPool, getFailSafeTimeout());
97
98         if (finalCheck != null) {
99             LOG.info("starting final check");
100             finalCheck.run();
101         }
102     }
103
104     /**
105      * test basic integration with OFLib running the handshake
106      *
107      * @throws Exception
108      */
109     @Test
110     public void handshakeAndNodeUpdate() throws Exception {
111
112         final TestInventoryListener listener = new TestInventoryListener();
113         BindingAwareConsumer openflowConsumer = new BindingAwareConsumer() {
114
115             @Override
116             public void onSessionInitialized(ConsumerContext session) {
117                 session.getSALService(NotificationService.class).registerNotificationListener(listener);
118             }
119         };
120         ConsumerContext consumerReg = broker.registerConsumer(openflowConsumer, ctx);
121         assertNotNull(consumerReg);
122
123         LOG.debug("handshake integration test");
124         LOG.debug("openflowPluginProvider: " + openflowPluginProvider);
125
126         switchSim.setSecuredClient(false);
127         ScenarioHandler scenario = new ScenarioHandler(ScenarioFactory.createHandshakeScenarioVBM(
128                 ScenarioFactory.VERSION_BITMAP_13, (short) 0, ScenarioFactory.VERSION_BITMAP_10_13, true));
129         switchSim.setScenarioHandler(scenario);
130         scenarioPool.execute(switchSim);
131
132         finalCheck = new Runnable() {
133             @Override
134             public void run() {
135                 assertEquals(1, listener.nodeUpdated.size());
136                 assertNotNull(listener.nodeUpdated.get(0));
137             }
138         };
139     }
140
141     /**
142      * @return bundle options
143      */
144     @Configuration
145     public Option[] config() {
146         return options(
147                 systemProperty("osgi.console").value("2401"),
148                 systemProperty("osgi.bundles.defaultStartLevel").value("4"),
149                 systemProperty("pax.exam.osgi.unresolved.fail").value("true"),
150
151                 OFPaxOptionsAssistant.osgiConsoleBundles(),
152                 OFPaxOptionsAssistant.loggingBudles(),
153                 OFPaxOptionsAssistant.ofPluginBundles());
154     }
155
156     private static class TestInventoryListener implements OpendaylightInventoryListener {
157
158         List<NodeUpdated> nodeUpdated = new ArrayList<>();
159         List<NodeRemoved> nodeRemoved = new ArrayList<>();
160         List<NodeConnectorUpdated> nodeConnectorUpdated = new ArrayList<>();
161         List<NodeConnectorRemoved> nodeConnectorRemoved = new ArrayList<>();
162
163         /**
164          * default ctor
165          */
166         protected TestInventoryListener() {
167             // do nothing
168         }
169
170         @Override
171         public void onNodeUpdated(NodeUpdated notification) {
172             nodeUpdated.add(notification);
173         }
174
175         @Override
176         public void onNodeRemoved(NodeRemoved notification) {
177             nodeRemoved.add(notification);
178         }
179
180         @Override
181         public void onNodeConnectorUpdated(NodeConnectorUpdated notification) {
182             nodeConnectorUpdated.add(notification);
183         }
184
185         @Override
186         public void onNodeConnectorRemoved(NodeConnectorRemoved notification) {
187             nodeConnectorRemoved.add(notification);
188         }
189     }
190
191 }