Merge changes I6244de55,I20d073c2 into topic/schema
[ovsdb.git] / schemas / Open_vSwitch / src / test / java / org / opendaylight / ovsdb / schema / openvswitch / TypedVSwitchdSchemaIT.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
9  */
10 package org.opendaylight.ovsdb.schema.openvswitch;
11
12 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
13
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;
20
21 import junit.framework.Assert;
22
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;
36
37 import com.google.common.collect.ImmutableMap;
38 import com.google.common.collect.Sets;
39 import com.google.common.util.concurrent.ListenableFuture;
40
41 public class TypedVSwitchdSchemaIT extends OvsdbTestBase {
42
43     Logger logger = LoggerFactory.getLogger(TypedVSwitchdSchemaIT.class);
44     OvsDBClientImpl ovs;
45     DatabaseSchema dbSchema = null;
46     static String testBridgeName = "br_test";
47     static UUID testBridgeUuid = null;
48
49     @Test
50     public void testTypedBridgeOperations() throws IOException, InterruptedException, ExecutionException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
51         this.createTypedBridge();
52         this.createTypedController();
53     }
54
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));
60
61         OpenVSwitch openVSwitch = ovs.createTypedRowWrapper(OpenVSwitch.class);
62         openVSwitch.setBridges(Sets.newHashSet(new UUID(testBridgeName)));
63
64         int insertOperationIndex = 0;
65
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()));
78
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());
87         }
88         testBridgeUuid = operationResults.get(insertOperationIndex).getUuid();
89     }
90
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");
96
97         Bridge bridge = ovs.getTypedRowWrapper(Bridge.class, null);
98
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))
108                         .build());
109
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());
119         }
120
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))
129                         .build());
130
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());
140         }
141     }
142
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;
151                 break;
152            }
153         }
154         Assert.assertTrue(OPEN_VSWITCH_SCHEMA+" schema is not supported by the switch", hasOpenVswitchSchema);
155     }
156
157     @Before
158     public  void setUp() throws IOException, ExecutionException, InterruptedException {
159         if (ovs != null) {
160             return;
161         }
162         OvsdbRPC rpc = getTestConnection();
163         if (rpc == null) {
164             System.out.println("Unable to Establish Test Connection");
165         }
166         ExecutorService executorService = Executors.newFixedThreadPool(3);
167         ovs = new OvsDBClientImpl(rpc, executorService);
168         testGetDBs();
169         dbSchema = ovs.getSchema(OPEN_VSWITCH_SCHEMA, true).get();
170     }
171
172     @After
173     public void tearDown() throws InterruptedException, ExecutionException {
174         Bridge bridge = ovs.getTypedRowWrapper(Bridge.class, null);
175         OpenVSwitch openVSwitch = ovs.getTypedRowWrapper(OpenVSwitch.class, null);
176
177         ListenableFuture<List<OperationResult>> results = ovs.transactBuilder()
178                 .add(op.delete(bridge.getSchema())
179                         .where(bridge.getNameColumn().getSchema().opEqual(testBridgeName))
180                         .build())
181                 .add(op.mutate(openVSwitch.getSchema())
182                         .addMutation(openVSwitch.getBridgesColumn().getSchema(), Mutator.DELETE, Sets.newHashSet(testBridgeUuid)))
183                 .add(op.commit(true))
184                 .execute();
185
186         List<OperationResult> operationResults = results.get();
187         System.out.println("Delete operation results = " + operationResults);
188     }
189
190     @Override
191     public void update(Object node, UpdateNotification upadateNotification) {
192         // TODO Auto-generated method stub
193
194     }
195     @Override
196     public void locked(Object node, List<String> ids) {
197         // TODO Auto-generated method stub
198
199     }
200     @Override
201     public void stolen(Object node, List<String> ids) {
202         // TODO Auto-generated method stub
203
204     }
205 }