bb1f20e2d4a98c6274648b8c20cb70c36126964e
[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.Set;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
21
22 import junit.framework.Assert;
23
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.ovsdb.lib.message.MonitorRequest;
27 import org.opendaylight.ovsdb.lib.message.MonitorRequestBuilder;
28 import org.opendaylight.ovsdb.lib.message.MonitorSelect;
29 import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
30 import org.opendaylight.ovsdb.lib.message.TableUpdate;
31 import org.opendaylight.ovsdb.lib.message.TableUpdates;
32 import org.opendaylight.ovsdb.lib.message.UpdateNotification;
33 import org.opendaylight.ovsdb.lib.operations.OperationResult;
34 import org.opendaylight.ovsdb.lib.schema.ColumnSchema;
35 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
36 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
37 import org.opendaylight.ovsdb.lib.schema.TableSchema;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import com.google.common.collect.Lists;
42 import com.google.common.collect.Sets;
43 import com.google.common.util.concurrent.ListenableFuture;
44
45
46 public class OvsDBClientTestIT extends OvsdbTestBase {
47     Logger logger = LoggerFactory.getLogger(OvsDBClientTestIT.class);
48
49     OvsDBClientImpl ovs;
50
51     @Test
52     public void testTransact() throws IOException, InterruptedException, ExecutionException {
53
54         ListenableFuture<DatabaseSchema> schema = ovs.getSchema(OvsDBClient.OPEN_VSWITCH_SCHEMA, true);
55         TableSchema<GenericTableSchema> bridge = schema.get().table("Bridge", GenericTableSchema.class);
56
57         for (Map.Entry<String, ColumnSchema> names : bridge.getColumnSchemas().entrySet()) {
58             System.out.println("names = " + names.getKey());
59             System.out.println("names.getValue().getType() = " + names.getValue().getType().getBaseType());
60         }
61
62         ColumnSchema<GenericTableSchema, String> name = bridge.column("name", String.class);
63         ColumnSchema<GenericTableSchema, String> fail_mode = bridge.column("fail_mode", String.class);
64         ColumnSchema<GenericTableSchema, Set<Integer>> flood_vlans = bridge.multiValuedColumn("flood_vlans", Integer.class);
65
66         ListenableFuture<List<OperationResult>> results = ovs.transactBuilder()
67                 .add(op.insert(bridge)
68                         .value(name, "br-test")
69                         .value(flood_vlans, Sets.newHashSet(100, 101, 4001)))
70                 .add(op.comment("Inserting Bridge br-int"))
71                 .add(op.update(bridge)
72                         .set(fail_mode, "secure")
73                         .where(name.opEqual("br-int"))
74                         .operation())
75                 .add(op.comment("Updating fail_mode to secure on Bridge br-int"))
76                 .add(op.select(bridge)
77                         .column(name)
78                         .where(name.opEqual("br-int"))
79                         .operation())
80                 .add(op.commit(true))
81                 .add(op.comment("Commiting the operation"))
82                 .execute();
83
84         List<OperationResult> operationResults = results.get();
85         Assert.assertFalse(operationResults.isEmpty());
86         System.out.println("Insert & Update operation results = " + operationResults);
87
88         results = ovs.transactBuilder()
89                 .add(op.delete(bridge)
90                         .where(name.opEqual("br-int"))
91                         .operation())
92                 .add(op.comment("Deleting Bridge br-int"))
93                 .add(op.commit(true))
94                 .add(op.comment("Commiting the operation"))
95                 .execute();
96
97         operationResults = results.get();
98         Assert.assertFalse(operationResults.isEmpty());
99         System.out.println("Delete operation results = " + operationResults);
100
101         /*
102          * Adding a separate Abort operation in a transaction. Lets not mix this with other
103          * valid transactions as above.
104          */
105         results = ovs.transactBuilder()
106                 .add(op.delete(bridge)
107                         .where(name.opEqual("br-int"))
108                         .operation())
109                 .add(op.abort())
110                 .execute();
111
112         operationResults = results.get();
113         Assert.assertFalse(operationResults.isEmpty());
114         System.out.println("Abort operation results = " + operationResults);
115
116     }
117
118     @Test
119     public void testMonitorRequest() throws ExecutionException, InterruptedException {
120
121         DatabaseSchema dbSchema = ovs.getSchema(OvsDBClient.OPEN_VSWITCH_SCHEMA, true).get();
122         GenericTableSchema bridge = dbSchema.table("Bridge", GenericTableSchema.class);
123
124         List<MonitorRequest<GenericTableSchema>> monitorRequests = Lists.newArrayList();
125         monitorRequests.add(
126                 MonitorRequestBuilder.builder(bridge)
127                         .addColumn(bridge.column("name"))
128                         .addColumn(bridge.column("fail_mode", String.class))
129                         .addColumn(bridge.multiValuedColumn("flood_vlans", Integer.class))
130                         .with(new MonitorSelect(true, true, true, true))
131                         .build());
132
133         final List<Object> results = Lists.newArrayList();
134
135         MonitorHandle monitor = ovs.monitor(dbSchema, monitorRequests, new MonitorCallBack() {
136             @Override
137             public void update(TableUpdates result) {
138                 results.add(result);
139                 System.out.println("result = " + result);
140             }
141
142             @Override
143             public void exception(Throwable t) {
144                 results.add(t);
145                 System.out.println("t = " + t);
146             }
147         });
148
149         for (int i = 0; i < 5 ; i++) { //wait 5 seconds to get a result
150             System.out.println("waiting");
151             Thread.sleep(1000);
152         }
153
154         Assert.assertTrue(!results.isEmpty());
155         Object result = results.get(0);
156         Assert.assertTrue(result instanceof TableUpdates);
157         TableUpdate bridgeUpdate = ((TableUpdates) result).getUpdate(bridge);
158         Assert.assertNotNull(bridgeUpdate);
159     }
160
161     @Test
162     public void testGetDBs() throws ExecutionException, InterruptedException {
163         ListenableFuture<List<String>> databases = ovs.getDatabases();
164         List<String> dbNames = databases.get();
165         Assert.assertNotNull(dbNames);
166         Assert.assertTrue(dbNames.size() > 0);
167     }
168
169     @Before
170     public  void initalize() throws IOException {
171         if (ovs != null) {
172             return;
173         }
174         OvsdbRPC rpc = getTestConnection();
175         if (rpc == null) {
176             System.out.println("Unable to Establish Test Connection");
177         }
178         ExecutorService executorService = Executors.newFixedThreadPool(3);
179         ovs = new OvsDBClientImpl(rpc, executorService);
180     }
181
182
183     @Override
184     public void update(Object node, UpdateNotification upadateNotification) {
185         // TODO Auto-generated method stub
186
187     }
188
189     @Override
190     public void locked(Object node, List<String> ids) {
191         // TODO Auto-generated method stub
192
193     }
194     @Override
195     public void stolen(Object node, List<String> ids) {
196         // TODO Auto-generated method stub
197
198     }
199 }