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 if (ovsVersion.compareTo(Constants.OPENFLOW13_SUPPORTED) >= 0) {
226 Assert.assertEquals(Constants.OPENFLOW13, netVirtConfigurationService.getOpenflowVersion(node));
231 public void testGetDefaultGatewayMacAddress() throws Exception {
232 // Thread.sleep(5000);
233 String defaultGatewayMacAddress = netVirtConfigurationService.getDefaultGatewayMacAddress(node);
235 if (defaultGatewayMacAddress != null) {
236 String[] splits = defaultGatewayMacAddress.split(":");
237 Assert.assertTrue("Unexpected mac format", splits.length == 6);
239 // log.info("testGetDefaultGatewayMacAddress got mac {}", defaultGatewayMacAddress);
243 public void tearDown() throws InterruptedException {
246 if (tearDownBridge) {
248 String uuid = bridgeConfigurationManager.getBridgeUuid(node,
249 netVirtConfigurationService.getIntegrationBridgeName());
250 ovsdbConfigurationService.deleteRow(node, ovsdbConfigurationService.getTableName(node, Bridge.class), uuid);
251 } catch (Exception e) {
252 log.error("tearDownBridge Exception : " + e.getMessage());
254 tearDownBridge = false;
257 if (tearDownOpenVSwitchOtherConfig != null) {
259 OpenVSwitch updateOvsRow = ovsdbConfigurationService.createTypedRow(node, OpenVSwitch.class);
260 updateOvsRow.setOtherConfig(tearDownOpenVSwitchOtherConfig.getRight());
261 ovsdbConfigurationService.updateRow(node,
262 ovsdbConfigurationService.getTableName(node, OpenVSwitch.class),
264 tearDownOpenVSwitchOtherConfig.getLeft().toString(),
265 updateOvsRow.getRow());
266 } catch (Exception e) {
267 log.error("tearDownOpenVSwitchOtherConfig Exception : " + e.getMessage());
269 tearDownOpenVSwitchOtherConfig = null;
272 DependencyManager dm = new DependencyManager(bc);
273 dm.remove(of13Provider);
276 private Version getOvsVersion(){
277 Map<String, Row> ovsRows = ovsdbConfigurationService.getRows(node,
278 ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
279 OpenVSwitch ovsRow = ovsdbConfigurationService.getTypedRow(node,
281 ovsRows.values().iterator().next());
282 return Version.fromString(ovsRow.getOvsVersionColumn().getData().iterator().next());
285 private class FakeOF13Provider implements NetworkingProvider {
288 public String getName() {
293 public boolean supportsServices() {
298 public boolean hasPerTenantTunneling() {
303 public Status handleInterfaceUpdate(String tunnelType, String tunnelKey) {
308 public Status handleInterfaceUpdate(NeutronNetwork network, Node source, Interface intf) {
313 public Status handleInterfaceDelete(String tunnelType, NeutronNetwork network, Node source, Interface intf,
314 boolean isLastInstanceOnNode) {
319 public void initializeFlowRules(Node node) {
324 public void initializeOFFlowRules(Node openflowNode) {