Commit operation as per Section 5.2.7 of RFC 7047
[ovsdb.git] / library / 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 static org.opendaylight.ovsdb.lib.operations.Operations.op;
13
14 import java.io.IOException;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20
21 import junit.framework.Assert;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.ovsdb.lib.message.MonitorRequest;
26 import org.opendaylight.ovsdb.lib.message.MonitorRequestBuilder;
27 import org.opendaylight.ovsdb.lib.message.MonitorSelect;
28 import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
29 import org.opendaylight.ovsdb.lib.message.TableUpdate;
30 import org.opendaylight.ovsdb.lib.message.TableUpdates;
31 import org.opendaylight.ovsdb.lib.message.UpdateNotification;
32 import org.opendaylight.ovsdb.lib.operations.OperationResult;
33 import org.opendaylight.ovsdb.lib.schema.ColumnSchema;
34 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
35 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
36 import org.opendaylight.ovsdb.lib.schema.TableSchema;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import com.google.common.collect.Lists;
41 import com.google.common.util.concurrent.ListenableFuture;
42
43
44 public class OvsDBClientTestIT extends OvsdbTestBase {
45     Logger logger = LoggerFactory.getLogger(OvsDBClientTestIT.class);
46
47     OvsDBClientImpl ovs;
48
49     @Test
50     public void testTransact() throws IOException, InterruptedException, ExecutionException {
51
52         ListenableFuture<DatabaseSchema> schema = ovs.getSchema(OvsDBClient.OPEN_VSWITCH_SCHEMA, true);
53         TableSchema<GenericTableSchema> bridge = schema.get().table("Bridge", GenericTableSchema.class);
54
55         for (Map.Entry<String, ColumnSchema> names : bridge.getColumnSchemas().entrySet()) {
56             System.out.println("names = " + names.getKey());
57             System.out.println("names.getValue().getType() = " + names.getValue().getType().getBaseType());
58         }
59
60         ColumnSchema<GenericTableSchema, String> name = bridge.column("name", String.class);
61         ColumnSchema<GenericTableSchema, String> fail_mode = bridge.column("fail_mode", String.class);
62
63         ListenableFuture<List<OperationResult>> results = ovs.transactBuilder()
64                 .add(op.insert(bridge).value(name, "br-int"))
65                 .add(op.update(bridge)
66                         .set(fail_mode, "secure")
67                         .where(name.opEqual("br-int"))
68                         //.and(name.opEqual("br-int"))
69                         .operation())
70                 .add(op.delete(bridge)
71                         .where(name.opEqual("br-int"))
72                         .operation())
73                 .add(op.commit(true))
74                 .execute();
75
76         List<OperationResult> operationResults = results.get();
77         Assert.assertFalse(operationResults.isEmpty());
78         System.out.println("operationResults = " + operationResults);
79     }
80
81     @Test
82     public void testMonitorRequest() throws ExecutionException, InterruptedException {
83
84         DatabaseSchema dbSchema = ovs.getSchema(OvsDBClient.OPEN_VSWITCH_SCHEMA, true).get();
85         GenericTableSchema bridge = dbSchema.table("Bridge", GenericTableSchema.class);
86
87         List<MonitorRequest<GenericTableSchema>> monitorRequests = Lists.newArrayList();
88         monitorRequests.add(
89                 MonitorRequestBuilder.builder(bridge)
90                         .addColumn(bridge.column("name"))
91                         .addColumn(bridge.column("fail_mode", String.class))
92                         .with(new MonitorSelect(true, true, true, true))
93                         .build());
94
95         final List<Object> results = Lists.newArrayList();
96
97         MonitorHandle monitor = ovs.monitor(dbSchema, monitorRequests, new MonitorCallBack() {
98             @Override
99             public void update(TableUpdates result) {
100                 results.add(result);
101                 System.out.println("result = " + result);
102             }
103
104             @Override
105             public void exception(Throwable t) {
106                 results.add(t);
107                 System.out.println("t = " + t);
108             }
109         });
110
111         //for (int i = 0; i < 5 && results.isEmpty(); i++) { //wait 5 seconds to get a result
112         for (int i = 0; i < 5 ; i++) { //wait 5 seconds to get a result
113             System.out.println("waiting");
114             Thread.sleep(1000);
115         }
116
117         Assert.assertTrue(!results.isEmpty());
118         Object result = results.get(0);
119         Assert.assertTrue(result instanceof TableUpdates);
120         TableUpdate bridgeUpdate = ((TableUpdates) result).getUpdate(bridge);
121         Assert.assertNotNull(bridgeUpdate);
122     }
123
124     @Test
125     public void testGetDBs() throws ExecutionException, InterruptedException {
126         ListenableFuture<List<String>> databases = ovs.getDatabases();
127         List<String> dbNames = databases.get();
128         Assert.assertNotNull(dbNames);
129         Assert.assertTrue(dbNames.size() > 0);
130     }
131
132     @Before
133     public  void initalize() throws IOException {
134         if (ovs != null) {
135             return;
136         }
137         OvsdbRPC rpc = getTestConnection();
138         if (rpc == null) {
139             System.out.println("Unable to Establish Test Connection");
140         }
141         ExecutorService executorService = Executors.newFixedThreadPool(3);
142         ovs = new OvsDBClientImpl(rpc, executorService);
143     }
144
145
146     @Override
147     public void update(Object node, UpdateNotification upadateNotification) {
148         // TODO Auto-generated method stub
149
150     }
151
152     @Override
153     public void locked(Object node, List<String> ids) {
154         // TODO Auto-generated method stub
155
156     }
157     @Override
158     public void stolen(Object node, List<String> ids) {
159         // TODO Auto-generated method stub
160
161     }
162 }