2 * Copyright (C) 2014 Red Hat, Inc.
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
8 * Authors : Madhu Venugopal
10 package org.opendaylight.ovsdb.schema.openvswitch;
12 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
14 import java.io.IOException;
15 import java.lang.reflect.InvocationTargetException;
16 import java.util.List;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
21 import junit.framework.Assert;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.ovsdb.lib.OvsDBClientImpl;
27 import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
28 import org.opendaylight.ovsdb.lib.message.UpdateNotification;
29 import org.opendaylight.ovsdb.lib.notation.Mutator;
30 import org.opendaylight.ovsdb.lib.notation.Row;
31 import org.opendaylight.ovsdb.lib.notation.UUID;
32 import org.opendaylight.ovsdb.lib.operations.OperationResult;
33 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
34 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
35 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
36 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import com.google.common.collect.ImmutableMap;
41 import com.google.common.collect.Maps;
42 import com.google.common.collect.Sets;
43 import com.google.common.util.concurrent.ListenableFuture;
45 public class TypedVSwitchdSchemaIT extends OvsdbTestBase {
47 Logger logger = LoggerFactory.getLogger(TypedVSwitchdSchemaIT.class);
49 DatabaseSchema dbSchema = null;
50 static String testBridgeName = "br_test";
51 static UUID testBridgeUuid = null;
54 public void testTypedBridgeOperations() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
55 this.createTypedBridge();
58 private void createTypedBridge() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
59 GenericTableSchema bridgeSchema = TyperUtils.getTableSchema(dbSchema, Bridge.class);
60 Bridge bridge = TyperUtils.getTypedRowWrapper(dbSchema, Bridge.class, new Row<GenericTableSchema>());
61 bridge.setName(testBridgeName);
62 bridge.setStatus(Maps.newHashMap(ImmutableMap.of("key","value")));
63 bridge.setFloodVlans(Sets.newHashSet(34));
65 GenericTableSchema ovsTable = TyperUtils.getTableSchema(dbSchema, OpenVSwitch.class);
66 OpenVSwitch openVSwitch = TyperUtils.getTypedRowWrapper(dbSchema, OpenVSwitch.class, new Row<GenericTableSchema>());
67 openVSwitch.setBridges(Sets.newHashSet(new UUID(testBridgeName)));
69 int insertOperationIndex = 0;
71 TransactionBuilder transactionBuilder = ovs.transactBuilder()
72 .add(op.insert(bridgeSchema)
73 .withId(testBridgeName)
74 .value(bridge.getNameColumn()))
75 .add(op.update(bridgeSchema)
76 .set(bridge.getStatusColumn())
77 .set(bridge.getFloodVlansColumn())
78 .where(bridge.getNameColumn().getSchema().opEqual(bridge.getName()))
79 .and(bridge.getNameColumn().getSchema().opEqual(bridge.getName())).build())
80 .add(op.mutate(ovsTable)
81 .addMutation(openVSwitch.getBridgesColumn().getSchema(), Mutator.INSERT,
82 openVSwitch.getBridgesColumn().getData()));
84 ListenableFuture<List<OperationResult>> results = transactionBuilder.execute();
85 List<OperationResult> operationResults = results.get();
86 Assert.assertFalse(operationResults.isEmpty());
87 // Check if Results matches the number of operations in transaction
88 Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
89 System.out.println("Insert & Update operation results = " + operationResults);
90 testBridgeUuid = operationResults.get(insertOperationIndex).getUuid();
92 public void testGetDBs() throws ExecutionException, InterruptedException {
93 ListenableFuture<List<String>> databases = ovs.getDatabases();
94 List<String> dbNames = databases.get();
95 Assert.assertNotNull(dbNames);
96 boolean hasOpenVswitchSchema = false;
97 for(String dbName : dbNames) {
98 if (dbName.equals(OPEN_VSWITCH_SCHEMA)) {
99 hasOpenVswitchSchema = true;
103 Assert.assertTrue(OPEN_VSWITCH_SCHEMA+" schema is not supported by the switch", hasOpenVswitchSchema);
107 public void setUp() throws IOException, ExecutionException, InterruptedException {
111 OvsdbRPC rpc = getTestConnection();
113 System.out.println("Unable to Establish Test Connection");
115 ExecutorService executorService = Executors.newFixedThreadPool(3);
116 ovs = new OvsDBClientImpl(rpc, executorService);
118 dbSchema = ovs.getSchema(OPEN_VSWITCH_SCHEMA, true).get();
122 public void tearDown() throws InterruptedException, ExecutionException {
123 GenericTableSchema bridgeSchema = TyperUtils.getTableSchema(dbSchema, Bridge.class);
124 Bridge bridge = TyperUtils.getTypedRowWrapper(dbSchema, Bridge.class, null);
126 GenericTableSchema ovsTable = TyperUtils.getTableSchema(dbSchema, OpenVSwitch.class);
127 OpenVSwitch openVSwitch = TyperUtils.getTypedRowWrapper(dbSchema, OpenVSwitch.class, null);
129 ListenableFuture<List<OperationResult>> results = ovs.transactBuilder()
130 .add(op.delete(bridgeSchema)
131 .where(bridge.getNameColumn().getSchema().opEqual(testBridgeName))
133 .add(op.mutate(ovsTable)
134 .addMutation(openVSwitch.getBridgesColumn().getSchema(), Mutator.DELETE, Sets.newHashSet(testBridgeUuid)))
135 .add(op.commit(true))
138 List<OperationResult> operationResults = results.get();
139 System.out.println("Delete operation results = " + operationResults);
143 public void update(Object node, UpdateNotification upadateNotification) {
144 // TODO Auto-generated method stub
148 public void locked(Object node, List<String> ids) {
149 // TODO Auto-generated method stub
153 public void stolen(Object node, List<String> ids) {
154 // TODO Auto-generated method stub