Reenabled integration tests - part2
[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.TimeUnit;
18
19 import javax.inject.Inject;
20
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
26 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
27 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
28 import org.opendaylight.controller.sal.binding.api.NotificationService;
29 import org.opendaylight.controller.test.sal.binding.it.TestHelper;
30 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioHandler;
31 import org.opendaylight.openflowjava.protocol.impl.clients.SimpleClient;
32 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener;
38 import org.ops4j.pax.exam.Configuration;
39 import org.ops4j.pax.exam.Option;
40 import org.ops4j.pax.exam.junit.PaxExam;
41 import org.osgi.framework.BundleContext;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * Exercise inventory listener ({@link OpendaylightInventoryListener#onNodeUpdated(NodeUpdated)})
47  */
48 @RunWith(PaxExam.class)
49 public class SalIntegrationTest {
50
51     private static final Logger LOG = LoggerFactory.getLogger(SalIntegrationTest.class);
52
53     @Inject
54     SwitchConnectionProvider switchConnectionProvider;
55
56     @Inject
57     BundleContext ctx;
58
59     @Inject
60     BindingAwareBroker broker;
61
62     /**
63      * @return timeout for case of failure
64      */
65     private static long getFailSafeTimeout() {
66         return 20000;
67     }
68     
69     /**
70      * test setup
71      * @throws InterruptedException
72      */
73     @Before
74     public void setUp() throws InterruptedException {
75         //FIXME: plugin should provide service exposing startup result via future 
76         Thread.sleep(5000);
77     }
78     
79     /**
80      * test basic integration with OFLib running the handshake
81      *
82      * @throws Exception
83      */
84     @Test
85     public void handshakeAndNodeUpdate() throws Exception {
86
87         final TestInventoryListener listener = new TestInventoryListener();
88         BindingAwareConsumer openflowConsumer = new BindingAwareConsumer() {
89
90             @Override
91             public void onSessionInitialized(ConsumerContext session) {
92                 session.getSALService(NotificationService.class).registerNotificationListener(listener);
93             }
94         };
95         ConsumerContext consumerReg = broker.registerConsumer(openflowConsumer, ctx);
96         assertNotNull(consumerReg);
97
98         LOG.debug("handshake integration test");
99         LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
100
101         SimpleClient switchSim = new SimpleClient("localhost", 6653);
102         switchSim.setSecuredClient(false);
103         ScenarioHandler scenario = new ScenarioHandler(ScenarioFactory.createHandshakeScenarioVBM(
104                 ScenarioFactory.VERSION_BITMAP_13, (short) 0, ScenarioFactory.VERSION_BITMAP_10_13));
105         switchSim.setScenarioHandler(scenario);
106         switchSim.start();
107
108         try {
109             LOG.debug("tearing down simulator");
110             switchSim.getScenarioDone().get(getFailSafeTimeout(), TimeUnit.MILLISECONDS);
111         } catch (Exception e) {
112             String msg = "waiting for scenario to finish failed: "+e.getMessage();
113             LOG.error(msg, e);
114             Assert.fail(msg);
115         }
116         
117         try {
118             LOG.debug("checking if simulator succeeded to connect to controller");
119             boolean simulatorWasOnline = switchSim.getIsOnlineFuture().get(100, TimeUnit.MILLISECONDS);
120             Assert.assertTrue("simulator failed to connect to controller", simulatorWasOnline);
121         } catch (Exception e) {
122             String message = "simulator probably failed to connect to controller";
123             LOG.error(message, e);
124             Assert.fail(message);
125         }
126         
127         Thread.sleep(4000);
128         assertEquals(1, listener.nodeUpdated.size());
129         assertNotNull(listener.nodeUpdated.get(0));
130     }
131
132     /**
133      * @return bundle options
134      */
135     @Configuration
136     public Option[] config() {
137         return options(systemProperty("osgi.console").value("2401"),
138                 OFPaxOptionsAssistant.osgiConsoleBundles(),
139                 OFPaxOptionsAssistant.loggingBudles(),
140                 
141                 TestHelper.junitAndMockitoBundles(),
142                 TestHelper.mdSalCoreBundles(), 
143                 TestHelper.configMinumumBundles(),
144                 TestHelper.baseModelBundles(),
145                 TestHelper.flowCapableModelBundles(), 
146                 
147                 OFPaxOptionsAssistant.ofPluginBundles()
148                 );
149     }
150
151     private static class TestInventoryListener implements OpendaylightInventoryListener {
152
153         List<NodeUpdated> nodeUpdated = new ArrayList<>();
154         List<NodeRemoved> nodeRemoved = new ArrayList<>();
155         List<NodeConnectorUpdated> nodeConnectorUpdated = new ArrayList<>();
156         List<NodeConnectorRemoved> nodeConnectorRemoved = new ArrayList<>();
157
158         /**
159          * default ctor
160          */
161         protected TestInventoryListener() {
162             // do nothing
163         }
164
165         @Override
166         public void onNodeUpdated(NodeUpdated notification) {
167             nodeUpdated.add(notification);
168         }
169
170         @Override
171         public void onNodeRemoved(NodeRemoved notification) {
172             nodeRemoved.add(notification);
173         }
174
175         @Override
176         public void onNodeConnectorUpdated(NodeConnectorUpdated notification) {
177             nodeConnectorUpdated.add(notification);
178         }
179
180         @Override
181         public void onNodeConnectorRemoved(NodeConnectorRemoved notification) {
182             nodeConnectorRemoved.add(notification);
183         }
184     }
185
186 }