3ab171c945ea77d0945dc46dded19145c7db79c7
[netvirt.git] / integrationtest / src / test / java / org / opendaylight / ovsdb / integrationtest / neutron / NeutronIT.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
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  * Authors : Dave Tucker
9  */
10
11 package org.opendaylight.ovsdb.integrationtest.neutron;
12
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.fail;
16 import static org.ops4j.pax.exam.CoreOptions.junitBundles;
17 import static org.ops4j.pax.exam.CoreOptions.options;
18 import static org.ops4j.pax.exam.CoreOptions.propagateSystemProperty;
19 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
20
21 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
22 import org.opendaylight.controller.sal.core.Node;
23 import org.opendaylight.controller.sal.utils.Status;
24 import org.opendaylight.ovsdb.integrationtest.ConfigurationBundles;
25 import org.opendaylight.ovsdb.integrationtest.OvsdbIntegrationTestBase;
26 import org.opendaylight.ovsdb.lib.notation.Row;
27 import org.opendaylight.ovsdb.lib.notation.Version;
28 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
29 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
30 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProvider;
31 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
32 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
33 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
34 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
35
36 import com.google.common.collect.ImmutableMap;
37 import org.apache.felix.dm.Component;
38 import org.apache.felix.dm.DependencyManager;
39 import org.junit.After;
40 import org.junit.Assert;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.ops4j.pax.exam.Configuration;
45 import org.ops4j.pax.exam.Option;
46 import org.ops4j.pax.exam.junit.PaxExam;
47 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
48 import org.ops4j.pax.exam.spi.reactors.PerClass;
49 import org.ops4j.pax.exam.util.PathUtils;
50 import org.osgi.framework.Bundle;
51 import org.osgi.framework.BundleContext;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 import java.io.IOException;
56 import java.net.InetAddress;
57 import java.util.Map;
58 import java.util.Properties;
59 import java.util.concurrent.ExecutionException;
60 import java.util.concurrent.TimeoutException;
61
62 import javax.inject.Inject;
63
64
65 @RunWith(PaxExam.class)
66 @ExamReactorStrategy(PerClass.class)
67 public class NeutronIT extends OvsdbIntegrationTestBase {
68     private Logger log = LoggerFactory.getLogger(NeutronIT.class);
69     @Inject
70     private BundleContext bc;
71
72     @Inject
73     private OvsdbConfigurationService ovsdbConfigurationService;
74     private Node node = null;
75
76     Component of10Provider;
77     Component of13Provider;
78
79     @Inject
80     BridgeConfigurationManager bridgeConfigurationManager;
81     @Inject
82     org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService netVirtConfigurationService;
83
84     Boolean tearDownBridge = false;
85
86     // Configure the OSGi container
87     @Configuration
88     public Option[] config() {
89         return options(
90                 //
91                 systemProperty("logback.configurationFile").value(
92                         "file:" + PathUtils.getBaseDir()
93                         + "/src/test/resources/logback.xml"
94                 ),
95                 // To start OSGi console for inspection remotely
96                 systemProperty("osgi.console").value("2401"),
97
98                 propagateSystemProperty("ovsdbserver.ipaddress"),
99                 propagateSystemProperty("ovsdbserver.port"),
100
101                 ConfigurationBundles.controllerBundles(),
102                 ConfigurationBundles.ovsdbLibraryBundles(),
103                 ConfigurationBundles.ovsdbDefaultSchemaBundles(),
104                 ConfigurationBundles.ovsdbPluginBundles(),
105                 ConfigurationBundles.ovsdbNeutronBundles(),
106                 junitBundles()
107         );
108     }
109
110     private String stateToString(int state) {
111         switch (state) {
112             case Bundle.ACTIVE:
113                 return "ACTIVE";
114             case Bundle.INSTALLED:
115                 return "INSTALLED";
116             case Bundle.RESOLVED:
117                 return "RESOLVED";
118             case Bundle.UNINSTALLED:
119                 return "UNINSTALLED";
120             default:
121                 return "Not CONVERTED";
122         }
123     }
124
125     @Before
126     public void areWeReady() throws InterruptedException, ExecutionException, IOException, TimeoutException {
127         assertNotNull(bc);
128         boolean debugit = false;
129         Bundle b[] = bc.getBundles();
130         for (Bundle element : b) {
131             int state = element.getState();
132             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
133                 log.info("Bundle:" + element.getSymbolicName() + " state:"
134                          + stateToString(state));
135                 debugit = true;
136             }
137         }
138         if (debugit) {
139             log.debug("Do some debugging because some bundle is unresolved");
140         }
141
142         assertFalse(debugit);
143
144         if (node == null) {
145             try {
146                 node = getPluginTestConnection();
147             } catch (Exception e) {
148                 fail("Exception : " + e.getMessage());
149             }
150         }
151
152         //Register fake NetworkingProviders
153         Properties of10Properties = new Properties();
154         of10Properties.put(Constants.OPENFLOW_VERSION_PROPERTY, Constants.OPENFLOW10);
155
156         Properties of13Properties = new Properties();
157         of13Properties.put(Constants.OPENFLOW_VERSION_PROPERTY, Constants.OPENFLOW13);
158
159         DependencyManager dm = new DependencyManager(bc);
160
161         of10Provider = dm.createComponent();
162         of10Provider.setInterface(NetworkingProvider.class.getName(), of10Properties);
163         of10Provider.setImplementation(new FakeOF10Provider());
164
165         of13Provider = dm.createComponent();
166         of13Provider.setInterface(NetworkingProvider.class.getName(), of13Properties);
167         of13Provider.setImplementation(new FakeOF13Provider());
168
169         dm.add(of10Provider);
170         dm.add(of13Provider);
171     }
172
173     @Test
174     public void testPrepareNode() throws Exception {
175         Thread.sleep(5000);
176
177         // Create the integration bridge
178         bridgeConfigurationManager.prepareNode(node);
179
180         Map<String, Row>
181                 bridgeRows =
182                 ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Bridge.class));
183         Assert.assertEquals(1, bridgeRows.size());
184
185         Bridge bridgeRow = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeRows.values().iterator().next());
186         Assert.assertEquals(netVirtConfigurationService.getIntegrationBridgeName(), bridgeRow.getName());
187
188         String uuid = bridgeConfigurationManager.getBridgeUuid(node, netVirtConfigurationService.getIntegrationBridgeName());
189         Assert.assertEquals(uuid, bridgeRow.getUuid().toString());
190
191         tearDownBridge = true;
192     }
193
194     @Test
195     public void testGetTunnelEndpoint() throws Exception {
196         Thread.sleep(5000);
197
198         final String endpointAddress = "10.10.10.10";
199
200         Map<String, Row> ovsRows = ovsdbConfigurationService.getRows(node,
201                                                               ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
202         OpenVSwitch ovsRow = ovsdbConfigurationService.getTypedRow(node,
203                                                             OpenVSwitch.class,
204                                                             ovsRows.values().iterator().next());
205
206         Assert.assertEquals(null, netVirtConfigurationService.getTunnelEndPoint(node));
207
208         ovsRow.setOtherConfig(ImmutableMap.of(netVirtConfigurationService.getTunnelEndpointKey(), endpointAddress));
209         ovsdbConfigurationService.updateRow(node,
210                                             ovsdbConfigurationService.getTableName(node, OpenVSwitch.class),
211                                             null,
212                                             ovsRow.getUuid().toString(),
213                                             ovsRow.getRow());
214
215         Assert.assertEquals(InetAddress.getByName(endpointAddress), netVirtConfigurationService.getTunnelEndPoint(node));
216     }
217
218     @Test
219     public void testGetOpenflowVersion() throws Exception {
220         Thread.sleep(5000);
221
222         Version ovsVersion = this.getOvsVersion();
223
224         if (ovsVersion.compareTo(Constants.OPENFLOW13_SUPPORTED) < 0) {
225             Assert.assertEquals(Constants.OPENFLOW10, netVirtConfigurationService.getOpenflowVersion(node));
226         } else {
227             Assert.assertEquals(Constants.OPENFLOW13, netVirtConfigurationService.getOpenflowVersion(node));
228         }
229     }
230
231     @Test
232     public void testGetDefaultGatewayMacAddress() throws Exception {
233         // Thread.sleep(5000);
234         String defaultGatewayMacAddress = netVirtConfigurationService.getDefaultGatewayMacAddress(node);
235
236         if (defaultGatewayMacAddress != null) {
237             String[] splits = defaultGatewayMacAddress.split(":");
238             Assert.assertTrue("Unexpected mac format", splits.length == 6);
239         }
240         // log.info("testGetDefaultGatewayMacAddress got mac {}", defaultGatewayMacAddress);
241     }
242
243     @After
244     public void tearDown() throws InterruptedException {
245         Thread.sleep(5000);
246
247         if (tearDownBridge) {
248             String uuid = bridgeConfigurationManager.getBridgeUuid(node,
249                                                                    netVirtConfigurationService.getIntegrationBridgeName());
250             ovsdbConfigurationService.deleteRow(node, ovsdbConfigurationService.getTableName(node, Bridge.class), uuid);
251             tearDownBridge = false;
252         }
253
254         DependencyManager dm = new DependencyManager(bc);
255         dm.remove(of10Provider);
256         dm.remove(of13Provider);
257     }
258
259     private Version getOvsVersion(){
260         Map<String, Row> ovsRows = ovsdbConfigurationService.getRows(node,
261                                                               ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
262         OpenVSwitch ovsRow = ovsdbConfigurationService.getTypedRow(node,
263                                                             OpenVSwitch.class,
264                                                             ovsRows.values().iterator().next());
265         return Version.fromString(ovsRow.getOvsVersionColumn().getData().iterator().next());
266     }
267
268     private class FakeOF10Provider implements NetworkingProvider {
269
270         @Override
271         public String getName() {
272             return null;
273         }
274
275         @Override
276         public boolean supportsServices() {
277             return false;
278         }
279
280         @Override
281         public boolean hasPerTenantTunneling() {
282             return true;
283         }
284
285         @Override
286         public Status handleInterfaceUpdate(String tunnelType, String tunnelKey) {
287             return null;
288         }
289
290         @Override
291         public Status handleInterfaceUpdate(NeutronNetwork network, Node source, Interface intf) {
292             return null;
293         }
294
295         @Override
296         public Status handleInterfaceDelete(String tunnelType, NeutronNetwork network, Node source, Interface intf,
297                                             boolean isLastInstanceOnNode) {
298             return null;
299         }
300
301         @Override
302         public void initializeFlowRules(Node node) {
303
304         }
305
306         @Override
307         public void initializeOFFlowRules(Node openflowNode) {
308
309         }
310     }
311
312     private class FakeOF13Provider implements NetworkingProvider {
313
314         @Override
315         public String getName() {
316             return null;
317         }
318
319         @Override
320         public boolean supportsServices() {
321             return false;
322         }
323
324         @Override
325         public boolean hasPerTenantTunneling() {
326             return false;
327         }
328
329         @Override
330         public Status handleInterfaceUpdate(String tunnelType, String tunnelKey) {
331             return null;
332         }
333
334         @Override
335         public Status handleInterfaceUpdate(NeutronNetwork network, Node source, Interface intf) {
336             return null;
337         }
338
339         @Override
340         public Status handleInterfaceDelete(String tunnelType, NeutronNetwork network, Node source, Interface intf,
341                                             boolean isLastInstanceOnNode) {
342             return null;
343         }
344
345         @Override
346         public void initializeFlowRules(Node node) {
347
348         }
349
350         @Override
351         public void initializeOFFlowRules(Node openflowNode) {
352
353         }
354     }
355 }