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.UUID;
31 import org.opendaylight.ovsdb.lib.operations.OperationResult;
32 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
33 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import com.google.common.collect.ImmutableMap;
38 import com.google.common.collect.Sets;
39 import com.google.common.util.concurrent.ListenableFuture;
41 public class TypedVSwitchdSchemaIT extends OvsdbTestBase {
43 Logger logger = LoggerFactory.getLogger(TypedVSwitchdSchemaIT.class);
45 DatabaseSchema dbSchema = null;
46 static String testBridgeName = "br_test";
47 static UUID testBridgeUuid = null;
50 public void testTypedBridgeOperations() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
51 this.createTypedBridge();
52 this.createTypedController();
55 private void createTypedBridge() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
56 Bridge bridge = ovs.createTypedRowWrapper(Bridge.class);
57 bridge.setName(testBridgeName);
58 bridge.setStatus(ImmutableMap.of("key","value"));
59 bridge.setFloodVlans(Sets.newHashSet(34));
61 OpenVSwitch openVSwitch = ovs.createTypedRowWrapper(OpenVSwitch.class);
62 openVSwitch.setBridges(Sets.newHashSet(new UUID(testBridgeName)));
64 int insertOperationIndex = 0;
66 TransactionBuilder transactionBuilder = ovs.transactBuilder()
67 .add(op.insert(bridge.getSchema())
68 .withId(testBridgeName)
69 .value(bridge.getNameColumn()))
70 .add(op.update(bridge.getSchema())
71 .set(bridge.getStatusColumn())
72 .set(bridge.getFloodVlansColumn())
73 .where(bridge.getNameColumn().getSchema().opEqual(bridge.getName()))
74 .and(bridge.getNameColumn().getSchema().opEqual(bridge.getName())).build())
75 .add(op.mutate(openVSwitch.getSchema())
76 .addMutation(openVSwitch.getBridgesColumn().getSchema(), Mutator.INSERT,
77 openVSwitch.getBridgesColumn().getData()));
79 ListenableFuture<List<OperationResult>> results = transactionBuilder.execute();
80 List<OperationResult> operationResults = results.get();
81 Assert.assertFalse(operationResults.isEmpty());
82 // Check if Results matches the number of operations in transaction
83 Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
84 System.out.println("Insert & Update operation results = " + operationResults);
85 for (OperationResult result : operationResults) {
86 Assert.assertNull(result.getError());
88 testBridgeUuid = operationResults.get(insertOperationIndex).getUuid();
91 private void createTypedController() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
92 Controller controller1 = ovs.createTypedRowWrapper(Controller.class);
93 controller1.setTarget("tcp:1.1.1.1:6640");
94 Controller controller2 = ovs.createTypedRowWrapper(Controller.class);
95 controller2.setTarget("tcp:2.2.2.2:6640");
97 Bridge bridge = ovs.getTypedRowWrapper(Bridge.class, null);
99 String transactionUuidStr = "controller";
100 TransactionBuilder transactionBuilder = ovs.transactBuilder()
101 .add(op.insert(controller1.getSchema())
102 .withId(transactionUuidStr)
103 .value(controller1.getTargetColumn()))
104 .add(op.mutate(bridge.getSchema())
105 .addMutation(bridge.getControllerColumn().getSchema(), Mutator.INSERT,
106 Sets.newHashSet(new UUID(transactionUuidStr)))
107 .where(bridge.getNameColumn().getSchema().opEqual(testBridgeName))
110 ListenableFuture<List<OperationResult>> results = transactionBuilder.execute();
111 List<OperationResult> operationResults = results.get();
112 Assert.assertFalse(operationResults.isEmpty());
113 // Check if Results matches the number of operations in transaction
114 Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
115 System.out.println("Insert & Mutate operation results for controller1 = " + operationResults);
116 // Check for any errors
117 for (OperationResult result : operationResults) {
118 Assert.assertNull(result.getError());
121 transactionBuilder = ovs.transactBuilder()
122 .add(op.insert(controller2.getSchema())
123 .withId(transactionUuidStr)
124 .value(controller2.getTargetColumn()))
125 .add(op.mutate(bridge.getSchema())
126 .addMutation(bridge.getControllerColumn().getSchema(), Mutator.INSERT,
127 Sets.newHashSet(new UUID(transactionUuidStr)))
128 .where(bridge.getNameColumn().getSchema().opEqual(testBridgeName))
131 results = transactionBuilder.execute();
132 operationResults = results.get();
133 Assert.assertFalse(operationResults.isEmpty());
134 // Check if Results matches the number of operations in transaction
135 Assert.assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
136 System.out.println("Insert & Mutate operation results for controller2 = " + operationResults);
137 // Check for any errors
138 for (OperationResult result : operationResults) {
139 Assert.assertNull(result.getError());
143 public void testGetDBs() throws ExecutionException, InterruptedException {
144 ListenableFuture<List<String>> databases = ovs.getDatabases();
145 List<String> dbNames = databases.get();
146 Assert.assertNotNull(dbNames);
147 boolean hasOpenVswitchSchema = false;
148 for(String dbName : dbNames) {
149 if (dbName.equals(OPEN_VSWITCH_SCHEMA)) {
150 hasOpenVswitchSchema = true;
154 Assert.assertTrue(OPEN_VSWITCH_SCHEMA+" schema is not supported by the switch", hasOpenVswitchSchema);
158 public void setUp() throws IOException, ExecutionException, InterruptedException {
162 OvsdbRPC rpc = getTestConnection();
164 System.out.println("Unable to Establish Test Connection");
166 ExecutorService executorService = Executors.newFixedThreadPool(3);
167 ovs = new OvsDBClientImpl(rpc, executorService);
169 dbSchema = ovs.getSchema(OPEN_VSWITCH_SCHEMA, true).get();
173 public void tearDown() throws InterruptedException, ExecutionException {
174 Bridge bridge = ovs.getTypedRowWrapper(Bridge.class, null);
175 OpenVSwitch openVSwitch = ovs.getTypedRowWrapper(OpenVSwitch.class, null);
177 ListenableFuture<List<OperationResult>> results = ovs.transactBuilder()
178 .add(op.delete(bridge.getSchema())
179 .where(bridge.getNameColumn().getSchema().opEqual(testBridgeName))
181 .add(op.mutate(openVSwitch.getSchema())
182 .addMutation(openVSwitch.getBridgesColumn().getSchema(), Mutator.DELETE, Sets.newHashSet(testBridgeUuid)))
183 .add(op.commit(true))
186 List<OperationResult> operationResults = results.get();
187 System.out.println("Delete operation results = " + operationResults);
191 public void update(Object node, UpdateNotification upadateNotification) {
192 // TODO Auto-generated method stub
196 public void locked(Object node, List<String> ids) {
197 // TODO Auto-generated method stub
201 public void stolen(Object node, List<String> ids) {
202 // TODO Auto-generated method stub