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