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