Adding generic GETDATA and GETCOLUMN proxy methods for _uuid and _version in TypedBas...
[ovsdb.git] / schemas / Open_vSwitch / src / test / java / org / opendaylight / ovsdb / schema / openvswitch / TypedVSwitchdSchemaIT.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
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 : Madhu Venugopal
9  */
10 package org.opendaylight.ovsdb.schema.openvswitch;
11
12 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
13
14 import java.io.IOException;
15 import java.lang.reflect.InvocationTargetException;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.ExecutorService;
21 import java.util.concurrent.Executors;
22
23 import junit.framework.Assert;
24
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.ovsdb.lib.MonitorCallBack;
29 import org.opendaylight.ovsdb.lib.MonitorHandle;
30 import org.opendaylight.ovsdb.lib.OvsDBClientImpl;
31 import org.opendaylight.ovsdb.lib.message.MonitorRequest;
32 import org.opendaylight.ovsdb.lib.message.MonitorRequestBuilder;
33 import org.opendaylight.ovsdb.lib.message.MonitorSelect;
34 import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
35 import org.opendaylight.ovsdb.lib.message.TableUpdate;
36 import org.opendaylight.ovsdb.lib.message.TableUpdates;
37 import org.opendaylight.ovsdb.lib.message.UpdateNotification;
38 import org.opendaylight.ovsdb.lib.notation.Mutator;
39 import org.opendaylight.ovsdb.lib.notation.Row;
40 import org.opendaylight.ovsdb.lib.notation.UUID;
41 import org.opendaylight.ovsdb.lib.operations.OperationResult;
42 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
43 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
44 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import com.google.common.collect.ImmutableMap;
49 import com.google.common.collect.Lists;
50 import com.google.common.collect.Sets;
51 import com.google.common.util.concurrent.ListenableFuture;
52
53 public class TypedVSwitchdSchemaIT extends OvsdbTestBase {
54
55     Logger logger = LoggerFactory.getLogger(TypedVSwitchdSchemaIT.class);
56     OvsDBClientImpl ovs;
57     DatabaseSchema dbSchema = null;
58     static String testBridgeName = "br_test";
59     static UUID testBridgeUuid = null;
60
61     @Test
62     public void testTypedBridgeOperations() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
63         this.monitorTables();
64         this.createTypedBridge();
65         this.createTypedController();
66     }
67
68     private void createTypedBridge() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
69         Bridge bridge = ovs.createTypedRowWrapper(Bridge.class);
70         bridge.setName(testBridgeName);
71         bridge.setStatus(ImmutableMap.of("key","value"));
72         bridge.setFloodVlans(Sets.newHashSet(34));
73
74         OpenVSwitch openVSwitch = ovs.createTypedRowWrapper(OpenVSwitch.class);
75         openVSwitch.setBridges(Sets.newHashSet(new UUID(testBridgeName)));
76
77         int insertOperationIndex = 0;
78
79         TransactionBuilder transactionBuilder = ovs.transactBuilder()
80                 .add(op.insert(bridge.getSchema())
81                         .withId(testBridgeName)
82                         .value(bridge.getNameColumn()))
83                 .add(op.update(bridge.getSchema())
84                         .set(bridge.getStatusColumn())
85                         .set(bridge.getFloodVlansColumn())
86                         .where(bridge.getNameColumn().getSchema().opEqual(bridge.getName()))
87                         .and(bridge.getNameColumn().getSchema().opEqual(bridge.getName())).build())
88                 .add(op.mutate(openVSwitch.getSchema())
89                         .addMutation(openVSwitch.getBridgesColumn().getSchema(), Mutator.INSERT,
90                                      openVSwitch.getBridgesColumn().getData()));
91
92         ListenableFuture<List<OperationResult>> results = transactionBuilder.execute();
93         List<OperationResult> operationResults = results.get();
94         Assert.assertFalse(operationResults.isEmpty());
95         // Check if Results matches the number of operations in transaction
96         Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
97         System.out.println("Insert & Update operation results = " + operationResults);
98         for (OperationResult result : operationResults) {
99             Assert.assertNull(result.getError());
100         }
101         testBridgeUuid = operationResults.get(insertOperationIndex).getUuid();
102
103         Row bridgeRow = tableCache.get(bridge.getSchema().getName()).get(testBridgeUuid);
104         Bridge monitoredBridge = ovs.getTypedRowWrapper(Bridge.class, bridgeRow);
105         Assert.assertEquals(monitoredBridge.getNameColumn().getData(), bridge.getNameColumn().getData());
106         Assert.assertNotNull(monitoredBridge.getVersion());
107         Assert.assertNotNull(this.getOpenVSwitchTableUuid());
108     }
109
110     private void createTypedController() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
111         Controller controller1 = ovs.createTypedRowWrapper(Controller.class);
112         controller1.setTarget("tcp:1.1.1.1:6640");
113         Controller controller2 = ovs.createTypedRowWrapper(Controller.class);
114         controller2.setTarget("tcp:2.2.2.2:6640");
115
116         Bridge bridge = ovs.getTypedRowWrapper(Bridge.class, null);
117
118         String transactionUuidStr = "controller";
119         TransactionBuilder transactionBuilder = ovs.transactBuilder()
120                 .add(op.insert(controller1.getSchema())
121                         .withId(transactionUuidStr)
122                         .value(controller1.getTargetColumn()))
123                 .add(op.mutate(bridge.getSchema())
124                         .addMutation(bridge.getControllerColumn().getSchema(), Mutator.INSERT,
125                                      Sets.newHashSet(new UUID(transactionUuidStr)))
126                         .where(bridge.getNameColumn().getSchema().opEqual(testBridgeName))
127                         .build());
128
129         ListenableFuture<List<OperationResult>> results = transactionBuilder.execute();
130         List<OperationResult> operationResults = results.get();
131         Assert.assertFalse(operationResults.isEmpty());
132         // Check if Results matches the number of operations in transaction
133         Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
134         System.out.println("Insert & Mutate operation results for controller1 = " + operationResults);
135         // Check for any errors
136         for (OperationResult result : operationResults) {
137             Assert.assertNull(result.getError());
138         }
139
140         Row bridgeRow = tableCache.get(bridge.getSchema().getName()).get(testBridgeUuid);
141         Bridge monitoredBridge = ovs.getTypedRowWrapper(Bridge.class, bridgeRow);
142         Assert.assertEquals(1, monitoredBridge.getControllerColumn().getData().size());
143
144         transactionBuilder = ovs.transactBuilder()
145                 .add(op.insert(controller2.getSchema())
146                         .withId(transactionUuidStr)
147                         .value(controller2.getTargetColumn()))
148                 .add(op.mutate(bridge.getSchema())
149                         .addMutation(bridge.getControllerColumn().getSchema(), Mutator.INSERT,
150                                      Sets.newHashSet(new UUID(transactionUuidStr)))
151                         .where(bridge.getNameColumn().getSchema().opEqual(testBridgeName))
152                         .build());
153
154         results = transactionBuilder.execute();
155         operationResults = results.get();
156         Assert.assertFalse(operationResults.isEmpty());
157         // Check if Results matches the number of operations in transaction
158         Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
159         System.out.println("Insert & Mutate operation results for controller2 = " + operationResults);
160         // Check for any errors
161         for (OperationResult result : operationResults) {
162             Assert.assertNull(result.getError());
163         }
164
165         bridgeRow = tableCache.get(bridge.getSchema().getName()).get(testBridgeUuid);
166         monitoredBridge = ovs.getTypedRowWrapper(Bridge.class, bridgeRow);
167         Assert.assertEquals(2, monitoredBridge.getControllerColumn().getData().size());
168     }
169
170     public void testGetDBs() throws ExecutionException, InterruptedException {
171         ListenableFuture<List<String>> databases = ovs.getDatabases();
172         List<String> dbNames = databases.get();
173         Assert.assertNotNull(dbNames);
174         boolean hasOpenVswitchSchema = false;
175         for(String dbName : dbNames) {
176            if (dbName.equals(OPEN_VSWITCH_SCHEMA)) {
177                 hasOpenVswitchSchema = true;
178                 break;
179            }
180         }
181         Assert.assertTrue(OPEN_VSWITCH_SCHEMA+" schema is not supported by the switch", hasOpenVswitchSchema);
182     }
183
184     private UUID getOpenVSwitchTableUuid() {
185         OpenVSwitch openVSwitch = ovs.getTypedRowWrapper(OpenVSwitch.class, null);
186         Map<UUID, Row> ovsTable = tableCache.get(openVSwitch.getSchema().getName());
187         if (ovsTable != null) {
188             if (ovsTable.keySet().size() >= 1) {
189                 return (UUID)ovsTable.keySet().toArray()[0];
190             }
191         }
192         return null;
193     }
194
195     public void monitorTables() throws ExecutionException, InterruptedException, IOException {
196         Assert.assertNotNull(dbSchema);
197         Bridge bridge = ovs.createTypedRowWrapper(Bridge.class);
198         OpenVSwitch openVSwitch = ovs.createTypedRowWrapper(OpenVSwitch.class);
199
200         List<MonitorRequest<GenericTableSchema>> monitorRequests = Lists.newArrayList();
201         MonitorRequestBuilder<GenericTableSchema> bridgeBuilder = MonitorRequestBuilder.builder(bridge.getSchema());
202         MonitorRequestBuilder<GenericTableSchema> ovsTableBuilder = MonitorRequestBuilder.builder(openVSwitch.getSchema());
203         monitorRequests.add(bridgeBuilder.with(new MonitorSelect(true, true, true, true)).build());
204         monitorRequests.add(ovsTableBuilder.with(new MonitorSelect(true, true, true, true)).build());
205
206         MonitorHandle monitor = ovs.monitor(dbSchema, monitorRequests, new UpdateMonitor());
207         Assert.assertNotNull(monitor);
208     }
209
210     static Map<String, Map<UUID, Row>> tableCache = new HashMap<String, Map<UUID, Row>>();
211     private static class UpdateMonitor implements MonitorCallBack {
212         @Override
213         public void update(TableUpdates result) {
214             for (String tableName : result.getUpdates().keySet()) {
215                 Map<UUID, Row> tUpdate = tableCache.get(tableName);
216                 TableUpdate update = result.getUpdates().get(tableName);
217                 if (update.getNew() != null) {
218                     if (tUpdate == null) {
219                         tUpdate = new HashMap<UUID, Row>();
220                         tableCache.put(tableName, tUpdate);
221                     }
222                     tUpdate.put(update.getUuid(), update.getNew());
223                 } else {
224                     tUpdate.remove(update.getUuid());
225                 }
226             }
227         }
228
229         @Override
230         public void exception(Throwable t) {
231             System.out.println("Exception t = " + t);
232         }
233     }
234
235     @Before
236     public  void setUp() throws IOException, ExecutionException, InterruptedException {
237         if (ovs != null) {
238             return;
239         }
240         OvsdbRPC rpc = getTestConnection();
241         if (rpc == null) {
242             System.out.println("Unable to Establish Test Connection");
243         }
244         ExecutorService executorService = Executors.newFixedThreadPool(3);
245         ovs = new OvsDBClientImpl(rpc, executorService);
246         testGetDBs();
247         dbSchema = ovs.getSchema(OPEN_VSWITCH_SCHEMA, true).get();
248     }
249
250     @After
251     public void tearDown() throws InterruptedException, ExecutionException {
252         Bridge bridge = ovs.getTypedRowWrapper(Bridge.class, null);
253         OpenVSwitch openVSwitch = ovs.getTypedRowWrapper(OpenVSwitch.class, null);
254
255         ListenableFuture<List<OperationResult>> results = ovs.transactBuilder()
256                 .add(op.delete(bridge.getSchema())
257                         .where(bridge.getNameColumn().getSchema().opEqual(testBridgeName))
258                         .build())
259                 .add(op.mutate(openVSwitch.getSchema())
260                         .addMutation(openVSwitch.getBridgesColumn().getSchema(), Mutator.DELETE, Sets.newHashSet(testBridgeUuid)))
261                 .add(op.commit(true))
262                 .execute();
263
264         List<OperationResult> operationResults = results.get();
265         System.out.println("Delete operation results = " + operationResults);
266         tableCache = new HashMap<String, Map<UUID, Row>>();
267     }
268
269     @Override
270     public void update(Object node, UpdateNotification upadateNotification) {
271         // TODO Auto-generated method stub
272
273     }
274     @Override
275     public void locked(Object node, List<String> ids) {
276         // TODO Auto-generated method stub
277
278     }
279     @Override
280     public void stolen(Object node, List<String> ids) {
281         // TODO Auto-generated method stub
282
283     }
284 }