Bug 1334 - Change the Typed Classes in Open_vSwitch and hardware_vtep schema
[ovsdb.git] / integrationtest / src / test / java / org / opendaylight / ovsdb / integrationtest / plugin / OvsdbPluginIT.java
index a3c8c77f6c93d9f5a8e7fbea2b3acf0f58a63a61..db0395e65bfe3b5daa74301b10d95fab36bf6592 100644 (file)
@@ -9,15 +9,20 @@
  */
 package org.opendaylight.ovsdb.integrationtest.plugin;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 import static org.ops4j.pax.exam.CoreOptions.junitBundles;
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
 import static org.ops4j.pax.exam.CoreOptions.options;
 import static org.ops4j.pax.exam.CoreOptions.propagateSystemProperty;
 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
 
-import java.io.IOException;
 import java.util.List;
+import java.util.concurrent.ConcurrentMap;
 
 import javax.inject.Inject;
 
@@ -26,9 +31,18 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.opendaylight.controller.sal.core.Node;
 import org.opendaylight.controller.sal.utils.ServiceHelper;
+import org.opendaylight.controller.sal.utils.Status;
 import org.opendaylight.ovsdb.integrationtest.ConfigurationBundles;
 import org.opendaylight.ovsdb.integrationtest.OvsdbIntegrationTestBase;
+import org.opendaylight.ovsdb.lib.OvsdbClient;
+import org.opendaylight.ovsdb.lib.OvsdbConnectionInfo;
+import org.opendaylight.ovsdb.lib.notation.Row;
+import org.opendaylight.ovsdb.plugin.Connection;
+import org.opendaylight.ovsdb.plugin.IConnectionServiceInternal;
 import org.opendaylight.ovsdb.plugin.OVSDBConfigService;
+import org.opendaylight.ovsdb.plugin.StatusWithUuid;
+import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
+import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
 import org.ops4j.pax.exam.Configuration;
 import org.ops4j.pax.exam.Option;
 import org.ops4j.pax.exam.junit.PaxExam;
@@ -38,6 +52,9 @@ import org.osgi.framework.BundleContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Sets;
+
 @RunWith(PaxExam.class)
 public class OvsdbPluginIT extends OvsdbIntegrationTestBase {
     private Logger log = LoggerFactory.getLogger(OvsdbPluginIT.class);
@@ -45,6 +62,7 @@ public class OvsdbPluginIT extends OvsdbIntegrationTestBase {
     private BundleContext bc;
     private OVSDBConfigService ovsdbConfigService = null;
     private Node node = null;
+    private OvsdbClient client = null;
 
     // Configure the OSGi container
     @Configuration
@@ -63,6 +81,8 @@ public class OvsdbPluginIT extends OvsdbIntegrationTestBase {
 
             ConfigurationBundles.controllerBundles(),
             ConfigurationBundles.ovsdbLibraryBundles(),
+            ConfigurationBundles.ovsdbDefaultSchemaBundles(),
+            mavenBundle("org.opendaylight.ovsdb", "ovsdb_plugin").versionAsInProject(),
             junitBundles()
         );
     }
@@ -99,22 +119,102 @@ public class OvsdbPluginIT extends OvsdbIntegrationTestBase {
             log.debug("Do some debugging because some bundle is unresolved");
         }
 
-        // Assert if true, if false we are good to go!
         assertFalse(debugit);
         try {
-            node = getTestConnection();
-        } catch (IOException e) {
-            e.printStackTrace();
+            node = getPluginTestConnection();
+        } catch (Exception e) {
+            fail("Exception : "+e.getMessage());
         }
         this.ovsdbConfigService = (OVSDBConfigService)ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
     }
 
     @Test
