1b1ab7c7886ebd4fccff714899bd3b8dc08202d2
[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.util.Filter;
44 import org.osgi.framework.BundleContext;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * Exercise inventory listener ({@link OpendaylightInventoryListener#onNodeUpdated(NodeUpdated)})
50  */
51 @RunWith(PaxExam.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
62     BundleContext ctx;
63
64     @Inject
65     @Filter(timeout=20*1000)
66     BindingAwareBroker broker;
67     
68     @Inject 
69     @Filter(timeout=20*1000)
70     OpenflowPluginProvider openflowPluginProvider;
71
72     /**
73      * @return timeout for case of failure
74      */
75     static long getFailSafeTimeout() {
76         return 30000;
77     }
78     
79     /**
80      * test setup
81      * @throws InterruptedException
82      */
83     @Before
84     public void setUp() throws InterruptedException {
85         switchSim = new SimpleClient("localhost", 6653);
86         scenarioPool = new ThreadPoolLoggingExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, SCENARIO_POOL_QUEUE, "scenario");
87     }
88
89     /**
90      * test tear down
91      */
92     @After
93     public void tearDown() {
94         SimulatorAssistant.waitForSwitchSimulatorOn(switchSim);
95         SimulatorAssistant.tearDownSwitchSimulatorAfterScenario(switchSim, scenarioPool, getFailSafeTimeout());
96         
97         if (finalCheck != null) {
98             LOG.info("starting final check");
99             finalCheck.run();
100         }
101     }
102
103     /**
104      * test basic integration with OFLib running the handshake
105      *
106      * @throws Exception
107      */
108     @Test
109     public void handshakeAndNodeUpdate() throws Exception {
110
111         final TestInventoryListener listener = new TestInventoryListener();
112         BindingAwareConsumer openflowConsumer = new BindingAwareConsumer() {
113
114             @Override
115             public void onSessionInitialized(ConsumerContext session) {
116                 session.getSALService(NotificationService.class).registerNotificationListener(listener);
117             }
118         };
119         ConsumerContext consumerReg = broker.registerConsumer(openflowConsumer, ctx);
120         assertNotNull(consumerReg);
121         
122         LOG.debug("handshake integration test");
123         LOG.debug("openflowPluginProvider: " + openflowPluginProvider);
124
125         switchSim.setSecuredClient(false);
126         ScenarioHandler scenario = new ScenarioHandler(ScenarioFactory.createHandshakeScenarioVBM(
127                 ScenarioFactory.VERSION_BITMAP_13, (short) 0, ScenarioFactory.VERSION_BITMAP_10_13, true));
128         switchSim.setScenarioHandler(scenario);
129         scenarioPool.execute(switchSim);
130
131         finalCheck = new Runnable() {
132             @Override
133             public void run() {
134                 assertEquals(1, listener.nodeUpdated.size());
135                 assertNotNull(listener.nodeUpdated.get(0));
136             }
137         };
138     }
139
140     /**
141      * @return bundle options
142      */
143     @Configuration
144     public Option[] config() {
145         return options(systemProperty("osgi.console").value("2401"),
146                 OFPaxOptionsAssistant.osgiConsoleBundles(),
147                 OFPaxOptionsAssistant.loggingBudles(),
148                 
149                 TestHelper.junitAndMockitoBundles(),
150                 TestHelper.mdSalCoreBundles(), 
151                 TestHelper.configMinumumBundles(),
152                 TestHelper.baseModelBundles(),
153                 OFPaxOptionsAssistant.ofLibraryBundles(),
154                 OFPaxOptionsAssistant.ofPluginBundles()
155                 );
156     }
157
158     private static class TestInventoryListener implements OpendaylightInventoryListener {
159
160         List<NodeUpdated> nodeUpdated = new ArrayList<>();
161         List<NodeRemoved> nodeRemoved = new ArrayList<>();
162         List<NodeConnectorUpdated> nodeConnectorUpdated = new ArrayList<>();
163         List<NodeConnectorRemoved> nodeConnectorRemoved = new ArrayList<>();
164
165         /**
166          * default ctor
167          */
168         protected TestInventoryListener() {
169             // do nothing
170         }
171
172         @Override
173         public void onNodeUpdated(NodeUpdated notification) {
174             nodeUpdated.add(notification);
175         }
176
177         @Override
178         public void onNodeRemoved(NodeRemoved notification) {
179             nodeRemoved.add(notification);
180         }
181
182         @Override
183         public void onNodeConnectorUpdated(NodeConnectorUpdated notification) {
184             nodeConnectorUpdated.add(notification);
185         }
186
187         @Override
188         public void onNodeConnectorRemoved(NodeConnectorRemoved notification) {
189             nodeConnectorRemoved.add(notification);
190         }
191     }
192
193 }