Bug 1471 : Fixing the Open_vSwitch Schema Controller Table's Target as String (from...
[ovsdb.git] / schemas / openvswitch / src / test / java / org / opendaylight / ovsdb / schema / openvswitch / ControllerTestCases.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, Dave Tucker
9  */
10
11 package org.opendaylight.ovsdb.schema.openvswitch;
12
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;
17
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;
23
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;
34
35 import com.google.common.collect.Sets;
36 import com.google.common.util.concurrent.ListenableFuture;
37
38 public class ControllerTestCases extends OpenVswitchSchemaTestBase {
39     Logger logger = LoggerFactory.getLogger(ControllerTestCases.class);
40
41     @Override
42     @Before
43     public void setUp() throws ExecutionException, InterruptedException, TimeoutException, IOException {
44         super.setUp();
45     }
46
47     @Test
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");
53
54         Bridge bridge = ovs.getTypedRowWrapper(Bridge.class, null);
55
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))
65                         .build());
66
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());
76         }
77
78         UUID controllerUUID = operationResults.get(0).getUuid();
79         Thread.sleep(3000); // Wait for cache to catchup
80
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());
84
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());
88
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))
97                         .build());
98
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());
108         }
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());
113     }
114
115     @Override
116     public void update(Object context, UpdateNotification upadateNotification) {
117
118     }
119
120     @Override
121     public void locked(Object context, List<String> ids) {
122
123     }
124
125     @Override
126     public void stolen(Object context, List<String> ids) {
127
128     }
129 }