Add Interface Table Columns and Version Info
[ovsdb.git] / schemas / Open_vSwitch / src / test / java / org / opendaylight / ovsdb / schema / openvswitch / TypedVSwitchdSchemaIT.java
index eca74105a4babfe027b917cc5f91d0ebacc9c838..cd36d912d58b83879fca3f9466380fadf69abdb6 100644 (file)
@@ -13,18 +13,29 @@ import static org.opendaylight.ovsdb.lib.operations.Operations.op;
 
 import java.io.IOException;
 import java.lang.reflect.InvocationTargetException;
+import java.util.HashMap;
+import java.math.BigInteger;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 
+import com.google.common.collect.ImmutableSet;
 import junit.framework.Assert;
 
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.opendaylight.ovsdb.lib.MonitorCallBack;
+import org.opendaylight.ovsdb.lib.MonitorHandle;
 import org.opendaylight.ovsdb.lib.OvsDBClientImpl;
+import org.opendaylight.ovsdb.lib.message.MonitorRequest;
+import org.opendaylight.ovsdb.lib.message.MonitorRequestBuilder;
+import org.opendaylight.ovsdb.lib.message.MonitorSelect;
 import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
+import org.opendaylight.ovsdb.lib.message.TableUpdate;
+import org.opendaylight.ovsdb.lib.message.TableUpdates;
 import org.opendaylight.ovsdb.lib.message.UpdateNotification;
 import org.opendaylight.ovsdb.lib.notation.Mutator;
 import org.opendaylight.ovsdb.lib.notation.Row;
@@ -33,12 +44,11 @@ import org.opendaylight.ovsdb.lib.operations.OperationResult;
 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
-import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Maps;
+import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
 import com.google.common.util.concurrent.ListenableFuture;
 
@@ -52,32 +62,33 @@ public class TypedVSwitchdSchemaIT extends OvsdbTestBase {
 
     @Test
     public void testTypedBridgeOperations() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+        this.monitorTables();
         this.createTypedBridge();
+        this.createTypedController();
+        this.testCreateTypedPortandInterface();
     }
 
     private void createTypedBridge() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
-        GenericTableSchema bridgeSchema = TyperUtils.getTableSchema(dbSchema, Bridge.class);
-        Bridge bridge = TyperUtils.getTypedRowWrapper(dbSchema, Bridge.class, new Row<GenericTableSchema>());
+        Bridge bridge = ovs.createTypedRowWrapper(Bridge.class);
         bridge.setName(testBridgeName);
-        bridge.setStatus(Maps.newHashMap(ImmutableMap.of("key","value")));
+        bridge.setStatus(ImmutableMap.of("key","value"));
         bridge.setFloodVlans(Sets.newHashSet(34));
 
-        GenericTableSchema ovsTable = TyperUtils.getTableSchema(dbSchema, OpenVSwitch.class);
-        OpenVSwitch openVSwitch = TyperUtils.getTypedRowWrapper(dbSchema, OpenVSwitch.class, new Row<GenericTableSchema>());
+        OpenVSwitch openVSwitch = ovs.createTypedRowWrapper(OpenVSwitch.class);
         openVSwitch.setBridges(Sets.newHashSet(new UUID(testBridgeName)));
 
         int insertOperationIndex = 0;
 
         TransactionBuilder transactionBuilder = ovs.transactBuilder()
