2 * Copyright (C) 2014 Red Hat, Inc.
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
8 * Authors : Dave Tucker
11 package org.opendaylight.ovsdb.integrationtest.neutron;
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;
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.UUID;
28 import org.opendaylight.ovsdb.lib.notation.Version;
29 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
30 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
31 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
32 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProvider;
33 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
34 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
35 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
36 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
38 import com.google.common.collect.ImmutableMap;
39 import org.apache.commons.lang3.tuple.ImmutablePair;
40 import org.apache.felix.dm.Component;
41 import org.apache.felix.dm.DependencyManager;
42 import org.junit.After;
43 import org.junit.Assert;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.ops4j.pax.exam.Configuration;
48 import org.ops4j.pax.exam.Option;
49 import org.ops4j.pax.exam.junit.PaxExam;
50 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
51 import org.ops4j.pax.exam.spi.reactors.PerClass;
52 import org.ops4j.pax.exam.util.PathUtils;
53 import org.osgi.framework.Bundle;
54 import org.osgi.framework.BundleContext;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 import java.io.IOException;
59 import java.net.InetAddress;
61 import java.util.Properties;
62 import java.util.concurrent.ExecutionException;
63 import java.util.concurrent.TimeoutException;
65 import javax.inject.Inject;
68 @RunWith(PaxExam.class)
69 @ExamReactorStrategy(PerClass.class)
70 public class NeutronIT extends OvsdbIntegrationTestBase {
71 private Logger log = LoggerFactory.getLogger(NeutronIT.class);
73 private BundleContext bc;
76 private OvsdbConfigurationService ovsdbConfigurationService;
77 private Node node = null;
79 Component of10Provider;
80 Component of13Provider;
83 BridgeConfigurationManager bridgeConfigurationManager;
85 ConfigurationService netVirtConfigurationService;
87 Boolean tearDownBridge = false;
88 ImmutablePair<UUID, Map<String, String>> tearDownOpenVSwitchOtherConfig = null;
90 // Configure the OSGi container
92 public Option[] config() {
95 systemProperty("logback.configurationFile").value(
96 "file:" + PathUtils.getBaseDir()
97 + "/src/test/resources/logback.xml"
99 // To start OSGi console for inspection remotely
100 systemProperty("osgi.console").value("2401"),
102 propagateSystemProperty("ovsdbserver.ipaddress"),
103 propagateSystemProperty("ovsdbserver.port"),
105 ConfigurationBundles.controllerBundles(),
106 ConfigurationBundles.ovsdbLibraryBundles(),
107 ConfigurationBundles.ovsdbDefaultSchemaBundles(),
108 ConfigurationBundles.ovsdbPluginBundles(),
109 ConfigurationBundles.ovsdbNeutronBundles(),
114 private String stateToString(int state) {
118 case Bundle.INSTALLED:
120 case Bundle.RESOLVED:
122 case Bundle.UNINSTALLED:
123 return "UNINSTALLED";
125 return "Not CONVERTED";
130 public void areWeReady() throws InterruptedException, ExecutionException, IOException, TimeoutException {
132 boolean debugit = false;
133 Bundle b[] = bc.getBundles();
134 for (Bundle element : b) {
135 int state = element.getState();
136 if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
137 log.info("Bundle:" + element.getSymbolicName() + " state:"
138 + stateToString(state));
143 log.debug("Do some debugging because some bundle is unresolved");
146 assertFalse(debugit);
150 node = getPluginTestConnection();
151 } catch (Exception e) {
152 fail("Exception : " + e.getMessage());
156 //Register fake NetworkingProviders
157 Properties of10Properties = new Properties();
158 of10Properties.put(Constants.OPENFLOW_VERSION_PROPERTY, Constants.OPENFLOW10);
160 Properties of13Properties = new Properties();
161 of13Properties.put(Constants.OPENFLOW_VERSION_PROPERTY, Constants.OPENFLOW13);
163 DependencyManager dm = new DependencyManager(bc);
165 of10Provider = dm.createComponent();
166 of10Provider.setInterface(NetworkingProvider.class.getName(), of10Properties);
167 of10Provider.setImplementation(new FakeOF10Provider());
169 of13Provider = dm.createComponent();
170 of13Provider.setInterface(NetworkingProvider.class.getName(), of13Properties);
171 of13Provider.setImplementation(new FakeOF13Provider());
173 dm.add(of10Provider);
174 dm.add(of13Provider);
178 public void testPrepareNode() throws Exception {
181 // Create the integration bridge
182 bridgeConfigurationManager.prepareNode(node);
186 ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Bridge.class));
187 Assert.assertEquals(1, bridgeRows.size());
189 Bridge bridgeRow = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeRows.values().iterator().next());
190 Assert.assertEquals(netVirtConfigurationService.getIntegrationBridgeName(), bridgeRow.getName());
192 String uuid = bridgeConfigurationManager.getBridgeUuid(node, netVirtConfigurationService.getIntegrationBridgeName());
193 Assert.assertEquals(uuid, bridgeRow.getUuid().toString());
195 tearDownBridge = true;
199 public void testGetTunnelEndpoint() throws Exception {
202 final String endpointAddress = "10.10.10.10";
204 Map<String, Row> ovsRows = ovsdbConfigurationService.getRows(node,
205 ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
206 OpenVSwitch ovsRow = ovsdbConfigurationService.getTypedRow(node,
208 ovsRows.values().iterator().next());
210 Assert.assertEquals(null, netVirtConfigurationService.getTunnelEndPoint(node));
211 final UUID originalVersion = ovsRow.getVersion();
213 OpenVSwitch updateOvsRow = ovsdbConfigurationService.createTypedRow(node, OpenVSwitch.class);
215 updateOvsRow.setOtherConfig(
216 ImmutableMap.of(netVirtConfigurationService.getTunnelEndpointKey(), endpointAddress));
218 ovsdbConfigurationService.updateRow(node,
219 ovsdbConfigurationService.getTableName(node, OpenVSwitch.class),
221 ovsRow.getUuid().toString(),
222 updateOvsRow.getRow());
224 // Remember original value so it can be restored on tearDown
225 tearDownOpenVSwitchOtherConfig = ImmutablePair.of(ovsRow.getUuid(),
226 ovsRow.getOtherConfigColumn().getData());
228 // Make sure tunnel end point was set
229 Assert.assertEquals(InetAddress.getByName(endpointAddress), netVirtConfigurationService.getTunnelEndPoint(node));
231 // Fetch rows again, and compare tunnel end point values
232 ovsRows = ovsdbConfigurationService.getRows(node,
233 ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
234 ovsRow = ovsdbConfigurationService.getTypedRow(node,
236 ovsRows.values().iterator().next());
238 Assert.assertEquals(ovsRow.getOtherConfigColumn(), updateOvsRow.getOtherConfigColumn());
240 // expect version of row to be changed, due to the update
241 Assert.assertNotEquals(ovsRow.getVersion(), originalVersion);
245 public void testGetOpenflowVersion() throws Exception {
248 Version ovsVersion = this.getOvsVersion();
250 if (ovsVersion.compareTo(Constants.OPENFLOW13_SUPPORTED) < 0) {
251 Assert.assertEquals(Constants.OPENFLOW10, netVirtConfigurationService.getOpenflowVersion(node));
253 Assert.assertEquals(Constants.OPENFLOW13, netVirtConfigurationService.getOpenflowVersion(node));
258 public void testGetDefaultGatewayMacAddress() throws Exception {
259 // Thread.sleep(5000);
260 String defaultGatewayMacAddress = netVirtConfigurationService.getDefaultGatewayMacAddress(node);
262 if (defaultGatewayMacAddress != null) {
263 String[] splits = defaultGatewayMacAddress.split(":");
264 Assert.assertTrue("Unexpected mac format", splits.length == 6);
266 // log.info("testGetDefaultGatewayMacAddress got mac {}", defaultGatewayMacAddress);
270 public void tearDown() throws InterruptedException {
273 if (tearDownBridge) {
275 String uuid = bridgeConfigurationManager.getBridgeUuid(node,
276 netVirtConfigurationService.getIntegrationBridgeName());
277 ovsdbConfigurationService.deleteRow(node, ovsdbConfigurationService.getTableName(node, Bridge.class), uuid);
278 } catch (Exception e) {
279 log.error("tearDownBridge Exception : " + e.getMessage());
281 tearDownBridge = false;
284 if (tearDownOpenVSwitchOtherConfig != null) {
286 OpenVSwitch updateOvsRow = ovsdbConfigurationService.createTypedRow(node, OpenVSwitch.class);
287 updateOvsRow.setOtherConfig(tearDownOpenVSwitchOtherConfig.getRight());
288 ovsdbConfigurationService.updateRow(node,
289 ovsdbConfigurationService.getTableName(node, OpenVSwitch.class),
291 tearDownOpenVSwitchOtherConfig.getLeft().toString(),
292 updateOvsRow.getRow());
293 } catch (Exception e) {
294 log.error("tearDownOpenVSwitchOtherConfig Exception : " + e.getMessage());
296 tearDownOpenVSwitchOtherConfig = null;
299 DependencyManager dm = new DependencyManager(bc);
300 dm.remove(of10Provider);
301 dm.remove(of13Provider);
304 private Version getOvsVersion(){
305 Map<String, Row> ovsRows = ovsdbConfigurationService.getRows(node,
306 ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
307 OpenVSwitch ovsRow = ovsdbConfigurationService.getTypedRow(node,
309 ovsRows.values().iterator().next());
310 return Version.fromString(ovsRow.getOvsVersionColumn().getData().iterator().next());
313 private class FakeOF10Provider implements NetworkingProvider {
316 public String getName() {
321 public boolean supportsServices() {
326 public boolean hasPerTenantTunneling() {
331 public Status handleInterfaceUpdate(String tunnelType, String tunnelKey) {
336 public Status handleInterfaceUpdate(NeutronNetwork network, Node source, Interface intf) {
341 public Status handleInterfaceDelete(String tunnelType, NeutronNetwork network, Node source, Interface intf,
342 boolean isLastInstanceOnNode) {
347 public void initializeFlowRules(Node node) {
352 public void initializeOFFlowRules(Node openflowNode) {
357 private class FakeOF13Provider implements NetworkingProvider {
360 public String getName() {
365 public boolean supportsServices() {
370 public boolean hasPerTenantTunneling() {
375 public Status handleInterfaceUpdate(String tunnelType, String tunnelKey) {
380 public Status handleInterfaceUpdate(NeutronNetwork network, Node source, Interface intf) {
385 public Status handleInterfaceDelete(String tunnelType, NeutronNetwork network, Node source, Interface intf,
386 boolean isLastInstanceOnNode) {
391 public void initializeFlowRules(Node node) {
396 public void initializeOFFlowRules(Node openflowNode) {