-    public void tableTest() throws Exception {
-        assertNotNull("Invalid Node. Check connection params", node);
-        Thread.sleep(3000); // Wait for a few seconds to get the Schema exchange done
+    public void apiTests() throws Exception {
+        Thread.sleep(5000);
+        IConnectionServiceInternal connectionService = (IConnectionServiceInternal)ServiceHelper.getGlobalInstance(IConnectionServiceInternal.class, this);
+
+        // Check for the ovsdb Connection as seen by the Plugin layer
+        assertNotNull(connectionService.getNodes());
+        assertTrue(connectionService.getNodes().size() > 0);
+        Node node = connectionService.getNodes().get(0);
+        Connection connection = connectionService.getConnection(node);
+        OvsdbConnectionInfo connectionInfo = connection.getClient().getConnectionInfo();
+        String identifier = IDENTIFIER;
+        if (connectionInfo.getType().equals(OvsdbConnectionInfo.ConnectionType.PASSIVE)) {
+            identifier = connectionInfo.getRemoteAddress().getHostAddress()+":"+connectionInfo.getRemotePort();
+        }
+        assertEquals(Node.fromString("OVS|"+identifier), connectionService.getNodes().get(0));
+        System.out.println("Nodes = "+ connectionService.getNodes());
+        /*
+         * Test sequence :
+         * 1. Print Cache and Assert to make sure the bridge is not created yet.
+         * 2. Create a bridge with a valid parent_uuid & Assert to make sure the return status is success.
+         * 3. Assert to make sure the bridge is created with a valid Uuid.
+         * 4. Delete the bridge & Assert to make sure the return status is success.
+         * 5. Assert to make sure the bridge is deleted
+         */
+
+        this.endToEndApiTest(connection, getOpenVSwitchTableUUID(connection));
+
+        /*
+         * Repeat all of the above tests without the parent_uuid
+         */
+
+        this.endToEndApiTest(connection, null);
+    }
+
+    public void endToEndApiTest(Connection connection, String parentUuid) throws Exception {
+        // 1. Print Cache and Assert to make sure the bridge is not created yet.
+        printCache();
+
+        // 2. Create a bridge with a valid parent_uuid & Assert to make sure the return status is success.
+        StatusWithUuid status = insertBridge(connection, parentUuid);
+        assertTrue(status.isSuccess());
+
+        Thread.sleep(2000); // TODO : Remove this Sleep once the Select operation is resolved.
+
+        // 3. Assert to make sure the bridge is created with a valid Uuid.
+        printCache();
+        Bridge bridge = connection.getClient().getTypedRowWrapper(Bridge.class, null);
+        Row bridgeRow = ovsdbConfigService.getRow(node, bridge.getSchema().getName(), status.getUuid().toString());
+        assertNotNull(bridgeRow);
+        bridge = connection.getClient().getTypedRowWrapper(Bridge.class, bridgeRow);
+        assertEquals(bridge.getUuid(), status.getUuid());
+
+        // 4. Delete the bridge & Assert to make sure the return status is success.
+        Status delStatus = ovsdbConfigService.deleteRow(node, bridge.getSchema().getName(), status.getUuid().toString());
+        assertTrue(delStatus.isSuccess());
+        Thread.sleep(2000); // TODO : Remove this Sleep once the Select operation is resolved.
+
+        // 5. Assert to make sure the bridge is deleted
+        bridgeRow = ovsdbConfigService.getRow(node, bridge.getSchema().getName(), status.getUuid().toString());
+        assertNull(bridgeRow);
+    }
+
+    public StatusWithUuid insertBridge(Connection connection, String parentUuid) throws Exception {
+        Bridge bridge = connection.getClient().createTypedRowWrapper(Bridge.class);
+        bridge.setName("br_test1");
+        bridge.setStatus(ImmutableMap.of("key", "value"));
+        bridge.setFloodVlans(Sets.newHashSet(34L));
+        return ovsdbConfigService.insertRow(node, bridge.getSchema().getName(), parentUuid, bridge.getRow());
+    }
+
+    public String getOpenVSwitchTableUUID(Connection connection) throws Exception {
+        OpenVSwitch openVSwitch = connection.getClient().getTypedRowWrapper(OpenVSwitch.class, null);
+        ConcurrentMap<String, Row> row = ovsdbConfigService.getRows(node, openVSwitch.getSchema().getName());
+        if (row == null || row.size() == 0) return null;
+        return (String)row.keySet().toArray()[0];
+    }
+
+    public void printCache() throws Exception {
         List<String> tables = ovsdbConfigService.getTables(node);
         System.out.println("Tables = "+tables);
         assertNotNull(tables);
+        for (String table : tables) {
+            System.out.println("Table "+table);
+            ConcurrentMap<String,Row> row = ovsdbConfigService.getRows(node, table);
+            System.out.println(row);
+        }
     }
+
 }