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