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 of13Provider;
82 BridgeConfigurationManager bridgeConfigurationManager;
84 ConfigurationService netVirtConfigurationService;
86 Boolean tearDownBridge = false;
87 ImmutablePair<UUID, Map<String, String>> tearDownOpenVSwitchOtherConfig = null;
89 // Configure the OSGi container
91 public Option[] config() {
94 systemProperty("logback.configurationFile").value(
95 "file:" + PathUtils.getBaseDir()
96 + "/src/test/resources/logback.xml"
98 // To start OSGi console for inspection remotely
99 systemProperty("osgi.console").value("2401"),
101 propagateSystemProperty("ovsdbserver.ipaddress"),
102 propagateSystemProperty("ovsdbserver.port"),
104 ConfigurationBundles.controllerBundles(),
105 ConfigurationBundles.ovsdbLibraryBundles(),
106 ConfigurationBundles.ovsdbDefaultSchemaBundles(),
107 ConfigurationBundles.ovsdbPluginBundles(),
108 ConfigurationBundles.ovsdbNeutronBundles(),
114 public void areWeReady() throws InterruptedException, ExecutionException, IOException, TimeoutException {
116 boolean debugit = false;
117 Bundle b[] = bc.getBundles();
118 for (Bundle element : b) {
119 int state = element.getState();
120 if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
121 log.info("Bundle:" + element.getSymbolicName() + " state:"
122 + stateToString(state));
127 log.debug("Do some debugging because some bundle is unresolved");
130 assertFalse(debugit);
134 node = getPluginTestConnection();
135 } catch (Exception e) {
136 fail("Exception : " + e.getMessage());
140 //Register fake NetworkingProviders
141 Properties of13Properties = new Properties();
142 of13Properties.put(Constants.OPENFLOW_VERSION_PROPERTY, Constants.OPENFLOW13);
144 DependencyManager dm = new DependencyManager(bc);
146 of13Provider = dm.createComponent();
147 of13Provider.setInterface(NetworkingProvider.class.getName(), of13Properties);
148 of13Provider.setImplementation(new FakeOF13Provider());
150 dm.add(of13Provider);
154 public void testPrepareNode() throws Exception {
157 // Create the integration bridge
158 bridgeConfigurationManager.prepareNode(node);
162 ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Bridge.class));
163 Assert.assertEquals(1, bridgeRows.size());
165 Bridge bridgeRow = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeRows.values().iterator().next());
166 Assert.assertEquals(netVirtConfigurationService.getIntegrationBridgeName(), bridgeRow.getName());
168 String uuid = bridgeConfigurationManager.getBridgeUuid(node, netVirtConfigurationService.getIntegrationBridgeName());
169 Assert.assertEquals(uuid, bridgeRow.getUuid().toString());
171 tearDownBridge = true;
175 public void testGetTunnelEndpoint() throws Exception {
178 final String endpointAddress = "10.10.10.10";
180 Map<String, Row> ovsRows = ovsdbConfigurationService.getRows(node,
181 ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
182 OpenVSwitch ovsRow = ovsdbConfigurationService.getTypedRow(node,
184 ovsRows.values().iterator().next());
186 Assert.assertEquals(null, netVirtConfigurationService.getTunnelEndPoint(node));
187 final UUID originalVersion = ovsRow.getVersion();
189 OpenVSwitch updateOvsRow = ovsdbConfigurationService.createTypedRow(node, OpenVSwitch.class);
191 updateOvsRow.setOtherConfig(
192 ImmutableMap.of(netVirtConfigurationService.getTunnelEndpointKey(), endpointAddress));
194 ovsdbConfigurationService.updateRow(node,
195 ovsdbConfigurationService.getTableName(node, OpenVSwitch.class),
197 ovsRow.getUuid().toString(),
198 updateOvsRow.getRow());
200 // Remember original value so it can be restored on tearDown
201 tearDownOpenVSwitchOtherConfig = ImmutablePair.of(ovsRow.getUuid(),
202 ovsRow.getOtherConfigColumn().getData());
204 // Make sure tunnel end point was set
205 Assert.assertEquals(InetAddress.getByName(endpointAddress), netVirtConfigurationService.getTunnelEndPoint(node));
207 // Fetch rows again, and compare tunnel end point values
208 ovsRows = ovsdbConfigurationService.getRows(node,
209 ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
210 ovsRow = ovsdbConfigurationService.getTypedRow(node,
212 ovsRows.values().iterator().next());
214 Assert.assertEquals(ovsRow.getOtherConfigColumn(), updateOvsRow.getOtherConfigColumn());
216 // expect version of row to be changed, due to the update
217 Assert.assertNotEquals(ovsRow.getVersion(), originalVersion);
221 public void testGetOpenflowVersion() throws Exception {
224 Version ovsVersion = this.getOvsVersion();
225 Assert.assertTrue(ovsVersion.compareTo(Constants.OPENFLOW13_SUPPORTED) >= 0);
226 Assert.assertEquals(Constants.OPENFLOW13, netVirtConfigurationService.getOpenflowVersion(node));
230 public void testGetDefaultGatewayMacAddress() throws Exception {
231 // Thread.sleep(5000);
232 String defaultGatewayMacAddress = netVirtConfigurationService.getDefaultGatewayMacAddress(node);
234 if (defaultGatewayMacAddress != null) {
235 String[] splits = defaultGatewayMacAddress.split(":");
236 Assert.assertTrue("Unexpected mac format", splits.length == 6);
238 // log.info("testGetDefaultGatewayMacAddress got mac {}", defaultGatewayMacAddress);
242 public void tearDown() throws InterruptedException {
245 if (tearDownBridge) {
247 String uuid = bridgeConfigurationManager.getBridgeUuid(node,
248 netVirtConfigurationService.getIntegrationBridgeName());
249 ovsdbConfigurationService.deleteRow(node, ovsdbConfigurationService.getTableName(node, Bridge.class), uuid);
250 } catch (Exception e) {
251 log.error("tearDownBridge Exception : " + e.getMessage());
253 tearDownBridge = false;
256 if (tearDownOpenVSwitchOtherConfig != null) {
258 OpenVSwitch updateOvsRow = ovsdbConfigurationService.createTypedRow(node, OpenVSwitch.class);
259 updateOvsRow.setOtherConfig(tearDownOpenVSwitchOtherConfig.getRight());
260 ovsdbConfigurationService.updateRow(node,
261 ovsdbConfigurationService.getTableName(node, OpenVSwitch.class),
263 tearDownOpenVSwitchOtherConfig.getLeft().toString(),
264 updateOvsRow.getRow());
265 } catch (Exception e) {
266 log.error("tearDownOpenVSwitchOtherConfig Exception : " + e.getMessage());
268 tearDownOpenVSwitchOtherConfig = null;
271 DependencyManager dm = new DependencyManager(bc);
272 dm.remove(of13Provider);
275 private Version getOvsVersion(){
276 Map<String, Row> ovsRows = ovsdbConfigurationService.getRows(node,
277 ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
278 OpenVSwitch ovsRow = ovsdbConfigurationService.getTypedRow(node,
280 ovsRows.values().iterator().next());
281 return Version.fromString(ovsRow.getOvsVersionColumn().getData().iterator().next());
284 private class FakeOF13Provider implements NetworkingProvider {
287 public String getName() {
292 public boolean supportsServices() {
297 public boolean hasPerTenantTunneling() {
302 public Status handleInterfaceUpdate(String tunnelType, String tunnelKey) {
307 public Status handleInterfaceUpdate(NeutronNetwork network, Node source, Interface intf) {
312 public Status handleInterfaceDelete(String tunnelType, NeutronNetwork network, Node source, Interface intf,
313 boolean isLastInstanceOnNode) {
318 public void initializeFlowRules(Node node) {
323 public void initializeOFFlowRules(Node openflowNode) {