From 9e96436790ff9685bda0466a7e352519ce94993d Mon Sep 17 00:00:00 2001 From: Sam Hague Date: Thu, 21 Jul 2016 21:38:49 -0400 Subject: [PATCH] Add support for setting the db schema version The db schema was a field in the ovsdb.yang but it was never copied from the ovsdb to mdsal. Also removed IT test code that was getting the schema version via connecting to the ovsdb nodes. Change-Id: Id7a49f9832c27eec746aeff9d190e9295dbd3244 Signed-off-by: Sam Hague --- .../md/OpenVSwitchUpdateCommand.java | 14 +- .../md/OpenVSwitchUpdateCommandTest.java | 23 ++- .../ovsdb/southbound/it/SouthboundIT.java | 75 ++++++---- .../it/SouthboundIntegrationTestUtils.java | 135 ------------------ 4 files changed, 79 insertions(+), 168 deletions(-) delete mode 100644 southbound/southbound-it/src/test/java/org/opendaylight/ovsdb/southbound/it/SouthboundIntegrationTestUtils.java diff --git a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/transactions/md/OpenVSwitchUpdateCommand.java b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/transactions/md/OpenVSwitchUpdateCommand.java index ea822d1f2..431df7317 100644 --- a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/transactions/md/OpenVSwitchUpdateCommand.java +++ b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/transactions/md/OpenVSwitchUpdateCommand.java @@ -76,7 +76,8 @@ public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand { OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder(); - setVersion(ovsdbNodeBuilder, openVSwitch); + setDbVersion(ovsdbNodeBuilder, openVSwitch); + setOvsVersion(ovsdbNodeBuilder, openVSwitch); setDataPathTypes(ovsdbNodeBuilder, openVSwitch); setInterfaceTypes(ovsdbNodeBuilder, openVSwitch); OpenVSwitch oldEntry = deletedOpenVSwitchRows.get(entry.getKey()); @@ -238,8 +239,7 @@ public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand { } } - private void setVersion(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder, - OpenVSwitch openVSwitch) { + private void setOvsVersion(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder, OpenVSwitch openVSwitch) { try { ovsdbNodeBuilder.setOvsVersion(openVSwitch.getOvsVersionColumn().getData().iterator().next()); } catch (NoSuchElementException e) { @@ -247,6 +247,14 @@ public class OpenVSwitchUpdateCommand extends AbstractTransactionCommand { } } + private void setDbVersion(OvsdbNodeAugmentationBuilder ovsdbNodeBuilder, OpenVSwitch openVSwitch) { + try { + ovsdbNodeBuilder.setDbVersion(openVSwitch.getDbVersionColumn().getData().iterator().next()); + } catch (NoSuchElementException e) { + LOG.debug("db_version is not set for this switch",e); + } + } + private InstanceIdentifier getInstanceIdentifier(OpenVSwitch ovs) { if (ovs.getExternalIdsColumn() != null && ovs.getExternalIdsColumn().getData() != null diff --git a/southbound/southbound-impl/src/test/java/org/opendaylight/ovsdb/southbound/transactions/md/OpenVSwitchUpdateCommandTest.java b/southbound/southbound-impl/src/test/java/org/opendaylight/ovsdb/southbound/transactions/md/OpenVSwitchUpdateCommandTest.java index 043ed9ac5..fcea92dfb 100644 --- a/southbound/southbound-impl/src/test/java/org/opendaylight/ovsdb/southbound/transactions/md/OpenVSwitchUpdateCommandTest.java +++ b/southbound/southbound-impl/src/test/java/org/opendaylight/ovsdb/southbound/transactions/md/OpenVSwitchUpdateCommandTest.java @@ -125,7 +125,9 @@ public class OpenVSwitchUpdateCommandTest { PowerMockito.whenNew(OvsdbNodeAugmentationBuilder.class).withNoArguments().thenReturn(ovsdbNodeBuilder); //suppress the setter methods of the class - MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setVersion", + MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setOvsVersion", + OvsdbNodeAugmentationBuilder.class, OpenVSwitch.class)); + MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setDbVersion", OvsdbNodeAugmentationBuilder.class, OpenVSwitch.class)); MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setDataPathTypes", OvsdbNodeAugmentationBuilder.class, OpenVSwitch.class)); @@ -364,7 +366,7 @@ public class OpenVSwitchUpdateCommandTest { @Test @SuppressWarnings("unchecked") - public void testSetVersion() throws Exception { + public void testSetOvsVersion() throws Exception { OpenVSwitch openVSwitch = mock(OpenVSwitch.class); Column> column = mock(Column.class); when(openVSwitch.getOvsVersionColumn()).thenReturn(column); @@ -374,10 +376,25 @@ public class OpenVSwitchUpdateCommandTest { OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class); when(ovsdbNodeBuilder.setOvsVersion(anyString())).thenReturn(ovsdbNodeBuilder); - Whitebox.invokeMethod(openVSwitchUpdateCommand, "setVersion", ovsdbNodeBuilder, openVSwitch); + Whitebox.invokeMethod(openVSwitchUpdateCommand, "setOvsVersion", ovsdbNodeBuilder, openVSwitch); verify(ovsdbNodeBuilder).setOvsVersion(anyString()); verify(openVSwitch).getOvsVersionColumn(); + } + @Test + @SuppressWarnings("unchecked") + public void testSetDbVersion() throws Exception { + OpenVSwitch openVSwitch = mock(OpenVSwitch.class); + Column> column = mock(Column.class); + when(openVSwitch.getDbVersionColumn()).thenReturn(column); + Set set = new HashSet<>(); + set.add("7.6.1"); + when(column.getData()).thenReturn(set); + OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class); + when(ovsdbNodeBuilder.setOvsVersion(anyString())).thenReturn(ovsdbNodeBuilder); + Whitebox.invokeMethod(openVSwitchUpdateCommand, "setDbVersion", ovsdbNodeBuilder, openVSwitch); + verify(ovsdbNodeBuilder).setDbVersion(anyString()); + verify(openVSwitch).getDbVersionColumn(); } } \ No newline at end of file diff --git a/southbound/southbound-it/src/test/java/org/opendaylight/ovsdb/southbound/it/SouthboundIT.java b/southbound/southbound-it/src/test/java/org/opendaylight/ovsdb/southbound/it/SouthboundIT.java index 6817bfa18..612e32abd 100644 --- a/southbound/southbound-it/src/test/java/org/opendaylight/ovsdb/southbound/it/SouthboundIT.java +++ b/southbound/southbound-it/src/test/java/org/opendaylight/ovsdb/southbound/it/SouthboundIT.java @@ -8,6 +8,7 @@ package org.opendaylight.ovsdb.southbound.it; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.ops4j.pax.exam.CoreOptions.composite; import static org.ops4j.pax.exam.CoreOptions.maven; @@ -16,6 +17,10 @@ import static org.ops4j.pax.exam.CoreOptions.vmOption; import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut; import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder; +import com.google.common.collect.ImmutableBiMap; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.net.InetAddress; @@ -26,10 +31,8 @@ import java.util.HashSet; import java.util.List; import java.util.Properties; import java.util.Set; - import javax.annotation.Nullable; import javax.inject.Inject; - import org.junit.After; import org.junit.Assert; import org.junit.Assume; @@ -44,12 +47,9 @@ import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.controller.mdsal.it.base.AbstractMdsalTestBase; -import org.opendaylight.ovsdb.lib.OvsdbClient; import org.opendaylight.ovsdb.lib.notation.Version; -import org.opendaylight.ovsdb.lib.schema.DatabaseSchema; import org.opendaylight.ovsdb.southbound.SouthboundConstants; import org.opendaylight.ovsdb.southbound.SouthboundMapper; -import org.opendaylight.ovsdb.southbound.SouthboundProvider; import org.opendaylight.ovsdb.southbound.SouthboundUtil; import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils; import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils; @@ -125,6 +125,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.re import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.TrunksBuilder; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey; @@ -145,15 +146,11 @@ import org.ops4j.pax.exam.karaf.options.LogLevelOption; import org.ops4j.pax.exam.options.MavenUrlReference; import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; import org.ops4j.pax.exam.spi.reactors.PerClass; +import org.ops4j.pax.exam.util.Filter; import org.osgi.framework.BundleContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.collect.ImmutableBiMap; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; -import com.google.common.collect.Sets; - /** * Integration tests for southbound-impl * @@ -176,10 +173,9 @@ public class SouthboundIT extends AbstractMdsalTestBase { private static MdsalUtils mdsalUtils = null; private static Node ovsdbNode; private static int testMethodsRemaining; - private static DataBroker dataBroker; private static Version schemaVersion; - private static OvsdbClient ovsdbClient; - private static DatabaseSchema dbSchema; + @Inject @Filter(timeout=60000) + private static DataBroker dataBroker = null; @Inject private BundleContext bundleContext; @@ -385,11 +381,11 @@ public class SouthboundIT extends AbstractMdsalTestBase { } catch (Exception e) { LOG.warn("Failed to setup test", e); } - //dataBroker = getSession().getSALService(DataBroker.class); - Thread.sleep(3000); - dataBroker = SouthboundProvider.getDb(); Assert.assertNotNull("db should not be null", dataBroker); + LOG.info("sleeping for 10s to let the features finish installing"); + Thread.sleep(10000); + addressStr = bundleContext.getProperty(SouthboundITConstants.SERVER_IPADDRESS); String portStr = bundleContext.getProperty(SouthboundITConstants.SERVER_PORT); try { @@ -408,6 +404,7 @@ public class SouthboundIT extends AbstractMdsalTestBase { } mdsalUtils = new MdsalUtils(dataBroker); + assertTrue("Did not find " + SouthboundUtils.OVSDB_TOPOLOGY_ID.getValue(), getOvsdbTopology()); final ConnectionInfo connectionInfo = getConnectionInfo(addressStr, portNumber); final InstanceIdentifier iid = SouthboundUtils.createInstanceIdentifier(connectionInfo); dataBroker.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION, @@ -416,17 +413,10 @@ public class SouthboundIT extends AbstractMdsalTestBase { iid, OPERATIONAL_LISTENER, AsyncDataBroker.DataChangeScope.SUBTREE); ovsdbNode = connectOvsdbNode(connectionInfo); - try { - ovsdbClient = SouthboundIntegrationTestUtils.getTestConnection(this); - assertNotNull("Invalid Client. Check connection params", ovsdbClient); - - dbSchema = ovsdbClient.getSchema(SouthboundIntegrationTestUtils.OPEN_VSWITCH_SCHEMA).get(); - assertNotNull("Invalid dbSchema.", dbSchema); - schemaVersion = dbSchema.getVersion(); - LOG.info("{} schema version = {}", SouthboundIntegrationTestUtils.OPEN_VSWITCH_SCHEMA, schemaVersion); - } catch (Exception e) { - fail("Error accessing schemaVersion in SouthboundIT setUp()." + usage()); - } + OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class); + assertNotNull("The OvsdbNodeAugmentation cannot be null", ovsdbNodeAugmentation); + schemaVersion = Version.fromString(ovsdbNodeAugmentation.getDbVersion()); + LOG.info("schemaVersion = {}", schemaVersion); // Let's count the test methods (we need to use this instead of @AfterClass on teardown() since the latter is // useless with pax-exam) @@ -463,6 +453,30 @@ public class SouthboundIT extends AbstractMdsalTestBase { } } + private Boolean getOvsdbTopology() { + LOG.info("getOvsdbTopology: looking for {}...", SouthboundUtils.OVSDB_TOPOLOGY_ID.getValue()); + Boolean found = false; + final TopologyId topologyId = SouthboundUtils.OVSDB_TOPOLOGY_ID; + InstanceIdentifier path = + InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, new TopologyKey(topologyId)); + for (int i = 0; i < 60; i++) { + Topology topology = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, path); + if (topology != null) { + LOG.info("getOvsdbTopology: found {}...", SouthboundUtils.OVSDB_TOPOLOGY_ID.getValue()); + found = true; + break; + } else { + LOG.info("getOvsdbTopology: still looking ({})...", i); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + LOG.warn("Interrupted while waiting for {}", SouthboundUtils.OVSDB_TOPOLOGY_ID.getValue(), e); + } + } + } + return found; + } + /** * Test passive connection mode. The southbound starts in a listening mode waiting for connections on port * 6640. This test will wait for incoming connections for {@link SouthboundITConstants#CONNECTION_INIT_TIMEOUT} ms. @@ -669,6 +683,13 @@ public class SouthboundIT extends AbstractMdsalTestBase { assertNotNull(ovsdbNodeAugmentation.getOvsVersion()); } + @Test + public void testOvsdbNodeDbVersion() throws InterruptedException { + OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class); + Assert.assertNotNull(ovsdbNodeAugmentation); + assertNotNull(ovsdbNodeAugmentation.getDbVersion()); + } + @Test public void testOpenVSwitchOtherConfig() throws InterruptedException { OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class); diff --git a/southbound/southbound-it/src/test/java/org/opendaylight/ovsdb/southbound/it/SouthboundIntegrationTestUtils.java b/southbound/southbound-it/src/test/java/org/opendaylight/ovsdb/southbound/it/SouthboundIntegrationTestUtils.java deleted file mode 100644 index 00993f8cf..000000000 --- a/southbound/southbound-it/src/test/java/org/opendaylight/ovsdb/southbound/it/SouthboundIntegrationTestUtils.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Copyright © 2015 Red Hat, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.ovsdb.southbound.it; - -import java.io.IOException; -import java.net.InetAddress; -import java.util.Objects; -import java.util.Properties; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.opendaylight.controller.sal.binding.api.BindingAwareProvider; -import org.opendaylight.ovsdb.lib.OvsdbClient; -import org.opendaylight.ovsdb.lib.OvsdbConnection; -import org.opendaylight.ovsdb.lib.OvsdbConnectionListener; -import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper; -import org.osgi.framework.Bundle; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Utilities for OVSDB Southbound integration tests. - */ -public final class SouthboundIntegrationTestUtils { - public final static String OPEN_VSWITCH_SCHEMA = "Open_vSwitch"; - public final static String HARDWARE_VTEP = "hardware_vtep"; - private static final Logger LOG = LoggerFactory.getLogger(SouthboundIntegrationTestUtils.class); - - /** - * Prevent instantiation of a utility class. - */ - private SouthboundIntegrationTestUtils() { - // Nothing to do - } - - public static OvsdbClient getTestConnection(BindingAwareProvider provider) throws IOException, InterruptedException, ExecutionException, TimeoutException { - Properties props = System.getProperties(); - String addressStr = props.getProperty(SouthboundITConstants.SERVER_IPADDRESS); - String portStr = props.getProperty(SouthboundITConstants.SERVER_PORT, SouthboundITConstants.DEFAULT_SERVER_PORT); - String connectionType = props.getProperty(SouthboundITConstants.CONNECTION_TYPE, "active"); - LOG.info("addressStr:{}, portStr:{}, connectionType:{}", addressStr, portStr, connectionType); - - // If the connection type is active, controller connects to the ovsdb-server - if (connectionType.equalsIgnoreCase(SouthboundITConstants.CONNECTION_TYPE_ACTIVE)) { - if (addressStr == null) { - throw new IllegalArgumentException(usage()); - } - - InetAddress address; - try { - address = InetAddress.getByName(addressStr); - } catch (Exception e) { - System.out.println("Unable to resolve " + addressStr); - e.printStackTrace(); - return null; - } - - Integer port; - try { - port = Integer.parseInt(portStr); - } catch (NumberFormatException e) { - System.out.println("Invalid port number : " + portStr); - e.printStackTrace(); - return null; - } - - OvsdbConnection connection = (OvsdbConnection) ServiceHelper.getGlobalInstance(OvsdbConnection.class, provider); - return connection.connect(address, port); - } else if (connectionType.equalsIgnoreCase(SouthboundITConstants.CONNECTION_TYPE_PASSIVE)) { - ExecutorService executor = Executors.newFixedThreadPool(1); - Future passiveConnection = executor.submit(new PassiveListener()); - return passiveConnection.get(60, TimeUnit.SECONDS); - } - throw new IllegalArgumentException("Connection parameter (" + SouthboundITConstants.CONNECTION_TYPE + ") must be either active or passive"); - } - - public static String bundleStateToString(int state) { - switch (state) { - case Bundle.ACTIVE: - return "ACTIVE"; - case Bundle.INSTALLED: - return "INSTALLED"; - case Bundle.RESOLVED: - return "RESOLVED"; - case Bundle.UNINSTALLED: - return "UNINSTALLED"; - default: - return "Not CONVERTED"; - } - } - - private static String usage() { - return "Integration Test needs a valid connection configuration as follows :\n" + - "active connection : mvn -Pintegrationtest -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify\n"+ - "passive connection : mvn -Pintegrationtest -Dovsdbserver.connection=passive verify\n"; - } - - private static class PassiveListener implements Callable, OvsdbConnectionListener { - OvsdbClient client = null; - @Override - public OvsdbClient call() throws Exception { - OvsdbConnection connection = (OvsdbConnection)ServiceHelper.getGlobalInstance(OvsdbConnection.class, this); - connection.registerConnectionListener(this); - while (client == null) { - Thread.sleep(500); - } - return client; - } - - @Override - public void connected(OvsdbClient client) { - this.client = client; - } - - @Override - public void disconnected(OvsdbClient client) { - if (!Objects.equals(this.client.getConnectionInfo(), client.getConnectionInfo())) { - throw new IllegalStateException("disconnected unexpected client"); - } - this.client = null; - } - } - -} -- 2.36.6