e6f4393f83c7d3966f94998890d9f09a590bea23
[openflowplugin.git] / openflowplugin-it / src / test / java / org / opendaylight / openflowplugin / openflow / md / it / SalIntegrationTest.java
1 package org.opendaylight.openflowplugin.openflow.md.it;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.baseModelBundles;
6 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.bindingAwareSalBundles;
7 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.configMinumumBundles;
8 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.flowCapableModelBundles;
9 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.mdSalCoreBundles;
10 import static org.ops4j.pax.exam.CoreOptions.junitBundles;
11 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
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.Test;
22 import org.junit.runner.RunWith;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
25 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
26 import org.opendaylight.controller.sal.binding.api.NotificationService;
27 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioHandler;
28 import org.opendaylight.openflowjava.protocol.impl.clients.SimpleClient;
29 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener;
35 import org.ops4j.pax.exam.Configuration;
36 import org.ops4j.pax.exam.Option;
37 import org.ops4j.pax.exam.junit.PaxExam;
38 import org.osgi.framework.BundleContext;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * @author mirehak
44  */
45 //@RunWith(PaxExam.class)
46 public class SalIntegrationTest {
47
48     private static final Logger LOG = LoggerFactory.getLogger(SalIntegrationTest.class);
49
50     /** base controller package */
51     public static final String ODL = "org.opendaylight.controller";
52     /** controller.model package */
53     public static final String ODL_MODEL = "org.opendaylight.controller.model";
54     /** yangtools package */
55     public static final String YANG = "org.opendaylight.yangtools";
56     /** yangtools.model package */
57     public static final String YANG_MODEL = "org.opendaylight.yangtools.model";
58     /** OFPlugin package */
59     public static final String OFPLUGIN = "org.opendaylight.openflowplugin";
60     /** OFLibrary package */
61     public static final String OFLIBRARY = "org.opendaylight.openflowjava";
62     /** netty.io package */
63     public static final String NETTY = "io.netty";
64
65     @Inject
66     SwitchConnectionProvider switchConnectionProvider;
67
68     @Inject
69     BundleContext ctx;
70
71     @Inject
72     BindingAwareBroker broker;
73
74     /**
75      * @return timeout for case of failure
76      */
77     private static long getFailSafeTimeout() {
78         return 20000;
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("switchConnectionProvider: " + switchConnectionProvider);
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.start();
109
110         switchSim.getScenarioDone().get(getFailSafeTimeout(), TimeUnit.MILLISECONDS);
111         Thread.sleep(2000);
112         assertEquals(1, listener.nodeUpdated.size());
113         assertNotNull(listener.nodeUpdated.get(0));
114
115     }
116
117     /**
118      * @return bundle options
119      */
120     @Configuration
121     public Option[] config() {
122         return options(systemProperty("osgi.console").value("2401"),
123                 mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),
124                 mavenBundle("org.slf4j", "log4j-over-slf4j").versionAsInProject(),
125                 mavenBundle("ch.qos.logback", "logback-core").versionAsInProject(),
126                 mavenBundle("ch.qos.logback", "logback-classic").versionAsInProject(),
127
128                 mavenBundle("org.apache.felix", "org.apache.felix.dependencymanager").versionAsInProject(),
129
130                 mavenBundle(OFLIBRARY, "openflow-protocol-impl").versionAsInProject(),
131                 mavenBundle(OFLIBRARY, "openflow-protocol-api").versionAsInProject(),
132                 mavenBundle(OFLIBRARY, "openflow-protocol-spi").versionAsInProject(),
133
134                 mavenBundle(ODL, "sal").versionAsInProject(),
135                 mavenBundle(ODL, "sal.connection").versionAsInProject(),
136                 mdSalCoreBundles(), baseModelBundles(), flowCapableModelBundles(),
137                 configMinumumBundles(),
138
139                 bindingAwareSalBundles(),
140
141                 mavenBundle(NETTY, "netty-handler").versionAsInProject(),
142                 mavenBundle(NETTY, "netty-buffer").versionAsInProject(),
143                 mavenBundle(NETTY, "netty-common").versionAsInProject(),
144                 mavenBundle(NETTY, "netty-transport").versionAsInProject(),
145                 mavenBundle(NETTY, "netty-codec").versionAsInProject(),
146
147                 mavenBundle(OFLIBRARY, "simple-client").versionAsInProject().start(),
148                 mavenBundle(OFPLUGIN, "openflowplugin").versionAsInProject(), junitBundles(),
149                 mavenBundle("org.opendaylight.controller.thirdparty", "org.openflow.openflowj").versionAsInProject()
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 }