Merge "Remove unused northbound"
[ovsdb.git] / library / it / src / test / java / org / opendaylight / ovsdb / integrationtest / ovsdbclient / OvsdbClientTestTypedIT.java
1 /*
2  * Copyright (C) 2014 EBay Software Foundation
3  *
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
7  *
8  * Authors : Ashwin Raveendran, Madhu Venugopal
9  */
10
11 package org.opendaylight.ovsdb.integrationtest.ovsdbclient;
12
13 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
14
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.Sets;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import java.io.IOException;
19 import java.lang.reflect.InvocationTargetException;
20 import java.util.List;
21 import java.util.Set;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.TimeoutException;
24 import org.junit.After;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.opendaylight.ovsdb.lib.OvsdbClient;
30 import org.opendaylight.ovsdb.lib.it.LibraryIntegrationTestBase;
31 import org.opendaylight.ovsdb.lib.it.LibraryIntegrationTestUtils;
32 import org.opendaylight.ovsdb.lib.notation.Mutator;
33 import org.opendaylight.ovsdb.lib.notation.UUID;
34 import org.opendaylight.ovsdb.lib.operations.OperationResult;
35 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
36 import org.opendaylight.ovsdb.lib.schema.ColumnSchema;
37 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
38 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
39 import org.opendaylight.ovsdb.lib.schema.TableSchema;
40 import org.ops4j.pax.exam.junit.PaxExam;
41 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
42 import org.ops4j.pax.exam.spi.reactors.PerSuite;
43
44 @RunWith(PaxExam.class)
45 @ExamReactorStrategy(PerSuite.class)
46 public class OvsdbClientTestTypedIT extends LibraryIntegrationTestBase {
47
48     OvsdbClient ovs;
49     DatabaseSchema dbSchema = null;
50     static String testBridgeName = "br_test";
51     static UUID testBridgeUuid = null;
52
53     /**
54      * Test creation of statically typed bridge table as defined in
55      * ovs-vswitchd.conf.db with get/set for all relevant columns. The
56      * SETDATA methods for "name", "status" and "flood_vlans" columns
57      * are verified.
58      */
59     @Test
60     public void testTypedBridgeCreate() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
61         TestBridge rBridge = ovs.createTypedRowWrapper(TestBridge.class);
62         rBridge.setName(testBridgeName);
63         rBridge.setStatus(ImmutableMap.of("key","value"));
64         rBridge.setFloodVlans(Sets.newHashSet(34));
65
66         GenericTableSchema ovsTable = dbSchema.table("Open_vSwitch", GenericTableSchema.class);
67         ColumnSchema<GenericTableSchema, Set<UUID>> bridges = ovsTable.multiValuedColumn("bridges", UUID.class);
68
69         String namedUuid = "br_test";
70         int insertOperationIndex = 0;
71
72         TransactionBuilder transactionBuilder = ovs.transactBuilder(dbSchema)
73                 .add(op.insert(rBridge)
74                         .withId(namedUuid))
75                 .add(op.mutate(ovsTable)
76                         .addMutation(bridges, Mutator.INSERT, Sets.newHashSet(new UUID(namedUuid))));
77
78         ListenableFuture<List<OperationResult>> results = transactionBuilder.execute();
79         List<OperationResult> operationResults = results.get();
80         Assert.assertFalse(operationResults.isEmpty());
81         // Check if Results matches the number of operations in transaction
82         Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
83         System.out.println("Insert & Update operation results = " + operationResults);
84         testBridgeUuid = operationResults.get(insertOperationIndex).getUuid();
85     }
86
87     public void testGetDBs() throws ExecutionException, InterruptedException {
88         ListenableFuture<List<String>> databases = ovs.getDatabases();
89         List<String> dbNames = databases.get();
90         Assert.assertNotNull(dbNames);
91         boolean hasOpenVswitchSchema = false;
92         for(String dbName : dbNames) {
93            if (dbName.equals(LibraryIntegrationTestUtils.OPEN_VSWITCH_SCHEMA)) {
94                 hasOpenVswitchSchema = true;
95                 break;
96            }
97         }
98         Assert.assertTrue(LibraryIntegrationTestUtils.OPEN_VSWITCH_SCHEMA
99                 + " schema is not supported by the switch", hasOpenVswitchSchema);
100     }
101
102     @Before
103     public  void setUp() throws IOException, ExecutionException, InterruptedException, TimeoutException {
104         if (ovs != null) {
105             return;
106         }
107         ovs = LibraryIntegrationTestUtils.getTestConnection(this);
108         testGetDBs();
109         dbSchema = ovs.getSchema(LibraryIntegrationTestUtils.OPEN_VSWITCH_SCHEMA).get();
110     }
111
112     @After
113     public void tearDown() throws InterruptedException, ExecutionException {
114         TableSchema<GenericTableSchema> bridge = dbSchema.table("Bridge", GenericTableSchema.class);
115         ColumnSchema<GenericTableSchema, String> name = bridge.column("name", String.class);
116         GenericTableSchema ovsTable = dbSchema.table("Open_vSwitch", GenericTableSchema.class);
117         ColumnSchema<GenericTableSchema, Set<UUID>> bridges = ovsTable.multiValuedColumn("bridges", UUID.class);
118
119         ListenableFuture<List<OperationResult>> results = ovs.transactBuilder(dbSchema)
120                 .add(op.delete(bridge)
121                         .where(name.opEqual(testBridgeName))
122                         .build())
123                 .add(op.mutate(ovsTable)
124                         .addMutation(bridges, Mutator.DELETE, Sets.newHashSet(testBridgeUuid)))
125                 .add(op.commit(true))
126                 .execute();
127
128         List<OperationResult> operationResults = results.get();
129         System.out.println("Delete operation results = " + operationResults);
130         ovs.disconnect();
131     }
132 }