Added Tests to test the Typed Class helper routine getTypedRowWrapper() and the Proxy...
[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.monitorBridge();
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     }
107
108     private void createTypedController() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
109         Controller controller1 = ovs.createTypedRowWrapper(Controller.class);
110         controller1.setTarget("tcp:1.1.1.1:6640");
111         Controller controller2 = ovs.createTypedRowWrapper(Controller.class);
112         controller2.setTarget("tcp:2.2.2.2:6640");
113
114         Bridge bridge = ovs.getTypedRowWrapper(Bridge.class, null);
115
116         String transactionUuidStr = "controller";
117         TransactionBuilder transactionBuilder = ovs.transactBuilder()
118                 .add(op.insert(controller1.getSchema())
119                         .withId(transactionUuidStr)
120                         .value(controller1.getTargetColumn()))
121                 .add(op.mutate(bridge.getSchema())
122                         .addMutation(bridge.getControllerColumn().getSchema(), Mutator.INSERT,
123                                      Sets.newHashSet(new UUID(transactionUuidStr)))
124                         .where(bridge.getNameColumn().getSchema().opEqual(testBridgeName))
125                         .build());
126
127         ListenableFuture<List<OperationResult>> results = transactionBuilder.execute();
128         List<OperationResult> operationResults = results.get();
129         Assert.assertFalse(operationResults.isEmpty());
130         // Check if Results matches the number of operations in transaction
131         Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
132         System.out.println("Insert & Mutate operation results for controller1 = " + operationResults);
133         // Check for any errors
134         for (OperationResult result : operationResults) {
135             Assert.assertNull(result.getError());
136         }
137
138         Row bridgeRow = tableCache.get(bridge.getSchema().getName()).get(testBridgeUuid);
139         Bridge monitoredBridge = ovs.getTypedRowWrapper(Bridge.class, bridgeRow);
140         Assert.assertEquals(1, monitoredBridge.getControllerColumn().getData().size());
141
142         transactionBuilder = ovs.transactBuilder()
143                 .add(op.insert(controller2.getSchema())
144                         .withId(transactionUuidStr)
145                         .value(controller2.getTargetColumn()))
146                 .add(op.mutate(bridge.getSchema())
147                         .addMutation(bridge.getControllerColumn().getSchema(), Mutator.INSERT,
148                                      Sets.newHashSet(new UUID(transactionUuidStr)))
149                         .where(bridge.getNameColumn().getSchema().opEqual(testBridgeName))
150                         .build());
151
152         results = transactionBuilder.execute();
153         operationResults = results.get();
154         Assert.assertFalse(operationResults.isEmpty());
155         // Check if Results matches the number of operations in transaction
156         Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
157         System.out.println("Insert & Mutate operation results for controller2 = " + operationResults);
158         // Check for any errors
159         for (OperationResult result : operationResults) {
160             Assert.assertNull(result.getError());
161         }
162
163         bridgeRow = tableCache.get(bridge.getSchema().getName()).get(testBridgeUuid);
164         monitoredBridge = ovs.getTypedRowWrapper(Bridge.class, bridgeRow);
165         Assert.assertEquals(2, monitoredBridge.getControllerColumn().getData().size());
166     }
167
168     public void testGetDBs() throws ExecutionException, InterruptedException {
169         ListenableFuture<List<String>> databases = ovs.getDatabases();
170         List<String> dbNames = databases.get();
171         Assert.assertNotNull(dbNames);
172         boolean hasOpenVswitchSchema = false;
173         for(String dbName : dbNames) {
174            if (dbName.equals(OPEN_VSWITCH_SCHEMA)) {
175                 hasOpenVswitchSchema = true;
176                 break;
177            }
178         }
179         Assert.assertTrue(OPEN_VSWITCH_SCHEMA+" schema is not supported by the switch", hasOpenVswitchSchema);
180     }
181
182     public void monitorBridge() throws ExecutionException, InterruptedException, IOException {
183         Assert.assertNotNull(dbSchema);
184         GenericTableSchema bridge = dbSchema.table("Bridge", GenericTableSchema.class);
185
186         List<MonitorRequest<GenericTableSchema>> monitorRequests = Lists.newArrayList();
187         MonitorRequestBuilder<GenericTableSchema> builder = MonitorRequestBuilder.builder(bridge);
188         monitorRequests.add(builder.with(new MonitorSelect(true, true, true, true))
189                                    .build());
190
191         MonitorHandle monitor = ovs.monitor(dbSchema, monitorRequests, new UpdateMonitor());
192         Assert.assertNotNull(monitor);
193     }
194
195     static Map<String, Map<UUID, Row>> tableCache = new HashMap<String, Map<UUID, Row>>();
196     private static class UpdateMonitor implements MonitorCallBack {
197         @Override
198         public void update(TableUpdates result) {
199             for (String tableName : result.getUpdates().keySet()) {
200                 Map<UUID, Row> tUpdate = tableCache.get(tableName);
201                 TableUpdate update = result.getUpdates().get(tableName);
202                 if (update.getNew() != null) {
203                     if (tUpdate == null) {
204                         tUpdate = new HashMap<UUID, Row>();
205                         tableCache.put(tableName, tUpdate);
206                     }
207                     tUpdate.put(update.getUuid(), update.getNew());
208                 } else {
209                     tUpdate.remove(update.getUuid());
210                 }
211             }
212         }
213
214         @Override
215         public void exception(Throwable t) {
216             System.out.println("Exception t = " + t);
217         }
218     }
219
220     @Before
221     public  void setUp() throws IOException, ExecutionException, InterruptedException {
222         if (ovs != null) {
223             return;
224         }
225         OvsdbRPC rpc = getTestConnection();
226         if (rpc == null) {
227             System.out.println("Unable to Establish Test Connection");
228         }
229         ExecutorService executorService = Executors.newFixedThreadPool(3);
230         ovs = new OvsDBClientImpl(rpc, executorService);
231         testGetDBs();
232         dbSchema = ovs.getSchema(OPEN_VSWITCH_SCHEMA, true).get();
233     }
234
235     @After
236     public void tearDown() throws InterruptedException, ExecutionException {
237         Bridge bridge = ovs.getTypedRowWrapper(Bridge.class, null);
238         OpenVSwitch openVSwitch = ovs.getTypedRowWrapper(OpenVSwitch.class, null);
239
240         ListenableFuture<List<OperationResult>> results = ovs.transactBuilder()
241                 .add(op.delete(bridge.getSchema())
242                         .where(bridge.getNameColumn().getSchema().opEqual(testBridgeName))
243                         .build())
244                 .add(op.mutate(openVSwitch.getSchema())
245                         .addMutation(openVSwitch.getBridgesColumn().getSchema(), Mutator.DELETE, Sets.newHashSet(testBridgeUuid)))
246                 .add(op.commit(true))
247                 .execute();
248
249         List<OperationResult> operationResults = results.get();
250         System.out.println("Delete operation results = " + operationResults);
251         tableCache = new HashMap<String, Map<UUID, Row>>();
252     }
253
254     @Override
255     public void update(Object node, UpdateNotification upadateNotification) {
256         // TODO Auto-generated method stub
257
258     }
259     @Override
260     public void locked(Object node, List<String> ids) {
261         // TODO Auto-generated method stub
262
263     }
264     @Override
265     public void stolen(Object node, List<String> ids) {
266         // TODO Auto-generated method stub
267
268     }
269 }