8793b3907ae7b459b1f0b0c4f45563dce4db5623
[ovsdb.git] / ovsdb / src / test / java / org / opendaylight / ovsdb / lib / OvsDBClientTestIT.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
9  */
10 package org.opendaylight.ovsdb.lib;
11
12 import com.google.common.util.concurrent.ListenableFuture;
13 import junit.framework.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
17 import org.opendaylight.ovsdb.lib.operations.OperationResult;
18 import org.opendaylight.ovsdb.lib.schema.ATableSchema;
19 import org.opendaylight.ovsdb.lib.schema.ColumnSchema;
20 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
21 import org.opendaylight.ovsdb.lib.schema.TableSchema;
22 import org.opendaylight.ovsdb.plugin.OvsdbTestBase;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import java.io.IOException;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.concurrent.ExecutionException;
30 import java.util.concurrent.ExecutorService;
31 import java.util.concurrent.Executors;
32
33 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
34
35
36 public class OvsDBClientTestIT extends OvsdbTestBase {
37     Logger logger = LoggerFactory.getLogger(OvsDBClientTestIT.class);
38
39     OvsDBClientImpl ovs;
40
41
42
43     @Test
44     public void testTransact() throws IOException, InterruptedException, ExecutionException {
45
46         ListenableFuture<DatabaseSchema> schema = ovs.getSchema(OvsDBClient.OPEN_VSWITCH_SCHEMA, true);
47         TableSchema<ATableSchema> bridge = schema.get().table("Bridge");
48
49         for (Map.Entry<String, ColumnSchema> names : bridge.getColumnSchemas().entrySet()) {
50             System.out.println("names = " + names.getKey());
51             System.out.println("names.getValue().getType() = " + names.getValue().getType().getBaseType());
52         }
53
54         ColumnSchema<ATableSchema, String> name = bridge.column("name", String.class);
55         ColumnSchema<ATableSchema, String> fail_mode = bridge.column("fail_mode", String.class);
56
57         ListenableFuture<List<OperationResult>> results = ovs.transactBuilder()
58                 .add(op.insert(bridge).value(name, "br-int"))
59                 .add(op.update(bridge)
60                         .set(fail_mode, "secure")
61                         .where(name.opEqual("br-int"))
62                         //.and(name.opEqual("br-int"))
63                         .operation())
64                 .execute();
65
66         List<OperationResult> operationResults = results.get();
67         Assert.assertFalse(operationResults.isEmpty());
68         System.out.println("operationResults = " + operationResults);
69     }
70
71     @Test
72     public void testGetDBs() throws ExecutionException, InterruptedException {
73         ListenableFuture<List<String>> databases = ovs.getDatabases();
74         List<String> dbNames = databases.get();
75         Assert.assertNotNull(dbNames);
76         Assert.assertTrue(dbNames.size() > 0);
77     }
78
79     @Before
80     public  void initalize() throws IOException {
81         if (ovs != null) {
82             return;
83         }
84         TestObjects testConnection = getTestConnection();
85         OvsdbRPC rpc = testConnection.connectionService.getConnection(testConnection.node).getRpc();
86
87         ExecutorService executorService = Executors.newFixedThreadPool(3);
88         ovs = new OvsDBClientImpl(rpc, executorService);
89     }
90
91 }