2 * Copyright (C) 2014 EBay Software Foundation
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 : Ashwin Raveendran, Madhu Venugopal
10 package org.opendaylight.ovsdb.lib;
12 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
14 import java.io.IOException;
15 import java.util.List;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
22 import junit.framework.Assert;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
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.UUID;
31 import org.opendaylight.ovsdb.lib.operations.OperationResult;
32 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
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.opendaylight.ovsdb.lib.schema.temp.Reference;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import com.google.common.collect.ImmutableMap;
42 import com.google.common.collect.Maps;
43 import com.google.common.collect.Sets;
44 import com.google.common.util.concurrent.ListenableFuture;
46 public class OvsDBClientTestITTyped extends OvsdbTestBase {
48 Logger logger = LoggerFactory.getLogger(OvsDBClientTestITTyped.class);
50 DatabaseSchema dbSchema = null;
51 static String testBridgeName = "br-test";
52 static UUID testBridgeUuid = null;
54 public static class Bridge extends TableSchema<Bridge> {
56 public Bridge (TableSchema<Bridge> tableSchema) {
57 super("Bridge", tableSchema.getColumnSchemas());
60 public Bridge(String name, Map<String, ColumnSchema> columns) {
64 public ColumnSchema<Bridge, String> name() {
65 return column("name", String.class);
68 public ColumnSchema<Bridge, Integer> floodVlans() {
69 return column("flood_vlans", Integer.class);
72 public ColumnSchema<Bridge, Map> status() {
73 return column("status", Map.class);
76 public ColumnSchema<Bridge, Reference> netflow() {
77 return column("netflow", Reference.class);
83 public void testTypedBridgeCreate() throws IOException, InterruptedException, ExecutionException {
84 Bridge bridge = dbSchema.table("Bridge", Bridge.class);
85 GenericTableSchema ovsTable = dbSchema.table("Open_vSwitch", GenericTableSchema.class);
86 ColumnSchema<GenericTableSchema, Set<UUID>> bridges = ovsTable.multiValuedColumn("bridges", UUID.class);
88 GenericTableSchema anytable = null;
89 String namedUuid = "br_test";
90 int insertOperationIndex = 0;
92 TransactionBuilder transactionBuilder = ovs.transactBuilder()
93 .add(op.insert(bridge)
95 .value(bridge.name(), testBridgeName))
96 .add(op.update(bridge)
97 .set(bridge.status(), Maps.newHashMap(ImmutableMap.of("key","value")))
98 .set(bridge.floodVlans(), 34)
99 .where(bridge.name().opEqual(testBridgeName))
100 .and(bridge.name().opEqual(testBridgeName)).build())
101 .add(op.mutate(ovsTable)
102 .addMutation(bridges, Mutator.INSERT, Sets.newHashSet(new UUID(namedUuid))));
104 ListenableFuture<List<OperationResult>> results = transactionBuilder.execute();
105 List<OperationResult> operationResults = results.get();
106 Assert.assertFalse(operationResults.isEmpty());
107 // Check if Results matches the number of operations in transaction
108 Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
109 System.out.println("Insert & Update operation results = " + operationResults);
110 testBridgeUuid = operationResults.get(insertOperationIndex).getUuid();
113 public void testGetDBs() throws ExecutionException, InterruptedException {
114 ListenableFuture<List<String>> databases = ovs.getDatabases();
115 List<String> dbNames = databases.get();
116 Assert.assertNotNull(dbNames);
117 boolean hasOpenVswitchSchema = false;
118 for(String dbName : dbNames) {
119 if (dbName.equals(OPEN_VSWITCH_SCHEMA)) {
120 hasOpenVswitchSchema = true;
124 Assert.assertTrue(OPEN_VSWITCH_SCHEMA+" schema is not supported by the switch", hasOpenVswitchSchema);
128 public void setUp() throws IOException, ExecutionException, InterruptedException {
132 OvsdbRPC rpc = getTestConnection();
134 System.out.println("Unable to Establish Test Connection");
136 ExecutorService executorService = Executors.newFixedThreadPool(3);
137 ovs = new OvsDBClientImpl(rpc, executorService);
139 dbSchema = ovs.getSchema(OPEN_VSWITCH_SCHEMA, true).get();
143 public void tearDown() throws InterruptedException, ExecutionException {
144 TableSchema<GenericTableSchema> bridge = dbSchema.table("Bridge", GenericTableSchema.class);
145 ColumnSchema<GenericTableSchema, String> name = bridge.column("name", String.class);
146 GenericTableSchema ovsTable = dbSchema.table("Open_vSwitch", GenericTableSchema.class);
147 ColumnSchema<GenericTableSchema, Set<UUID>> bridges = ovsTable.multiValuedColumn("bridges", UUID.class);
149 ListenableFuture<List<OperationResult>> results = ovs.transactBuilder()
150 .add(op.delete(bridge)
151 .where(name.opEqual(testBridgeName))
153 .add(op.mutate(ovsTable)
154 .addMutation(bridges, Mutator.DELETE, Sets.newHashSet(testBridgeUuid)))
155 .add(op.commit(true))
158 List<OperationResult> operationResults = results.get();
159 System.out.println("Delete operation results = " + operationResults);
163 public void update(Object node, UpdateNotification upadateNotification) {
164 // TODO Auto-generated method stub
168 public void locked(Object node, List<String> ids) {
169 // TODO Auto-generated method stub
173 public void stolen(Object node, List<String> ids) {
174 // TODO Auto-generated method stub