-                .add(op.insert(bridgeSchema)
+                .add(op.insert(bridge.getSchema())
                         .withId(testBridgeName)
                         .value(bridge.getNameColumn()))
-                .add(op.update(bridgeSchema)
+                .add(op.update(bridge.getSchema())
                         .set(bridge.getStatusColumn())
                         .set(bridge.getFloodVlansColumn())
                         .where(bridge.getNameColumn().getSchema().opEqual(bridge.getName()))
                         .and(bridge.getNameColumn().getSchema().opEqual(bridge.getName())).build())
-                .add(op.mutate(ovsTable)
+                .add(op.mutate(openVSwitch.getSchema())
                         .addMutation(openVSwitch.getBridgesColumn().getSchema(), Mutator.INSERT,
                                      openVSwitch.getBridgesColumn().getData()));
 
@@ -87,8 +98,122 @@ public class TypedVSwitchdSchemaIT extends OvsdbTestBase {
         // Check if Results matches the number of operations in transaction
         Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
         System.out.println("Insert & Update operation results = " + operationResults);
+        for (OperationResult result : operationResults) {
+            Assert.assertNull(result.getError());
+        }
         testBridgeUuid = operationResults.get(insertOperationIndex).getUuid();
+
+        Row bridgeRow = tableCache.get(bridge.getSchema().getName()).get(testBridgeUuid);
+        Bridge monitoredBridge = ovs.getTypedRowWrapper(Bridge.class, bridgeRow);
+        Assert.assertEquals(monitoredBridge.getNameColumn().getData(), bridge.getNameColumn().getData());
+        Assert.assertNotNull(monitoredBridge.getVersion());
+        Assert.assertNotNull(this.getOpenVSwitchTableUuid());
+    }
+
+    private void createTypedController() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+        Controller controller1 = ovs.createTypedRowWrapper(Controller.class);
+        controller1.setTarget("tcp:1.1.1.1:6640");
+        Controller controller2 = ovs.createTypedRowWrapper(Controller.class);
+        controller2.setTarget("tcp:2.2.2.2:6640");
+
+        Bridge bridge = ovs.getTypedRowWrapper(Bridge.class, null);
+
+        String transactionUuidStr = "controller";
+        TransactionBuilder transactionBuilder = ovs.transactBuilder()
+                .add(op.insert(controller1.getSchema())
+                        .withId(transactionUuidStr)
+                        .value(controller1.getTargetColumn()))
+                .add(op.mutate(bridge.getSchema())
+                        .addMutation(bridge.getControllerColumn().getSchema(), Mutator.INSERT,
+                                     Sets.newHashSet(new UUID(transactionUuidStr)))
+                        .where(bridge.getNameColumn().getSchema().opEqual(testBridgeName))
+                        .build());
+
+        ListenableFuture<List<OperationResult>> results = transactionBuilder.execute();
+        List<OperationResult> operationResults = results.get();
+        Assert.assertFalse(operationResults.isEmpty());
+        // Check if Results matches the number of operations in transaction
+        Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
+        System.out.println("Insert & Mutate operation results for controller1 = " + operationResults);
+        // Check for any errors
+        for (OperationResult result : operationResults) {
+            Assert.assertNull(result.getError());
+        }
+
+        Row bridgeRow = tableCache.get(bridge.getSchema().getName()).get(testBridgeUuid);
+        Bridge monitoredBridge = ovs.getTypedRowWrapper(Bridge.class, bridgeRow);
+        Assert.assertEquals(1, monitoredBridge.getControllerColumn().getData().size());
+
+        transactionBuilder = ovs.transactBuilder()
+                .add(op.insert(controller2.getSchema())
+                        .withId(transactionUuidStr)
+                        .value(controller2.getTargetColumn()))
+                .add(op.mutate(bridge.getSchema())
+                        .addMutation(bridge.getControllerColumn().getSchema(), Mutator.INSERT,
+                                Sets.newHashSet(new UUID(transactionUuidStr)))
+                        .where(bridge.getNameColumn().getSchema().opEqual(testBridgeName))
+                        .build());
+
+        results = transactionBuilder.execute();
+        operationResults = results.get();
+        Assert.assertFalse(operationResults.isEmpty());
+        // Check if Results matches the number of operations in transaction
+        Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
+        System.out.println("Insert & Mutate operation results for controller2 = " + operationResults);
+        // Check for any errors
+        for (OperationResult result : operationResults) {
+            Assert.assertNull(result.getError());
+        }
+
+        bridgeRow = tableCache.get(bridge.getSchema().getName()).get(testBridgeUuid);
+        monitoredBridge = ovs.getTypedRowWrapper(Bridge.class, bridgeRow);
+        Assert.assertEquals(2, monitoredBridge.getControllerColumn().getData().size());
     }
+
+    private void testCreateTypedPortandInterface() throws InterruptedException, ExecutionException{
+        String portUuidStr = "testPort";
+        String intfUuidStr = "testIntf";
+        Port port = ovs.createTypedRowWrapper(Port.class);
+        port.setName("testPort");
+        port.setTag(ImmutableSet.of(BigInteger.ONE));
+        port.setMac(ImmutableSet.of("00:00:00:00:00:01"));
+        port.setInterfaces(ImmutableSet.of(new UUID(intfUuidStr)));
+
+        Interface intf = ovs.createTypedRowWrapper(Interface.class);
+        intf.setName(port.getNameColumn().getData());
+        intf.setExternalIds(ImmutableMap.of("vm-id", "12345abcedf78910"));
+
+        Bridge bridge = ovs.getTypedRowWrapper(Bridge.class, null);
+        TransactionBuilder transactionBuilder = ovs.transactBuilder()
+                .add(op.insert(port.getSchema())
+                        .withId(portUuidStr)
+                        .value(port.getNameColumn())
+                        .value(port.getMacColumn()))
+                .add(op.insert(intf.getSchema())
+                        .withId(intfUuidStr)
+                        .value(intf.getNameColumn()))
+                .add(op.update(port.getSchema())
+                        .set(port.getTagColumn())
+                        .set(port.getMacColumn())
+                        .set(port.getInterfacesColumn())
+                        .where(port.getNameColumn().getSchema().opEqual(port.getName()))
+                        .build())
+                .add(op.update(intf.getSchema())
+                        .set(intf.getExternalIdsColumn())
+                        .where(intf.getNameColumn().getSchema().opEqual(intf.getName()))
+                        .build())
+                .add(op.mutate(bridge.getSchema())
+                        .addMutation(bridge.getPortsColumn().getSchema(), Mutator.INSERT, Sets.newHashSet(new UUID(portUuidStr)))
+                        .where(bridge.getNameColumn().getSchema().opEqual(testBridgeName))
+                        .build());
+        ListenableFuture<List<OperationResult>> results = transactionBuilder.execute();
+        List<OperationResult> operationResults = results.get();
+        Assert.assertFalse(operationResults.isEmpty());
+        // Check if Results matches the number of operations in transaction
+        Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
+        System.out.println("Insert & Mutate operation results for Port and Interface = " + operationResults);
+    }
+
     public void testGetDBs() throws ExecutionException, InterruptedException {
         ListenableFuture<List<String>> databases = ovs.getDatabases();
         List<String> dbNames = databases.get();
@@ -103,6 +228,57 @@ public class TypedVSwitchdSchemaIT extends OvsdbTestBase {
         Assert.assertTrue(OPEN_VSWITCH_SCHEMA+" schema is not supported by the switch", hasOpenVswitchSchema);
     }
 
+    private UUID getOpenVSwitchTableUuid() {
+        OpenVSwitch openVSwitch = ovs.getTypedRowWrapper(OpenVSwitch.class, null);
+        Map<UUID, Row> ovsTable = tableCache.get(openVSwitch.getSchema().getName());
+        if (ovsTable != null) {
+            if (ovsTable.keySet().size() >= 1) {
+                return (UUID)ovsTable.keySet().toArray()[0];
+            }
+        }
+        return null;
+    }
+
+    public void monitorTables() throws ExecutionException, InterruptedException, IOException {
+        Assert.assertNotNull(dbSchema);
+        Bridge bridge = ovs.createTypedRowWrapper(Bridge.class);
+        OpenVSwitch openVSwitch = ovs.createTypedRowWrapper(OpenVSwitch.class);
+
+        List<MonitorRequest<GenericTableSchema>> monitorRequests = Lists.newArrayList();
+        MonitorRequestBuilder<GenericTableSchema> bridgeBuilder = MonitorRequestBuilder.builder(bridge.getSchema());
+        MonitorRequestBuilder<GenericTableSchema> ovsTableBuilder = MonitorRequestBuilder.builder(openVSwitch.getSchema());
+        monitorRequests.add(bridgeBuilder.with(new MonitorSelect(true, true, true, true)).build());
+        monitorRequests.add(ovsTableBuilder.with(new MonitorSelect(true, true, true, true)).build());
+
+        MonitorHandle monitor = ovs.monitor(dbSchema, monitorRequests, new UpdateMonitor());
+        Assert.assertNotNull(monitor);
+    }
+
+    static Map<String, Map<UUID, Row>> tableCache = new HashMap<String, Map<UUID, Row>>();
+    private static class UpdateMonitor implements MonitorCallBack {
+        @Override
+        public void update(TableUpdates result) {
+            for (String tableName : result.getUpdates().keySet()) {
+                Map<UUID, Row> tUpdate = tableCache.get(tableName);
+                TableUpdate update = result.getUpdates().get(tableName);
+                if (update.getNew() != null) {
+                    if (tUpdate == null) {
+                        tUpdate = new HashMap<UUID, Row>();
+                        tableCache.put(tableName, tUpdate);
+                    }
+                    tUpdate.put(update.getUuid(), update.getNew());
+                } else {
+                    tUpdate.remove(update.getUuid());
+                }
+            }
+        }
+
+        @Override
+        public void exception(Throwable t) {
+            System.out.println("Exception t = " + t);
+        }
+    }
+
     @Before
     public  void setUp() throws IOException, ExecutionException, InterruptedException {
         if (ovs != null) {
@@ -120,23 +296,21 @@ public class TypedVSwitchdSchemaIT extends OvsdbTestBase {
 
     @After
     public void tearDown() throws InterruptedException, ExecutionException {
-        GenericTableSchema bridgeSchema = TyperUtils.getTableSchema(dbSchema, Bridge.class);
-        Bridge bridge = TyperUtils.getTypedRowWrapper(dbSchema, Bridge.class, null);
-
-        GenericTableSchema ovsTable = TyperUtils.getTableSchema(dbSchema, OpenVSwitch.class);
-        OpenVSwitch openVSwitch = TyperUtils.getTypedRowWrapper(dbSchema, OpenVSwitch.class, null);
+        Bridge bridge = ovs.getTypedRowWrapper(Bridge.class, null);
+        OpenVSwitch openVSwitch = ovs.getTypedRowWrapper(OpenVSwitch.class, null);
 
         ListenableFuture<List<OperationResult>> results = ovs.transactBuilder()
-                .add(op.delete(bridgeSchema)
+                .add(op.delete(bridge.getSchema())
                         .where(bridge.getNameColumn().getSchema().opEqual(testBridgeName))
                         .build())
-                .add(op.mutate(ovsTable)
+                .add(op.mutate(openVSwitch.getSchema())
                         .addMutation(openVSwitch.getBridgesColumn().getSchema(), Mutator.DELETE, Sets.newHashSet(testBridgeUuid)))
                 .add(op.commit(true))
                 .execute();
 
         List<OperationResult> operationResults = results.get();
         System.out.println("Delete operation results = " + operationResults);
+        tableCache = new HashMap<String, Map<UUID, Row>>();
     }
 
     @Override