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, Dave Tucker
11 package org.opendaylight.ovsdb.schema.openvswitch;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNull;
16 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
18 import java.io.IOException;
19 import java.lang.reflect.InvocationTargetException;
20 import java.util.List;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.TimeoutException;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.ovsdb.lib.message.UpdateNotification;
27 import org.opendaylight.ovsdb.lib.notation.Mutator;
28 import org.opendaylight.ovsdb.lib.notation.Row;
29 import org.opendaylight.ovsdb.lib.notation.UUID;
30 import org.opendaylight.ovsdb.lib.operations.OperationResult;
31 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import com.google.common.collect.Sets;
36 import com.google.common.util.concurrent.ListenableFuture;
38 public class ControllerTestCases extends OpenVswitchSchemaTestBase {
39 Logger logger = LoggerFactory.getLogger(ControllerTestCases.class);
43 public void setUp() throws ExecutionException, InterruptedException, TimeoutException, IOException {
48 public void createTypedController() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
49 Controller controller1 = ovs.createTypedRowWrapper(Controller.class);
50 controller1.setTarget("tcp:1.1.1.1:6640");
51 Controller controller2 = ovs.createTypedRowWrapper(Controller.class);
52 controller2.setTarget("tcp:2.2.2.2:6640");
54 Bridge bridge = ovs.getTypedRowWrapper(Bridge.class, null);
56 String transactionUuidStr = "controller";
57 TransactionBuilder transactionBuilder = ovs.transactBuilder(OpenVswitchSchemaSuiteIT.dbSchema)
58 .add(op.insert(controller1.getSchema())
59 .withId(transactionUuidStr)
60 .value(controller1.getTargetColumn()))
61 .add(op.mutate(bridge.getSchema())
62 .addMutation(bridge.getControllerColumn().getSchema(), Mutator.INSERT,
63 Sets.newHashSet(new UUID(transactionUuidStr)))
64 .where(bridge.getNameColumn().getSchema().opEqual(TEST_BRIDGE_NAME))
67 ListenableFuture<List<OperationResult>> results = transactionBuilder.execute();
68 List<OperationResult> operationResults = results.get();
69 assertFalse(operationResults.isEmpty());
70 // Check if Results matches the number of operations in transaction
71 assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
72 logger.info("Insert & Mutate operation results for controller1 = " + operationResults);
73 // Check for any errors
74 for (OperationResult result : operationResults) {
75 assertNull(result.getError());
78 UUID controllerUUID = operationResults.get(0).getUuid();
79 Thread.sleep(3000); // Wait for cache to catchup
81 Row controllerRow = OpenVswitchSchemaSuiteIT.getTableCache().get(controller1.getSchema().getName()).get(controllerUUID);
82 Controller monitoredController = ovs.getTypedRowWrapper(Controller.class, controllerRow);
83 assertEquals(controller1.getTargetColumn().getData(), monitoredController.getTargetColumn().getData());
85 Row bridgeRow = OpenVswitchSchemaSuiteIT.getTableCache().get(bridge.getSchema().getName()).get(OpenVswitchSchemaSuiteIT.getTestBridgeUuid());
86 Bridge monitoredBridge = ovs.getTypedRowWrapper(Bridge.class, bridgeRow);
87 assertEquals(1, monitoredBridge.getControllerColumn().getData().size());
89 transactionBuilder = ovs.transactBuilder(OpenVswitchSchemaSuiteIT.dbSchema)
90 .add(op.insert(controller2.getSchema())
91 .withId(transactionUuidStr)
92 .value(controller2.getTargetColumn()))
93 .add(op.mutate(bridge.getSchema())
94 .addMutation(bridge.getControllerColumn().getSchema(), Mutator.INSERT,
95 Sets.newHashSet(new UUID(transactionUuidStr)))
96 .where(bridge.getNameColumn().getSchema().opEqual(TEST_BRIDGE_NAME))
99 results = transactionBuilder.execute();
100 operationResults = results.get();
101 assertFalse(operationResults.isEmpty());
102 // Check if Results matches the number of operations in transaction
103 assertEquals(transactionBuilder.getOperations().size(), operationResults.size());
104 logger.info("Insert & Mutate operation results for controller2 = " + operationResults);
105 // Check for any errors
106 for (OperationResult result : operationResults) {
107 assertNull(result.getError());
109 Thread.sleep(3000); // Wait for cache to catchup
110 bridgeRow = OpenVswitchSchemaSuiteIT.getTableCache().get(bridge.getSchema().getName()).get(OpenVswitchSchemaSuiteIT.getTestBridgeUuid());
111 monitoredBridge = ovs.getTypedRowWrapper(Bridge.class, bridgeRow);
112 assertEquals(2, monitoredBridge.getControllerColumn().getData().size());
116 public void update(Object context, UpdateNotification upadateNotification) {
121 public void locked(Object context, List<String> ids) {
126 public void stolen(Object context, List<String> ids) {